Skip to content
Advertisement

How do I approximate search multiple terms in a string in JS?

I have a string S and a list of strings allItems, allItems contains strings that may have common “sub-words” but one element is never an extension of another:

JavaScript

My goal is to find every match or approximate match of a string in allItems in S alongside their index(can be start or end, or ideally both). I’ve been searching for some algorithms to do this, similar to the aho-corasick algorithm, but that doesn’t do approximate string matching.

Example:

JavaScript

->

JavaScript

I’m still pretty new to pattern matching so I’d appreciate some resources on how to code any of the recommended algorithms.

UPDATE: I’ve found a Fuzzified Aho-Corasick automata as described here, but I have very little idea on how to implement this in JS.I also have no issues with the code being slow as I use this for a one time run, and rarely need to do this often.

Advertisement

Answer

Python version (fuzzy-matching) [splits string by spaces]:

JavaScript

Python version (fuzzy-matching) [split by all possible substrings]:

JavaScript

After a bunch of online searching:

JS version (exact-matching):

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement