-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbalancers.py
More file actions
375 lines (286 loc) · 13 KB
/
balancers.py
File metadata and controls
375 lines (286 loc) · 13 KB
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import collections
import csv
from abc import ABC, abstractmethod
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from httplib2 import Http, ProxyInfo, socks
from mysql import connector
from nltk.corpus import stopwords
from nltk.data import load
from nltk.tokenize import RegexpTokenizer, word_tokenize
from numpy import dot
from RegexClassifier import RegexClassifier
from utils import top_information_gain_words
class TextDatasetBalancer(ABC):
@abstractmethod
def balance(self, data):
pass
class GoogleDatasetBalancer(TextDatasetBalancer):
def __init__(self, query_type="sentence", snippet_parsing="sentence",
similarity="jaccard", doc2vec=None, threshold=1/3,
quotes=False, site=None, keys=None, proxies=None,
engine_id=None, pages_per_query=1, outfile=None,
use_phrase_file=False, verbose=False):
self.__query_type = query_type
self.__snippet_parsing = snippet_parsing
self.__similarity = similarity
self.__doc2vec = doc2vec
self.__threshold = threshold
self.__quotes = quotes
self.__site = site
self.__keys = keys
self.proxies = proxies
self.__key_index = 0 if keys and len(keys) > 0 else -1
self.__engine_id = engine_id
self.__pages_per_query = pages_per_query
self.__outfile = outfile
self.__verbose = verbose
self.__sentence_tokenizer = None
self.__stopwords = None
def balance(self, data, labels):
counter = collections.Counter(labels)
most_common = counter.most_common(1)[0][0]
search = True
X = []
y = []
if self.__query_type == "sentence":
queries, query_labels = data, labels
elif self.__query_type == "regex":
queries, query_labels = self.__regex_queries(data, counter,
most_common)
elif self.__query_type == "phrase":
queries, query_labels = data, labels
most_common = "__ignore__"
counter[most_common] = float("inf")
else:
self.__print("Query type not recognized.")
return
for class_value in set(labels):
if counter[class_value] >= counter[most_common]:
continue
for i in range(len(queries)):
if query_labels[i] == class_value:
for j in range(self.__pages_per_query):
sentences = self.__search_for_sentences(queries[i], j)
if sentences is None:
search = False
break
elif len(sentences) == 0:
break
for sentence in sentences:
counter[class_value] += 1
X.append(sentence)
y.append(class_value)
if counter[class_value] >= counter[most_common]:
break
if counter[class_value] >= counter[most_common] or not search:
break
if not search:
break
if self.__outfile and len(X) > 0:
self.__write_dataset_file(data + X, labels + y)
self.__print("Added " + str(len(X)) + " new instances.")
return X, y
def __append(self, similar_sentences, sentence):
self.__print(" " + sentence)
similar_sentences.append(sentence)
def __google_search(self, sentence, **kwargs):
http = None
if self.__key_index < 0:
return None
elif self.__key_index > 0:
proxy = self.proxies[self.__key_index - 1]
http = Http(proxy_info=ProxyInfo(socks.PROXY_TYPE_HTTP,
proxy_host=proxy[0],
proxy_port=proxy[1]))
g = build(serviceName="customsearch", version="v1", http=http,
developerKey=self.__keys[self.__key_index])
q = ('"' + sentence + '"' if self.__quotes else sentence)
q += (" site:" + self.__site if self.__site and len(self.__site) > 0
else "")
try:
response = g.cse().list(q=q, cx=self.__engine_id,
**kwargs).execute()
except HttpError:
self.__key_index += 1
if self.__key_index >= len(self.__keys):
self.__key_index = -1
response = self.__google_search(sentence, **kwargs)
return response
def __parse_snippet(self, sentence, snippet, similar_sentences):
snippet = snippet.replace("\n", "")
if self.__snippet_parsing == "sentence":
self.__parse_sentences(sentence, snippet, similar_sentences)
elif self.__snippet_parsing == "ellipsis":
self.__parse_ellipses(sentence, snippet, similar_sentences)
elif self.__snippet_parsing == "snippet" and \
self.__similar(sentence, self.__tokenize(snippet)):
self.__append(similar_sentences, snippet)
def __parse_ellipses(self, sentence, snippet, similar_sentences):
for result_sentence in snippet.split(" ... "):
result_sentence = self.__remove_ellipses(result_sentence)
if self.__similar(sentence, self.__tokenize(result_sentence)):
self.__append(similar_sentences, result_sentence)
def __parse_sentences(self, sentence, snippet, similar_sentences):
if not self.__sentence_tokenizer:
self.__sentence_tokenizer = load('tokenizers/punkt/english.pickle')
for result_sentence in self.__sentence_tokenizer.tokenize(snippet):
result_sentence = self.__remove_ellipses(result_sentence)
if self.__similar(sentence, self.__tokenize(result_sentence)):
self.__append(similar_sentences, result_sentence)
def __print(self, s):
if self.__verbose:
print(s)
def __regex_queries(self, data, labels, counter, most_common):
regex_classifier = RegexClassifier(jump_length=0)
sample_weights = [counter[most_common] / counter[lb] for lb in labels]
regex_classifier.fit(data, labels, sample_weights)
regex_rules = regex_classifier.regex_rules
query_dictionary = {}
X = []
y = []
for regex_rule in regex_rules[0:len(regex_rules) - 1]:
if regex_rule.class_value not in query_dictionary.keys():
query_dictionary[regex_rule.class_value] = set()
query_dictionary[regex_rule.class_value].add(regex_rule.phrase)
for class_value in query_dictionary.keys():
for query in query_dictionary[class_value]:
X.append(query)
y.append(class_value)
return X, y
def __remove_ellipses(self, sentence):
leading_ellipse = "... "
trailing_ellipse = " ..."
if sentence.startswith(leading_ellipse):
sentence = sentence.replace(leading_ellipse, "", 1)
if sentence.endswith(trailing_ellipse):
sentence = sentence[0:sentence.rfind(trailing_ellipse)]
return sentence.strip()
def __search_for_sentences(self, sentence, page=0):
if self.__key_index < 0 or not self.__engine_id or len(sentence) == 0 \
or (self.__similarity == "cosine" and not self.__doc2vec):
self.__print("No valid keys.")
return None
self.__print(sentence)
similar_sentences = []
num = 10
start = page * num + 1
response = self.__google_search(sentence, num=num, start=start)
if not response:
self.__print("Query limit reached.")
return None
elif "items" not in response.keys():
return similar_sentences
sentence = self.__tokenize(sentence)
if self.__similarity == "cosine" and self.__doc2vec:
sentence = self.__doc2vec.infer_vector(sentence)
for search_result in response["items"]:
self.__parse_snippet(sentence, search_result["snippet"],
similar_sentences)
self.__print("")
return similar_sentences
def __similar(self, a, b):
if self.__similarity == "jaccard":
if not self.__stopwords:
self.__stopwords = set(stopwords.words("english"))
a = set(a) - self.__stopwords
b = set(b) - self.__stopwords
intersection_count = float(len(a.intersection(b)))
union_count = float(len(a.union(b)))
return intersection_count / union_count >= self.__threshold if \
union_count > 0.0 else 0.0
elif self.__similarity == "cosine":
return dot(a, self.__doc2vec.infer_vector(b)) >= self.__threshold
return False
def __tokenize(self, sentence):
return RegexpTokenizer(r"\w+").tokenize(sentence.lower())
def __write_dataset_file(self, data, labels):
with open(self.__outfile, 'w', newline="", errors="ignore") as file:
writer = csv.writer(file, lineterminator="\n")
for i in range(len(data)):
writer.writerow([data[i], labels[i]])
class PPDBDatasetBalancer(object):
def __init__(self, host, database, user, password, input_range=(1, 1),
scoring_feature="PPDB2Score", threshold=0, ig_threshold=0,
verbose=False):
self.__scoring_feature = scoring_feature
self.__threshold = threshold
self.__ig_threshold = ig_threshold
self.__input_range = (max(1, input_range[0]), max(1, input_range[1]))
self.__connection = connector.connect(host=host, database=database,
user=user, password=password)
self.__verbose = verbose
if self.__input_range[0] > self.__input_range[1]:
self.__input_range = self.__input_range[::-1]
def balance(self, data, labels):
if not self.__connection.is_connected():
print("Not connected to database.")
return
if self.__ig_threshold > 0:
top_words = top_information_gain_words(data, labels,
min_ig=self.__ig_threshold)
else:
top_words = None
counter = collections.Counter(labels)
most_common = counter.most_common(1)[0][0]
X = []
y = []
for i in range(len(data)):
if labels[i] == most_common:
continue
if self.__verbose:
print(data[i])
words = word_tokenize(data[i])
num_words = len(words)
paraphrases = {}
for j in range(num_words):
min_cutoff = min(num_words, j + self.__input_range[0])
max_cutoff = min(num_words, j + self.__input_range[1])
phrase = ""
length = 0
for k in range(j, min_cutoff):
phrase += (" " if length > 0 else "") + words[k]
length += 1
if length < self.__input_range[0]:
break
self.__find_paraphrases(phrase, paraphrases, top_words)
for k in range(j + length, max_cutoff):
phrase += (" " if length > 0 else "") + words[k]
length += 1
self.__find_paraphrases(phrase, paraphrases, top_words)
for phrase in paraphrases.keys():
index = data[i].find(phrase)
text = data[i].replace(phrase, "")
for paraphrase in paraphrases[phrase].keys():
paraphrased = text[:index] + paraphrase + text[index:]
if self.__verbose:
print(" " + paraphrased)
X.append(paraphrased)
y.append(labels[i])
if self.__verbose:
print("Added " + str(len(X)) + " new instances.")
return X, y
def close_connection(self):
self.__connection.close()
def __find_paraphrases(self, phrase, paraphrases, top_words):
if phrase in paraphrases.keys():
return
if top_words is not None:
valid = False
for word in top_words:
if phrase.find(word) > -1:
valid = True
break
if not valid:
return
sql = "SELECT paraphrase, " + self.__scoring_feature + " FROM ppdb " \
+ "WHERE phrase = '" \
+ phrase.replace("\\", "\\\\").replace("'", "\\'") + "' AND " \
+ self.__scoring_feature + " > " + str(self.__threshold)
cursor = self.__connection.cursor()
cursor.execute(sql)
for row in cursor.fetchall():
if phrase not in paraphrases:
paraphrases[phrase] = {}
paraphrases[phrase][row[0]] = row[1]
cursor.close()