amazon_counterfactual / amazon_counterfactual.py
lewtun's picture
lewtun HF staff
Add external subsets
67d5b13
raw
history blame
No virus
4.65 kB
# 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": "DE", "en": "EN", "en-ext": "EN-ext", "ja": "JP"}
_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=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
def _info(self):
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=datasets.Features(
{
"text": datasets.Value("string"),
"label": datasets.Value("int32"),
"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=_LANGUAGES[lang]) for lang in self.config.languages]
dev_urls = [_DATA_DIR.format(split="valid", lang=_LANGUAGES[lang]) for lang in self.config.languages]
test_urls = [_DATA_DIR.format(split="test", lang=_LANGUAGES[lang]) 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