-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautocomplete.py
More file actions
105 lines (84 loc) · 2.87 KB
/
autocomplete.py
File metadata and controls
105 lines (84 loc) · 2.87 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import string
import datrie
import operator
class CommonEqualityMixin(object):
def __eq__(self, other):
"""Override the default Equals behavior"""
if isinstance(other, self.__class__):
return self.__dict__ == other.__dict__
return NotImplemented
def __ne__(self, other):
"""Define a non-equality test"""
if isinstance(other, self.__class__):
return not self.__eq__(other)
return NotImplemented
def __hash__(self):
"""Override the default hash behavior (that returns the id or the object)"""
return hash(tuple(sorted(self.__dict__.items())))
class Candidate(CommonEqualityMixin):
word = None
confidence = None
def __init__(self, word, confidence):
self.word = word
self.confidence = confidence
def getWord(self):
"""
:return the autocomplete candidate
"""
return self.word
def getConfidence(self):
"""
:return the confidence* for the candidate
"""
return self.confidence
def __str__(self):
"""
:return: candidate data in the format "<word>" (<confidence>)
"""
return '"' + self.word + '" (' + str(self.confidence) + ')' #
class AutocompleteProvider():
# class variables
trie_characters = string.ascii_lowercase
def __init__(self):
self.trie = datrie.Trie(self.trie_characters)
def getWords(self, fragment):
"""
:param fragment: string to lookup
:return list of candidates ordered by confidence*
"""
items = self.trie.items(fragment)
items.sort(key=operator.itemgetter(1), reverse=True)
candidates = []
for item in items:
candidates.append(Candidate(item[0], item[1]))
return candidates
def train(self, passage):
"""
trains the algorithm with the provided passage
:param passage: string to train autocomplete upon
:return: nothing
"""
words = passage.lower().split()
for word in words:
word = self.scrub(word)
self.trie.setdefault(word, 0)
self.trie[word] += 1
def scrub(self, value):
return ''.join(s for s in value.lower() if s in self.trie_characters)
@staticmethod
def words2String(words):
return ', '.join(map(str, words))
autocompleteProvider = AutocompleteProvider()
def trainPassage(passage):
print("Train: \"" + passage + "\"")
autocompleteProvider.train(passage)
def printWords(word):
words = autocompleteProvider.getWords(word)
print("Input: \""+ word + "\" --> " + AutocompleteProvider.words2String(words))
def main():
trainPassage("The third thing that I need to tell you is that this thing does not think thoroughly.")
printWords("thi")
printWords("nee")
printWords("th")
if __name__ == "__main__":
main()