proyectos ejemplos python string nlp fuzzy-search

ejemplos - Python difusa búsqueda y reemplazar



django (1)

Necesito realizar una búsqueda difusa de subcadena en cadena y reemplazar esa parte. Por ejemplo:

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.

La búsqueda en sí es simple con el módulo fuzzywuzzy , pero me da solo una relación de diferencia entre cadenas. ¿Hay alguna forma de encontrar una posición en la secuencia original donde la subcadena difusa coincide con?


Prueba esto..

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)

Esto imprime

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