File size: 6,422 Bytes
038ae25
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# coding=utf-8
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""A Dataset loading script for the QA-Discourse dataset (Pyatkin et. al., ACL 2020)."""


import datasets
from pathlib import Path
from typing import List
import pandas as pd


_CITATION = """\
@inproceedings{pyatkin2020qadiscourse,
  title={QADiscourse-Discourse Relations as QA Pairs: Representation, Crowdsourcing and Baselines},
  author={Pyatkin, Valentina and Klein, Ayal and Tsarfaty, Reut and Dagan, Ido},
  booktitle={Proceedings of the 2020 Conference on Empirical Methods in Natural Language Processing (EMNLP)},
  pages={2804--2819},
  year={2020}
}"""


_DESCRIPTION = """\
The dataset contains question-answer pairs to model discourse relations. 
While answers roughly correspond to spans of the sentence, these spans could have been freely adjusted by annotators to grammaticaly fit the question;
Therefore, answers are given just as text and not as identified spans of the original sentence.    
See the paper for details: QADiscourse - Discourse Relations as QA Pairs: Representation, Crowdsourcing and Baselines, Pyatkin et. al., 2020
"""

_HOMEPAGE = "https://github.com/ValentinaPy/QADiscourse"

_LICENSE = """Resources on this page are licensed CC-BY 4.0, a Creative Commons license requiring Attribution (https://creativecommons.org/licenses/by/4.0/)."""


_URLs = {
    "wikinews.train": "https://github.com/ValentinaPy/QADiscourse/raw/master/Dataset/wikinews_train.tsv",
    "wikinews.dev": "https://github.com/ValentinaPy/QADiscourse/raw/master/Dataset/wikinews_dev.tsv",
    "wikinews.test": "https://github.com/ValentinaPy/QADiscourse/raw/master/Dataset/wikinews_test.tsv",
    "wikipedia.train": "https://github.com/ValentinaPy/QADiscourse/raw/master/Dataset/wikipedia_train.tsv",
    "wikipedia.dev": "https://github.com/ValentinaPy/QADiscourse/raw/master/Dataset/wikipedia_dev.tsv",
    "wikipedia.test": "https://github.com/ValentinaPy/QADiscourse/raw/master/Dataset/wikipedia_test.tsv",      
}

# TODO: Name of the dataset usually match the script name with CamelCase instead of snake_case
class QaDiscourse(datasets.GeneratorBasedBuilder):
    """QA-Discourse: Discourse Relations as Question-Answer Pairs.  """

    VERSION = datasets.Version("1.0.0")

    BUILDER_CONFIGS = [
        datasets.BuilderConfig(
            name="plain_text", version=VERSION, description="This provides the QA-Discourse dataset"
        ),
    ]

    DEFAULT_CONFIG_NAME = (
        "plain_text"  # It's not mandatory to have a default configuration. Just use one if it make sense.
    )

    def _info(self):
        features = datasets.Features(
            {
                "sentence": datasets.Value("string"),
                "sent_id": datasets.Value("string"),
                "question": datasets.Sequence(datasets.Value("string")),
                "answers": datasets.Sequence(datasets.Value("string")),
            }
        )
        return datasets.DatasetInfo(
            # This is the description that will appear on the datasets page.
            description=_DESCRIPTION,
            # This defines the different columns of the dataset and their types
            features=features,  # Here we define them above because they are different between the two configurations
            # If there's a common (input, target) tuple from the features,
            # specify them here. They'll be used if as_supervised=True in
            # builder.as_dataset.
            supervised_keys=None,
            # Homepage of the dataset for documentation
            homepage=_HOMEPAGE,
            # License for the dataset if available
            license=_LICENSE,
            # Citation for the dataset
            citation=_CITATION,
        )
            
    def _split_generators(self, dl_manager: datasets.utils.download_manager.DownloadManager):
        """Returns SplitGenerators."""            
        
        # Download and prepare all files - keep same structure as _URLs 
        corpora = {section:  Path(dl_manager.download_and_extract(_URLs[section])) 
                   for section in _URLs} 
            
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                # These kwargs will be passed to _generate_examples
                gen_kwargs={
                    "filepaths": [corpora["wikinews.train"], 
                                  corpora["wikipedia.train"]],
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                # These kwargs will be passed to _generate_examples
                gen_kwargs={
                    "filepaths": [corpora["wikinews.dev"], 
                                  corpora["wikipedia.dev"]],
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                # These kwargs will be passed to _generate_examples
                gen_kwargs={
                    "filepaths": [corpora["wikinews.test"], 
                                  corpora["wikipedia.test"]],
                },
            ),
        ]
    
    def _generate_examples(self, filepaths: List[str]):

        """ Yields QA-Discourse examples from a tsv file."""

        # merge annotations from sections 
        df = pd.concat([pd.read_csv(fn, separator='\t') for fn in filepaths]).reset_index()
        for counter, row in df.iterrows():
            # Prepare question (3 "slots" and question mark)
            question = [row.question_start, row.question_aux, row.question_body.str.rstrip('?'), '?']
            
            yield counter, {
                "sentence": row.sentence,
                "sent_id": row.qasrl_id,
                "question": question,
                "answers": [row.answer],
            }