This repository has been archived by the owner on Nov 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree_struct.py
91 lines (68 loc) · 2.48 KB
/
tree_struct.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
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
import pickle
class tree:
def __init__(self, letter, alphabet = None):
self.letter = letter
self.finished_word = False
if alphabet == None:
self.alphabet = [chr(ord('A') + i) for i in range(26)]
else:
self.alphabet = alphabet
self.neighbors = {}
def from_words_rec_(self, words):
words1 = [i[1:] for i in words]
if len(words1[0]) == 0:
self.finished_word = True
words1 = words1[1:]
if len(words1) == 0:
return None
subwords = dict([(i,[]) for i in self.alphabet])
for i in words1:
subwords[i[0]].append(i)
for l, w in subwords.items():
if len(w) > 0:
if self.neighbors.get(l) == None:
self.neighbors[l] = tree(l, alphabet = self.alphabet)
self.neighbors[l].from_words_rec_(w)
def from_words(self, words):
self.from_words_rec_(sorted(set(words), key = len))
def display(self):
self.display_()
print()
def display_(self):
print("[" + str(self.letter) + ", {}, [".format(self.finished_word), end = "")
for i in self.neighbors.values():
if i != None:
i.display_()
print(" ", end = "")
print("]]", end = "")
class lexicon:
def __init__(self, words = None, alphabet = None):
if alphabet == None:
self.alphabet = [chr(ord('A') + i) for i in range(26)]
else:
self.alphabet = alphabet
self.trees_ = dict()
self.add_words(words)
def add_words(self, words):
if words == None:
return None
subwords = dict([(i,[]) for i in self.alphabet])
for i in words:
subwords[i[0]].append(i)
for l, w in subwords.items():
if len(w) > 0:
if self.trees_.get(l) == None:
self.trees_[l] = tree(l, alphabet = self.alphabet)
self.trees_[l].from_words(w)
def save(self, filename):
with open(filename, 'wb') as fhandle:
pickle.dump((self.alphabet, self.trees_), fhandle)
def load(self, filename):
with open(filename, 'rb') as fhandle:
self.alphabet, self.trees_ = pickle.load(fhandle)
def get_tree(self, char):
return self.trees_[char]
def display(self):
for i, t in self.trees_.items():
print("{}: ".format(i), end = "")
t.display()