如何检查字符串是否相似

时间:2021-07-08 04:45:55

标签: python fuzzywuzzy fuzzy

我有以下字符串

text
USA guidances/regulations
US guidances/regulations
96
text
US guidances/regulations
US guidances/regulations
100
text
Australia guidances/regulations
US guidances/regulations
92
text
China Guidances/Regulations
US guidances/regulations
92
text
EU guidances/regulations
US guidances/regulations
98

text 下的第一个是输入字符串,第二个是与之匹配的字符串。最后是他们的fuzzywuzzy比率。我是这样匹配的:

ratio = fuzz.partial_ratio(t.lower(), txt.lower())

如果国家/地区名称不同,则应返回较低的分数,而不是相似时。有没有办法做到这一点?

1 个答案:

答案 0 :(得分:1)

根据您提供的代码,要比较的文本似乎有一个模式

country_name + "guidances/regulations"

可以通过 spilt() 方法获取国家名称

>>> str = 'US guidances/regulations'
>>> myList = str.split(' ') //spilt by the space after the country name
>>> myList[0]
US
>>> myList[1]
guidances/regulations

然后比较仅国家名称

anotherStr = 'USA guidances/regulations'
anotherList = anotherStr.split(' ') //spilt by the space after the country name
ratio = fuzz.partial_ratio(myList[0].lower(), anotherList[0]())