-
Notifications
You must be signed in to change notification settings - Fork 6
/
grade.py
79 lines (62 loc) · 2.55 KB
/
grade.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
72
73
74
75
76
77
78
import argparse
import json
import random
def get_languages():
return json.load(open('languages.json'))
def parse_test_file(test_file):
return {
d['example']: d['text']
for d in (json.loads(l) for l in open(test_file))
}
def parse_answer_file(answer_file):
return {
d['example']: d['lang']
for d in (json.loads(l) for l in open(answer_file))
}
def grade(reference_answer_file, team_answer_file, test_file):
reference_answers = parse_answer_file(reference_answer_file)
team_answers = parse_answer_file(team_answer_file)
test_data = parse_test_file(test_file)
languages = get_languages()
max_score = len(reference_answers)
correct_answers = []
incorrect_answers = []
blank_answers = []
for example, answer in team_answers.items():
test_text = test_data[example]
correct_answer = reference_answers[example]
if answer is None:
blank_answers.append((answer, correct_answer, test_text))
elif answer == correct_answer:
correct_answers.append((answer, correct_answer, test_text))
else:
incorrect_answers.append((answer, correct_answer, test_text))
score = len(correct_answers) - len(incorrect_answers)
print("Final score {}/{}!".format(score, max_score))
print("Correct answers: {}!".format(len(correct_answers)))
print("Incorrect answers: {}!".format(len(incorrect_answers)))
print("Blank answers: {}!".format(len(blank_answers)))
print("Examples of correct answers from team:")
for answer, correct_answer, text in random.sample(correct_answers, 5):
print("({}) {}".format(languages[answer], text))
print()
print("Examples of incorrect answers from team:")
for answer, correct_answer, text in random.sample(incorrect_answers, 5):
try:
print("(team answer: {}, correct answer: {}) {}".format(languages[answer], languages[correct_answer], text))
print()
except KeyError:
print("team answer: {} is not a recognised language code".format(answer))
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Grade dojo lang detector')
parser.add_argument('reference_answer_file', type=str)
parser.add_argument('team_answer_file', type=str)
parser.add_argument('test_file', type=str, default='')
args = parser.parse_args()
print(args.reference_answer_file)
print(args.team_answer_file)
grade(
args.reference_answer_file,
args.team_answer_file,
args.test_file,
)