def get_match_score(phrase, words, min_distance=2): score = 0 phrase_len = len(''.join(phrase)) for p in phrase: matcher = StringMatcher(seq1=p) for w in words: matcher.set_seq2(w) match_distance = matcher.distance() if match_distance <= min_distance: score += max(0, len(p) - match_distance) / phrase_len return score
def is_typo(word, word_from_dict): sm = StringMatcher() sm.set_seq1(word) sm.set_seq2(word_from_dict) dist = sm.distance() return dist == 1 or (dist == 2 and fl(word, word_from_dict))