Fuzzy String Comparison

后端 未结 4 1044
情歌与酒
情歌与酒 2020-11-29 17:01

What I am striving to complete is a program which reads in a file and will compare each sentence according to the original sentence. The sentence which is a perfect match to

4条回答
  •  佛祖请我去吃肉
    2020-11-29 17:28

    There is a module in the standard library (called difflib) that can compare strings and return a score based on their similarity. The SequenceMatcher class should do what you are after.

    EDIT: Small example from python prompt:

    >>> from difflib import SequenceMatcher as SM
    >>> s1 = ' It was a dark and stormy night. I was all alone sitting on a red chair. I was not completely alone as I had three cats.'
    >>> s2 = ' It was a murky and stormy night. I was all alone sitting on a crimson chair. I was not completely alone as I had three felines.'
    >>> SM(None, s1, s2).ratio()
    0.9112903225806451
    

    HTH!

提交回复
热议问题