-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlistfilter.py
31 lines (24 loc) · 900 Bytes
/
listfilter.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
import unicodedata
from curses.ascii import isprint
from fuzzywuzzy import fuzz
def filter_printable(s):
return ''.join(c for c in s if c in string.printable)
class ListFilter(object):
def __init__(self):
self._search_key = ""
def update_search_key(self, search_key):
self._search_key = self._normalize(search_key).decode('utf-8','ignore')
def _normalize(self, title):
title = filter_printable(title)
for c in [" ", "\n", "\t"]:
title = title.replace(c, "")
title = title.lower()
return title
def get_candidate_score(self, candidate):
if not self._search_key:
return 100
candidate = self._normalize(candidate)
if candidate in self._search_key or self._search_key in candidate:
return 100
score = fuzz.ratio(self._search_key, candidate)
return score