Skip to main content

simpler-fuzzyset - A fuzzy string set for python.

Forked from https://github.com/axiak/fuzzyset

  • Removed levenshtein support

  • No dependencies on install

fuzzyset is a data structure that performs something akin to fulltext search against data to determine likely mispellings and approximate string matching.

Usage

The usage is simple. Just add a string to the set, and ask for it later by using either .get or []:

>>> a = fuzzyset.FuzzySet()
>>> a.add("michael axiak")
>>> a.get("micael asiak")
[(0.8461538461538461, u'michael axiak')]

The result will be a list of (score, mached_value) tuples. The score is between 0 and 1, with 1 being a perfect match.

For roughly 15% performance increase, there is also a Cython-implemented version called cfuzzyset. So you can write the following, akin to cStringIO and cPickle:

try:
    from cfuzzyset import cFuzzySet as FuzzySet
except ImportError:
    from fuzzyset import FuzzySet

Construction Arguments

  • iterable: An iterable that yields strings to initialize the data structure with

  • gram_size_lower: The lower bound of gram sizes to use, inclusive (see Theory of operation). Default: 2

  • gram_size_upper: The upper bound of gram sizes to use, inclusive (see Theory of operation). Default: 3

  • use_levenshtein: Whether or not to use the levenshtein distance to determine the match scoring. Default: True

Theory of operation

Adding to the data structure

First let’s look at adding a string, ‘michaelich’ to an empty set. We first break apart the string into n-grams (strings of length n). So trigrams of ‘michaelich’ would look like:

'-mi'
'mic'
'ich'
'cha'
'hae'
'ael'
'eli'
'lic'
'ich'
'ch-'

Note that fuzzyset will first normalize the string by removing non word characters except for spaces and commas and force everything to be lowercase.

Next the fuzzyset essentially creates a reverse index on those grams. Maintaining a dictionary that says:

'mic' -> (1, 0)
'ich' -> (2, 0)
...

And there’s a list that looks like:

[(3.31, 'michaelich')]

Note that we maintain this reverse index for all grams from gram_size_lower to gram_size_upper in the constructor. This becomes important in a second.

Retrieving

To search the data structure, we take the n-grams of the query string and perform a reverse index look up. To illustrate, let’s consider looking up 'michael' in our fictitious set containing 'michaelich' where the gram_size_upper and gram_size_lower parameters are default (3 and 2 respectively).

We begin by considering first all trigrams (the value of gram_size_upper). Those grams are:

'-mi'
'mic'
'ich'
'cha'
'el-'

Then we create a list of any element in the set that has at least one occurrence of a trigram listed above. Note that this is just a dictionary lookup 5 times. For each of these matched elements, we compute the cosine similarity between each element and the query string. We then sort to get the most similar matched elements.

If use_levenshtein is false, then we return all top matched elements with the same cosine similarity.

If use_levenshtein is true, then we truncate the possible search space to 50, compute a score based on the levenshtein distance (so that we handle transpositions), and return based on that.

In the event that none of the trigrams matched, we try the whole thing again with bigrams (note though that if there are no matches, the failure to match will be quick). Bigram searching will always be slower because there will be a much larger set to order.

Install

easy_install fuzzyset

License

BSD

Author

Mike Axiak <mike@axiak.net>

Metadata

Release files for simplefuzzyset 0.0.12

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

Source distribution (sdist)

Source distribution for simplefuzzyset 0.0.12
File Size Uploaded Provenance
simplefuzzyset-0.0.12.tar.gz 319.9 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for simplefuzzyset 0.0.12
File Interpreter ABI Platform
simplefuzzyset-0.0.12-py3-none-any.whl Python 3 none any Details

Total release size: 326.9 kB

Release files / simplefuzzyset-0.0.12.tar.gz

Download URL simplefuzzyset-0.0.12.tar.gz
Size 319.9 kB
Tags Source
SHA-256 checksum
How to use checksums
9a1b30c38b6afb76c6600bdd66c1c1dc3d8505b082e9e3d466f60f40e8b7e1f2
BLAKE2b-256 checksum
How to use checksums
cebc7e5d5eaa5566ade033cda9ff0eb51b0942ab2138288b445c469d2814cd2f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / simplefuzzyset-0.0.12-py3-none-any.whl

Download URL simplefuzzyset-0.0.12-py3-none-any.whl
Size 7.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c7f146e600ac9fb8d87a013439903f6bb8337ce902e740d25b8ed5f41b8f44da
BLAKE2b-256 checksum
How to use checksums
858ed05f60a19b82edb7e40b8a8767f29b804f4212ea58e7d5c5432a85084936
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for simplefuzzyset 0.0.12

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Release history Release notifications | RSS feed

This release

0.0.12 This release

2 release files No provenance

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