Используя алгоритмы типа leveinstein (leveinstein или difflib), легко найти приблизительные соответствия.
>>> import difflib
>>> difflib.SequenceMatcher(None,"amazing","amaging").ratio()
0.8571428571428571
Нечеткие совпадения могут быть обнаружены путем определения порога по мере необходимости.
Текущее требование: найти нечеткую подстроку на основе порога в большей строке.
например.
large_string = "thelargemanhatanproject is a great project in themanhattincity"
query_string = "manhattan"
#result = "manhatan","manhattin" and their indexes in large_string
Одно решение для грубой силы состоит в том, чтобы сгенерировать все подстроки длиной от N-1 до N + 1 (или другой подходящей длины), где N - длина query_string, и использовать левенштайн на них один за другим и видеть порог.
Есть ли лучшее решение, доступное в python, желательно включенный модуль в python 2.7 или внешний доступный модуль.
UPDATE: модуль регулярного выражения Python работает очень хорошо, хотя он немного медленнее, чем встроенный модуль re
для случаев нечеткой подстроки, что является очевидным результатом из-за дополнительных операций.
Желаемый результат хорош, и контроль над величиной нечеткости можно легко определить.
>>> import regex
>>> input = "Monalisa was painted by Leonrdo da Vinchi"
>>> regex.search(r'\b(leonardo){e<3}\s+(da)\s+(vinci){e<2}\b',input,flags=regex.IGNORECASE)
<regex.Match object; span=(23, 41), match=' Leonrdo da Vinchi', fuzzy_counts=(0, 2, 1)>