python fuzzywuzzy fuzzy matching - exclude terms

Viewed 396

I am fairly new to python, have been using fuzzywuzzy to do some fuzzy matching with success. I am wondering, however, if there is way to exclude terms from the algorithm? Generic terms can often be matched to a ton of options, and I would like to prevent the algorithm from matching on those terms without doing a lot of pre-processing. I cannot seem to find any examples / documentation.

1 Answers

You could use the builtin difflib for this.

import difflib
search_list = ['ape', 'apple', 'peach', 'puppy']
matches = difflib.get_close_matches('appel', possibilities=search_list, cutoff=0.6)

print(matches)
['apple', 'ape']

exclude_list = ['ape']

matches_with_exclusion = [x for x in matches if x not in exclude_list]
print(matches_with_exclusion) 
['apple']
Related