Method: difflib.get_close_matches
Calls: 4, Exceptions: 0, Paths: 2Back
Path 1: 3 calls (0.75)
'appel' (1) 'wheel' (1) 'accept' (1)
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'f...
3 (3)
0.6 (3)
['apple', 'ape'] (1) ['while'] (1) ['except'] (1)
1def get_close_matches(word, possibilities, n=3, cutoff=0.6):
2 """Use SequenceMatcher to return list of the best "good enough" matches.
3
4 word is a sequence for which close matches are desired (typically a
5 string).
6
7 possibilities is a list of sequences against which to match word
8 (typically a list of strings).
9
10 Optional arg n (default 3) is the maximum number of close matches to
11 return. n must be > 0.
12
13 Optional arg cutoff (default 0.6) is a float in [0, 1]. Possibilities
14 that don't score at least that similar to word are ignored.
15
16 The best (no more than n) matches among the possibilities are returned
17 in a list, sorted by similarity score, most similar first.
18
19 >>> get_close_matches("appel", ["ape", "apple", "peach", "puppy"])
20 ['apple', 'ape']
21 >>> import keyword as _keyword
22 >>> get_close_matches("wheel", _keyword.kwlist)
23 ['while']
24 >>> get_close_matches("Apple", _keyword.kwlist)
25 []
26 >>> get_close_matches("accept", _keyword.kwlist)
27 ['except']
28 """
29
30 if not n > 0:
31 raise ValueError("n must be > 0: %r" % (n,))
32 if not 0.0 <= cutoff <= 1.0:
33 raise ValueError("cutoff must be in [0.0, 1.0]: %r" % (cutoff,))
34 result = []
35 s = SequenceMatcher()
36 s.set_seq2(word)
37 for x in possibilities:
38 s.set_seq1(x)
39 if s.real_quick_ratio() >= cutoff and \
40 s.quick_ratio() >= cutoff and \
41 s.ratio() >= cutoff:
42 result.append((s.ratio(), x))
43
44 # Move the best scorers to head of list
45 result = _nlargest(n, result)
46 # Strip scores for the best n matches
47 return [x for score, x in result]
Path 2: 1 calls (0.25)
'Apple' (1)
['False', 'None', 'True', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'f...
3 (1)
0.6 (1)
[] (1)
1def get_close_matches(word, possibilities, n=3, cutoff=0.6):
2 """Use SequenceMatcher to return list of the best "good enough" matches.
3
4 word is a sequence for which close matches are desired (typically a
5 string).
6
7 possibilities is a list of sequences against which to match word
8 (typically a list of strings).
9
10 Optional arg n (default 3) is the maximum number of close matches to
11 return. n must be > 0.
12
13 Optional arg cutoff (default 0.6) is a float in [0, 1]. Possibilities
14 that don't score at least that similar to word are ignored.
15
16 The best (no more than n) matches among the possibilities are returned
17 in a list, sorted by similarity score, most similar first.
18
19 >>> get_close_matches("appel", ["ape", "apple", "peach", "puppy"])
20 ['apple', 'ape']
21 >>> import keyword as _keyword
22 >>> get_close_matches("wheel", _keyword.kwlist)
23 ['while']
24 >>> get_close_matches("Apple", _keyword.kwlist)
25 []
26 >>> get_close_matches("accept", _keyword.kwlist)
27 ['except']
28 """
29
30 if not n > 0:
31 raise ValueError("n must be > 0: %r" % (n,))
32 if not 0.0 <= cutoff <= 1.0:
33 raise ValueError("cutoff must be in [0.0, 1.0]: %r" % (cutoff,))
34 result = []
35 s = SequenceMatcher()
36 s.set_seq2(word)
37 for x in possibilities:
38 s.set_seq1(x)
39 if s.real_quick_ratio() >= cutoff and \
40 s.quick_ratio() >= cutoff and \
41 s.ratio() >= cutoff:
42 result.append((s.ratio(), x))
43
44 # Move the best scorers to head of list
45 result = _nlargest(n, result)
46 # Strip scores for the best n matches
47 return [x for score, x in result]