| 1 |
.. image:: https://travis-ci.org/seatgeek/fuzzywuzzy.svg?branch=master |
| 2 |
:target: https://travis-ci.org/seatgeek/fuzzywuzzy |
| 3 |
|
| 4 |
FuzzyWuzzy |
| 5 |
========== |
| 6 |
|
| 7 |
Fuzzy string matching like a boss. It uses `Levenshtein Distance <https://en.wikipedia.org/wiki/Levenshtein_distance>`_ to calculate the differences between sequences in a simple-to-use package. |
| 8 |
|
| 9 |
Requirements |
| 10 |
============ |
| 11 |
|
| 12 |
- Python 2.7 or higher |
| 13 |
- difflib |
| 14 |
- `python-Levenshtein <https://github.com/ztane/python-Levenshtein/>`_ (optional, provides a 4-10x speedup in String |
| 15 |
Matching, though may result in `differing results for certain cases <https://github.com/seatgeek/fuzzywuzzy/issues/128>`_) |
| 16 |
|
| 17 |
For testing |
| 18 |
~~~~~~~~~~~ |
| 19 |
- pycodestyle |
| 20 |
- hypothesis |
| 21 |
- pytest |
| 22 |
|
| 23 |
Installation |
| 24 |
============ |
| 25 |
|
| 26 |
Using PIP via PyPI |
| 27 |
|
| 28 |
.. code:: bash |
| 29 |
|
| 30 |
pip install fuzzywuzzy |
| 31 |
|
| 32 |
or the following to install `python-Levenshtein` too |
| 33 |
|
| 34 |
.. code:: bash |
| 35 |
|
| 36 |
pip install fuzzywuzzy[speedup] |
| 37 |
|
| 38 |
|
| 39 |
Using PIP via Github |
| 40 |
|
| 41 |
.. code:: bash |
| 42 |
|
| 43 |
pip install git+git://github.com/seatgeek/[email protected]#egg=fuzzywuzzy |
| 44 |
|
| 45 |
Adding to your ``requirements.txt`` file (run ``pip install -r requirements.txt`` afterwards) |
| 46 |
|
| 47 |
.. code:: bash |
| 48 |
|
| 49 |
git+ssh://[email protected]/seatgeek/[email protected]#egg=fuzzywuzzy |
| 50 |
|
| 51 |
Manually via GIT |
| 52 |
|
| 53 |
.. code:: bash |
| 54 |
|
| 55 |
git clone git://github.com/seatgeek/fuzzywuzzy.git fuzzywuzzy |
| 56 |
cd fuzzywuzzy |
| 57 |
python setup.py install |
| 58 |
|
| 59 |
|
| 60 |
Usage |
| 61 |
===== |
| 62 |
|
| 63 |
.. code:: python |
| 64 |
|
| 65 |
>>> from fuzzywuzzy import fuzz |
| 66 |
>>> from fuzzywuzzy import process |
| 67 |
|
| 68 |
Simple Ratio |
| 69 |
~~~~~~~~~~~~ |
| 70 |
|
| 71 |
.. code:: python |
| 72 |
|
| 73 |
>>> fuzz.ratio("this is a test", "this is a test!") |
| 74 |
97 |
| 75 |
|
| 76 |
Partial Ratio |
| 77 |
~~~~~~~~~~~~~ |
| 78 |
|
| 79 |
.. code:: python |
| 80 |
|
| 81 |
>>> fuzz.partial_ratio("this is a test", "this is a test!") |
| 82 |
100 |
| 83 |
|
| 84 |
Token Sort Ratio |
| 85 |
~~~~~~~~~~~~~~~~ |
| 86 |
|
| 87 |
.. code:: python |
| 88 |
|
| 89 |
>>> fuzz.ratio("fuzzy wuzzy was a bear", "wuzzy fuzzy was a bear") |
| 90 |
91 |
| 91 |
>>> fuzz.token_sort_ratio("fuzzy wuzzy was a bear", "wuzzy fuzzy was a bear") |
| 92 |
100 |
| 93 |
|
| 94 |
Token Set Ratio |
| 95 |
~~~~~~~~~~~~~~~ |
| 96 |
|
| 97 |
.. code:: python |
| 98 |
|
| 99 |
>>> fuzz.token_sort_ratio("fuzzy was a bear", "fuzzy fuzzy was a bear") |
| 100 |
84 |
| 101 |
>>> fuzz.token_set_ratio("fuzzy was a bear", "fuzzy fuzzy was a bear") |
| 102 |
100 |
| 103 |
|
| 104 |
Process |
| 105 |
~~~~~~~ |
| 106 |
|
| 107 |
.. code:: python |
| 108 |
|
| 109 |
>>> choices = ["Atlanta Falcons", "New York Jets", "New York Giants", "Dallas Cowboys"] |
| 110 |
>>> process.extract("new york jets", choices, limit=2) |
| 111 |
[('New York Jets', 100), ('New York Giants', 78)] |
| 112 |
>>> process.extractOne("cowboys", choices) |
| 113 |
("Dallas Cowboys", 90) |
| 114 |
|
| 115 |
You can also pass additional parameters to ``extractOne`` method to make it use a specific scorer. A typical use case is to match file paths: |
| 116 |
|
| 117 |
.. code:: python |
| 118 |
|
| 119 |
>>> process.extractOne("System of a down - Hypnotize - Heroin", songs) |
| 120 |
('/music/library/good/System of a Down/2005 - Hypnotize/01 - Attack.mp3', 86) |
| 121 |
>>> process.extractOne("System of a down - Hypnotize - Heroin", songs, scorer=fuzz.token_sort_ratio) |
| 122 |
("/music/library/good/System of a Down/2005 - Hypnotize/10 - She's Like Heroin.mp3", 61) |
| 123 |
|
| 124 |
.. |Build Status| image:: https://api.travis-ci.org/seatgeek/fuzzywuzzy.png?branch=master |
| 125 |
:target: https:travis-ci.org/seatgeek/fuzzywuzzy |
| 126 |
|
| 127 |
Known Ports |
| 128 |
============ |
| 129 |
|
| 130 |
FuzzyWuzzy is being ported to other languages too! Here are a few ports we know about: |
| 131 |
|
| 132 |
- Java: `xpresso's fuzzywuzzy implementation <https://github.com/WantedTechnologies/xpresso/wiki/Approximate-string-comparison-and-pattern-matching-in-Java>`_ |
| 133 |
- Java: `fuzzywuzzy (java port) <https://github.com/xdrop/fuzzywuzzy>`_ |
| 134 |
- Rust: `fuzzyrusty (Rust port) <https://github.com/logannc/fuzzyrusty>`_ |
| 135 |
- JavaScript: `fuzzball.js (JavaScript port) <https://github.com/nol13/fuzzball.js>`_ |
| 136 |
- C++: `Tmplt/fuzzywuzzy <https://github.com/Tmplt/fuzzywuzzy>`_ |
| 137 |
- C#: `fuzzysharp (.Net port) <https://github.com/BoomTownRoi/BoomTown.FuzzySharp>`_ |
| 138 |
- Go: `go-fuzzywuzz (Go port) <https://github.com/paul-mannino/go-fuzzywuzzy>`_ |
| 139 |
- Free Pascal: `FuzzyWuzzy.pas (Free Pascal port) <https://github.com/DavidMoraisFerreira/FuzzyWuzzy.pas>`_ |
| 140 |
- Kotlin multiplatform: `FuzzyWuzzy-Kotlin <https://github.com/willowtreeapps/fuzzywuzzy-kotlin>`_ |
| 141 |
- R: `fuzzywuzzyR (R port) <https://github.com/mlampros/fuzzywuzzyR>`_ |