Datasets:
File size: 4,956 Bytes
f3e7b4b bd1f7ab f3e7b4b bd1f7ab f3e7b4b |
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 |
# 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.
"""Amazon Multilingual Counterfactual Dataset"""
import csv
import datasets
_CITATION = """\
@misc{oneill2021i,
title={I Wish I Would Have Loved This One, But I Didn't -- A Multilingual Dataset for Counterfactual Detection in Product Reviews},
author={James O'Neill and Polina Rozenshtein and Ryuichi Kiryo and Motoko Kubota and Danushka Bollegala},
year={2021},
eprint={2104.06893},
archivePrefix={arXiv},
primaryClass={cs.CL}
}
"""
_DESCRIPTION = """\
The dataset contains sentences from Amazon customer reviews (sampled from Amazon product review dataset) annotated for counterfactual detection (CFD) binary classification. Counterfactual statements describe events that did not or cannot take place. Counterfactual statements may be identified as statements of the form – If p was true, then q would be true (i.e. assertions whose antecedent (p) and consequent (q) are known or assumed to be false).
"""
_HOMEPAGE_URL = "https://github.com/amazon-research/amazon-multilingual-counterfactual-dataset"
_LICENSE = "CC BY-SA 4.0"
_LANGUAGES = {"de": "German", "en": "English", "jp": "Japanese"}
_ALL_LANGUAGES = "all_languages"
_DATA_DIR = "data/{lang}_{split}.tsv"
_VERSION = "1.0.0"
id2label = {"0": "not-counterfactual", "1": "counterfactual"}
class AmazonCounterfactualConfig(datasets.BuilderConfig):
"""BuilderConfig for AmazonCounterfactualConfig."""
def __init__(self, languages=None, **kwargs):
super(AmazonCounterfactualConfig, self).__init__(version=datasets.Version(_VERSION, ""), **kwargs),
self.languages = languages
class AmazonCounterfactual(datasets.GeneratorBasedBuilder):
"""The Amazon Multilingual Counterfactual Dataset"""
BUILDER_CONFIGS = [
AmazonCounterfactualConfig(
name=_ALL_LANGUAGES,
languages=_LANGUAGES,
description="Sentences from Amazon customer reviews annotated for counterfactual detection binary classification.",
)
] + [
AmazonCounterfactualConfig(
name=lang,
languages=[lang],
description=f"{_LANGUAGES[lang]} sentences from Amazon customer reviews annotated for counterfactual detection binary classification.",
)
for lang in _LANGUAGES
]
BUILDER_CONFIG_CLASS = AmazonCounterfactualConfig
DEFAULT_CONFIG_NAME = _ALL_LANGUAGES
def _info(self):
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=datasets.Features(
{
"text": datasets.Value("string"),
"label": datasets.Value("string"),
"label_text": datasets.Value("string"),
}
),
supervised_keys=None,
license=_LICENSE,
homepage=_HOMEPAGE_URL,
citation=_CITATION,
)
def _split_generators(self, dl_manager):
train_urls = [_DATA_DIR.format(split="train", lang=lang.upper()) for lang in self.config.languages]
dev_urls = [_DATA_DIR.format(split="valid", lang=lang.upper()) for lang in self.config.languages]
test_urls = [_DATA_DIR.format(split="test", lang=lang.upper()) for lang in self.config.languages]
train_paths = dl_manager.download_and_extract(train_urls)
dev_paths = dl_manager.download_and_extract(dev_urls)
test_paths = dl_manager.download_and_extract(test_urls)
return [
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"file_paths": train_paths}),
datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"file_paths": dev_paths}),
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"file_paths": test_paths}),
]
def _generate_examples(self, file_paths):
row_count = 0
for file_path in file_paths:
with open(file_path, "r", encoding="utf-8") as f:
csv_reader = csv.reader(
f,
delimiter="\t",
)
# skip header
next(csv_reader)
for row in csv_reader:
yield row_count, {"text": row[0], "label": row[1], "label_text": id2label[row[1]]}
row_count += 1
|