forked from leod/hncynic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter_unk.py
executable file
·42 lines (33 loc) · 928 Bytes
/
filter_unk.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
#!/usr/bin/env python3
import sys
voc = set()
with open(sys.argv[1]) as f_voc:
for line in f_voc:
voc.add(line.strip())
n_done = 0
n_filtered = 0
n_invalid = 0
for line in sys.stdin:
cols = line.strip().split('\t')
if len(cols) != 2:
# It seems there are some training examples with empty title/comment.
# Might as well remove these here.
n_invalid += 1
else:
toks = cols[1].split()
keep = True
for tok in toks:
if tok not in voc:
keep = False
break
if keep:
sys.stdout.write(line)
else:
n_filtered += 1
n_done += 1
if n_done % 1000000 == 0:
sys.stderr.write('[{}M]\n'.format(n_done / 1000000))
sys.stderr.write('Done. Filtered {} lines out of {} ({:.2f}%)\n'
.format(n_filtered, n_done, float(n_filtered) / n_done * 100.0))
sys.stderr.write('Invalid lines: {} ({:.2f}%)\n'
.format(n_invalid, float(n_invalid) / n_done * 100.0))