Finding the most similar string among a set of millions of strings

Viewed 1009

Let's say I have a dictionary (word list) of millions upon millions of words. Given a query word, I want to find the word from that huge list that is most similar.

So let's say my query is elepant, then the result would most likely be elephant.

If my word is fentist, the result will probably be dentist.

Of course assuming both elephant and dentist are present in my initial word list.

What kind of index, data structure or algorithm can I use for this so that the query is fast? Hopefully complexity of O(log N).

What I have: The most naive thing to do is to create a "distance function" (which computes the "distance" between two words, in terms of how different they are) and then in O(n) compare the query with every word in the list, and return the one with the closest distance. But I wouldn't use this because it's slow.

2 Answers

The problem you're describing is a Nearest Neighbor Search (NNS). There are two main methods of solving NNS problems: exact and approximate.

If you need an exact solution, I would recommend a metric tree, such as the M-tree, the MVP-tree, and the BK-tree. These trees take advantage of the triangle inequality to speed up search.

If you're willing to accept an approximate solution, there are much faster algorithms. The current state of the art for approximate methods is Hierarchical Navigable Small World (hnsw). The Non-Metric Space Library (nmslib) provides an efficient implementation of hnsw as well as several other approximate NNS methods.

(You can compute the Levenshtein distance with Hirschberg's algorithm)

I made similar algorythm some time ago

Idea is to have an array char[255] with characters and values is a list of words hashes (word ids) that contains this character

When you are searching 'dele....' search(d) will return empty list search(e) will find everything with character e, including elephant (two times, as it have two 'e') search(l) will brings you new list, and you need to combine this list with results from previous step

... at the end of input you will have a list then you can try to do group by wordHash and order by desc by count

Also intresting thing, if your input is missing one or more characters, you will just receive empty list in the middle of the search and it will not affect this idea

My initial algorythm was without ordering, and i was storing for every character wordId and lineNumber and char position. My main problem was that i want to search with ee to find 'elephant' with eleant to find 'elephant' with antph to find 'elephant' Every words was actually a line from file, so it's often was very long And number of files and lines was big I wanted quick search for directories with more than 1gb text files So it was a problem even store them in memory, for this idea you need 3 parts function to fill your cache function to find by char from input function to filter and maybe order results (i didn't use ordering, as i was trying to fill my cache in same order as i read the file, and i wanted to put lines that contains input in the same order upper )

I hope it make sense

Related