fuzzywuzzy's process.extract() returns the list in reverse sorted order , with the best match coming first.
so to find just the best match, you can set the limit argument as 1 , so that it only returns the best match, and if that is greater than 60 , you can write it to the csv, like you are doing now.
Example -
from fuzzywuzzy import process
## For each row in the lookup compute the partial ratio
for row in parse_csv("names_2.csv"):
for found, score, matchrow in process.extract(row, data, limit=1):
if score >= 60:
print('%d%% partial match: "%s" with "%s" ' % (score, row, found))
Digi_Results = [row, score, found]
writer.writerow(Digi_Results)
Answer from Anand S Kumar on Stack Overflow
» pip install fuzzywuzzy
fuzzywuzzy's process.extract() returns the list in reverse sorted order , with the best match coming first.
so to find just the best match, you can set the limit argument as 1 , so that it only returns the best match, and if that is greater than 60 , you can write it to the csv, like you are doing now.
Example -
from fuzzywuzzy import process
## For each row in the lookup compute the partial ratio
for row in parse_csv("names_2.csv"):
for found, score, matchrow in process.extract(row, data, limit=1):
if score >= 60:
print('%d%% partial match: "%s" with "%s" ' % (score, row, found))
Digi_Results = [row, score, found]
writer.writerow(Digi_Results)
Several pieces of your code can be greatly simplified by using process.extractOne() from FuzzyWuzzy. Not only does it just return the top match, you can set a score threshold for it within the function call, rather than needing to perform a separate logical step, e.g.:
process.extractOne(row, data, score_cutoff = 60)
This function will return a tuple of the highest match plus the accompanying score if it finds a match satisfying the condition. It will return None otherwise.
The other answer is wrong in a key respect - the inference that the result of process.extract was the same as fuzz.partial_ratio in one case, therefore they are doing the same thing by default.
process.extract actually uses WRatio() by default, which is a weighted combination of the four fuzz ratios. This is actually a cool functionality that empirically works pretty well across fuzzy matching scenarios.
Still, you can manually specify the string comparison function via the scorer argument to extract
Source for process.extract:https://github.com/seatgeek/fuzzywuzzy/blob/master/fuzzywuzzy/process.py
There are four ratio in fuzzywuzzy comparison.
base_ratio: The Levenshtein Distance of two string.partial_ratio: The ratio of most similar substring.token_sort_ratio: Measure of the sequences' similarity sorting the token before comparing.token_set_ratio: Find all alphanumeric tokens in each string.
More details on ration can be found here http://chairnerd.seatgeek.com/fuzzywuzzy-fuzzy-string-matching-in-python/
By default process.extract() use Partial_ratio for comparison, but you can also override it with third parameter to process.extract()
Ex.
print(fuzz.partial_ratio('Total replenishment lead time (in workdays)', 'Lead_time_planning'))
query = 'Total replenishment lead time (in workdays)'
choices = ['PLANNING_TIME_FENCE_CODE', 'BUILD_IN_WIP_FLAG','Lead_time_planning']
print(process.extract(query, choices))
Results will be :
50
[('Lead_time_planning', 50), ('PLANNING_TIME_FENCE_CODE', 38), ('BUILD_IN_WIP_FLAG', 26)]
Which shows it is by default using partial_ratio, which you can override anytime.