-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathNineRootedTreeWords.py
96 lines (77 loc) · 2.52 KB
/
NineRootedTreeWords.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
92
93
94
95
import gemNumFuncs as gnf
import getwordsfromdbs as gwdb
class Word:
def __init__(self, value):
self.value = value
self.children = {}
self.words = []
return
class NineRootedTree:
def __init__(self, words, cipher):
self.roots = [None, None, None, None, None, None, None, None, None, None]
if words:
for i in words:
self.addWord(i, cipher)
return
def addWord(self, word, cipher):
route = gnf.getParentList(gnf.getGematria(word, cipher))
route.reverse()
parent = self.roots[route[0]]
if parent is None:
self.roots[route[0]] = Word(route[0])
parent = self.roots[route[0]]
for digit in route[1:]:
next = parent.children.get(digit)
if next is None:
next = Word(digit)
parent.children[digit] = next
parent=next
if not word in parent.words:
parent.words+=[word]
return word
def findWords(self, value):
if value == 0:
return []
route = gnf.getParentList(value)
route.reverse()
node = self.roots[route[0]]
if value == node.value:
return node.words
for i in route[1:]:
node = node.children.get(i)
if node != None:
if i == value:
return node.words
return []
def route_to_root(self, word, cipher):
return gnf.getParentList(gnf.getGematria(word, cipher))
def __str__(self):
tree_str = ''
for root in self.roots:
if root is not None:
tree_str += self.print_tree(root, '')
return tree_str
def print_tree(self, word, prefix):
tree_str = prefix + str(word.value) + str(word.words) + '\n'
prefix += '| '
for child in word.children.values():
tree_str += self.print_tree(child, prefix)
return tree_str
def generateNestedList(self):
result = "<ul>"
for root in self.roots:
if root is not None:
result+=self.generateListItems(root)
result+="</ul>"
return result
def generateListItems(self, word):
result = "<li>" + str(word.value) + "<div>"+str(word.words)+"</div></li>"
if word.children:
result += "<ul>"
for child in word.children.values():
result += self.generateListItems(child)
result += "</ul>"
return result
def wordList_to_NineRootedTree(words, cipher):
tree = NineRootedTree(words, cipher)
return tree