News for hackers. Stuff that matters.
Re:Levenshtein Distance by jorangreef on Thursday August 22, 2019 @02:58PM (#20771299)
Here's a helpful tip in case it comes in handy one day:

If you need to run Levenshtein Distance over tens of thousands of strings, perhaps as part of a fuzzy search, and you only need to match on distances of 0 or 1, not any distance in general, then there's an optimization on Levenshtein Distance where you hard code the function just for distances of 0 or 1. It's not difficult to do and the payoff might enable new applications of what would otherwise be too costly.

Re:Levenshtein Distance by apendleton on Thursday August 22, 2019 @03:52PM (#20771761)
If you're fuzzily looking up members in a fixed corpus, you can also employ the trick from https://github.com/wolfgarbe/SymSpell, which is essentially just to, for each string in your corpus, enumerate all of the variants of that string with one letter removed, and put both the original string and each variant in a hashtable as a key, with the original string as the value. To do a lookup, you do the same enumeration (original query plus each variant with one letter dropped), and look all of them up in your hashtable. The values you get out of that are a list of all of the strings in your corpus at edit distances 0 or 1, and potentially some at edit distance 2; you can do a subsequent Levenshtein calculation on each to weed out the distance-2 strings, but you only have to do it on this massively reduced set rather than on your whole corpus.

So like, to index a string "hello", you'd add all of {"hello":"hello","ello":"hello","hllo":"hello","helo":"hello","hell":"hello"} into your table, and for the query "gello", you'd look up all of ["gello", "ello", "gllo", "gelo", "gell"], and get a match on "ello"->"hello", then do a Levenshtein calculation dist("gello","hello") to confirm it's within ED=1 (it is), and be done. (Bonus: the same method works with Damerau-Levenshtein distance as well.)

Re:Levenshtein Distance by hackcasual on Thursday August 22, 2019 @07:03PM (#20773168)
I've heard this called a deletion neighborhood
Re:Levenshtein Distance by fuzzyexplainer on Thursday August 22, 2019 @06:17PM (#20772901)
Does this work if you want to look up multiple words and grade them?

For example if the input query is "ello" and there is a dict.txt with millions of entries, it should output the words that are closest to the query: "hell", "hello", "fellowship", "mellow" and their respective matching scores (e.g 0.8, 0.9, 0.3, 0.7 respectively). The scores are just random numbers in this case.

(edited for clarity)

Re:Levenshtein Distance by developer2 on Thursday August 22, 2019 @07:58PM (#20773492)
In which case do you wind up with distance=2? I figured it'd jump out at me in an obvious way, but it hasn't.
Re:Levenshtein Distance by bryondowd on Thursday August 22, 2019 @08:22PM (#20773620)
Can't think on a real word example, but abc would match bcd in the above algo, as both permute to bc, but they are distance 2. So, any case where one letter is deleted and another is added at a different position.
Re:Levenshtein Distance by aripickar on Thursday August 22, 2019 @08:30PM (#20773654)
Boat and Oats is a real world example with Levenshtein distance 2 that would pass through the algo
Re:Levenshtein Distance by apendleton on Friday August 23, 2019 @10:52AM (#20778230)
Yeah, that's the crux -- two different real words storing the same variant but having omitted different letters.
Re:Levenshtein Distance by developer2 on Monday August 26, 2019 @10:24PM (#20805795)
Haha, I knew the answer was going to be obvious, and it was so obviously obvious. Thanks for answering. :)
Re:Levenshtein Distance by visarga on Thursday August 22, 2019 @05:34PM (#20772594)
Ha ha, I imagined this algorithm for spell checking almost 20 years ago when I was wondering how Google did it.
Re:Levenshtein Distance by HeyImAlex on Thursday August 22, 2019 @10:00PM (#20774003)
There's a specialized algorithm specifically for running levenshtein distance over tens of thousands of strings called bitap[1]. I wrote an impl in rust a while back[2].

But your advice still applies! Run time scales with max distance.

[1] https://en.wikipedia.org/wiki/Bitap_algorithm [2] https://github.com/heyimalex/bitap

Re:Levenshtein Distance by dorgo on Friday August 23, 2019 @03:48AM (#20775344)
Interesting. I used a similar trick to compute a lower bound on the levenshtein distance. I was able to skip the real levenshtein distance calculation, if the lower bound already exceeded a threshold ( I used it for clustering ).
Re:Levenshtein Distance by sixdimensional on Friday August 23, 2019 @12:45AM (#20774576)