简体   繁体   中英

How to check if strings are similar

I have the following strings

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

First one under text is input string and the second is the one against which it is matched. Last is their fuzzywuzzy ratio. I'm matching it like this:

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

If the country name is different it should return a lower score as opposed to when it is similar. Is there any way to do this?

According to the code you've provided, the text to compare seems to have a pattern of

country_name + "guidances/regulations"

You can get the country name by spilt() method

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

then compare only the Country name

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM