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
» pip install fuzzywuzzy
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.
I'm testing fuzzywuzzy's process.extractBests as follows:
from fuzzywuzzy import process
# Define the query string
query = "Apple"
# Define the list of choices
choices = ["Apple", "Apple Inc.", "Apple Computer", "Apple Records", "Apple TV"]
# Call the process.extractBests function
results = process.extractBests(query, choices)
# Print the results
for result in results:
print(result)It outputs:
('Apple', 100)
('Apple Inc.', 90)
('Apple Computer', 90)
('Apple Records', 90)
('Apple TV', 90)Why didn't the scorer give 100 to all strings since they all 100% contain the query string ("Apple")?
I use fuzzywuzzy==0.18.0 with Python 3.11.7.