Python fuzzy search and replace

半腔热情 提交于 2019-12-13 19:35:36

问题


I need to perfom fuzzy search for sub-string in string and replace that part. For example:

str_a = "Alabama"
str_b = "REPLACED"
orig_str = "Flabama is a state located in the southeastern region of the United States."
print(fuzzy_replace(str_a, str_b, orig_str)) # fuzzy_replace code should be implemented
# Output: REPLACED is a state located in the southeastern region of the United States.

The search itself is simple with fuzzywuzzy module, but it gives me only ratio of difference between strings. Are there any ways to find a position in original string where sub-string fuzzy matches to?


回答1:


Try this..

from fuzzywuzzy import fuzz

def fuzzy_replace(str_a, str_b, orig_str):
    l = len(str_a.split()) # Length to read orig_str chunk by chunk
    splitted = orig_str.split()
    for i in range(len(splitted)-l+1):
        test = " ".join(splitted[i:i+l])
        if fuzz.ratio(str_a, test) > 75: #Using fuzzwuzzy library to test ratio
            before = " ".join(splitted[:i])
            after = " ".join(splitted[i+1:])
            return before+" "+str_b+" "+after #Output will be sandwich of these three strings

str_a = "Alabama is a"
str_b = "REPLACED"
orig_str = "Flabama is a state located in the southeastern region of the United States."
print fuzzy_replace(str_a, str_b, orig_str)

This prints

 REPLACED state located in the southeastern region of the United States.


来源:https://stackoverflow.com/questions/34197917/python-fuzzy-search-and-replace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!