Skip to main content

Fast implementation of the edit distance(Levenshtein distance).

This library simply implements Levenshtein distance with C++ and Cython.

The algorithm used in this library is proposed by Heikki Hyyrö, “Explaining and extending the bit-parallel approximate string matching algorithm of Myers”, (2001)..

Tested on Python 2.7.5 and 3.3.2.

Install

You can install via pip:

pip install editdistance

Usage

It’s quite simple:

>>> import editdistance
>>> editdistance.eval('banana', 'bahama')
2L

Simple Benchmark

With IPython, I tried several libraries:

On Python 2.7.5:

In [12]: a = 'fsffvfdsbbdfvvdavavavavavava'

In [13]: b = 'fvdaabavvvvvadvdvavavadfsfsdafvvav'

In [14]: import pylev

In [15]: timeit pylev.levenshtein(a, b)
100 loops, best of 3: 7.48 ms per loop

In [16]: from pyxdameraulevenshtein import damerau_levenshtein_distance

In [17]: timeit damerau_levenshtein_distance(a, b)
100000 loops, best of 3: 11.4 µs per loop

In [19]: timeit editdistance.eval(a, b)  # my library
100000 loops, best of 3: 3.5 µs per loop

In [20]: import Levenshtein

In [21]: timeit Levenshtein.distance(a, b)
100000 loops, best of 3: 3.21 µs per loop

Distance with Any Object

Above libraries only support strings. But Sometimes other type of objects such as list of strings(words). I support any iterable, only requires hashable object of it:

In [22]: Levenshtein.distance(['spam', 'egg'], ['spam', 'ham'])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-22-3e0b30d145ac> in <module>()
----> 1 Levenshtein.distance(['spam', 'egg'], ['spam', 'ham'])

TypeError: distance expected two Strings or two Unicodes

In [29]: editdistance.eval(['spam', 'egg'], ['spam', 'ham'])
Out[29]: 1L

So if object’s hash is same, it’s same. You can provide __hash__ method to your object instances.

Enjoy!

License

It is released under the MIT license.

Copyright (c) 2013 Hiroyuki Tanaka

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 NONINFRINGEMENT. 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.

Release files for editdistance 0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for editdistance 0.2
File Size Uploaded
editdistance-0.2.tar.gz 19.9 kB Details

Release files / editdistance-0.2.tar.gz

Download URL editdistance-0.2.tar.gz
Size 19.9 kB
Tags Source
SHA-256 checksum
How to use checksums
b7848e894ce6ebf34fcdf4485cbf6a4e3aa4cd3139a813a8359194175de5a290
BLAKE2b-256 checksum
How to use checksums
2558ae951cbb2012f963cd8b41e479fa7e1bf070ba9f9c60948265ac23cec20e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

0.8.1

71 release files

0.8.0

71 release files

0.6.2

78 release files

0.6.1

67 release files

0.5.3

30 release files

0.5.2

30 release files

0.5

17 release files

0.4

27 release files

0.3.1

1 release file

0.3

1 release file

This release

0.2 This release

1 release file

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page