-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuzzystringmatch.py
64 lines (45 loc) · 1.78 KB
/
fuzzystringmatch.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import re
from thefuzz import fuzz
def fuzzy_approx_match_string(query_string, sentence):
strings = query_string.split(' ')
strings = [string.strip() for string in strings if string != '']
ret = []
for string in strings:
match_ratio = fuzz.partial_token_sort_ratio(string, sentence)
if match_ratio >= 70.0:
ret.append(sentence)
break
return len(ret) > 0, ret
def fuzzy_approx_match_string_list(query_string, sentence_list):
matched_sentence = []
for sentence in sentence_list:
matched, match_info = fuzzy_approx_match_string(query_string, sentence)
if matched:
matched_sentence.append(sentence)
return matched_sentence
def fuzzy_regex_match_string(query_string, sentence):
strings = query_string.split(' ')
strings = [string.strip() for string in strings if string != '']
ret = []
for string in strings:
match = re.search(string, sentence, re.IGNORECASE)
if match is not None:
ret.append(match)
return len(ret) > 0, ret
def fuzzy_regex_match_string_list(query_string, sentence_list):
matched_sentence = []
for sentence in sentence_list:
matched, match_info = fuzzy_regex_match_string(query_string, sentence)
if matched:
matched_sentence.append((match_info, sentence))
return matched_sentence
def fuzzy_regex_match_window_list(query_string, window_list):
matched_sentence = []
for window in window_list:
matched, match_info = fuzzy_regex_match_string(query_string, window.title)
if matched:
matched_sentence.append((match_info, window))
return matched_sentence
if __name__ == '__main__':
ret = fuzzy_regex_match_string("win open", "Windows list open windows")
print(ret)