Tadewos Bellete
Tadewos Bellete

Reputation: 49

what is best way to match words from list to words from sentence in javascript?

i have two sentences and i would like to find all the words they share regardless of capitalization or punctuation. currently this is what I am doing:

    searchWords = sentence1.split(" ");
    var wordList = sentence2.split(" ");
    const matchList = wordList.filter(value => -1 !== searchWords.indexOf(value));

it works ok but obviously capitalization and punctuation cause issues. i know i need to incorporate something like .match() in there but i don't know how to work with it. I am sure this is something someone has done before just havent found the code yet, any refrences are also appreciated.

Thank you,

Best

This dude.

Upvotes: 0

Views: 1262

Answers (2)

zfrisch
zfrisch

Reputation: 8660

If you're looking for any words that match you can use RegExp with String.prototype.replace and verify a match using String.prototype.search with a created RegExp and an i flag to allow case insensitivity.

function compare(str1, str2, matches = []) {
     str1.replace(/(\w+)/g, m => str2.search(new RegExp(m, "i")) >= 0 && matches.push(m));
     return matches;
 }
 
 console.log( compare("Hello there this is a test", "Hello Test this is a world") );


If you're looking for specific words that match you can use functional composition to split each string into an Array, filter each by possible matches, and then filter one against the other.

function compare(str1, str2, matchables) {
     let containFilter = (a) => (i) => a.includes(i),
     matchFilter = s => s.toLowerCase().split(" ").filter(containFilter(matchables));
     
    return matchFilter(str1).filter(containFilter( matchFilter(str2) ));
 }
 
 let matchables = ["hello", "test", "world"];
 console.log( compare("Hello there this is a test", "Hi Test this is a world", matchables) );

Upvotes: 1

Jesse
Jesse

Reputation: 1425

I think you may be over-thinking this. Would just converting both sentences to an array and using a for loop to cycle through the words work? For example:

var searchWords = sentence1.split(" ");
var wordList = sentence2.toLowerCase().split(" ");
var commonWords = [];
for(var i = 0; i < searchWords.length; i++){
    if(wordList.includes(searchWords[i].toLowerCase())){
        commonWords.push(searchWords[i])
    }
}
console.log(commonWords);

Or some variation of that.

As for the punctuation, you could probably add .replace(/[^A-Za-z0-9\s]/g,"") to the end of searchWords[i].toLowerCase() as mentioned in the following answer: https://stackoverflow.com/a/33408855/10601203

Upvotes: 0

Related Questions