|
import csv |
|
from dataclasses import dataclass |
|
from typing import Any, Dict, List, Tuple |
|
|
|
import datasets |
|
|
|
logger = datasets.logging.get_logger(__name__) |
|
|
|
_DESCRIPTION = ( |
|
"日本語の感情分析データセット WRIME を、ポジティブ/ネガティブの二値分類のタスクに加工したデータセットです。" |
|
"GitHub リポジトリ ids-cv/wrime で公開されているデータセットを利用しています。" |
|
) |
|
|
|
_URL = "https://raw.githubusercontent.com/ids-cv/wrime/master/wrime-ver2.tsv" |
|
|
|
|
|
@dataclass |
|
class WrimeSentimentConfig(datasets.BuilderConfig): |
|
remove_neutral: bool = True |
|
|
|
|
|
class WrimeSentiment(datasets.GeneratorBasedBuilder): |
|
BUILDER_CONFIG_CLASS = WrimeSentimentConfig |
|
|
|
def _info(self) -> datasets.DatasetInfo: |
|
labels = ["positive", "negative"] |
|
if not self.config.remove_neutral: |
|
labels += ["neutral"] |
|
return datasets.DatasetInfo( |
|
description=_DESCRIPTION, |
|
features=datasets.Features( |
|
{ |
|
"sentence": datasets.Value("string"), |
|
"label": datasets.ClassLabel( |
|
num_classes=len(labels), names=labels |
|
), |
|
"user_id": datasets.Value("int64"), |
|
"datetime": datasets.Value("string") |
|
} |
|
), |
|
) |
|
|
|
def _split_generators( |
|
self, dl_manager: datasets.DownloadManager |
|
) -> List[datasets.SplitGenerator]: |
|
downloaded_file = dl_manager.download_and_extract(_URL) |
|
return [ |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TRAIN, |
|
gen_kwargs={ |
|
"filepath": downloaded_file, |
|
"split": "train", |
|
}, |
|
), |
|
datasets.SplitGenerator( |
|
name=datasets.Split.VALIDATION, |
|
gen_kwargs={ |
|
"filepath": downloaded_file, |
|
"split": "dev", |
|
}, |
|
), |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TEST, |
|
gen_kwargs={ |
|
"filepath": downloaded_file, |
|
"split": "test", |
|
}, |
|
), |
|
] |
|
|
|
def _generate_examples( |
|
self, filepath: str, split: str |
|
) -> Tuple[str, Dict[str, Any]]: |
|
logger.info(f"generating examples from {filepath}") |
|
_key = 0 |
|
|
|
with open(filepath, "r", encoding="utf-8") as f: |
|
reader = csv.DictReader(f, delimiter="\t") |
|
for data in reader: |
|
if data["Train/Dev/Test"].lower() != split: |
|
continue |
|
|
|
sentiment_score = int(data["Avg. Readers_Sentiment"]) |
|
if sentiment_score > 0: |
|
label = "positive" |
|
elif sentiment_score < 0: |
|
label = "negative" |
|
else: |
|
label = "neutral" |
|
|
|
if self.config.remove_neutral and label == "neutral": |
|
continue |
|
yield _key, { |
|
"sentence": data["Sentence"], |
|
"label": label, |
|
"user_id": data["UserID"], |
|
"datetime": data["Datetime"].strip(), |
|
} |
|
_key += 1 |
|
|