Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

look for the similarity of words in sentences javascript

I am trying to find the similarity between the sentences in javascript, example :

let mastersentences = "Gambir, Kecamatan Gambir, Kota Jakarta Pusat, Daerah Khusus Ibukota Jakarta";
let keywordsentences = "DKI JAKARTA";
let result = ( mastersentences  === keywordsentences ) // true because 'jakarta' is match

the result of equating ‘mastersentence’ and ‘keywordsentence’ is "jakarta", i have try using more method of javascript but i still can’t find the result like use :

includes()
indexOf()
lastIndexOf()
localeCompare()
match()
search()

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can split each sentence by \W+ which any character isn’t text or number or underscore, and use some method, to check if there are any matches after converting each work to lower case to prevent sensitivity.

let mastersentences = "Gambir, Kecamatan Gambir, Kota Jakarta Pusat, Daerah Khusus Ibukota Jakarta";
let keywordsentences = "DKI JAKARTA";

const result = mastersentences.split(/\W+/).some(w => keywordsentences.split(/\W+/).some(k => k.toLowerCase() === w.toLowerCase()))

console.log(result)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading