Skip to main content

This package provides facilities for computing Levenshtein and Hamming distance between arbitrary Python objects. It is only available for Python 3.3+.

Installation

This is a C extension, so you need a C compiler available on your computer: typically Microsoft Visual C++ 2010 on Windows, and GCC on Mac and Linux. Python development files are also necessary to compile the package. On a Debian-like system, you can get all of these with:

$ apt-get install gcc python3.3-dev

Then you can do:

$ python3.3 setup.py install

Usage

Fist import the module:

>>> import distance

Two functions are provided: levenshtein and hamming. They both take two arguments, which are the objects to compare. Those objects can be of any type, as long as they support the sequence protocol: unicode strings, byte strings, lists, and tuples are ok. In case the objects provided are lists or tuples, they also should contain comparable objects.

Typical use case is to compare single words for similarity, as in spelling correction softwares:

>>> distance.levenshtein("lenvestein", "levenshtein")
3
>>> distance.hamming("hamming", "hamning")
1

Comparing lists of strings can also be useful for computing similarities between sentences, paragraphs, etc., in articles or books, as for plagiarism recognition:

>>> sent1 = ['the', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']
>>> sent2 = ['the', 'lazy', 'fox', 'jumps', 'over', 'the', 'crazy', 'dog']
>>> distance.levenshtein(sent1, sent2)
3

The above of course also works with numbers, etc.:

>>> distance.levenshtein([1,2,3], [1,3,2])
2

Implementation details

Unicode strings are handled separately from the other sequence objects, in an efficient manner. Computing similarities between lists, tuples, and byte strings is likely to be slower, in particular for byte objects, which are internally converted to tuples.

Release files for Distance 0.1

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

Source distribution (sdist)

Source distribution for Distance 0.1
File Size Uploaded
distance.tar.gz 34.1 kB Details

Release files / distance.tar.gz

Download URL distance.tar.gz
Size 34.1 kB
Tags Source
SHA-256 checksum
How to use checksums
5b26973dc040064f8b48ff29a4d82035076bc91056ebb1b0f446872952c42e9d
BLAKE2b-256 checksum
How to use checksums
052e5dd635d1ba751fa46e10e57c9fe767cae134ff85c17e9dfbfd91bdf9ee65
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

0.1.3

1 release file

0.1.2.5

1 release file

0.1.2

1 release file

0.1.1

1 release file

This release

0.1 This release

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