Fast String Comparator

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.

1.Abstract

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.

2.Usage

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

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

4.Download

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

Changelog

4.FAQ

4.1.Reducing the time complexity from O(mn) to O(n)? Who cares?

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.

4.2.Why only measure edit distance up to two?

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.

4.3.Is the technique same as Ukkonen's optimization?

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.

4.4.Is it your original algorithm?

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.

5.Performance

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)

FunctionN=3N=7N=10
fastcomp.py7.75sec10.68sec12.94sec
editdist.py*26.00sec77.23sec140.96sec

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

6.Algorithm

6.1.Basic Idea

7.License

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.


Top

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