-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparse_amt_result.py
71 lines (61 loc) · 2.1 KB
/
parse_amt_result.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
66
67
68
69
70
71
import csv
import sys
import numpy as np
from collections import defaultdict
from argparse import ArgumentParser
MAX_REQUEST_NUM = 10
def parse_amt_result(input_filepath):
result = defaultdict(list)
with open(input_filepath) as f:
rows = csv.reader(f)
header = None
for row in rows:
if header is None:
header = row
else:
row_values = {key: value for key, value in zip(header, row)}
res = parse_row(row_values)
for r in res:
key = (r['qid'], r['did'])
result[key].append(r['rel'])
return result
def parse_row(row_values):
result = []
for i in range(2, MAX_REQUEST_NUM+1):
qid = row_values["Input.qid{}".format(i)]
did = row_values["Input.did{}".format(i)]
rel = None
for score in range(3):
score_bool = row_values["Answer.option{0}_{1}.{1}"
.format(i, score)]
if score_bool == 'true':
rel = score
break
true_count = 0
for score in range(3):
score_bool = row_values["Answer.option{0}_{1}.{1}"
.format(i, score)]
if score_bool == 'true':
true_count += 1
assert true_count == 1
result.append(dict(qid=qid, did=did, rel=rel))
return result
def main():
parser = ArgumentParser(description="Parse a CSV file downloaded "
"from AMT.")
parser.add_argument('input_filepaths', nargs='+',
help="Files downloaded from Lancers")
if len(sys.argv) == 1:
parser.print_help(sys.stderr)
sys.exit(1)
args = parser.parse_args()
result = defaultdict(list)
for input_filepath in args.input_filepaths:
res = parse_amt_result(input_filepath)
for key in res:
result[key] += res[key]
for key in sorted(result):
scores = result[key]
print(" ".join(map(str, list(key) + scores)))
if __name__ == '__main__':
main()