Skip to main content

edit_distance

build PyPI version codecov

Python module for computing edit distances and alignments between sequences.

I needed a way to compute edit distances between sequences in Python. I wasn't able to find any appropriate libraries that do this so I wrote my own. There appear to be numerous edit distance libraries available for computing edit distances between two strings, but not between two sequences.

This is written entirely in Python. This implementation could likely be optimized to be faster within Python. And could probably be much faster if implemented in C.

The library API is modeled after difflib.SequenceMatcher. This is very similar to difflib, except that this module computes edit distance (Levenshtein distance) rather than the Ratcliff and Oberhelp method that Python's difflib uses. difflib "does not yield minimal edit sequences, but does tend to yield matches that 'look right' to people."

If you find this library useful or have any suggestions, please send me a message.

Installing & uninstalling

The easiest way to install is using pip:

pip install edit_distance

To uninstall with pip:

pip uninstall edit_distance

This requires Python 3.9 or later.

API usage

To see examples of usage, view the difflib documentation. Additional API-level documentation is available on ReadTheDocs

Example API usage:

import edit_distance
ref = ["hi", "there", "how", "are", "you"]
hyp = ["hi", "here", "how", "are", "you", "doing"]
sm = edit_distance.SequenceMatcher(a=ref, b=hyp)

sm.distance()
# 2
sm.get_opcodes()
# [['equal', 0, 1, 0, 1], ['replace', 1, 2, 1, 2], ['equal', 2, 3, 2, 3], ['equal', 3, 4, 3, 4], ['equal', 4, 5, 4, 5], ['insert', 5, 5, 5, 6]]
list(sm.get_matching_blocks())
# [[0, 0, 1], [2, 2, 1], [3, 3, 1], [4, 4, 1]]
sm.ratio()
# 0.7272727272727273

Command line usage

Installing the package also installs an edit-distance command, which compares two files line by line and prints the edit distance between each pair of lines:

edit-distance file1.txt file2.txt

Differences from difflib

In addition to the SequenceMatcher methods, distance() and matches() methods are provided which compute the edit distance and the number of matches.

sm.distance()
sm.matches()

Even if the alignment of the two sequences is identical to difflib, get_opcodes() and get_matching_blocks() may return slightly different sequences. The opcodes returned by this library represent individual character operations, and thus should never span two or more characters.

It's also possible to compute the maximum number of matches rather than the minimum number of edits:

sm = edit_distance.SequenceMatcher(a=ref, b=hyp, 
     action_function=edit_distance.highest_match_action)

Notes

This doesn't implement the 'junk' matching features in difflib.

Hacking

To run unit tests:

python -m unittest

To deploy...

Contributing and code of conduct

For contributions, it's best to Github issues and pull requests. Proper testing and documentation required.

Code of conduct is expected to be reasonable, especially as specified by the Contributor Covenant

Release files for edit-distance 1.0.9

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

Source distribution (sdist)

Source distribution for edit-distance 1.0.9
File Size Uploaded
edit_distance-1.0.9.tar.gz 10.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for edit-distance 1.0.9
File Interpreter ABI Platform
edit_distance-1.0.9-py3-none-any.whl Python 3 none any Details

Total release size: 21.4 kB

Release files / edit_distance-1.0.9.tar.gz

Download URL edit_distance-1.0.9.tar.gz
Size 10.0 kB
Tags Source
SHA-256 checksum
How to use checksums
074c29ec00c8e74ff2627759cf3d40aa2d95cd24a0cde4acc62c820df9f3af26
BLAKE2b-256 checksum
How to use checksums
6038f5ceab4c9dfa045827921138d5c0b5a6411d7b29ebd4011b0364f7724b2e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.12

Release files / edit_distance-1.0.9-py3-none-any.whl

Download URL edit_distance-1.0.9-py3-none-any.whl
Size 11.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6b3e95263b548e335c4982486afad31a17f79b6d2f48c3d1e7a37a5dfc1838f9
BLAKE2b-256 checksum
How to use checksums
5308a6c994306096eb15ad398def16ffe87df68314599b9f82f3235770c21095
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.12

Release history Release notifications | RSS feed

This release

1.0.9 This release

2 release files

1.0.7

2 release files

1.0.6

2 release files

1.0.5

2 release files

1.0.4

2 release files

1.0.3

2 release files

1.0.1

1 release file

1.0.0

1 release file

0.2.4

1 release file

0.2.3

1 release file

0.2.2

1 release file

0.2.1

1 release file

0.2

1 release file

0.1.0

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