gpt4 book ai didi

python - 没有重叠词的句子的 Fuzzywuzzy 分数高于有重叠词的句子?

转载 作者:行者123 更新时间:2023-11-28 18:10:07 24 4
gpt4 key购买 nike

我正在使用 fuzzywuzzy 来计算两个句子之间的相似度。以下是一些对我来说毫无意义的结果:

from fuzzywuzzy import fuzz

s1 = "moist tender pork loin chop"
s2 = "corn bicolor"
fuzz.token_sort_ratio(s1,s2)

这给了我 41 分。另一方面:

s1 = "store cut sweet yellow corn tray"
s2 = "corn bicolor"
fuzz.token_sort_ratio(s1,s2)

给我 18 分。

实际上有重叠词(在本例中为“corn”)的两个句子之间的分数怎么会低于没有重叠词的句子的分数?

谢谢!

最佳答案

Fuzzywuzzy使用 Levenshtein 距离实现。来自wikipedia :

Informally, the Levenshtein distance between two words is the minimum number of single-character edits (insertions, deletions or substitutions) required to change one word into the other.

编辑正如@dennis-golomazov 所指出的那样。 token_sort_ratio 和 token_set_ratio 之间存在重要的细节差异。

token_sort_ratio 有四个步骤:

  1. 将字符串拆分为标记
  2. 排序 token
  3. https://github.com/ztane/python-Levenshtein 调用 Levenshtein 比率在排序的 token 上。
  4. 返回比例 * 100

注意这个算法不关心部分匹配

当这些步骤发生在你的字符串上时,代码基本上变成了:

from Levenshtein import StringMatcher as sm

s1 = "chop loin moist tender pork"
s2 = "bicolor corn"

m = sm.StringMatcher(None, s1, s2)
print(int(m.ratio() * 100))

s1 = "corn cut store sweet tray yellow"
s2 = "bicolor corn"

m = sm.StringMatcher(None, s1, s2)
print(int(m.ratio() * 100))

您会注意到这些比率与您在测试用例中看到的比率相匹配。

因此,您肯定会希望使用 fuzz.token_set_ratio,因为它说明了 corn 在两个字符串中并且可以进行相应匹配的事实

关于python - 没有重叠词的句子的 Fuzzywuzzy 分数高于有重叠词的句子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51197857/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com