Faster Computation of Levenshtein Distance for Spell Checker.

2015-06-15

Now the development moved to GitHub! This page is a historical archive. Please refer to the GitHub page for the latest information.

Fast String Comparator is an efficient tool to determine **whether two strings are within two edit distance or not.** Given two strings of length m and n (m <= n), the computation requires O(1) space and O(n) time, which is much smaller and faster than when using well-known algorithm with O(mn) space and time for the same purpose. It is mainly targeted at the use in spell checker, where considering words within two edit distance suffices.

**2014-02-01**

Here is a live demo you can actually try! Also, javascript implementation of this algorithm (used in the demo) is available here.

The fastcomp.py has a function named compare(). It takes two strings as arguments and returns an integer, which is...

- the exact distance between two strings (if they are within two edit distance)
- -1 (if they are over two edit distance away)

Therefore, the return value should be any one of 0, 1, 2 or -1.

Example> from fastcomp import compare

> compare("than", "then")

> 1

> compare("meeting", "eating")

> 2

> compare("book", "talk") # distance 3

> -1

Download from the link: fastcomp.py (Python 3.0 or later Recommended / Python 2 Compatible)

Changelog

- 2012-3-31: Refactoring the whole script. The code gets much more readable.
- 2012-3-17: First release.

Imagine correcting spells of thousand-word text with thousand-word dictionary. If you try to list all words in dictionary within edit distance 2 for each word in the text, you may end up computing 1,000*1,000=1,000,000 pairs. In this situation, even a small difference easily adds up. See the performance test for the effect of this.

Actually, same technique is applicable to any finite bound *k*. Moreover, we can construct a general function which take the upper bound k as an argument. Such a general function, however, needs much more complicated code, thus produces non-trivial cost. So I choose two as bound, because, anyway, distance 2 seems enough to me.

No. Although Ukkonen algorithm produces the same result with O(n) time and space, Fast String Comparator is still faster and uses less space than that.

The answer is both "Yes" and "No". The problem of edit distance computation has a long long history of re-inventions, partly because this problem has been studied independently in various fields (for example, Wagner-Fischer algorithm has been rediscovered independently at least 10 times in the past). Although I conceived this algorithm by myself, I'm certain that there is someone who had invented the same stuff already.

Compare randomly generated 1000 string pairs for 1000 times (= 1 million comparison). Each string has length between [N/2] to N. Here's the source code used in this test.

(Test Machine - CPU: Celeron T3000 1.80GHz / Memory: 2GB)

Function | N=3 | N=7 | N=10 |
---|---|---|---|

fastcomp.py | 7.75sec | 10.68sec | 12.94sec |

editdist.py* | 26.00sec | 77.23sec | 140.96sec |

* An (slightly modified) implementation of Wagnerâ€“Fischer algorithm in Python. Here is the source code.

The fastcomp.py is released under the MIT License. See below for details.

Copyright (c) 2012 Fujimoto<fujimoto@writingarchives.sakura.ne.jp>

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Report to: Fujimoto <fujimoto@writingarchives.sakura.ne.jp>