tfidf_matcher is a package for fuzzymatching large datasets together.
Most fuzzy matching libraries like fuzzywuzzy get great results, but
don't scale well due to their O(n^2) complexity.
How does it work?
This package provides two functions:
ngrams(): Simple ngram generator.matcher(): Matches a list of strings against a reference corpus. Does this by:- Vectorizing the reference corpus using TF-IDF into a term-document matrix.
- Fitting a K-NearestNeighbours model to the sparse matrix.
- Vectorizing the list of strings to be matched and passing it in
to the KNN model to calculate the cosine distance (the OOTB
cosine_similarityfunction in sklearn is very memory-inefficient for our use case). - Some data manipulation to emit
k_matchesclosest matches.
Yeah ok, but how do I use it?
Define two lists; your original list (list you want matches for) and
your lookup list (list you want to match against). Typically your
lookup list will be much longer than your original list. Pass them into
the matcher function along with the number of matches you want to
display from the lookup list using the k_matches argument. The
result will be a pandas DataFrame containing 1 row per item in your
original list, along with k_matches columns containing the closest
match from the lookup list, and a match score for the closest match
(which is 1 - the cosine distance between the matches normalised to
[0,1])
Simply import with import tfidf_matcher as tm, and call the matcher
function with tm.matcher(). It takes the following arguments:
original: List of strings you want to match.lookup: List of strings you want to match against.k_matches: Number of the closest results fromlookupto return (1 per column).ngram_length: Length ofngramsused in the algorithm. Anecdotal testing shows 2 or 3 to be optimal, but feel free to tinker.
Strengths and Weaknesses
- Quick. Very quick.
- Can emit however many closest matches you want. I found that 3 worked best.
- Not very well tested so potentially unstable results. Worked well for 640 company names matched against a lookup corpus of >700,000 company names.
Who do I thank?
For the method, thank Josh Taylor and Chris van den Berg. I wanted to adapt the methods to work nicely on a company mathcing problem I was having, and decided to build out my resultant code into a package for two reasons:
- Package building experience.
- Utility for future projects which may require large-domain fuzzy matching.
I understand the algorithms behind k-Nearest Neighbours & TF-IDF Vectorisation, but it was through implementing the ideas in the blogs linked that I was able to build this project out.
Metadata
Release files for tfidf-matcher 0.3.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| tfidf_matcher-0.3.0.tar.gz | 8.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| tfidf_matcher-0.3.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 16.1 kB
Release files / tfidf_matcher-0.3.0.tar.gz
| Download URL | tfidf_matcher-0.3.0.tar.gz |
|---|---|
| Size | 8.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
26939ab3f7f12c6c7421af83ab974c1707aa9724eb99de2f66f00bd5d5d5b468
|
|
BLAKE2b-256 checksum How to use checksums |
cccdaf378c05b1f199879f2deed7c31abc4175dad73052f08832a4b4752cdbf6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.7
|
Release files / tfidf_matcher-0.3.0-py3-none-any.whl
| Download URL | tfidf_matcher-0.3.0-py3-none-any.whl |
|---|---|
| Size | 8.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
1f5ba14465a9d6a3fbb244d77319240908110ab81cd453bbb26ab99fff687210
|
|
BLAKE2b-256 checksum How to use checksums |
02bf9c086937798418e7eb283b97638e478ec39333bbed646b85c98024fd4b8c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.10.7
|