| | |
| |
|
| | """The Multilingual Amazon Reviews Corpus""" |
| |
|
| |
|
| | import json |
| |
|
| | import datasets |
| | from datasets.exceptions import DefunctDatasetError |
| |
|
| |
|
| | _DESCRIPTION = """\ |
| | Please refer to https://huggingface.co/datasets/defunct-datasets/amazon_reviews_multi. |
| | """ |
| |
|
| | _LANGUAGES = { |
| | "de": "German", |
| | "en": "English", |
| | "es": "Spanish", |
| | "fr": "French", |
| | "ja": "Japanese", |
| | "zh": "Chinese", |
| | } |
| | _ALL_LANGUAGES = "all_languages" |
| | _VERSION = "1.0.0" |
| | _HOMEPAGE_URL = "https://huggingface.co/datasets/defunct-datasets/amazon_reviews_multi" |
| | _DOWNLOAD_URL = "https://huggingface.co/datasets/buruzaemon/amazon_reviews_multi/resolve/main/{lang}/{split}.jsonl.gz" |
| |
|
| |
|
| | class AmazonReviewsMultiConfig(datasets.BuilderConfig): |
| | """BuilderConfig for AmazonReviewsMultiConfig.""" |
| |
|
| | def __init__(self, languages=None, **kwargs): |
| | super(AmazonReviewsMultiConfig, self).__init__(version=datasets.Version(_VERSION, ""), **kwargs), |
| | self.languages = languages |
| |
|
| |
|
| | class AmazonReviewsMulti(datasets.GeneratorBasedBuilder): |
| | """The Multilingual Amazon Reviews Corpus""" |
| |
|
| | BUILDER_CONFIGS = [ |
| | AmazonReviewsMultiConfig( |
| | name=_ALL_LANGUAGES, |
| | languages=_LANGUAGES, |
| | description="A collection of Amazon reviews specifically designed to aid research in multilingual text classification.", |
| | ) |
| | ] + [ |
| | AmazonReviewsMultiConfig( |
| | name=lang, |
| | languages=[lang], |
| | description=f"{_LANGUAGES[lang]} examples from a collection of Amazon reviews specifically designed to aid research in multilingual text classification", |
| | ) |
| | for lang in _LANGUAGES |
| | ] |
| | BUILDER_CONFIG_CLASS = AmazonReviewsMultiConfig |
| | DEFAULT_CONFIG_NAME = _ALL_LANGUAGES |
| |
|
| | def _info(self): |
| | |
| | |
| | |
| | return datasets.DatasetInfo( |
| | description=_DESCRIPTION, |
| | features=datasets.Features( |
| | { |
| | "review_id": datasets.Value("string"), |
| | "product_id": datasets.Value("string"), |
| | "reviewer_id": datasets.Value("string"), |
| | "stars": datasets.Value("int32"), |
| | "review_body": datasets.Value("string"), |
| | "review_title": datasets.Value("string"), |
| | "language": datasets.Value("string"), |
| | "product_category": datasets.Value("string"), |
| | } |
| | ), |
| | supervised_keys=None, |
| | license=None, |
| | homepage=_HOMEPAGE_URL, |
| | citation=None, |
| | ) |
| |
|
| | def _split_generators(self, dl_manager): |
| | train_urls = [_DOWNLOAD_URL.format(split="train", lang=lang) for lang in self.config.languages] |
| | dev_urls = [_DOWNLOAD_URL.format(split="validation", lang=lang) for lang in self.config.languages] |
| | test_urls = [_DOWNLOAD_URL.format(split="test", lang=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: |
| | for line in f: |
| | yield row_count, json.loads(line) |
| | row_count += 1 |
| |
|