How to get matched text from a given list which is given to fuzzy wuzzy partial_ratio()?

1.2k views Asked by At

I have a string and a list of strings. I just want to know which text in the list is 100% partially matched with the given string.

from fuzzywuzzy import fuzz 

s1 = "Hello"
s_list= ["Hai all", "Hello world", "Thank you"]

fuzz.partial_ratio(s1, s_list)

For this am getting 100. Since "Hello" has a partial match with "Hello world" But how can I get "Hello World" as output?

Could anyone help me with this? Thanks in advance.

2

There are 2 answers

1
Neil On BEST ANSWER

You do not need fuzzywuzzy for exact matching. Fuzzywuzzy is for fuzzy matching. Fuzzywuzzy cannot produce indexes for matches precisely because, in general, there is no "match", just distances.

All you need is Python.

s1 = "Hello"
s_list= ["Hai all", "Hello world", "Thank you"]

for item in s_list:
    if s1 in item:
        print("item: " + item + "\ns1" + s1)
4
kuco 23 On

You can use a different function

from fuzzywuzzy import process

s1 = "Hello"
s_list= ["Hai all", "Hello world", "Thank you"]

[s for s, m in process.extract(s1, s_list) if m == 100]

For more info check help(process.extract). If you strictly want the 100% partial matches, Neil's answer is better.