动态调用python方法[重复]

问题描述 投票:1回答:1

我想遍历所有模糊匹配方法,从Fuzzywuzzy包中确定哪一个最适合我的数据。

代码:

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无效。

我看了其他答案,但听不懂。

python nlp fuzzywuzzy
1个回答
0
投票

method_list正在存储方法名称列表,而不是实际方法。调用它们时添加getattr方法。

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

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

成功功能仍然存在问题。 high_ratio和low_ratio不可比较,因为它们是SequenceMatchers。您将需要首先获得分数。

© www.soinside.com 2019 - 2024. All rights reserved.