MCPcopy Index your code
hub / github.com/RustPython/RustPython / get_close_matches

Function get_close_matches

Lib/difflib.py:666–712  ·  view source on GitHub ↗

Use SequenceMatcher to return list of the best "good enough" matches. word is a sequence for which close matches are desired (typically a string). possibilities is a list of sequences against which to match word (typically a list of strings). Optional arg n (default 3) is the

(word, possibilities, n=3, cutoff=0.6)

Source from the content-addressed store, hash-verified

664
665
666def get_close_matches(word, possibilities, n=3, cutoff=0.6):
667 """Use SequenceMatcher to return list of the best "good enough" matches.
668
669 word is a sequence for which close matches are desired (typically a
670 string).
671
672 possibilities is a list of sequences against which to match word
673 (typically a list of strings).
674
675 Optional arg n (default 3) is the maximum number of close matches to
676 return. n must be > 0.
677
678 Optional arg cutoff (default 0.6) is a float in [0, 1]. Possibilities
679 that don't score at least that similar to word are ignored.
680
681 The best (no more than n) matches among the possibilities are returned
682 in a list, sorted by similarity score, most similar first.
683
684 >>> get_close_matches("appel", ["ape", "apple", "peach", "puppy"])
685 ['apple', 'ape']
686 >>> import keyword as _keyword
687 >>> get_close_matches("wheel", _keyword.kwlist)
688 ['while']
689 >>> get_close_matches("Apple", _keyword.kwlist)
690 []
691 >>> get_close_matches("accept", _keyword.kwlist)
692 ['except']
693 """
694
695 if not n > 0:
696 raise ValueError("n must be > 0: %r" % (n,))
697 if not 0.0 <= cutoff <= 1.0:
698 raise ValueError("cutoff must be in [0.0, 1.0]: %r" % (cutoff,))
699 result = []
700 s = SequenceMatcher()
701 s.set_seq2(word)
702 for x in possibilities:
703 s.set_seq1(x)
704 if s.real_quick_ratio() >= cutoff and \
705 s.quick_ratio() >= cutoff and \
706 s.ratio() >= cutoff:
707 result.append((s.ratio(), x))
708
709 # Move the best scorers to head of list
710 result = _nlargest(n, result)
711 # Strip scores for the best n matches
712 return [x for score, x in result]
713
714
715def _keep_original_ws(s, tag_s):

Callers

nothing calls this directly

Calls 7

set_seq2Method · 0.95
set_seq1Method · 0.95
real_quick_ratioMethod · 0.95
quick_ratioMethod · 0.95
ratioMethod · 0.95
SequenceMatcherClass · 0.85
appendMethod · 0.45

Tested by

no test coverage detected