Get partial string match of string Array using Lodash

51 Views Asked by At

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);
2

There are 2 best solutions below

1
mandy8055 On BEST ANSWER

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

0
Joey Zhang On

Another way to optimize this and make more readability code is to use RegRex in javascript.

const textToSearch = 'The broken ship that caught fire also had viruses onboard';
const arrayOfKeywords = ['virus', 'fire', 'broken'];
const pattern = new RegExp(arrayOfKeywords.join('|'), 'i');;
const tokenize = text =>
    text
      .split(/(\w+)/)
      .map(x =>
        pattern.test(x)
          ? {content: x, type: 'keyword'}
          : x
      );

const tokens = tokenize(textToSearch);
console.log(tokens);