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

Get partial string match of string Array using Lodash

I have an array of strings and want to find partial string matches using Lodash. I’d like the search to find full matches (such as with broken) but also partial matches (such as with virus being part of viruses)

const textToSearch = 'The broken ship that caught fire also had viruses onboard';
const arrayOfKeywords = ['virus', 'fire', 'broken'];

So far I have a method that will split the textToSearch into an array and then find full matches using _.include()

const tokenize = text =>
    text
      .split(/(\w+)/)
      .map(x =>
        _.includes(arrayOfKeywords, x.toLowerCase())
          ? {content: x, type: 'keyword'}
          : x
      );

const tokens = tokenize(textToSearch);

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 :

One way to achieve fuzzy matches is using some function with a custom predicate to check for partial matches. Something like:

const textToSearch = 'The broken ship that caught fire also had viruses onboard';
const arrayOfKeywords = ['virus', 'fire', 'broken'];

const tokenize = text =>
  text
    .split(/(\w+)/)
    .map((x) =>
      _.some(arrayOfKeywords, (keyword) => x.toLowerCase().includes(keyword.toLowerCase()))
        ? { content: x, type: 'keyword' }
        : x
    );

const tokens = tokenize(textToSearch);
console.log(tokens);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>

Footnote: In case you want to do it without using lodash, you can replace _.some with Array.protoype.some

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