| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
|
|
| import ast |
| import csv |
| import textwrap |
|
|
| import datasets |
|
|
| LANGUAGES = ['malay', 'hindi', 'japanese', 'german', |
| 'italian', 'english', 'portuguese', 'french', |
| 'spanish', 'chinese', 'indonesian', 'arabic' |
| ] |
|
|
|
|
| class MultilingualSentimentsConfig(datasets.BuilderConfig): |
| """BuilderConfig for Multilingual Sentiments""" |
|
|
| def __init__( |
| self, |
| text_features, |
| label_column, |
| label_classes, |
| train_url, |
| valid_url, |
| test_url, |
| citation, |
| **kwargs, |
| ): |
| """BuilderConfig for Multilingual Sentiments. |
| |
| Args: |
| text_features: `dict[string, string]`, map from the name of the feature |
| dict for each text field to the name of the column in the txt/csv/tsv file |
| label_column: `string`, name of the column in the txt/csv/tsv file corresponding |
| to the label |
| label_classes: `list[string]`, the list of classes if the label is categorical |
| train_url: `string`, url to train file from |
| valid_url: `string`, url to valid file from |
| test_url: `string`, url to test file from |
| citation: `string`, citation for the data set |
| **kwargs: keyword arguments forwarded to super. |
| """ |
| super(MultilingualSentimentsConfig, self).__init__( |
| version=datasets.Version("1.0.0", ""), **kwargs) |
| self.text_features = text_features |
| self.label_column = label_column |
| self.label_classes = label_classes |
| self.train_url = train_url |
| self.valid_url = valid_url |
| self.test_url = test_url |
| self.citation = citation |
|
|
|
|
| class MultilingualSentiments(datasets.GeneratorBasedBuilder): |
| """Multilingual Sentiments benchmark""" |
|
|
| BUILDER_CONFIGS = [] |
|
|
| BUILDER_CONFIGS.append( |
| MultilingualSentimentsConfig( |
| name="all", |
| description=textwrap.dedent( |
| f"""\ |
| All datasets.""" |
| ), |
| text_features={"text": "text", "language": "language"}, |
| label_classes=["positive", "neutral", "negative"], |
| label_column="label", |
| train_url=f"https://raw.githubusercontent.com/tyqiangz/multilingual-sentiment-datasets/main/data/all/train.tsv", |
| valid_url=f"https://raw.githubusercontent.com/tyqiangz/multilingual-sentiment-datasets/main/data/all/valid.tsv", |
| test_url=f"https://raw.githubusercontent.com/tyqiangz/multilingual-sentiment-datasets/main/data/all/test.tsv", |
| citation=textwrap.dedent( |
| f"""\ |
| All citation""" |
| ), |
| ), |
| ) |
|
|
| for lang in LANGUAGES: |
| BUILDER_CONFIGS.append( |
| MultilingualSentimentsConfig( |
| name=lang, |
| description=textwrap.dedent( |
| f"""\ |
| {lang} dataset.""" |
| ), |
| text_features={"text": "text"}, |
| label_classes=["positive", "neutral", "negative"], |
| label_column="label", |
| train_url=f"https://raw.githubusercontent.com/tyqiangz/multilingual-sentiment-datasets/main/data/{lang}/train.tsv", |
| valid_url=f"https://raw.githubusercontent.com/tyqiangz/multilingual-sentiment-datasets/main/data/{lang}/valid.tsv", |
| test_url=f"https://raw.githubusercontent.com/tyqiangz/multilingual-sentiment-datasets/main/data/{lang}/test.tsv", |
| citation=textwrap.dedent( |
| f"""\ |
| {lang} citation""" |
| ), |
| ), |
| ) |
|
|
| def _info(self): |
| features = {text_feature: datasets.Value( |
| "string") for text_feature in self.config.text_features} |
|
|
| features["label"] = datasets.features.ClassLabel( |
| names=self.config.label_classes) |
|
|
| return datasets.DatasetInfo( |
| description=self.config.description, |
| features=datasets.Features(features), |
| citation=self.config.citation, |
| ) |
|
|
| def _split_generators(self, dl_manager): |
| """Returns SplitGenerators.""" |
| train_path = dl_manager.download_and_extract(self.config.train_url) |
| valid_path = dl_manager.download_and_extract(self.config.valid_url) |
| test_path = dl_manager.download_and_extract(self.config.test_url) |
| return [ |
| datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={ |
| "filepath": train_path}), |
| datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={ |
| "filepath": valid_path}), |
| datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={ |
| "filepath": test_path}), |
| ] |
|
|
| def _generate_examples(self, filepath): |
|
|
| with open(filepath, encoding="utf-8") as f: |
| reader = csv.reader(f, delimiter="\t", quoting=csv.QUOTE_NONE) |
|
|
| next(reader, None) |
|
|
| for id_, row in enumerate(reader): |
| if self.config.name != "all": |
| text = '\t'.join(row[:-1]) |
| label = row[-1] |
|
|
| yield id_, {"text": text, "label": label} |
|
|
| else: |
| text = '\t'.join(row[:-3]) |
| label = row[-2] |
| language = row[-1] |
|
|
| yield id_, {"text": text, "label": label, "language": language} |
|
|