Skip to main content

Computing edit distance on arbitrary Python sequences.

Project description

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

Alternatively you can clone this git repo and install using distutils:

git clone git@github.com:belambert/edit_distance.git
cd edit_distance
python setup.py install

To uninstall with pip:

pip uninstall edit_distance

API usage

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

This requires Python 2.7+ since it uses argparse for the command line interface. The rest of the code should be OK with earlier versions of Python

Example API usage:

import edit_distance
ref = [1, 2, 3, 4]
hyp = [1, 2, 4, 5, 6]
sm = edit_distance.SequenceMatcher(a=ref, b=hyp)
sm.get_opcodes()
sm.ratio()
sm.get_matching_blocks()

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

edit_distance-1.0.6.tar.gz (21.9 kB view details)

Uploaded Source

Built Distribution

edit_distance-1.0.6-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file edit_distance-1.0.6.tar.gz.

File metadata

  • Download URL: edit_distance-1.0.6.tar.gz
  • Upload date:
  • Size: 21.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.2

File hashes

Hashes for edit_distance-1.0.6.tar.gz
Algorithm Hash digest
SHA256 bf0c1a2edd113d4159b31a55b3810c258dbbcb508fcf2fbc1da6cd51f936de2d
MD5 07f6486b2b300a3b18ec42db85e9b8ee
BLAKE2b-256 16b227b30ff8c251012789f0976de74a5b497a03ab259c7ae2b08502b81618bb

See more details on using hashes here.

File details

Details for the file edit_distance-1.0.6-py3-none-any.whl.

File metadata

File hashes

Hashes for edit_distance-1.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 019df0dcd07881d7ead52a3a6bb4cdd5b4d2fbe9d84ad5889f3b8afee524a2d8
MD5 c273d858d4d04fb880e8814543ff5b9e
BLAKE2b-256 211d559e453bc451ab12c7cb79b953f108ed92b378fb8ea4296db1d7c62bbb5e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page