openbookqa.py 2.21 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
"""
Can a Suit of Armor Conduct Electricity? A New Dataset for Open Book Question Answering
https://arxiv.org/pdf/1809.02789.pdf

OpenBookQA is a question-answering dataset modeled after open book exams for
assessing human understanding of a subject. It consists of 5,957 multiple-choice
elementary-level science questions (4,957 train, 500 dev, 500 test), which probe
the understanding of a small “book” of 1,326 core science facts and the application
of these facts to novel situations. For training, the dataset includes a mapping
from each question to the core science fact it was designed to probe. Answering
OpenBookQA questions requires additional broad common knowledge, not contained
in the book. The questions, by design, are answered incorrectly by both a retrieval-
based algorithm and a word co-occurrence algorithm.

Homepage: https://allenai.org/data/open-book-qa
16
17
"""
from lm_eval.base import MultipleChoiceTask
18

19
20

_CITATION = """
21
@inproceedings{OpenBookQA2018,
22
23
24
25
    title={Can a Suit of Armor Conduct Electricity? A New Dataset for Open Book Question Answering},
    author={Todor Mihaylov and Peter Clark and Tushar Khot and Ashish Sabharwal},
    booktitle={EMNLP},
    year={2018}
26
27
}
"""
Charles Foster's avatar
Charles Foster committed
28

Jon Tow's avatar
Jon Tow committed
29

Jonathan Tow's avatar
Jonathan Tow committed
30
class OpenBookQA(MultipleChoiceTask):
Leo Gao's avatar
Leo Gao committed
31
    VERSION = 0
Charles Foster's avatar
Charles Foster committed
32
33
34
35
36
37
38
39
40
41
42
43
    DATASET_PATH = "openbookqa"
    DATASET_NAME = "main"

    def has_training_docs(self):
        return True

    def has_validation_docs(self):
        return True

    def has_test_docs(self):
        return True

Jonathan Tow's avatar
Jonathan Tow committed
44
45
    def training_docs(self):
        if self._training_docs is None:
Jon Tow's avatar
Jon Tow committed
46
            self._training_docs = list(map(self._process_doc, self.dataset["train"]))
Jon Tow's avatar
Jon Tow committed
47
        return self._training_docs
Jonathan Tow's avatar
Jonathan Tow committed
48
49

    def validation_docs(self):
Jon Tow's avatar
Jon Tow committed
50
        return map(self._process_doc, self.dataset["validation"])
Jonathan Tow's avatar
Jonathan Tow committed
51
52

    def test_docs(self):
Jon Tow's avatar
Jon Tow committed
53
        return map(self._process_doc, self.dataset["test"])
Jonathan Tow's avatar
Jonathan Tow committed
54

Jon Tow's avatar
Jon Tow committed
55
    def _process_doc(self, doc):
Jon Tow's avatar
Jon Tow committed
56
57
58
59
60
61
62
63
        out_doc = {
            "id": doc["id"],
            "query": doc["question_stem"],
            "choices": doc["choices"]["text"],
            "gold": ["A", "B", "C", "D"].index(doc["answerKey"].strip()),
        }
        return out_doc

64
    def doc_to_text(self, doc):
Jon Tow's avatar
Jon Tow committed
65
        return doc["query"]