Skip to main content

Fuzzy Sequence Matcher

https://img.shields.io/pypi/v/fuzzy_sequence_matcher.svg https://img.shields.io/travis/catherinedevlin/fuzzy_sequence_matcher.svg Documentation Status

Purpose

Finds best pairings of elements between two sequences.

Each element can only be used at most once, and the order of elements is preserved. That is, if X1 -> Y2, then X2 cannot match to Y1 or Y2; it must match to Y3 or later. This is appropriate for sequences where Y is a garbled or mutated copy of X.

Example

>>> from fuzzy_sequence_matcher.fuzzy_sequence_matcher import best_matches
>>> from jellyfish import jaro_distance
>>> declaration = "We hold these truths to be self evident".split()
>>> degradation = ("I guess wee hold them tooths and stuff "
...     "for being sort of evidence, y'know?").split()
>>> best_matches(declaration, degradation, scorer=jaro_distance)
[('We', 'wee'), ('hold', 'hold'), ('these', 'them'), ('truths', 'tooths'), ('to', 'for'), ('be', 'being'), ('self', 'sort'), ('evident', 'evidence,')]

Features

  • Match any objects you can write a scoring function for

  • No dependencies outside standard library

Scoring function

The matching is done with a scoring function you specify. It should look something like:

def score(element_from_seq1: Any, element_from_seq2: Any) -> float

with high scores indicating better matches.

For comparing strings, you might use jellyfish.jaro_distance. For comparing numbers, -abs(n1 - n2) works.

threshold

By default, fuzzy_sequence_matcher finds the combination that maximizes the sum scores of all the pairings. However, when one sequence is much longer than the other, the number of possible combinations grows impractically large to try them all. The itertools documentation gives the number of combinations as

len(Y)! / len(X)! / (len(Y) - len(X))!

when len(Y) >= len(X).

This function is exposed as fuzzy_sequence_matcher.n_combinations.

If the number of possible combinations exceeds a threshold - by default, 1_000_000, which happens when the long seq is ~ 15 or more elements longer than the short - then elements from the longer sequence will be dropped from consideration entirely, starting with those whose best match to the shorter sequence is worst, until n_combinations is under the threshold. This could conceivably give a result that is not the ideal-scoring set of matches.

Credits

This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template.

Thanks to Dayton Dynamic Languages for advice and brainstorming

History

0.1.0 (2019-12-14)

  • First release on PyPI.

Download files

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

Source Distribution

fuzzy_sequence_matcher-0.1.0.tar.gz (13.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fuzzy_sequence_matcher-0.1.0-py2.py3-none-any.whl (6.3 kB view details)

Uploaded Python 2Python 3

File details

Details for the file fuzzy_sequence_matcher-0.1.0.tar.gz.

File metadata

  • Download URL: fuzzy_sequence_matcher-0.1.0.tar.gz
  • Upload date:
  • Size: 13.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.8.0

File hashes

Hashes for fuzzy_sequence_matcher-0.1.0.tar.gz
Algorithm Hash digest
SHA256 16867214cd21a61c3250060cae0b60de76f1569ff045df4e1ee86b00172498c4
MD5 8876be317bd0469a6ebce91133d8bea9
BLAKE2b-256 a665d614eb5132c02d7edf49e095ef0c69eec208996912c8ee83262698e9cfb3

See more details on using hashes here.

File details

Details for the file fuzzy_sequence_matcher-0.1.0-py2.py3-none-any.whl.

File metadata

  • Download URL: fuzzy_sequence_matcher-0.1.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 6.3 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.40.2 CPython/3.8.0

File hashes

Hashes for fuzzy_sequence_matcher-0.1.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 62f78b084659e8ee236e6e7ffa6394b147485639e489dec098226c602430b677
MD5 4908f8d1bf85864178e1fc205dc6f098
BLAKE2b-256 bc1103fed753185303da553e8b3e02e753d733ea14d664cdee9c61aad28d56ad

See more details on using hashes here.

Release history Release notifications | RSS feed

0.1.1

2 files

This release

0.1.0 This release

2 files

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