summaryrefslogtreecommitdiffstats
path: root/tree.py
blob: f39aff477451cb2bd77046e1a96449f74c026a73 (plain)
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
import json
import pickle

class Tree:

    def __init__(self, letter, children=None):
        self.letter = letter
        self.children = []
        if children is not None:
            for child in children:
                self.children.append(child)

    def add_child(self, node):
        assert isinstance(node, Tree)
        self.children.append(node)


def get_letters(children):
    output = []
    for child in children:
        output.append(child.letter)
    return output

def create_tree():
    with open("pruned_words.json", 'r') as f:
        word_list = json.load(f)
    root = Tree('')

    # word_list = ['anger', 'angry', 'awesome']
    for word in word_list:
        branch = root
        for letter in word:
            if letter not in get_letters(branch.children):
                branch.add_child(Tree(letter))
            branch = branch.children[get_letters(branch.children).index(letter)]
    pickle.dump( root, open( "tree.p", "wb" ) )
    print("Tree Created")
# from queue import Queue
#
# q = Queue()
# q.put((root,""))
# while not q.empty():
#     current, branch_word = q.get()
#
#     print(branch_word)
#
#     for i in current.children:
#         q.put((i, branch_word + i.letter))


# def check_word(w):
#     b = root
#     for i,l in enumerate(w):
#         if l not in get_letters(b.children):
#             return False
#         b = b.children[get_letters(b.children).index(l)]
#     return True


# print(check_word("anger"))
# print(check_word("awidnawiuohndoa"))
# print(check_word("awesome"))