Create amazon_reviews_multi.py
#1
by
nouamanetazi
HF staff
- opened
- amazon_reviews_multi.py +106 -0
amazon_reviews_multi.py
ADDED
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
import datasets
|
3 |
+
|
4 |
+
|
5 |
+
_CITATION = """\
|
6 |
+
@inproceedings{marc_reviews,
|
7 |
+
title={The Multilingual Amazon Reviews Corpus},
|
8 |
+
author={Keung, Phillip and Lu, Yichao and Szarvas, György and Smith, Noah A.},
|
9 |
+
booktitle={Proceedings of the 2020 Conference on Empirical Methods in Natural Language Processing},
|
10 |
+
year={2020}
|
11 |
+
}
|
12 |
+
"""
|
13 |
+
|
14 |
+
_LICENSE = """\
|
15 |
+
By accessing the Multilingual Amazon Reviews Corpus ("Reviews Corpus"), you agree that the Reviews Corpus is an Amazon Service subject to the Amazon.com Conditions of Use (https://www.amazon.com/gp/help/customer/display.html/ref=footer_cou?ie=UTF8&nodeId=508088) and you agree to be bound by them, with the following additional conditions:
|
16 |
+
In addition to the license rights granted under the Conditions of Use, Amazon or its content providers grant you a limited, non-exclusive, non-transferable, non-sublicensable, revocable license to access and use the Reviews Corpus for purposes of academic research. You may not resell, republish, or make any commercial use of the Reviews Corpus or its contents, including use of the Reviews Corpus for commercial research, such as research related to a funding or consultancy contract, internship, or other relationship in which the results are provided for a fee or delivered to a for-profit organization. You may not (a) link or associate content in the Reviews Corpus with any personal information (including Amazon customer accounts), or (b) attempt to determine the identity of the author of any content in the Reviews Corpus. If you violate any of the foregoing conditions, your license to access and use the Reviews Corpus will automatically terminate without prejudice to any of the other rights or remedies Amazon may have.
|
17 |
+
"""
|
18 |
+
|
19 |
+
_DESCRIPTION = """\
|
20 |
+
We provide an Amazon product reviews dataset for multilingual text classification. The dataset contains reviews in English, Japanese, German, French, Chinese and Spanish, collected between November 1, 2015 and November 1, 2019. Each record in the dataset contains the review text, the review title, the star rating, an anonymized reviewer ID, an anonymized product ID and the coarse-grained product category (e.g. ‘books’, ‘appliances’, etc.) The corpus is balanced across stars, so each star rating constitutes 20% of the reviews in each language.
|
21 |
+
For each language, there are 200,000, 5,000 and 5,000 reviews in the training, development and test sets respectively. The maximum number of reviews per reviewer is 20 and the maximum number of reviews per product is 20. All reviews are truncated after 2,000 characters, and all reviews are at least 20 characters long.
|
22 |
+
Note that the language of a review does not necessarily match the language of its marketplace (e.g. reviews from amazon.de are primarily written in German, but could also be written in English, etc.). For this reason, we applied a language detection algorithm based on the work in Bojanowski et al. (2017) to determine the language of the review text and we removed reviews that were not written in the expected language.
|
23 |
+
"""
|
24 |
+
|
25 |
+
_LANGUAGES = {
|
26 |
+
"de": "German",
|
27 |
+
"en": "English",
|
28 |
+
"es": "Spanish",
|
29 |
+
"fr": "French",
|
30 |
+
"ja": "Japanese",
|
31 |
+
"zh": "Chinese",
|
32 |
+
}
|
33 |
+
_ALL_LANGUAGES = "all_languages"
|
34 |
+
_HOMEPAGE_URL = "https://registry.opendata.aws/amazon-reviews-ml/"
|
35 |
+
_DOWNLOAD_URL = "{lang}/{split}.jsonl"
|
36 |
+
_VERSION = "1.0.0"
|
37 |
+
|
38 |
+
|
39 |
+
class AmazonReviewsMultiConfig(datasets.BuilderConfig):
|
40 |
+
"""BuilderConfig for AmazonReviewsMultiConfig."""
|
41 |
+
|
42 |
+
def __init__(self, languages=None, **kwargs):
|
43 |
+
super(AmazonReviewsMultiConfig, self).__init__(c**kwargs),
|
44 |
+
self.languages = languages
|
45 |
+
|
46 |
+
|
47 |
+
class AmazonReviewsMulti(datasets.GeneratorBasedBuilder):
|
48 |
+
|
49 |
+
"""The Multilingual Amazon Reviews Corpus"""
|
50 |
+
|
51 |
+
BUILDER_CONFIGS = [
|
52 |
+
AmazonReviewsMultiConfig(
|
53 |
+
name=_ALL_LANGUAGES,
|
54 |
+
languages=_LANGUAGES,
|
55 |
+
description="A collection of Amazon reviews specifically designed to aid research in multilingual text classification.",
|
56 |
+
)
|
57 |
+
] + [
|
58 |
+
AmazonReviewsMultiConfig(
|
59 |
+
name=lang,
|
60 |
+
languages=[lang],
|
61 |
+
description=f"{_LANGUAGES[lang]} examples from a collection of Amazon reviews specifically designed to aid research in multilingual text classification",
|
62 |
+
)
|
63 |
+
for lang in _LANGUAGES
|
64 |
+
]
|
65 |
+
BUILDER_CONFIG_CLASS = AmazonReviewsMultiConfig
|
66 |
+
DEFAULT_CONFIG_NAME = _ALL_LANGUAGES
|
67 |
+
|
68 |
+
def _info(self):
|
69 |
+
return datasets.DatasetInfo(
|
70 |
+
description=_DESCRIPTION,
|
71 |
+
features=datasets.Features(
|
72 |
+
{
|
73 |
+
"id": datasets.Value("int64"),
|
74 |
+
"text": datasets.Value("string"),
|
75 |
+
"label": datasets.Value("int32"),
|
76 |
+
"label_text": datasets.Value("string"),
|
77 |
+
}
|
78 |
+
),
|
79 |
+
supervised_keys=None,
|
80 |
+
license=_LICENSE,
|
81 |
+
homepage=_HOMEPAGE_URL,
|
82 |
+
citation=_CITATION,
|
83 |
+
)
|
84 |
+
|
85 |
+
def _split_generators(self, dl_manager):
|
86 |
+
train_urls = [_DOWNLOAD_URL.format(split="train", lang=lang) for lang in self.config.languages]
|
87 |
+
dev_urls = [_DOWNLOAD_URL.format(split="validation", lang=lang) for lang in self.config.languages]
|
88 |
+
test_urls = [_DOWNLOAD_URL.format(split="test", lang=lang) for lang in self.config.languages]
|
89 |
+
|
90 |
+
train_paths = dl_manager.download_and_extract(train_urls)
|
91 |
+
dev_paths = dl_manager.download_and_extract(dev_urls)
|
92 |
+
test_paths = dl_manager.download_and_extract(test_urls)
|
93 |
+
|
94 |
+
return [
|
95 |
+
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"file_paths": train_paths}),
|
96 |
+
datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"file_paths": dev_paths}),
|
97 |
+
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"file_paths": test_paths}),
|
98 |
+
]
|
99 |
+
|
100 |
+
def _generate_examples(self, file_paths):
|
101 |
+
row_count = 0
|
102 |
+
for file_path in file_paths:
|
103 |
+
with open(file_path, "r", encoding="utf-8") as f:
|
104 |
+
for line in f:
|
105 |
+
yield row_count, json.loads(line)
|
106 |
+
row_count += 1
|