triviaqa.py 3.15 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
"""
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/

@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},
}
"""
22
import os
Anish Thite's avatar
Anish Thite committed
23
import json
Leo Gao's avatar
Leo Gao committed
24
import jsonlines
&'s avatar
& committed
25
26
from lm_eval.base import Task, rf
from ..metrics import mean
Anish Thite's avatar
Anish Thite committed
27
from ..utils import sh
Leo Gao's avatar
Leo Gao committed
28
from best_download import download_file
Anish Thite's avatar
Anish Thite committed
29

30

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

    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
49
        return False
Anish Thite's avatar
Anish Thite committed
50
51

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

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

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

    def doc_to_target(self, doc):
Leo Gao's avatar
Leo Gao committed
64
65
66
67
68
69
70
71
72
73
74
75
        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
76

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

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

    def aggregation(self):
90
91
92
        return {
            "acc": mean,
        }
Leo Gao's avatar
Leo Gao committed
93
94

    def higher_is_better(self):
95
96
        return {
            "acc": True
Leo Gao's avatar
Leo Gao committed
97
        }