File size: 3,434 Bytes
d9e8645
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import json

import datasets
from datasets.tasks import QuestionAnsweringExtractive


_CITATION = """"""

_DESCRIPTION = """\
Manually generated dataset for policies qa
"""

_URLS = {
    "train": "./data/train.json",
    "test": "./data/test.json"
}


class PoliciesQAConfig(datasets.BuilderConfig):
    """BuilderConfig for Ineract Policies."""

    def __init__(self, **kwargs):
        """BuilderConfig for Ineract Policies.    
        Args:
          **kwargs: keyword arguments forwarded to super.
        """
        super(PoliciesQAConfig, self).__init__(**kwargs)


class PoliciesQA(datasets.GeneratorBasedBuilder):
    """Ineract Policies: The Policy Question Answering Dataset. Version 0.1"""

    BUILDER_CONFIGS = [
        PoliciesQAConfig(
            name="plain_text",
            version=datasets.Version("1.0.0", ""),
            description="Plain text",
        ),
    ]

    DEFAULT_CONFIG_NAME = "plain_text"

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "id": datasets.Value("string"),
                    "context": datasets.Value("string"),
                    "question": datasets.Value("string"),
                    "answers": datasets.features.Sequence(
                        {
                            "text": datasets.Value("string"),
                            "answer_start": datasets.Value("int32"),
                        }
                    ),
                }
            ),
            # No default supervised_keys (as we have to pass both question
            # and context as input).
            supervised_keys=None,
            homepage="ineract.com",
            task_templates=[
                QuestionAnsweringExtractive(
                    question_column="question", context_column="context", answers_column="answers"
                )
            ],
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        downloaded_files = dl_manager.download_and_extract(_URLS)

        return [
            datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={
                                    "filepath": downloaded_files["train"], "split": "train"}),
            datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={
                                    "filepath": downloaded_files["test"], "split": "test"})
        ]

    def _generate_examples(self, filepath, split):
        """This function returns the examples in the raw (text) form."""
        key = 0
        with open(filepath, encoding="utf-8") as f:
            policies = json.load(f)
            for policy in policies["data"]:
                id = policy["id"]
                context = policy["context"]
                question = policy["question"]
                answer_starts = [answer_start
                                 for answer_start in policy["answers"]["answer_start"]]
                answers = [
                    answer_text for answer_text in policy["answers"]["text"]]
                yield key, {
                    "id": id,
                    "context": context,
                    "question": question,
                    "answers": {
                        "answer_start": answer_starts,
                        "text": answers,
                    },
                }
                key += 1