File size: 5,904 Bytes
0d6e52b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bd70953
0d6e52b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bd70953
0d6e52b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bd70953
0d6e52b
 
 
 
 
 
 
 
 
 
 
 
 
 
bd70953
0d6e52b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa2dc33
0d6e52b
 
 
 
 
 
 
 
 
 
 
 
 
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
# coding=utf-8
# Copyright 2021 The TensorFlow Datasets Authors and the 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
"""PolEmo2 dataset."""
from dataclasses import dataclass
from typing import List, Dict, Generator, Union, Optional, Tuple

import datasets

_DESCRIPTION = """PolEmo 2.0:  Corpus of Multi-Domain Consumer Reviews, evaluation data for article presented at CoNLL."""
_CITATION = """
@inproceedings{kocon-etal-2019-multi,
    title = "Multi-Level Sentiment Analysis of {P}ol{E}mo 2.0: Extended Corpus of Multi-Domain Consumer Reviews",
    author = "Koco{\'n}, Jan  and
      Mi{\l}kowski, Piotr  and
      Za{\'s}ko-Zieli{\'n}ska, Monika",
    booktitle = "Proceedings of the 23rd Conference on Computational Natural Language Learning (CoNLL)",
    month = nov,
    year = "2019",
    address = "Hong Kong, China",
    publisher = "Association for Computational Linguistics",
    url = "https://www.aclweb.org/anthology/K19-1092",
    doi = "10.18653/v1/K19-1092",
    pages = "980--991",}
"""
_HOMEPAGE = "https://clarin-pl.eu/dspace/handle/11321/710"
_LICENSE = "CC-BY-4.0"

_DOMAINS = [
    "all",
    "hotels",
    "medicine",
    "products",
    "reviews",
]
_OUT_DOMAINS = ["Nhotels", "Nmedicine", "Nproducts", "Nreviews"]
_CONFIGS_TEXT = ["text", "sentence"]

_LABELS = ["zero", "minus", "plus", "amb"]

URL_PATH = (
    "https://huggingface.co/datasets/clarin-pl/polemo2-official/resolve/main/data"
)
_URLS = {
    cfg: {
        **{
            domain: {
                split_type: f"{URL_PATH}/{domain}.{cfg}.{split_type}.txt"
                for split_type in ["train", "dev", "test"]
            }
            for domain in _DOMAINS
        },
        **{
            domain: {
                split_type: f"{URL_PATH}/{domain}.{cfg}.{split_type}.txt"
                for split_type in ["train", "dev"]
            }
            for domain in _OUT_DOMAINS
        },
    }
    for cfg in _CONFIGS_TEXT
}


@dataclass
class PolEmo2Config(datasets.BuilderConfig):
    text_cfg: Optional[str] = None
    domain: Optional[str] = None
    train_domains: Optional[List[str]] = None
    dev_domains: Optional[List[str]] = None
    test_domains: Optional[List[str]] = None


class PolEmo2(datasets.GeneratorBasedBuilder):
    BUILDER_CONFIG_CLASS = PolEmo2Config
    BUILDER_CONFIGS = [
        *[
            PolEmo2Config(
                name=f"{domain}_{text_type}",
                domain=domain,
                text_cfg=text_type,
                train_domains=[domain],
                dev_domains=[domain],
                test_domains=[domain],
            )
            for domain in _DOMAINS
            for text_type in _CONFIGS_TEXT
        ]
    ]
    DEFAULT_CONFIG_NAME = "all_text"

    def _info(self) -> datasets.DatasetInfo:
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "text": datasets.Value("string"),
                    "target": datasets.features.ClassLabel(
                        names=_LABELS, num_classes=len(_LABELS)
                    ),
                }
            ),
            homepage=_HOMEPAGE,
            citation=_CITATION,
            license=_LICENSE
        )

    def _get_files_by_domains(self, domains: List[str], split: str) -> List[str]:
        return [_URLS[self.config.text_cfg][domain][split] for domain in domains]

    def _split_generators(
        self, dl_manager: datasets.DownloadManager
    ) -> List[datasets.SplitGenerator]:
        files = {
            "train": dl_manager.download_and_extract(
                self._get_files_by_domains(
                    domains=self.config.train_domains, split="train"
                )
            ),
            "dev": dl_manager.download_and_extract(
                self._get_files_by_domains(domains=self.config.dev_domains, split="dev")
            ),
            "test": dl_manager.download_and_extract(
                self._get_files_by_domains(
                    domains=self.config.test_domains, split="test"
                )
            ),
        }
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={"filepath": files["train"]},
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={"filepath": files["dev"]},
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={"filepath": files["test"]},
            ),
        ]

    def _generate_examples(
        self, filepath: Union[str, List[str]]
    ) -> Generator[Tuple[int, Dict[str, str]], None, None]:

        gid = 0
        for path in filepath:
            with open(path, "r", encoding="utf-8") as f:
                for line in f:
                    splitted_line = line.split(" ")
                    yield gid, {
                        "text": " ".join(splitted_line[:-1]),
                        "target": (
                            splitted_line[-1]
                            .strip()
                            .replace("minus_m", "minus")
                            .replace("plus_m", "plus")
                            .split("_")[-1]
                        ),
                    }
                    gid += 1