What is a simple fuzzy string matching algorithm in Python?

前端 未结 7 901
不知归路
不知归路 2020-12-13 00:55

I\'m trying to find some sort of a good, fuzzy string matching algorithm. Direct matching doesn\'t work for me — this isn\'t too good because unless my strings are a 100% si

7条回答
  •  佛祖请我去吃肉
    2020-12-13 01:28

    I like Drew's answer.

    You can use difflib to find the longest match:

    >>> a = 'The quick brown fox.'
    >>> b = 'The quick brown fox jumped over the lazy dog.'
    >>> import difflib
    >>> s = difflib.SequenceMatcher(None, a, b)
    >>> s.find_longest_match(0,len(a),0,len(b))
    Match(a=0, b=0, size=19) # returns NamedTuple (new in v2.6)
    

    Or pick some minimum matching threshold. Example:

    >>> difflib.SequenceMatcher(None, a, b).ratio()
    0.61538461538461542
    

提交回复
热议问题