File size: 1,046 Bytes
dee113c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright (c) Microsoft Corporation. 
# Licensed under the MIT license.
import json

def extract_answers(filename):
	cluster={}
	with open(filename) as f:
		for line in f:
			line=line.strip()
			js=json.loads(line)
			if js['label'] not in cluster:
				cluster[js['label']]=set()
			cluster[js['label']].add(js['index'])
	answers=[]
	for key in cluster:
		for idx1 in cluster[key]:
			temp={}
			temp['index']=idx1
			temp['answers']=[]
			for idx2 in cluster[key]:
				if idx1!=idx2:
					temp['answers'].append(idx2)
			answers.append(temp)
	return answers


def main():
    import argparse
    parser = argparse.ArgumentParser(description='Extract answers from code files.')
    parser.add_argument('--codefile', '-c',help="filename of the code examples.")
    parser.add_argument('--outfile', '-o',help="filename of output.")
    args = parser.parse_args()
    answers=extract_answers(args.codefile)
    with open(args.outfile,'w') as f:
    	for line in answers:
    		f.write(json.dumps(line)+'\n')

if __name__ == '__main__':
    main()