File size: 6,697 Bytes
2f07800
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6e92088
 
2f07800
 
 
 
 
 
 
 
6e92088
2f07800
 
 
 
 
 
 
 
6e92088
2f07800
 
 
 
 
 
 
 
6e92088
2f07800
 
 
 
6e92088
 
 
 
2f07800
 
 
 
 
 
 
 
 
 
 
 
 
 
6e92088
 
2f07800
 
 
 
 
 
 
 
 
 
 
 
 
 
ddeef5b
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# coding=utf-8
# Copyright 2020 HuggingFace Datasets Authors.
#
# 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.

# Lint as: python3
"""Inquisitive Question Generation for High Level Text Comprehension"""


import itertools

import datasets


_CITATION = """\
@InProceedings{ko2020inquisitive,
  author    = {Ko, Wei-Jen and Chen, Te-Yuan and Huang, Yiyan and Durrett, Greg and Li, Junyi Jessy},
  title     = {Inquisitive Question Generation for High Level Text Comprehension},
  booktitle = {Proceedings of EMNLP},
  year      = {2020},
}
"""

_DESCRIPTION = """\
A dataset of about 20k questions that are elicited from readers as they naturally read through a document sentence by sentence. \
Compared to existing datasets, INQUISITIVE questions target more towards high-level (semantic and discourse) comprehension of text. \
Because these questions are generated while the readers are processing the information, the questions directly communicate gaps between \
the reader’s and writer’s knowledge about the events described in the text, and are not necessarily answered in the document itself. \
This type of question reflects a real-world scenario: if one has questions during reading, some of them are answered by the text later on, \
the rest are not, but any of them would help further the reader’s understanding at the particular point when they asked it. \
This resource could enable question generation models to simulate human-like curiosity and cognitive processing, which may open up a new realm of applications.
"""

_ARTICLES_URL = "https://github.com/wjko2/INQUISITIVE/raw/master/articles.tgz"
_QUESTIONS_URL = "https://github.com/wjko2/INQUISITIVE/raw/master/questions.txt"

ALL_ARTICLE_IDS = list(range(1, 1501))
DEV_ARTICLE_IDS = list(itertools.chain(range(1, 101), range(1051, 1101)))
TEST_ARTICLE_IDS = list(itertools.chain(range(101, 151), range(501, 551), range(1101, 1151)))
DEV_AND_TEST_IDS = DEV_ARTICLE_IDS + TEST_ARTICLE_IDS
TRAIN_ARTICLE_IDS = [id_ for id_ in ALL_ARTICLE_IDS if id_ not in DEV_AND_TEST_IDS]


class InquisitiveQgConfig(datasets.BuilderConfig):
    """BuilderConfig for INQUISITIVE."""

    def __init__(self, **kwrags):
        """BuilderConfig for INQUISITIVE.

        Args:
          **kwargs: keyword arguments forwarded to super.
        """
        super(InquisitiveQgConfig, self).__init__(**kwrags)


class InquisitiveQg(datasets.GeneratorBasedBuilder):
    """Inquisitive Question Generation for High Level Text Comprehension"""

    VERSION = datasets.Version("1.0.0")
    BUILDER_CONFIGS = [
        InquisitiveQgConfig(name="plain_text", version=datasets.Version("1.0.0", ""), description="plain_text"),
    ]

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "id": datasets.Value("int32"),
                    "article_id": datasets.Value("int32"),
                    "article": datasets.Value("string"),
                    "sentence_id": datasets.Value("int32"),
                    "sentence": datasets.Value("string"),
                    "span": datasets.Value("string"),
                    "question": datasets.Value("string"),
                    "span_start_position": datasets.Value("int32"),
                    "span_end_position": datasets.Value("int32"),
                }
            ),
            supervised_keys=None,
            homepage="https://github.com/wjko2/INQUISITIVE",
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        questions_file = dl_manager.download(_QUESTIONS_URL)
        archive = dl_manager.download(_ARTICLES_URL)
        articles_dir = "article"

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "articles_dir": articles_dir,
                    "questions_file": questions_file,
                    "article_ids": TRAIN_ARTICLE_IDS,
                    "files": dl_manager.iter_archive(archive),
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={
                    "articles_dir": articles_dir,
                    "questions_file": questions_file,
                    "article_ids": DEV_ARTICLE_IDS,
                    "files": dl_manager.iter_archive(archive),
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={
                    "articles_dir": articles_dir,
                    "questions_file": questions_file,
                    "article_ids": TEST_ARTICLE_IDS,
                    "files": dl_manager.iter_archive(archive),
                },
            ),
        ]

    def _generate_examples(self, articles_dir, questions_file, article_ids, files):
        articles = {}
        for path, f in files:
            articles[path] = f.read().decode("utf-8")
        with open(questions_file, encoding="utf-8") as f:
            questions_counter = 0
            rows = f.readlines()
            for i, row in enumerate(rows):
                if i == 0:
                    continue  # skip header line
                row = row.strip()
                cols = row.split("\t")

                article_id = int(cols[0])
                if article_id not in article_ids:
                    continue

                fname = str(article_id).rjust(4, "0") + ".txt"
                article_path = articles_dir + "/" + fname
                article = articles[article_path]

                id_ = str(questions_counter)
                example = {
                    "article_id": article_id,
                    "sentence_id": int(cols[1]),
                    "sentence": cols[2],
                    "span": cols[3],
                    "question": cols[4],
                    "span_start_position": cols[5],
                    "span_end_position": cols[6],
                    "id": id_,
                    "article": article,
                }
                yield id_, example
                questions_counter += 1