1

I want to loop through all fuzzy matching methods to determine which is the best for my data, from the package fuzzywuzzy.

Code:

from fuzzywuzzy import fuzz

# Discover ratio.
# This set should give a higher match than the set below.
high_match = ('AMERICAN SIGN LANG & ENG SECONDAR',
                   '47 The American Sign Language and English Secondary School')

low_match = ('AMERICAN SIGN LANG & ENG SECONDAR',
                   'The 47 American Sign Language & English Lower School')

method_list = [func for func in dir(fuzz) if callable(getattr(fuzz, func))]

for method in method_list:

    high_ratio = fuzz.method(*high_match)

    low_ratio = fuzz.method(*low_match)

    def success(high_ratio, low_ratio):
        if high_ratio > low_ratio:
            return 'success'
        else:
            return 'failure'

    print(f'The method {method} produced {success(high_ratio,low_ratio)}')

fuzz.method is invalid.

I looked at other answers but didn't understand.

Christina Zhou
  • 1,483
  • 1
  • 7
  • 17
  • 1
    You already have your answer, `getattr(fuzz, method)` will dynamically access the attribute, now you just have to call it, e.g. `getattr(fuzz, method)(*low_match)` – juanpa.arrivillaga Mar 02 '20 at 21:17

1 Answers1

0

method_list was storing a list of the method names not the actual method. Add the getattr method when calling them.

for method in method_list:
    high_ratio = getattr(fuzz,method)(*high_match)

    low_ratio =  getattr(fuzz,method)(*low_match)

There is still an issue in your success function. high_ratio and low_ratio are not comparable because they are SequenceMatchers. You will need to get the score from each first.

Rusty Robot
  • 1,725
  • 2
  • 13
  • 29