-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdictionary_tree.py
64 lines (52 loc) · 2.01 KB
/
dictionary_tree.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
from timeit import default_timer as timer
from word_classification import WordClassification
class DictionaryTree:
def __init__(self, letter, is_end=False, word=None):
self.children = dict()
self.letter = letter
self.is_end = is_end
self.word = word
class TreeCreator:
def __init__(self, dictionary='dictionary.txt'):
self.dictionary = dictionary
self.wc = WordClassification()
self.read_words()
def insert(self, root, letters, word):
letter = letters[:1]
left_letters = letters[1:]
if not root.children.has_key(letter):
root.children[letter] = DictionaryTree(letter)
if left_letters.__len__() == 0:
root.children[letter].is_end = True
root.children[letter].word = word
return
self.insert(root.children[letter], left_letters, word)
def read_words(self):
self.root = DictionaryTree(None)
fo = open(self.dictionary, "r")
for line in fo.readlines():
line = line.strip()
self.insert(self.root, line, line)
fo.close()
def search_for_word(self, root, letters):
if letters is None or letters.__len__() == 0:
return
for letter in root.children:
if letter in letters:
if root.children[letter].is_end:
self.wc.run(root.children[letter].word)
self.found.append(root.children[letter].word)
left_letters = letters[:]
left_letters.remove(letter)
self.search_for_word(root.children[letter], left_letters)
def search(self, letters):
self.current_word = ''
self.found = []
self.wc.new_search()
start = timer()
self.search_for_word(self.root, sorted(letters))
end = timer()
print('{} finding word {}'.format(end - start, self.wc.get_word()))
return self.wc.get_word()
def get_next_word(self):
return self.wc.get_next_word()