"tests/models/xlnet/test_modeling_tf_xlnet.py" did not exist on "63e3827c6bc5af9807b77e07fdcdae74b7d57161"
quac.py 2.46 KB
Newer Older
Charles Foster's avatar
Charles Foster committed
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
import json
import random
import os
from lm_eval.base import Dataset
from ..utils import sh


class QuAC(Dataset):    
    def __init__(self):
        super().__init__()

    def download(self):
        if not os.path.exists('data/quac'):
            sh("""
                mkdir -p data/quac 
                wget https://s3.amazonaws.com/my89public/quac/train_v0.2.json -O data/quac/train_v0.2.json
                wget https://s3.amazonaws.com/my89public/quac/val_v0.2.json -O data/quac/val_v0.2.json
                """)

    def has_training_docs(self):
        return True

    def has_validation_docs(self):
        return True

    def has_test_docs(self):
        return False

    def training_docs(self):
        myjson = json.load(open('data/quac/train_v0.2.json'))['data']
        return self.load_doc(myjson)

    def validation_docs(self):
        myjson = json.load(open('data/quac/val_v0.2.json'))['data']    
        return self.load_doc(myjson)

    def test_docs(self):
        raise NotImplementedError("QuAC has no test docs.")
    
    def fewshot_examples(self, k):
        traindocs = list(self.training_docs())
        random.shuffle(traindocs)

        return traindocs[:k]
    
    def fewshot_description(self):
        desc = "TITLE: Title of the context passage - subtitle of the passage\nPARAGRAPH: Passage describing the relevant information for answering questions.\n\nQ: Text of a question.\n\nA: Answer to the question, based on the passage. If it cannot be answered based on the passage, write CANNOTANSWER"
        return desc

    def load_doc(self, myjson):
        docs = []
        for item in myjson:
            title = item['title'] + ' - ' + item['section_title']
            paragraph = item['paragraphs'][0]['context'].replace("CANNOTANSWER", "")
            qas = item['paragraphs'][0]['qas']
            qa_pairs = [(qa['question'], qa['answers'][0]['text']) for qa in qas]
            for (question, answer) in qa_pairs:
                doc = { 'title': title, 'paragraph': paragraph, 'question': question, 'answer': answer }
                docs.append(doc)  
        return docs
    
    def doc_to_text(self, doc, include_target=True):
        text = 'TITLE: ' + doc['title'] + '\n' + 'PARAGRAPH: ' + doc['paragraph'] + '\n\n' + 'Q: ' + doc['question'] + '\n\n' + 'A: '
        if include_target:
            text += doc['answer']
        return text

    def evaluate(self, docs, lm):
        pass