maximedb commited on
Commit
a0268ef
1 Parent(s): 9289956

update_margin_score.py

Browse files
Files changed (1) hide show
  1. update_margin_score.py +49 -0
update_margin_score.py ADDED
@@ -0,0 +1,49 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import tqdm
4
+ import argparse
5
+ import functools
6
+ import multiprocessing
7
+
8
+
9
+ def process_file(filename, margin_data=None):
10
+
11
+ in_file = open(filename, "r")
12
+ out_file = open(filename + ".tmp", "w+")
13
+
14
+ for line in in_file:
15
+ line = json.loads(line)
16
+ line["margin_score"] = margin_data.get(line["id"], -1000)
17
+ out_file.write(json.dumps(line, ensure_ascii=False) + "\n")
18
+
19
+ in_file.close()
20
+ out_file.close()
21
+ os.rename(filename + ".tmp", filename)
22
+
23
+
24
+ def main(args):
25
+
26
+ with open(args.positive_file) as f:
27
+ positive_data = json.load(f)
28
+
29
+ with open(args.negative_file) as f:
30
+ negative_data = json.load(f)
31
+
32
+ margin_data = {k: positive_data[k] - negative_data[k] for k in positive_data.keys()}
33
+
34
+ process_file_fn = functools.partial(process_file, margin_data=margin_data)
35
+ filenames = args.valid_filenames + args.train_filenames
36
+ with multiprocessing.Pool(args.num_workers) as p:
37
+ for _ in tqdm.tqdm(p.imap_unordered(process_file_fn, filenames), total=len(filenames)):
38
+ pass
39
+
40
+
41
+ if __name__ == "__main__":
42
+ parser = argparse.ArgumentParser()
43
+ parser.add_argument("--positive_file")
44
+ parser.add_argument("--negative_file")
45
+ parser.add_argument("--valid_filenames", nargs="+")
46
+ parser.add_argument("--train_filenames", nargs="+")
47
+ parser.add_argument("--num_workers", type=int, default=os.cpu_count())
48
+ args = parser.parse_args()
49
+ main(args)