forked from SCOAP3/scoap3-legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrawtext_search.py
282 lines (236 loc) · 9.09 KB
/
rawtext_search.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import re
class RawTextSearch:
def __init__(self, searchstring):
self.searchstring = searchstring
self.operators = []
self.operators.append((" ", "OR"))
self.operators.append(("OR", "OR"))
self.operators.append(("-", "NEG"))
self.operators.append(('\'', "none"))
self.operators.append(('/', "none"))
self.operators.append(("(", "LBR"))
self.operators.append((")", "RBR"))
self.rules = [
("NEGT", ("NEG", "ST")),
("ST", ("NEGT",), ("LBR", "OR", "RBR"), ("LBR", "ST", "RBR")),
("OR", ("ST", "OR", "ST"), ("OR", "OR", "ST"), ("ST", "OR", "OR"), ("OR", "OR", "OR"))
]
self.pick_rules = {
("NEGT",): (0,),
("NEG", "ST"): (1,),
("LBR", "OR", "RBR"): (1,),
("LBR", "ST", "RBR"): (1,),
("ST", "OR", "ST"): (0, 2),
("OR", "OR", "ST"): (0, 2),
("ST", "OR", "OR"): (0, 2),
("OR", "OR", "OR"): (0, 2)
}
self.operator_actions = {
"ST": self.__st_action,
"OR": self.__or_action,
"NEGT": self.__neg_action
}
self.raw_splits = self.__split()
self.assigned_splits = self.__assign_meanings(self.raw_splits)
self.searchtree = self.__build_search_tree(self.assigned_splits)
def __st_action(self, results):
return results[0]
def __or_action(self, results):
val = False
for result in results:
val = val or result
return val
def __neg_action(self, results):
return not results[0]
# SPLIT THE SEARCH INTO ATOMIC PARTS
def __is_operator(self, index):
operator_length = 0
for operator in self.operators:
if operator[0] == self.searchstring[index:index+len(operator[0])]:
if operator[0] == '\'':
next_position = self.searchstring.find('\'', index+1)
return next_position-index+1
elif operator[0] == '/':
next_position = self.searchstring.find('/', index+1)
return next_position-index+1
else:
return len(operator[0])
return operator_length
def __is_searchterm(self, index):
substring = self.searchstring[index:]
for i in range(len(substring)):
char = substring[i]
if is_operator(i, substring):
return i
return len(substring)
def __split(self):
splits = []
i = 0
while i < len(self.searchstring):
char = self.searchstring[i]
operator_length = self.__is_operator(i)
if operator_length > 0:
splits.append(self.searchstring[i:i+operator_length])
i += operator_length
else:
searchterm_length = self.__is_searchterm(i, searchstring)
splits.append(self.searchstring[i:i+searchterm_length])
i += searchterm_length
for i in range(len(splits)):
if splits[i] == " ":
splits[i] = "OR"
return splits
# ASSIGN MEANING TO THE ATOMIC PARTS
def __assign_meanings(self, splits):
new_splits = []
for i in range(len(splits)):
searchterm = True
for operator in self.operators:
if splits[i] == operator[0]:
searchterm = False
new_splits.append((operator[1], splits[i]))
if searchterm:
new_splits.append(("ST", splits[i]))
return new_splits
#BUILD THE SEARCH TREE
def __fits_rule(self, subrule, index, new_splits):
fits = True
for i in range(len(subrule)):
try:
if subrule[i] != new_splits[index+i][0]:
fits = False
break
except Exception:
fits = False
break
return fits
def __combine(self, rule, subrule, index, new_splits):
tmp = new_splits
new_tuple_list = [rule[0]]
picks = self.pick_rules[subrule]
for i in picks:
new_tuple_list.append(new_splits[index+i])
for i in range(len(subrule)):
del(tmp[index])
tmp.insert(index, tuple(new_tuple_list))
return tmp
def __build_search_tree(self, splits):
new_splits = splits
start_over = False
while len(new_splits) != 1:
start_over = False
for rule in self.rules:
for subrule in rule[1:]:
for i in range(len(new_splits)):
if self.__fits_rule(subrule, i, new_splits):
new_splits = self.__combine(rule,
subrule,
i,
new_splits)
start_over = True
break
if start_over:
break
if start_over:
break
if not start_over:
raise Exception
return new_splits[0]
#PRINT TREE
def __print_tree(self, new_split, indentation_level=0):
indentation = ''
for i in range(indentation_level):
indentation += "\t"
print indentation + new_split[0]
for split in new_split[1:]:
if isinstance(split, (list, tuple)):
self.__print_tree(split, indentation_level+1)
else:
print indentation + '\t' + split
def print_tree(self, indentation_level=0):
indentation = ''
for i in range(indentation_level):
indentation += "\t"
print indentation + self.searchtree[0]
for split in self.searchtree[1:]:
if isinstance(split, (list, tuple)):
self.__print_tree(split, indentation_level+1)
else:
print indentation + '\t' + split
#SEARCH STUFF
def __is_regex(self, searchterm):
if searchterm[0] == '/':
return True
return False
def __get_active_operator(self, operator_string):
return self.operator_actions[operator_string]
def __clean_searchterm(self, searchterm):
if searchterm[0] == "'" or searchterm[0] == "/":
return searchterm[1:len(searchterm)-1]
return searchterm
def __perform_search(self, raw_text, searchterm):
if not self.__is_regex(searchterm):
searchterm = self.__clean_searchterm(searchterm)
return searchterm in raw_text
else:
searchterm = self.__clean_searchterm(searchterm)
pattern = re.compile(searchterm, re.IGNORECASE)
return pattern.search(raw_text) is not None
def __search(self, raw_text, searchtree):
active_operator = None
results = []
if isinstance(searchtree, (tuple)):
active_operator = self.__get_active_operator(searchtree[0])
for sub_tree in searchtree[1:]:
results.append(self.__search(raw_text, sub_tree))
else:
return self.__perform_search(raw_text, searchtree)
return active_operator(results)
def search(self, raw_text):
active_operator = None
results = []
if isinstance(self.searchtree, (tuple)):
active_operator = self.__get_active_operator(self.searchtree[0])
for sub_tree in self.searchtree[1:]:
results.append(self.__search(raw_text, sub_tree))
else:
return perform_search(raw_text, self.searchtree)
return active_operator(results)
if __name__ == '__main__':
print 'Running some tests...'
# searchstring1 = "-'CC-BY' -'CC BY' -'Creative Commons Attribution'"
searchstring1 = "-('CC-BY' 'CC BY' 'Creative Commons Attribution')"
searchstring2 = "-'funded by SCOAP'"
searchstring3 = "-(/(c|\(c\)) the author/ 'copyright cern')"
searchstring4 = "/(copyright|c)[^.]*(IOP|Institute of Physics)/"
search1 = RawTextSearch(searchstring1)
search2 = RawTextSearch(searchstring2)
search3 = RawTextSearch(searchstring3)
search4 = RawTextSearch(searchstring4)
search1.print_tree()
search2.print_tree()
search3.print_tree()
search4.print_tree()
raw_text1 = '''
CC BY Banana
Open Access, c The Authors, (c) The Authors.
Article funded by SCOAP3.
c . Institute of Physics.
'''
raw_text2 = '''
Open Access
Article by SCOAP3.
c blah blah Institute of Physics.
'''
print "For the following text everything should be false"
print raw_text1
print search1.search(raw_text1)
print search2.search(raw_text1)
print search3.search(raw_text1)
print search4.search(raw_text1)
print "For the following text everything should be false"
print raw_text2
print search1.search(raw_text2)
print search2.search(raw_text2)
print search3.search(raw_text2)
print search4.search(raw_text2)