为fuzzywuzzy process.extractOne设置阈值



我目前正在两个不同零售商之间进行一些字符串产品相似性匹配,并使用fuzzywuzzyprocess.extractOne函数来找到最佳匹配。

然而,我希望能够设置一个评分阈值,这样只有当评分高于某个阈值时,产品才会匹配,因为目前它只是根据最接近的字符串匹配每一个产品。

下面的代码给了我最好的匹配:(目前正在获取错误(

title, index, score = process.extractOne(text, choices_dict)

然后我尝试了以下代码来尝试设置阈值:

title, index, score = process.extractOne(text, choices_dict, score_cutoff=80)

导致以下类型错误:

TypeError: cannot unpack non-iterable NoneType object

最后,我还尝试了以下代码:

title, index, scorer, score = process.extractOne(text, choices_dict, scorer=fuzz.token_sort_ratio, score_cutoff=80)

这导致以下错误:

ValueError: not enough values to unpack (expected 4, got 3)

当最佳分数低于score_cutoff时,

process.extractOne将返回None。因此,您要么必须检查None,要么捕获异常:

best_match = process.extractOne(text, choices_dict, score_cutoff=80)
if best_match:
value, score, key = best_match
print(f"best match is {key}:{value} with the similarity {score}")
else:
print("no match found")

try:
value, score, key = process.extractOne(text, choices_dict, score_cutoff=80)
print(f"best match is {key}:{value} with the similarity {score}")
except TypeError:
print("no match found")

最新更新