Datasets:
File size: 3,347 Bytes
e715ed5 c70aece e715ed5 fb9b134 e715ed5 c70aece e715ed5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
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
|