Git Product home page Git Product logo

jarowinkler's Introduction

RapidFuzz

Rapid fuzzy string matching in Python and C++ using the Levenshtein Distance

Continuous Integration PyPI package version Conda Version Python versions
Documentation Code Coverage GitHub license

DescriptionInstallationUsageLicense


Description

RapidFuzz is a fast string matching library for Python and C++, which is using the string similarity calculations from FuzzyWuzzy. However there are a couple of aspects that set RapidFuzz apart from FuzzyWuzzy:

  1. It is MIT licensed so it can be used whichever License you might want to choose for your project, while you're forced to adopt the GPL license when using FuzzyWuzzy
  2. It provides many string_metrics like hamming or jaro_winkler, which are not included in FuzzyWuzzy
  3. It is mostly written in C++ and on top of this comes with a lot of Algorithmic improvements to make string matching even faster, while still providing the same results. For detailed benchmarks check the documentation
  4. Fixes multiple bugs in the partial_ratio implementation
  5. It can be largely used as a drop in replacement for fuzzywuzzy. However there are a couple API differences described here

Requirements

Installation

There are several ways to install RapidFuzz, the recommended methods are to either use pip(the Python package manager) or conda (an open-source, cross-platform, package manager)

with pip

RapidFuzz can be installed with pip the following way:

pip install rapidfuzz

There are pre-built binaries (wheels) of RapidFuzz for MacOS (10.9 and later), Linux x86_64 and Windows. Wheels for armv6l (Raspberry Pi Zero) and armv7l (Raspberry Pi) are available on piwheels.

✖️   failure "ImportError: DLL load failed"

If you run into this error on Windows the reason is most likely, that the Visual C++ 2019 redistributable is not installed, which is required to find C++ Libraries (The C++ 2019 version includes the 2015, 2017 and 2019 version).

with conda

RapidFuzz can be installed with conda:

conda install -c conda-forge rapidfuzz

from git

RapidFuzz can be installed directly from the source distribution by cloning the repository. This requires a C++17 capable compiler.

git clone --recursive https://github.com/rapidfuzz/rapidfuzz.git
cd rapidfuzz
pip install .

Usage

Some simple functions are shown below. A complete documentation of all functions can be found here.
Note that from RapidFuzz 3.0.0, strings are not preprocessed(removing all non alphanumeric characters, trimming whitespaces, converting all characters to lower case) by default. Which means that when comparing two strings that have the same characters but different cases("this is a word", "THIS IS A WORD") their similarity score value might be different, so when comparing such strings you might see a difference in score value compared to previous versions. Some examples of string matching with preprocessing can be found here.

Scorers

Scorers in RapidFuzz can be found in the modules fuzz and distance.

Simple Ratio

> from rapidfuzz import fuzz
> fuzz.ratio("this is a test", "this is a test!")
96.55172413793103

Partial Ratio

> from rapidfuzz import fuzz
> fuzz.partial_ratio("this is a test", "this is a test!")
100.0

Token Sort Ratio

> from rapidfuzz import fuzz
> fuzz.ratio("fuzzy wuzzy was a bear", "wuzzy fuzzy was a bear")
90.9090909090909
> fuzz.token_sort_ratio("fuzzy wuzzy was a bear", "wuzzy fuzzy was a bear")
100.0

Token Set Ratio

> from rapidfuzz import fuzz
> fuzz.token_sort_ratio("fuzzy was a bear", "fuzzy fuzzy was a bear")
84.21052631578947
> fuzz.token_set_ratio("fuzzy was a bear", "fuzzy fuzzy was a bear")
100.0

Weighted Ratio

> from rapidfuzz import fuzz
> fuzz.WRatio("this is a test", "this is a new test!!!")
85.5

> from rapidfuzz import fuzz, utils
> # Removing non alpha numeric characters("!") from the string
> fuzz.WRatio("this is a test", "this is a new test!!!", processor=utils.default_process) # here "this is a new test!!!" is converted to "this is a new test"
95.0
> fuzz.WRatio("this is a test", "this is a new test")
95.0

> # Converting string to lower case
> fuzz.WRatio("this is a word", "THIS IS A WORD")
21.42857142857143
> fuzz.WRatio("this is a word", "THIS IS A WORD", processor=utils.default_process) # here "THIS IS A WORD" is converted to "this is a word"
100.0

Quick Ratio

> from rapidfuzz import fuzz
> fuzz.QRatio("this is a test", "this is a new test!!!")
80.0

> from rapidfuzz import fuzz, utils
> # Removing non alpha numeric characters("!") from the string
> fuzz.QRatio("this is a test", "this is a new test!!!", processor=utils.default_process)
87.5
> fuzz.QRatio("this is a test", "this is a new test")
87.5

> # Converting string to lower case
> fuzz.QRatio("this is a word", "THIS IS A WORD")
21.42857142857143
> fuzz.QRatio("this is a word", "THIS IS A WORD", processor=utils.default_process)
100.0

Process

The process module makes it compare strings to lists of strings. This is generally more performant than using the scorers directly from Python. Here are some examples on the usage of processors in RapidFuzz:

> from rapidfuzz import process, fuzz
> choices = ["Atlanta Falcons", "New York Jets", "New York Giants", "Dallas Cowboys"]
> process.extract("new york jets", choices, scorer=fuzz.WRatio, limit=2)
[('New York Jets', 76.92307692307692, 1), ('New York Giants', 64.28571428571428, 2)]
> process.extractOne("cowboys", choices, scorer=fuzz.WRatio)
('Dallas Cowboys', 83.07692307692308, 3)

> # With preprocessing
> from rapidfuzz import process, fuzz, utils
> process.extract("new york jets", choices, scorer=fuzz.WRatio, limit=2, processor=utils.default_process)
[('New York Jets', 100.0, 1), ('New York Giants', 78.57142857142857, 2)]
> process.extractOne("cowboys", choices, scorer=fuzz.WRatio, processor=utils.default_process)
('Dallas Cowboys', 90.0, 3)

The full documentation of processors can be found here

Benchmark

The following benchmark gives a quick performance comparison between RapidFuzz and FuzzyWuzzy. More detailed benchmarks for the string metrics can be found in the documentation. For this simple comparison I generated a list of 10.000 strings with length 10, that is compared to a sample of 100 elements from this list:

words = [
    "".join(random.choice(string.ascii_letters + string.digits) for _ in range(10))
    for _ in range(10_000)
]
samples = words[:: len(words) // 100]

The first benchmark compares the performance of the scorers in FuzzyWuzzy and RapidFuzz when they are used directly from Python in the following way:

for sample in samples:
    for word in words:
        scorer(sample, word)

The following graph shows how many elements are processed per second with each of the scorers. There are big performance differences between the different scorers. However each of the scorers is faster in RapidFuzz

Benchmark Scorer

The second benchmark compares the performance when the scorers are used in combination with cdist in the following way:

cdist(samples, words, scorer=scorer)

The following graph shows how many elements are processed per second with each of the scorers. In RapidFuzz the usage of scorers through processors like cdist is a lot faster than directly using it. That's why they should be used whenever possible.

Benchmark cdist

Support the project

If you are using RapidFuzz for your work and feel like giving a bit of your own benefit back to support the project, consider sending us money through GitHub Sponsors or PayPal that we can use to buy us free time for the maintenance of this great library, to fix bugs in the software, review and integrate code contributions, to improve its features and documentation, or to just take a deep breath and have a cup of tea every once in a while. Thank you for your support.

Support the project through GitHub Sponsors or via PayPal:

.

License

RapidFuzz is licensed under the MIT license since I believe that everyone should be able to use it without being forced to adopt the GPL license. That's why the library is based on an older version of fuzzywuzzy that was MIT licensed as well. This old version of fuzzywuzzy can be found here.

jarowinkler's People

Contributors

cclauss avatar maxbachmann avatar mgorny avatar pekkarr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

jarowinkler's Issues

Difference between Rapidfuzz JaroWinkler and this

Hi,

First, thanks for the great work! Rapidfuzz and this are killers in terms of performance and usability!

I only have one question: What are the differences between using rapidfuzz python implementation of jaronwinkler (rapidfuzz.distance.JaroWinkler.similiarity) and this library aside from just the API?

Regards,
Joshua

-1 and -2 are treated as equivalent

equivalence in JaroWinkler / RapidFuzz is implemented in terms of the hash of an element. For this purpose the Python hash function is used. However this implementation uses the value -1 to indicate an error and therefore has the same hash for -1 and -2:

hash(-1) == hash(-2) == -2

this leads to:

>>> from jarowinkler import jaro_similarity
>>> jaro_similarity([0, -1], [0, -2])
1.0

We do not need this as error value and should implement the hash function as:

def rapidfuzz_hash(x):
    if x == -1:
        return -1
    return hash(x)

@orsinium this came up here: https://cloud.drone.io/life4/textdistance/53/1/8

Function for numericals

I use compare.string or compare.exact for data in object format, and these functions find probable matches. But for the numerical data compare.numeric seems to be not working anymore. I have the construction year of the building column, I need a function to find most near years matches.

Thanks in advance.

Please explain in README how to run tests

PyTest fails even though Python module is in PYTHONPATH.

==================================================================================== test session starts =====================================================================================
platform freebsd13 -- Python 3.9.13, pytest-7.1.2, pluggy-1.0.0 -- /usr/local/bin/python3.9
cachedir: .pytest_cache
hypothesis profile 'default' -> database=DirectoryBasedExampleDatabase('/disk-samsung/freebsd-ports/textproc/py-jarowinkler/work-py39/jarowinkler-1.0.5/.hypothesis/examples')
rootdir: /disk-samsung/freebsd-ports/textproc/py-jarowinkler/work-py39/jarowinkler-1.0.5
plugins: forked-1.4.0, rerunfailures-10.1, hypothesis-6.47.3, xdist-2.5.0, cov-2.9.0
collected 0 items / 2 errors                                                                                                                                                                 

=========================================================================================== ERRORS ===========================================================================================
_________________________________________________________________________ ERROR collecting tests/test_JaroWinkler.py _________________________________________________________________________
ImportError while importing test module '/disk-samsung/freebsd-ports/textproc/py-jarowinkler/work-py39/jarowinkler-1.0.5/tests/test_JaroWinkler.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/local/lib/python3.9/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
tests/test_JaroWinkler.py:7: in <module>
    from jarowinkler import jarowinkler_similarity
jarowinkler/__init__.py:5: in <module>
    from ._initialize import *
E   ModuleNotFoundError: No module named 'jarowinkler._initialize'
_________________________________________________________________________ ERROR collecting tests/test_hypothesis.py __________________________________________________________________________
ImportError while importing test module '/disk-samsung/freebsd-ports/textproc/py-jarowinkler/work-py39/jarowinkler-1.0.5/tests/test_hypothesis.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
/usr/local/lib/python3.9/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
tests/test_hypothesis.py:5: in <module>
    from jarowinkler import jarowinkler_similarity
jarowinkler/__init__.py:5: in <module>
    from ._initialize import *
E   ModuleNotFoundError: No module named 'jarowinkler._initialize'
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 2 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
===================================================================================== 2 errors in 1.21s ======================================================================================
*** Error code 2

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.