2024-10-03 02:44:00 发布
网友
我用fuzzyfuzzy来计算两个句子之间的相似度。以下是一些对我来说毫无意义的结果:
from fuzzywuzzy import fuzz s1 = "moist tender pork loin chop" s2 = "corn bicolor" fuzz.token_sort_ratio(s1,s2)
这给了我41分。另一方面:
我得了18分。在
事实上有重叠词的两个句子之间的分数怎么可能低于没有重叠词的句子的分数?在
谢谢你!在
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_比率和token_set_比率之间存在重要的细节差异。在
token_sort_比率有四个步骤:
注意,这个算法不关心部分匹配
当这些步骤发生在字符串上时,代码本质上变成:
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_比率因为这说明了玉米在这两条线中都有,并且可以相应地匹配
Fuzzywuzzy是使用Levenshtein距离实现的。从wikipedia:
编辑 正如@dennis golomazov所指出的。token_sort_比率和token_set_比率之间存在重要的细节差异。在
token_sort_比率有四个步骤:
注意,这个算法不关心部分匹配
当这些步骤发生在字符串上时,代码本质上变成:
您将注意到这些比率与您在测试用例中看到的比率相匹配。在
所以,你肯定想用fuzz.token_set_比率因为这说明了玉米在这两条线中都有,并且可以相应地匹配
相关问题 更多 >
编程相关推荐