-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathletter_tree.py
37 lines (33 loc) · 1.15 KB
/
letter_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
class LetterTreeNode:
def __init__(self, is_word):
self.is_word = is_word
self.children = dict()
class LetterTree:
def __init__(self, words):
self.root = LetterTreeNode(False)
for word in words:
current_node = self.root
for letter in word:
if letter not in current_node.children.keys():
current_node.children[letter] = LetterTreeNode(False)
current_node = current_node.children[letter]
current_node.is_word = True
def lookup(self, word):
current_node = self.root
for letter in word:
if letter not in current_node.children.keys():
return None
current_node = current_node.children[letter]
return current_node
def is_word(self, word):
word_node = self.lookup(word)
if word_node is None:
return False
return word_node.is_word
def basic_english():
with open('basic_english_word_list.txt', 'rt') as file:
words = []
for line in file:
word = line.strip()
words.append(word)
return LetterTree(words)