-
Notifications
You must be signed in to change notification settings - Fork 0
/
Trie.py
184 lines (175 loc) · 6.78 KB
/
Trie.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
from tkinter import END
class Node :
def __init__(self , dWord , flag ):
self.data = dWord
self.Rc = None
self.Lc = None
self.next = None
self.flag = flag
self.address = []
class MyTrie :
def __init__(self ,fileOpen ):
self.root = Node("" ,0 )
self.listfileSearch = []
self.fileOpen = fileOpen
self. wordNum = 0
self.delList = []
def insertChild(self , word ,address ):
#add node
count =1
end =0 ;
current = self.root
for c in word.lower() :
if (count == len(word)) :
end = 1 ;
if(current.Lc is None) :
current.Lc = Node(c,end )
if (end ==1 ):
current.Lc.address.append(address)
current = current.Lc
elif (current.Rc is None) and (c != current.Lc.data) :
current.Rc = Node(c, end)
if (end ==1 ) :
current.Rc.address.append(address)
current.Lc.next= current.Rc
current = current.Rc
else :
cur2= current.Lc
x= 0
while(cur2 is not None ) :
if(cur2.data == c) :
x=1
if len(cur2.address) ==0 :
if (end ==1 ) :
cur2.address.append(address)
elif not address == cur2.address[len(cur2.address)-1] :
if (end ==1 ) :
cur2.address.append(address)
current = cur2
break
cur2 = cur2.next
if x ==0 :
c= Node (c,end)
current.Rc.next = c
current.Rc = c
current = c
count +=1
def visit (self, reroot , ch , listShow ,filename , p):
if reroot is not None :
ch= ch + reroot.data
if(reroot.flag == 1) :
if p==1 and reroot.address :
#for print all of tree nodes
s = ch + ' -> '
for a in reroot.address :
s = s + " " + a
self.fileOpen.writeList(listShow , s)
self.wordNum +=1
elif p==2 :
#for updating list
counter =0
for a in reroot.address :
if a ==filename :
reroot.address.pop(counter)
if len(reroot.address) == 0:
reroot.flag = 0
break
counter +=1
current = reroot.Lc
while (current is not None):
self.visit(current, ch , listShow, filename ,p)
current = current.next
def reSearchW (self , reroot , word , realW , p , listShow , filename ) :
# search word recursive
ch= word[0]
if ch == reroot.data :
if p ==2 :
self.delList.append(reroot)
if not word[1:]:
if p==1 :
first = 0
str =""
for a in reroot.address :
if first ==1 :
str = str + " , " + a
else:
first =1
str= a
self.fileOpen.writeList(listShow , str)
self.listfileSearch = reroot.address
return reroot
elif p==2 :
counter = 0
for f in reroot.address:
if f == filename:
reroot.address.pop(counter)
if len(reroot.address)==0 :
# reroot.flag = 0
for d in self.delList[::-1] :
if (d.Lc is None):
del d
elif (d.Lc is not None) :
d.flag = 0
break
for d in self.delList :
for a in d.address :
if a ==filename :
del a
break
del self.delList[:]
break
counter += 1
del reroot
else:
self.listfileSearch = reroot.address
return reroot
else :
current = reroot.Lc
while current is not None :
if(current.data == word[1]) :
return self.reSearchW( current ,word[1:] , realW , p, listShow , filename)
current = current.next
self.listfileSearch = []
return
def searchOneWord (self ,word , p ,listShow) :
char = word[0]
current = self.root.Lc
while current is not None :
if char == current.data :
return self.reSearchW(current , word , word , p , listShow , "" )
current = current.next
def isStopWord(self, word, stopwords):
if not word.isalpha():
return True
for w in stopwords:
if w.lower() == word.lower():
return True
return False
def searchOneLine(self, line , stopword , listShow):
# search line with function search word and then return th intersect of lists
first = 1
list1 = []
for w in line.split():
if (not self.isStopWord(w, stopword)):
if first == 1:
self.searchOneWord(w ,0 , listShow)
list1 = self.listfileSearch
first = 0
else:
self.searchOneWord(w , 0, listShow)
list2 = self.listfileSearch
list1 = list(set(list1).intersection(list2))
str = "answer : "
for a in list1:
str = str + " " + a
self.fileOpen.writeList(listShow, str)
def deleteNode(self, word, filename , listShow):
char = word[0]
current = self.root.Lc
while current is not None:
if char == current.data:
self.reSearchW(current, word, word, 2, listShow , filename)
current = current.next
return
def updeateNode (self,filename , listshow ) :
self.visit(self.root , "" , listshow , filename ,2 )