Here is a piece of code to compare two sentences word by word and return the number of word matches with some conditions:
hint: the word in the first sentence :::: the word in the second sentence
1) protecting :::: i should result Not matched
2) protecting :::: protect should result matched
3) protect :::: protecting should result matched
4) him :::: i should result Not matched
5) i :::: i should result matched but only once not twice: (let me explain this)
We have this string as the first sentence:
let speechResult = "they're were protecting him i knew that i was aware";
It has two i as you see but there is only one i in the second sentence here:
let expectSt = ['i was sent to earth to protect you'];
So we should consider this match as one occurrence not two, If we had two i occurrences in the second sentence too, then we would consider the i matches as two occurrences.
6) was :::: was should result matched
Here is my code so far:
// Sentences we should compare word by word
let speechResult = "they're were protecting him i knew that i was aware";
let expectSt = ['i was sent to earth to protect you'];
// Create arrays of words from above sentences
let speechResultWords = speechResult.split(/\s+/);
let expectStWords = expectSt[0].split(/\s+/);
// Here you are..
//console.log(speechResultWords)
//console.log(expectStWords)
// Count Matches between two sentences
function includeWords(){
// Declare a variable to hold the count number of matches
let countMatches = 0;
for(let a = 0; a < speechResultWords.length; a++){
for(let b = 0; b < expectStWords.length; b++){
if(speechResultWords[a].includes(expectStWords[b])){
console.log(speechResultWords[a] + ' includes in ' + expectStWords[b]);
countMatches++
}
} // End of first for loop
} // End of second for loop
return countMatches;
};
// Finally initiate the function to count the matches
let matches = includeWords();
console.log('Matched words: ' + matches);
You could count the wanted words with a Map
and iterate the given words by checking the word count.
function includeWords(wanted, seen) {
var wantedMap = wanted.split(/\s+/).reduce((m, s) => m.set(s, (m.get(s) || 0) + 1), new Map),
wantedArray = Array.from(wantedMap.keys()),
count = 0;
seen.split(/\s+/)
.forEach(s => {
var key = wantedArray.find(t => s === t || s.length > 3 && t.length > 3 && (s.startsWith(t) || t.startsWith(s)));
if (!wantedMap.get(key)) return;
console.log(s, key)
++count;
wantedMap.set(key, wantedMap.get(key) - 1);
});
return count;
}
let matches = includeWords('i was sent to earth to protect you', 'they\'re were protecting him i knew that i was aware');
console.log('Matched words: ' + matches);
.as-console-wrapper { max-height: 100% !important; top: 0; }
I think this should work:
let speechResult = "they're were protecting him i knew that i was aware";
let expectSt = ['i was sent to earth to protect you'];
function includeWords(){
let countMatches = 0;
let ArrayFromStr = speechResult.split(" ");
let Uniq = new Set(ArrayFromStr)
let NewArray = [Uniq]
let str2 = expectSt[0]
for (word in NewArray){
if (str2.includes(word)){
countMatches += 1
}
}
return countMatches;
};
let matches = includeWords();
I get the speechResult, made it into a array, remove duplicates, convert to array again, and then check if the expectSt string contains every word on the NewArray array.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With