-
Notifications
You must be signed in to change notification settings - Fork 1
/
bicleaner-hardrules.py
356 lines (289 loc) · 11.2 KB
/
bicleaner-hardrules.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
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
import argparse
import logging
import os
import pycld2
import regex
import sys
import traceback
from heapq import heappush, heappop
from multiprocessing import Queue, Process, Value, cpu_count
from tempfile import NamedTemporaryFile, gettempdir
from timeit import default_timer
from util import logging_setup
regex_blank = regex.compile("[ \u00A0]")
regex_digit = regex.compile("[[:digit:]]")
regex_alpha = regex.compile("[[:alpha:]]")
regex_url = regex.compile('((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]|\((:?[^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?\xab\xbb\u201c\u201d\u2018\u2019]))')
regex_breadcrumbs = regex.compile("([ ][-/»][ ]|[|<>→←]|[ ][:][:][ ])")
regex_unicode_noise = regex.compile("[\x80-\xFF]{3,}")
regex_spaces_noise = regex.compile("([ ].){4,}[ ]")
regex_paren = regex.compile("[][(){}]")
regex_unwanted = regex.compile("[+*]")
regex_inconditional = regex.compile("=\"")
safe_noise_detection_langs = {"en", "es", "fr", "pl", "de", "it", "pt", "nl", "cs", "ro", "fi", "lv", "et", "bg", "hr", "da", "hu", "ga", "eu", "gl", "sl", "sv", "mt", "sk"}
def initialization():
parser = argparse.ArgumentParser(prog=os.path.basename(sys.argv[0]), formatter_class=argparse.ArgumentDefaultsHelpFormatter, description=__doc__)
parser.add_argument('input', nargs='?', type=argparse.FileType('rt'), default=sys.stdin, help="Tab-separated bilingual tagged file")
parser.add_argument('output', nargs='?', type=argparse.FileType('wt'), default=sys.stdout, help="Output of the classification")
groupM = parser.add_argument_group('Mandatory')
groupM.add_argument("-s", "--source_lang", type=str, required=True, help="Source language (SL) of the input")
groupM.add_argument("-t", "--target_lang", type=str, required=True, help="Target language (TL) of the input")
groupO = parser.add_argument_group('Optional')
groupO.add_argument('--tmp_dir', default=gettempdir(), help="Temporary directory where creating the temporary files of this program")
groupO.add_argument('-b', '--block_size', type=int, default=10000, help="Sentence pairs per block")
groupO.add_argument('-p', '--processes', type=int, default=max(1, cpu_count()-1), help="Number of processes to use")
args = parser.parse_args()
# Ensure that directory exists; if not, create it
if not os.path.exists(args.tmp_dir):
os.makedirs(args.tmp_dir)
return args
def c_identical(left, right):
return left != right
def c_identical_wo_digits(left, right):
left = regex_digit.sub("", left)
right = regex_digit.sub("", right)
return left != right
def c_minimal_length(sentence):
""" Counts number of whitespace, requires > 2 """
return len(regex_blank.findall(sentence)) > 2
def c_length(left, right):
return 0.5 <= float(len(left))/float(len(right)) <= 2.0
def c_different_language(left, right):
l_reliable = False
l_bytes = 0
l_details = ()
try:
l_reliable, l_bytes, l_details = pycld2.detect(left)
except:
return False # encoding error -> noise
r_reliable = False
r_bytes = 0
r_details = ()
try:
r_reliable, r_bytes, r_details = pycld2.detect(right)
except:
return False # encoding error -> noise
if l_reliable and r_reliable and l_details[0][1] != r_details[0][1]:
return True
elif not l_reliable or not r_reliable:
return True
else:
return False
def c_reliable_long_language(sentence, language):
reliable = False
bytes = 0
details = ()
try:
reliable, bytes, details = pycld2.detect(sentence)
except:
return True # encoding error -> noise
if len(sentence) > 30 and reliable and details[0][1] != language:
return False
else:
return True
def c_alpha(sentence):
return len(regex_alpha.findall(sentence)) > 0
def c_majority_alpha(sentence):
return float(len(regex_alpha.findall(sentence))) / float(len(sentence)) >= 0.5
def c_no_urls(sentence):
return sum([len("".join(i)) for i in regex_url.findall(sentence)]) < 15
def c_no_breadcrumbs(sentence):
return len(regex_breadcrumbs.findall(sentence)) < 3
def c_no_noise(sentence):
return len(regex_unicode_noise.findall(sentence)) == 0
def c_no_space_noise(sentence):
return len(regex_spaces_noise.findall(sentence)) == 0
def c_no_paren(sentence):
return len(regex_paren.findall(sentence)) < 10
def c_unwanted(sentence):
return len(regex_unwanted.findall(sentence)) < 5
def c_inconditional(sentence):
return len(regex_inconditional.findall(sentence)) < 1
def wrong_tu(left, right, args):
if len(left) >= 1024:
return True
if len(right) >= 1024:
return True
elif not c_minimal_length(left):
return True
elif not c_minimal_length(right):
return True
elif not c_length(left, right):
return True
elif not c_identical(left, right):
return True
elif not c_identical_wo_digits(left, right):
return True
elif not c_different_language(left, right):
return True
elif not c_majority_alpha(left):
return True
elif not c_majority_alpha(right):
return True
elif not c_no_urls(left):
return True
elif not c_no_urls(right):
return True
elif not c_no_breadcrumbs(left):
return True
elif not c_no_breadcrumbs(right):
return True
elif args.source_lang in safe_noise_detection_langs and not c_no_noise(left):
return True
elif args.target_lang in safe_noise_detection_langs and not c_no_noise(right):
return True
elif not c_no_space_noise(left):
return True
elif not c_no_space_noise(right):
return True
elif not c_no_paren(left):
return True
elif not c_no_paren(right):
return True
elif not c_unwanted(left):
return True
elif not c_unwanted(right):
return True
elif not c_inconditional(left):
return True
elif not c_inconditional(right):
return True
elif not c_reliable_long_language(left, args.source_lang):
return True
elif not c_reliable_long_language(right, args.target_lang):
return True
return False
def reduce_process(output_queue, args):
h = []
last_block = 0
while True:
logging.debug("Reduce: heap status {0}".format(h.__str__()))
while len(h) > 0 and h[0][0] == last_block:
nblock, filein_name = heappop(h)
last_block += 1
with open(filein_name, 'r') as filein:
for i in filein:
args.output.write(i)
filein.close()
os.unlink(filein_name)
job = output_queue.get()
if job:
nblock, filein_name = job
heappush(h, (nblock, filein_name))
else:
logging.debug("Exiting reduce loop")
break
if len(h) > 0:
logging.debug("Still elements in heap")
while len(h) > 0 and h[0][0] == last_block:
nblock, filein_name = heapq.heappop(h)
last_block += 1
with open(filein_name, 'r') as filein:
for i in filein:
args.output.write(i)
filein.close()
os.unlink(filein_name)
if len(h) != 0:
logging.error("The queue is not empty and it should!")
logging.info("Hard rules applied. Output available in {}".format(args.output.name))
args.output.close()
def worker_process(i, jobs_queue, output_queue, args):
while True:
job = jobs_queue.get()
if job:
logging.debug("Job {0}".format(job.__repr__()))
nblock, filein_name = job
ojob = None
with open(filein_name, 'r') as filein, NamedTemporaryFile(mode="w", delete=False, dir=args.tmp_dir) as fileout:
logging.debug("Classification: creating temporary filename {0}".format(fileout.name))
for i in filein:
parts = i.strip().split("\t")
left = parts[0]
right = parts[1] if len(parts) >= 2 else ""
if wrong_tu(left, right, args):
fileout.write("{}\t{}\t0.0000000000000000\tdiscard\n".format(left, right))
else:
fileout.write(i)
ojob = (nblock, fileout.name)
filein.close()
fileout.close()
if ojob:
output_queue.put(ojob)
os.unlink(filein_name)
else:
logging.debug("Exiting worker")
break
def mapping_process(args, jobs_queue):
logging.info("Start mapping")
nblock = 0
nline = 0
mytemp = None
for line in args.input:
if (nline % args.block_size) == 0:
logging.debug("Creating block {}".format(nblock))
if mytemp:
job = (nblock, mytemp.name)
mytemp.close()
jobs_queue.put(job)
nblock += 1
mytemp = NamedTemporaryFile(mode="w", delete=False, dir=args.tmp_dir)
logging.debug("Mapping: creating temporary filename {0}".format(mytemp.name))
mytemp.write(line)
nline += 1
if nline > 0:
job = (nblock, mytemp.name)
mytemp.close()
jobs_queue.put(job)
return nline
def perform_hardrules_filtering(args):
time_start = default_timer()
logging.info("Starting process")
logging.info("Running {0} workers at {1} rows per block".format(args.processes, args.block_size))
process_count = max(1, args.processes)
maxsize = 1000 * process_count
output_queue = Queue(maxsize = maxsize)
worker_count = process_count
# Start reducer
reduce = Process(target = reduce_process,
args = (output_queue, args))
reduce.start()
# Start workers
jobs_queue = Queue(maxsize = maxsize)
workers = []
for i in range(worker_count):
filter = Process(target = worker_process,
args = (i, jobs_queue, output_queue, args))
filter.daemon = True # dies with the parent process
filter.start()
workers.append(filter)
# Mapper process (foreground - parent)
nline = mapping_process(args, jobs_queue)
args.input.close()
# Worker termination
for _ in workers:
jobs_queue.put(None)
logging.info("End mapping")
for w in workers:
w.join()
# Reducer termination
output_queue.put(None)
reduce.join()
# Stats
logging.info("Finished")
elapsed_time = default_timer() - time_start
logging.info("Total: {0} rows".format(nline))
logging.info("Elapsed time {0:.2f} s".format(elapsed_time))
logging.info("Troughput: {0} rows/s".format(int((nline*1.0)/elapsed_time)))
def main(args):
logging.info("Executing main program...")
perform_hardrules_filtering(args)
logging.info("Program finished")
if __name__ == '__main__':
try:
logging_setup()
args = initialization()
main(args)
except Exception as ex:
tb = traceback.format_exc()
logging.error(tb)
sys.exit(1)