File size: 8,819 Bytes
2361927
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72f0c4f
2361927
 
27753d4
2361927
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3fedebe
e9bb999
2361927
c6e9551
2361927
 
 
 
72f0c4f
2361927
 
f1ee938
 
 
 
 
 
2361927
 
 
f1ee938
 
 
 
 
 
 
72f0c4f
 
f1ee938
72f0c4f
 
40ea8f9
 
 
 
 
 
27753d4
 
 
 
 
 
2361927
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72f0c4f
 
 
2361927
 
 
 
f1ee938
72f0c4f
 
2361927
 
 
 
72f0c4f
 
 
f1ee938
72f0c4f
 
f1ee938
72f0c4f
 
f1ee938
 
 
 
 
 
 
27753d4
 
 
 
 
 
 
 
 
 
 
 
 
f1ee938
40ea8f9
 
 
72f0c4f
27753d4
 
72f0c4f
 
f1ee938
 
72f0c4f
 
27753d4
 
 
40ea8f9
27753d4
 
 
 
 
 
 
40ea8f9
27753d4
 
 
69e1d00
f1ee938
 
 
 
 
 
 
 
 
 
 
 
 
40ea8f9
 
f1ee938
 
 
 
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# coding=utf-8
#
# 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
"""ETPC: The Extended Typology Paraphrase Corpus"""

import os
from typing import Any, Dict, Generator, List, Optional, Tuple, Union

import datasets
import numpy as np
from datasets.tasks import TextClassification
from lxml import etree

logger = datasets.logging.get_logger(__name__)


_CITATION = """\
@inproceedings{kovatchev-etal-2018-etpc,
    title = "{ETPC} - A Paraphrase Identification Corpus Annotated with Extended Paraphrase Typology and Negation",
    author = "Kovatchev, Venelin  and
      Mart{\'\i}, M. Ant{\`o}nia  and
      Salam{\'o}, Maria",
    booktitle = "Proceedings of the Eleventh International Conference on Language Resources and Evaluation ({LREC} 2018)",
    month = may,
    year = "2018",
    address = "Miyazaki, Japan",
    publisher = "European Language Resources Association (ELRA)",
    url = "https://aclanthology.org/L18-1221",
}
"""

_DESCRIPTION = """\
The EPT typology addresses several practical limitations of existing paraphrase typologies: it is the first typology that copes with the non-paraphrase pairs in the paraphrase identification corpora and distinguishes between contextual and habitual paraphrase types. ETPC is the largest corpus to date annotated with atomic paraphrase types.
"""

_HOMEPAGE = "https://github.com/venelink/ETPC"

_LICENSE = "Unknown"

_URLS = [
    "https://raw.githubusercontent.com/venelink/ETPC/master/Corpus/text_pairs.xml",
    "https://raw.githubusercontent.com/venelink/ETPC/master/Corpus/textual_paraphrases.xml",
]


class ETPC(datasets.GeneratorBasedBuilder):
    """ETPC dataset."""

    VERSION = datasets.Version("0.95.0")

    def _info(self):
        features = datasets.Features(
            {
                "idx": datasets.Value("string"),
                "sentence1": datasets.Value("string"),
                "sentence2": datasets.Value("string"),
                "sentence1_tokenized": datasets.Sequence(
                    datasets.Value("string")
                ),
                "sentence2_tokenized": datasets.Sequence(
                    datasets.Value("string")
                ),
                "etpc_label": datasets.Value("int8"),
                "mrpc_label": datasets.Value("int8"),
                "negation": datasets.Value("int8"),
                "paraphrase_types": datasets.Sequence(
                    datasets.Value("string")
                ),
                "paraphrase_type_ids": datasets.Sequence(
                    datasets.Value("string")
                ),
                "sentence1_segment_location": datasets.Sequence(
                    datasets.Value("int32")
                ),
                "sentence2_segment_location": datasets.Sequence(
                    datasets.Value("int32")
                ),
                "sentence1_segment_location_indices": datasets.Sequence(
                    datasets.Sequence(datasets.Value("int32"))
                ),
                "sentence2_segment_location_indices": datasets.Sequence(
                    datasets.Sequence(datasets.Value("int32"))
                ),
                "sentence1_segment_text": datasets.Sequence(
                    datasets.Value("string")
                ),
                "sentence2_segment_text": datasets.Sequence(
                    datasets.Value("string")
                ),
            }
        )

        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        dl_dir = dl_manager.download_and_extract(_URLS)
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "file_paths": dl_manager.iter_files(dl_dir),
                },
            ),
        ]

    def _generate_examples(self, file_paths):
        file_paths = list(file_paths)
        text_pairs_path = file_paths[0]
        paraphrase_types_path = file_paths[1]

        parser = etree.XMLParser(encoding="utf-8", recover=True)

        tree_text_pairs = etree.parse(text_pairs_path, parser=parser)
        tree_paraphrase_types = etree.parse(
            paraphrase_types_path, parser=parser
        )

        root_text_pairs = tree_text_pairs.getroot()
        root_paraphrase_types = tree_paraphrase_types.getroot()

        idx = 0

        for row in root_text_pairs:
            current_pair_id = row.find(".//pair_id").text
            paraphrase_types = root_paraphrase_types.xpath(
                f".//pair_id[text()='{current_pair_id}']/parent::relation/type_name/text()"
            )
            paraphrase_type_ids = root_paraphrase_types.xpath(
                f".//pair_id[text()='{current_pair_id}']/parent::relation/type_id/text()"
            )
            sentence1_segment_location = root_paraphrase_types.xpath(
                f".//pair_id[text()='{current_pair_id}']/parent::relation/s1_scope/text()"
            )
            sentence2_segment_location = root_paraphrase_types.xpath(
                f".//pair_id[text()='{current_pair_id}']/parent::relation/s2_scope/text()"
            )
            sentence1_segment_text = root_paraphrase_types.xpath(
                f".//pair_id[text()='{current_pair_id}']/parent::relation/s1_text/text()"
            )
            sentence2_segment_text = root_paraphrase_types.xpath(
                f".//pair_id[text()='{current_pair_id}']/parent::relation/s2_text/text()"
            )

            sentence1_tokenized = row.find(".//sent1_tokenized").text.split(
                " "
            )
            sentence2_tokenized = row.find(".//sent2_tokenized").text.split(
                " "
            )

            sentence1_segment_location_full = np.zeros(
                len(sentence1_tokenized)
            )
            sentence2_segment_location_full = np.zeros(
                len(sentence2_tokenized)
            )

            sentence1_segment_indices = []
            sentence2_segment_indices = []

            for (
                sentence1_segment_locations,
                sentence2_segment_locations,
                paraphrase_type_id,
            ) in zip(
                sentence1_segment_location,
                sentence2_segment_location,
                paraphrase_type_ids,
            ):
                segment_locations_1 = [
                    int(i) for i in sentence1_segment_locations.split(",")
                ]
                sentence1_segment_indices.append(segment_locations_1)
                sentence1_segment_location_full[segment_locations_1] = [
                    paraphrase_type_id
                ] * len(segment_locations_1)

                segment_locations_2 = [
                    int(i) for i in sentence2_segment_locations.split(",")
                ]
                sentence2_segment_indices.append(segment_locations_2)
                sentence2_segment_location_full[segment_locations_2] = [
                    paraphrase_type_id
                ] * len(segment_locations_2)

            yield idx, {
                "idx": row.find(".//pair_id").text + "_" + str(idx),
                "sentence1": row.find(".//sent1_raw").text,
                "sentence2": row.find(".//sent2_raw").text,
                "sentence1_tokenized": sentence1_tokenized,
                "sentence2_tokenized": sentence2_tokenized,
                "etpc_label": int(row.find(".//etpc_label").text),
                "mrpc_label": int(row.find(".//mrpc_label").text),
                "negation": int(row.find(".//negation").text),
                "paraphrase_types": paraphrase_types,
                "paraphrase_type_ids": paraphrase_type_ids,
                "sentence1_segment_location": sentence1_segment_location_full,
                "sentence2_segment_location": sentence2_segment_location_full,
                "sentence1_segment_location_indices": sentence1_segment_indices,
                "sentence2_segment_location_indices": sentence2_segment_indices,
                "sentence1_segment_text": sentence1_segment_text,
                "sentence2_segment_text": sentence2_segment_text,
            }
            idx += 1