I am looking to make a small script in Node.js that will match words with another word that is similar. For example I am searching for ***ing
and I have an array like ['loving', 'mating', 'cats', 'wording']
then I would expect it to return ['loving', 'mating']
and exclude ['cats']
(because it does not end in ing), and ['wording']
(because it is seven characters and not six.).
This is my current not working code that I have written.
let foundWords = [];
for (let i = 0, len = wordList.length; i < len; i++) {
for (let j = 0, len = wordList[i].split('').length; j < len; j++) {
if (wordToFind.charAt(j) == '*') {
return;
};
if (wordToFind.charAt(j) === wordList[i].charAt(j)) {
if (foundWords.includes(wordList[i]) == false) {
foundWords.push(wordList[i]);
};
}
}
}
console.log(foundWords);
The objective of writing this code is to allow me to brute force with a dictionary list all the binations for this cryptogram and the words inside.
I am looking to make a small script in Node.js that will match words with another word that is similar. For example I am searching for ***ing
and I have an array like ['loving', 'mating', 'cats', 'wording']
then I would expect it to return ['loving', 'mating']
and exclude ['cats']
(because it does not end in ing), and ['wording']
(because it is seven characters and not six.).
This is my current not working code that I have written.
let foundWords = [];
for (let i = 0, len = wordList.length; i < len; i++) {
for (let j = 0, len = wordList[i].split('').length; j < len; j++) {
if (wordToFind.charAt(j) == '*') {
return;
};
if (wordToFind.charAt(j) === wordList[i].charAt(j)) {
if (foundWords.includes(wordList[i]) == false) {
foundWords.push(wordList[i]);
};
}
}
}
console.log(foundWords);
The objective of writing this code is to allow me to brute force with a dictionary list all the binations for this cryptogram and the words inside.
Share asked May 14, 2018 at 21:23 user7885981user78859814 Answers
Reset to default 4i really remend you to read about Levenshtein distance sound exactly like what you trying to achieve here
https://en.wikipedia/wiki/Levenshtein_distance#Example
an implementation in java script also https://en.wikibooks/wiki/Algorithm_Implementation/Strings/Levenshtein_distance#JavaScript
in information theory and puter science, the Levenshtein distance is a metric for measuring the amount of difference between two sequences (i.e. an edit distance). The Levenshtein distance between two strings is defined as the minimum number of edits needed to transform one string into the other, with the allowable edit operations being insertion, deletion, or substitution of a single character.
Example The Levenshtein distance between "kitten" and "sitting" is 3, since the following three edits change one into the other, and there isn't a way to do it with fewer than three edits:
kitten sitten (substitution of 'k' with 's')
sitten sittin (substitution of 'e' with 'i')
sittin sitting (insert 'g' at the end).
You can use Array.prototype.filter
along with a RegExp
.
To construct the regex you will need to replace your wildcard characters *
with the wildcard character of a regex: .
. Then add ^
and $
to anchor the regex to match all the way from the beginning to the end of the string.
function filterMatches(needle, haystack) {
const regex = new RegExp('^' + needle.replace(/\*/g, '.') + '$');
return haystack.filter(word => regex.test(word));
}
console.log(filterMatches('***ing', ['loving', 'mating', 'cats', 'wording']));
Hey I think this should work. If you are not understanding a part try to look up the String.prototype
functions at MDN. It really helps to know some of these functions since it will make you code more easy.
let input = '***ing';
let inputLength = input.length
let results = [];
while (input.charAt(0) === "*") {
input = input.substr(1);
}
const arr = ['loving', 'mating', 'cats', 'wording'];
for (let i = 0; i < arr.length; i++) {
if (inputLength != arr[i].length) {
continue;
}
if(arr[i].indexOf(input) != -1) {
results.push(arr[i]);
}
}
console.log(results);
Another approach could be like;
function getMatches(ts, ss){
var es = ts.split(/\*+/)[1]; // or ts.match(/[^\*]+$/)[0];
return ss.filter(s => s.endsWith(es) && s.length === ts.length)
}
var res = getMatches("***ing",['loving', 'mating', 'cats', 'wording']);
console.log(res);