-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuffixIndexers.unittest.py
153 lines (131 loc) · 4.24 KB
/
suffixIndexers.unittest.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import unittest
import tools_karkkainen_sanders as tks
import array
import suffixIndexers as si
import random
import re
def get_all_pos(w, pattern) :
i = 0
lt = len(w)
res = []
while i < lt :
try :
pos = w.index(pattern, i)
res.append(pos)
i = pos + 1
except Exception, e :
return res
return res
def naive_get_all_list(l, pattern) :
res = []
for i in xrange(len(l)) :
w = l[i]
if is_pattern_in(pattern, w) :
res.append(i)
return res
def naive_get_all_dict(d, pattern) :
res = []
for i,w in d.iteritems() :
if is_pattern_in(pattern, w) :
res.append(i)
return res
def naive_get_all_with_pos_list(l, pattern) :
res = []
for i in xrange(len(l)) :
w = l[i]
r = get_all_pos(w, pattern)
for o in r :
res.append((i,o))
return res
def naive_get_all_with_pos_dict(d, pattern) :
res = []
for i,w in d.iteritems() :
r = get_all_pos(w, pattern)
for o in r :
res.append((i,o))
return res
class Test_list_indexer :
def setUp(self) :
self.l,self.pattern = self.getData()
self.index = si.ListIndexer(self.l)
def test_one_word(self) :
res = self.index.searchOneWord(self.pattern)
naive_res = naive_get_all_list(self.l, self.pattern)
if res == None :
self.assertTrue(naive_res == [])
else :
self.assertTrue(res in naive_res)
def test_all_words(self) :
res = self.index.searchAllWords(self.pattern).sort()
naive_res = naive_get_all_list(self.l, self.pattern).sort()
self.assertTrue(res == naive_res)
def test_one_word_and_pos(self) :
res = self.index.searchOneWordAndPos(self.pattern)
naive_res = naive_get_all_with_pos_list(self.l, self.pattern)
if res == None :
self.assertTrue(naive_res == [])
else :
self.assertTrue(res in naive_res)
def test_all_words_and_pos(self) :
res = self.index.searchAllWordsAndPos(self.pattern).sort()
naive_res = naive_get_all_with_pos_list(self.l, self.pattern).sort()
self.assertTrue(res == naive_res)
class Test_dict_values_indexer :
def setUp(self) :
self.d, self.pattern = self.getData()
self.index = si.DictValuesIndexer(self.d)
def test_one_word(self) :
res = self.index.searchOneWord(self.pattern)
naive_res = naive_get_all_dict(self.d, self.pattern)
if res == None :
self.assertTrue(naive_res == [])
else :
self.assertTrue(res in naive_res)
def test_all_words(self) :
res = self.index.searchAllWords(self.pattern).sort()
naive_res = naive_get_all_dict(self.d, self.pattern).sort()
self.assertTrue(res == naive_res)
def test_one_word_and_pos(self) :
res = self.index.searchOneWordAndPos(self.pattern)
naive_res = naive_get_all_with_pos_dict(self.d, self.pattern)
if res == None :
self.assertTrue(naive_res == [])
else :
self.assertTrue(res in naive_res)
def test_all_words_and_pos(self) :
res = self.index.searchAllWordsAndPos(self.pattern).sort()
naive_res = naive_get_all_with_pos_dict(self.d, self.pattern).sort()
self.assertTrue(res == naive_res)
class Test_list_a_b(Test_list_indexer, unittest.TestCase) :
def getData(self) :
l = ['a'*100 for i in xrange(100)]
return l,'bbb'
class Test_list_a_a(Test_list_indexer, unittest.TestCase) :
def getData(self) :
l = ['a'*100 for i in xrange(100)]
return l,'aaa'
#class Test_list_python(Test_list_indexer, unittest.TestCase) :
# def getData(self) :
# s = open('Python.htm','r').read()
# s_unicode = unicode(s,'utf-8','replace')[20000:25000]
# l = re.split('\s', s_unicode)
# return l, 'ython'
class Test_dict_a_a(Test_dict_values_indexer, unittest.TestCase) :
def getData(self) :
d = dict([(i,'a'*10) for i in xrange(10)])
return d,'aaa'
class Test_dict_a_b(Test_dict_values_indexer, unittest.TestCase) :
def getData(self) :
d = dict([(i,'a'*10) for i in xrange(10)])
return d,'bbb'
#class Test_dict_python(Test_dict_values_indexer, unittest.TestCase) :
# def getData(self) :
# s = open('Python.htm','r').read()
# s_unicode = unicode(s,'utf-8','replace')[20000:25000]
# l = re.split('\s', s_unicode)
# d = dict((i, l[i]) for i in xrange(len(l)))
# return d, 'ython'
if (__name__ == '__main__') :
unittest.main()