forked from se4u/neural_wfst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrim_test_file.py
60 lines (47 loc) · 1.85 KB
/
trim_test_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
import sys
import os
from random import choices
def trim_test_file(test_file_path, trimmed_test_file_path):
(map_query_2_positives, map_query_2_negatives) = get_map_query_2_lines(test_file_path)
with open(trimmed_test_file_path, 'w') as write_file:
counter = 0
for (k, v) in map_query_2_positives.items():
num_pos_lines = 0
for v in map_query_2_positives[k]:
line = '\t'.join(v) + '\n'
write_file.write(line)
num_pos_lines += 1
if(num_pos_lines >= 100):
break
num_neg_lines = choices(map_query_2_negatives[k], k = (100 - num_pos_lines))
for i in num_neg_lines:
line = '\t'.join(i) + '\n'
write_file.write(line)
counter += 1
if(counter >= 50):
return
def get_map_query_2_lines(filepath):
map_query_2_positives = {}
map_query_2_negatives = {}
with open(filepath, 'r') as file:
for line in file.readlines():
entities = line.strip('\n').split('\t')
query = entities[0]
label = int(entities[2])
if(label == 1):
if query in map_query_2_positives.keys():
map_query_2_positives[query].append(entities)
else:
map_query_2_positives[query] = [entities]
else:
if query in map_query_2_negatives.keys():
map_query_2_negatives[query].append(entities)
else:
map_query_2_negatives[query] = [entities]
return (map_query_2_positives, map_query_2_negatives)
def main():
file_path = sys.argv[1]
trimmed_file_path = file_path + "_trimmed"
trim_test_file(file_path, trimmed_file_path)
if __name__ == "__main__":
main()