preprocess_race.py 1.08 KB
Newer Older
lintangsutawika's avatar
lintangsutawika committed
1
2
import ast

3
4
5
6

def process_ast(string):
    return ast.literal_eval(string)

lintangsutawika's avatar
lintangsutawika committed
7

8
def last_problem(doc):
9
    return process_ast(doc["problems"])[-1]
10

lintangsutawika's avatar
lintangsutawika committed
11

12
13
14
15
16
def get_answer_option(problem):
    letter_to_num = {"A": 0, "B": 1, "C": 2, "D": 3}
    answer = letter_to_num[problem["answer"]]
    return problem["options"][answer]

lintangsutawika's avatar
lintangsutawika committed
17

18
19
20
21
22
def create_choices(doc):
    problem = last_problem(doc)
    choices = [problem["options"][i] for i in range(4)]
    return choices

lintangsutawika's avatar
lintangsutawika committed
23

24
25
def doc_to_text(doc):
    text = "Article: " + doc["article"] + "\n\n"
26
    for problem in process_ast(doc["problems"])[:-1]:
27
        if problem["question"][-6:] == "  _  .":
lintangsutawika's avatar
lintangsutawika committed
28
            text += problem["question"][-5:] + get_answer_option(problem) + "\n"
29
30
31
32
33
34
35
        else:
            question = "Question: " + problem["question"] + "\n"
            answer = "Answer: " + get_answer_option(problem) + "\n"
            text += question + answer
    text += last_problem(doc)["question"]
    return text

lintangsutawika's avatar
lintangsutawika committed
36

37
38
39
40
def doc_to_target(doc):
    letter_to_num = {"A": 0, "B": 1, "C": 2, "D": 3}
    answer = letter_to_num[last_problem(doc)["answer"]]
    return answer