-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpytrie.py
204 lines (166 loc) · 6.21 KB
/
pytrie.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import numpy as np
import time
import threading
class node:
def __init__(self):
self.sub = dict()
self.data = None
def insert(self, key_list, data=None):
"""
Insert a value. If the corresponding node exits, this method will revise the self.data to the input data.
If you donot want to revise the data, use method self.setdefault()
:param key_list: a list of key
:param data: the value
:return: return the node
"""
if len(key_list) == 0:
self.data = data
return self
key = key_list[0]
if key not in self.sub:
sub_node = node()
self.sub[key] = sub_node
else:
sub_node = self.sub[key]
return sub_node.insert(key_list[1:], data)
def setdefault(self, key_list, data=None):
"""
Set the default value.
If the node exits, this method return the data.
If not, this method will set the self.data to the input data and return the input data
:param key_list: key list
:param data: input data
:return: return the found node
"""
if len(key_list) == 0:
if self.data is None:
self.data = data
return self
key = key_list[0]
if key not in self.sub:
sub_node = node()
self.sub[key] = sub_node
else:
sub_node = self.sub[key]
return sub_node.setdefault(key_list[1:], data)
def find_node(self, key_list):
"""find a node and return the node"""
if len(key_list) == 0:
return self
if key_list[0] in self.sub:
return self.sub[key_list[0]].find_node(key_list[1:])
return None
def find(self, key_list):
"""find a node the return the data"""
fnode = self.find_node(key_list)
if fnode is not None:
return fnode.data
return None
class trie_iter:
def __init__(self, head, key_list=list(), is_sorted=False):
if is_sorted:
self.iter = iter(sorted(head.sub.items(), key=lambda x: x[0]))
else:
self.iter = iter(head.sub.items())
self.sub_trie_iter = None
self.key_list = key_list
self.is_sorted = is_sorted
def __next__(self):
if self.sub_trie_iter is not None:
try:
return self.sub_trie_iter.__next__()
except StopIteration:
self.sub_trie_iter = None
if self.sub_trie_iter is None:
try:
sub_key, sub_trie = self.iter.__next__()
self.sub_trie_iter = trie_iter(sub_trie, self.key_list + [sub_key], self.is_sorted)
return self.key_list + [sub_key], sub_trie
except StopIteration:
raise StopIteration
def __iter__(self):
return self
class level_iter:
def __init__(self, trie_root, level, key_list=list(), is_sorted=False):
self.trie_root = trie_root
self.level = level # the level need to iter
if is_sorted:
self.iter = iter(sorted(iter(trie_root.sub.items()), key=lambda x:x[0])) # iter the sub trie
else:
self.iter = iter(trie_root.sub.items()) # iter the sub trie
self.sub_level_iter = None # the sub level iter
self.key_list = key_list
self.is_sorted = is_sorted
def __next__(self):
if self.level == 0:
return self.key_list, self.trie_root
elif self.level == 1:
sub_key, sub_trie = self.iter.__next__()
return self.key_list + [sub_key], sub_trie
if self.sub_level_iter is not None:
try:
return self.sub_level_iter.__next__()
except StopIteration:
self.sub_level_iter = None
while self.sub_level_iter is None:
try:
sub_key, sub_trie = self.iter.__next__()
except StopIteration:
raise StopIteration
self.sub_level_iter = level_iter(sub_trie, self.level-1, self.key_list + [sub_key], self.is_sorted)
try:
return self.sub_level_iter.__next__()
except StopIteration:
self.sub_level_iter = None
def __iter__(self):
return self
def search_epoch(trie, file, max_order):
total_count = 0
with open(file, 'rt') as f:
for line in f:
a = [-1] + [int(i) for i in line.split()] + [-2]
n = len(a)
for order in range(1, max_order+1):
for pos in range(0, n-order+1):
count = trie.find(a[pos: pos+order])
total_count += count
print('fun_res=', total_count)
def test():
file = 'ptb.train.id'
write = 'ptb.train.py.count'
max_order = 3
print('load trie...')
beg = time.time()
trie = node()
with open(file, 'rt') as f:
for line in f:
a = [-1] + [int(i) for i in line.split()] + [-2]
n = len(a)
for order in range(1, max_order+1):
for pos in range(0, n-order+1):
sub = trie.setdefault(a[pos: pos+order], 0)
sub.data += 1
print('time=', time.time() - beg, 's')
print('search test...')
beg = time.time()
for i in range(2):
search_epoch(trie, file, max_order)
print('time=', time.time() - beg, 's')
print('search test (threading)...')
beg = time.time()
thr = []
for i in range(2):
thr.append(threading.Thread(target=search_epoch, name=str(i), args=(trie, file, max_order)))
for t in thr:
t.start()
for t in thr:
t.join()
print('time=', time.time() - beg, 's')
# beg = time.time()
# print 'write...'
# with open(write, 'wt') as f:
# for keys, sub in trie_iter(trie):
# f.write(' '.join(str(i) for i in keys) + '\t{}\n'.format(sub.data))
# print 'time=', time.time() - beg, 's'
if __name__ == '__main__':
test()