triviaqa.py 3.17 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
"""
TriviaQA: A Large Scale Distantly Supervised Challenge Dataset for Reading Comprehension
https://arxiv.org/pdf/1705.03551.pdf

TriviaQA is a reading comprehension dataset containing over 650K question-answer-evidence
triples. TriviaQA includes 95K question-answer pairs authored by trivia enthusiasts
and independently gathered evidence documents, six per question on average, that provide
high quality distant supervision for answering the questions.

Homepage: https://nlp.cs.washington.edu/triviaqa/
"""
12
import os
Anish Thite's avatar
Anish Thite committed
13
import json
Leo Gao's avatar
Leo Gao committed
14
import jsonlines
&'s avatar
& committed
15
16
from lm_eval.base import Task, rf
from ..metrics import mean
Anish Thite's avatar
Anish Thite committed
17
from ..utils import sh
Leo Gao's avatar
Leo Gao committed
18
from best_download import download_file
Anish Thite's avatar
Anish Thite committed
19

20

21
22
23
24
25
26
27
28
29
30
31
32
33
_CITATION = """
@InProceedings{JoshiTriviaQA2017,
    author = {Joshi, Mandar and Choi, Eunsol and Weld, Daniel S. and Zettlemoyer, Luke},
    title = {TriviaQA: A Large Scale Distantly Supervised Challenge Dataset for Reading Comprehension},
    booktitle = {Proceedings of the 55th Annual Meeting of the Association for Computational Linguistics},
    month = {July},
    year = {2017},
    address = {Vancouver, Canada},
    publisher = {Association for Computational Linguistics},
}
"""


34
class TriviaQA(Task):
Leo Gao's avatar
Leo Gao committed
35
    VERSION = 0
Anish Thite's avatar
Anish Thite committed
36
    def download(self):
Leo Gao's avatar
Leo Gao committed
37
38
        if not os.path.exists('data/triviaqa/unfiltered-web-train.jsonl'):
            os.makedirs("data/triviaqa/", exist_ok=True)
39
            download_file("http://eaidata.bmk.sh/data/triviaqa-unfiltered.tar.gz", local_file="data/triviaqa/triviaqa-unfiltered.tar.gz", expected_checksum="adc19b42769062d241a8fbe834c56e58598d9322eb6c614e9f33a68a2cf5523e")
40
            sh("""
Leo Gao's avatar
Leo Gao committed
41
42
            cd data/triviaqa/
            tar -xf triviaqa-unfiltered.tar.gz
43
            """)
Anish Thite's avatar
Anish Thite committed
44
45
46
47
48
49
50
51

    def has_training_docs(self):
        return True

    def has_validation_docs(self):
        return True

    def has_test_docs(self):
Leo Gao's avatar
Leo Gao committed
52
        return False
Anish Thite's avatar
Anish Thite committed
53
54

    def training_docs(self):
Leo Gao's avatar
Leo Gao committed
55
        return jsonlines.open('data/triviaqa/unfiltered-web-train.jsonl')
Anish Thite's avatar
Anish Thite committed
56
57

    def validation_docs(self):
Leo Gao's avatar
Leo Gao committed
58
        return jsonlines.open('data/triviaqa/unfiltered-web-dev.jsonl')
Anish Thite's avatar
Anish Thite committed
59
60

    def test_docs(self):
61
        raise NotImplementedError()
Anish Thite's avatar
Anish Thite committed
62
    
63
    def doc_to_text(self, doc):
64
        return f"Question: {doc['Question']}\nAnswer:"
65
66

    def doc_to_target(self, doc):
Leo Gao's avatar
Leo Gao committed
67
68
69
70
71
72
73
74
75
76
77
78
        return " " + doc['Answer']['Value']

    def _remove_prefixes(self, aliases):
        # Optimization: Remove any alias that has a strict prefix elsewhere in the list
        # we can do this because if the prefix is acceptable by isgreedy, we can stop looking
        aliases.sort()
        ret = [aliases[0]]
        for alias in aliases[1:]:
            if not alias.startswith(ret[-1]):
                ret.append(alias)

        return ret
Anish Thite's avatar
Anish Thite committed
79

Leo Gao's avatar
Leo Gao committed
80
    def construct_requests(self, doc, ctx):
Leo Gao's avatar
Leo Gao committed
81
82
83
84
85
        ret = []
        for alias in self._remove_prefixes(doc['Answer']['Aliases']):
            _, is_prediction = rf.loglikelihood(ctx, " " + alias)
            ret.append(is_prediction)
        return ret
86

Leo Gao's avatar
Leo Gao committed
87
    def process_results(self, doc, results):
88
        return {
Leo Gao's avatar
Leo Gao committed
89
            "acc": float(any(results))
90
        }
Leo Gao's avatar
Leo Gao committed
91
92

    def aggregation(self):
93
94
95
        return {
            "acc": mean,
        }
Leo Gao's avatar
Leo Gao committed
96
97

    def higher_is_better(self):
98
99
        return {
            "acc": True
Leo Gao's avatar
Leo Gao committed
100
        }