-
Notifications
You must be signed in to change notification settings - Fork 0
/
label_file.py
65 lines (48 loc) · 1.39 KB
/
label_file.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
'''
'''
import pickle
import sys
import joblib
import json
def load_remove_set(f):
the_set = []
with open(f) as handle:
for new_line in handle:
the_set.append(' '.join(new_line.split()[1:]))
return the_set
if __name__ == '__main__':
filename = sys.argv[1]
clf = joblib.load(sys.argv[2])
dest = sys.argv[3]
if len(sys.argv) > 4:
remove_set = load_remove_set(sys.argv[4])
else:
remove_set = []
all_lines = []
with open(filename) as handle:
for new_line in handle:
tokens = new_line.split()
if ' '.join(tokens) in remove_set:
continue
if len(tokens) > 100:
continue
all_lines.append(' '.join(tokens))
import operator
probabilities = clf.predict_proba(all_lines)
items = []
with open(dest, 'w') as handle:
for i, line in enumerate(all_lines):
items.append((line, probabilities[i,1]))
sorted_items = sorted(items, key=operator.itemgetter(1), reverse=True)
for k, v in sorted_items:
handle.write(
json.dumps(
{
'text': k,
'prob': v,
'label': 1,
'category': 0
}
)
)
handle.write('\n')