File size: 4,646 Bytes
2361927
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# 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 Dict, Generator, Any, Union, Optional, List, Tuple

import datasets
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 Sst2(datasets.GeneratorBasedBuilder):
    """SST-2 dataset."""

    VERSION = datasets.Version("2.0.0")

    def _info(self):
        features = datasets.Features(
            {
                "idx": datasets.Value("int32"),
                "sentence1": datasets.Value("string"),
                "sentence2": datasets.Value("string"),
                "etpc_label": datasets.Value("int8"),
                "mrpc_label": datasets.Value("int8"),
                "negation": datasets.Value("int8"),
                "paraphrase_types": 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()

        for idx, row in enumerate(root_text_pairs):
            children = row.getchildren()
            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()"
            )
            str_paraphrase_types = ",".join(paraphrase_types)
            yield idx, {
                "idx": int(row.find(".//pair_id").text)
                if row.find(".//pair_id") is not None
                else int(idx),
                "sentence1": row.find(".//sent1_raw").text,
                "sentence2": row.find(".//sent2_raw").text,
                "etpc_label": int(row.find(".//etpc_label").text),
                "mrpc_label": int(row.find(".//mrpc_label").text),
                "negation": int(row.find(".//negation").text),
                "paraphrase_types": str_paraphrase_types,
            }