Datasets:
Initial commit
Browse files- README.md +61 -0
- wrime-sentiment.py +96 -0
README.md
ADDED
@@ -0,0 +1,61 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Dataset Card for llm-book/wrime-sentiment
|
2 |
+
日本語の感情分析データセット WRIME を、ポジティブ/ネガティブの二値分類のタスクに加工したデータセットです。
|
3 |
+
GitHub リポジトリ [ids-cv/wrime](https://github.com/ids-cv/wrime) で公開されているデータセットを利用しています。
|
4 |
+
|
5 |
+
書籍『大規模言語モデル入門』のサンプルコードで利用することを想定しています。
|
6 |
+
詳しくは[書籍のGitHubリポジトリ](https://github.com/ghmagazine/llm-book)をご覧ください。
|
7 |
+
|
8 |
+
## 使い方
|
9 |
+
以下のようにデータセットを読み込むことができます。
|
10 |
+
```python
|
11 |
+
from datasets import load_dataset
|
12 |
+
|
13 |
+
dataset = load_dataset("hf_datasets/wrime-sentiment")
|
14 |
+
print(dataset["train"].features["label"])
|
15 |
+
print(dataset)
|
16 |
+
```
|
17 |
+
|
18 |
+
```python
|
19 |
+
ClassLabel(names=['positive', 'negative'], id=None)
|
20 |
+
DatasetDict({
|
21 |
+
train: Dataset({
|
22 |
+
features: ['sentence', 'label'],
|
23 |
+
num_rows: 20149
|
24 |
+
})
|
25 |
+
validation: Dataset({
|
26 |
+
features: ['sentence', 'label'],
|
27 |
+
num_rows: 1608
|
28 |
+
})
|
29 |
+
test: Dataset({
|
30 |
+
features: ['sentence', 'label'],
|
31 |
+
num_rows: 1781
|
32 |
+
})
|
33 |
+
})
|
34 |
+
```
|
35 |
+
デフォルトの設定では、元のデータセットから極性がニュートラルであるものを除いています。
|
36 |
+
`remove_netural=False`と指定することで、ニュートラルなデータも含めた三値分類のデータセットを読み込むことができます。
|
37 |
+
```python
|
38 |
+
from datasets import load_dataset
|
39 |
+
|
40 |
+
dataset = load_dataset("hf_datasets/wrime-sentiment", remove_neutral=False)
|
41 |
+
print(dataset["train"].features["label"])
|
42 |
+
print(dataset)
|
43 |
+
```
|
44 |
+
|
45 |
+
```python
|
46 |
+
ClassLabel(names=['positive', 'negative', 'neutral'], id=None)
|
47 |
+
DatasetDict({
|
48 |
+
train: Dataset({
|
49 |
+
features: ['sentence', 'label'],
|
50 |
+
num_rows: 30000
|
51 |
+
})
|
52 |
+
validation: Dataset({
|
53 |
+
features: ['sentence', 'label'],
|
54 |
+
num_rows: 2500
|
55 |
+
})
|
56 |
+
test: Dataset({
|
57 |
+
features: ['sentence', 'label'],
|
58 |
+
num_rows: 2500
|
59 |
+
})
|
60 |
+
})
|
61 |
+
```
|
wrime-sentiment.py
ADDED
@@ -0,0 +1,96 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import csv
|
2 |
+
from dataclasses import dataclass
|
3 |
+
from typing import Any, Dict, List, Tuple
|
4 |
+
|
5 |
+
import datasets
|
6 |
+
|
7 |
+
logger = datasets.logging.get_logger(__name__)
|
8 |
+
|
9 |
+
_DESCRIPTION = (
|
10 |
+
"日本語の感情分析データセット WRIME を、ポジティブ/ネガティブの二値分類のタスクに加工したデータセットです。"
|
11 |
+
"GitHub リポジトリ ids-cv/wrime で公開されているデータセットを利用しています。"
|
12 |
+
)
|
13 |
+
|
14 |
+
_URL = "https://raw.githubusercontent.com/ids-cv/wrime/master/wrime-ver2.tsv"
|
15 |
+
|
16 |
+
|
17 |
+
@dataclass
|
18 |
+
class WrimeSentimentConfig(datasets.BuilderConfig):
|
19 |
+
remove_neutral: bool = True
|
20 |
+
|
21 |
+
|
22 |
+
class WrimeSentiment(datasets.GeneratorBasedBuilder):
|
23 |
+
BUILDER_CONFIG_CLASS = WrimeSentimentConfig
|
24 |
+
|
25 |
+
def _info(self) -> datasets.DatasetInfo:
|
26 |
+
labels = ["positive", "negative"]
|
27 |
+
if not self.config.remove_neutral:
|
28 |
+
labels += ["neutral"]
|
29 |
+
return datasets.DatasetInfo(
|
30 |
+
description=_DESCRIPTION,
|
31 |
+
features=datasets.Features(
|
32 |
+
{
|
33 |
+
"sentence": datasets.Value("string"),
|
34 |
+
"label": datasets.ClassLabel(
|
35 |
+
num_classes=len(labels), names=labels
|
36 |
+
),
|
37 |
+
}
|
38 |
+
),
|
39 |
+
)
|
40 |
+
|
41 |
+
def _split_generators(
|
42 |
+
self, dl_manager: datasets.DownloadManager
|
43 |
+
) -> List[datasets.SplitGenerator]:
|
44 |
+
downloaded_file = dl_manager.download_and_extract(_URL)
|
45 |
+
return [
|
46 |
+
datasets.SplitGenerator(
|
47 |
+
name=datasets.Split.TRAIN,
|
48 |
+
gen_kwargs={
|
49 |
+
"filepath": downloaded_file,
|
50 |
+
"split": "train",
|
51 |
+
},
|
52 |
+
),
|
53 |
+
datasets.SplitGenerator(
|
54 |
+
name=datasets.Split.VALIDATION,
|
55 |
+
gen_kwargs={
|
56 |
+
"filepath": downloaded_file,
|
57 |
+
"split": "dev",
|
58 |
+
},
|
59 |
+
),
|
60 |
+
datasets.SplitGenerator(
|
61 |
+
name=datasets.Split.TEST,
|
62 |
+
gen_kwargs={
|
63 |
+
"filepath": downloaded_file,
|
64 |
+
"split": "test",
|
65 |
+
},
|
66 |
+
),
|
67 |
+
]
|
68 |
+
|
69 |
+
def _generate_examples(
|
70 |
+
self, filepath: str, split: str
|
71 |
+
) -> Tuple[str, Dict[str, Any]]:
|
72 |
+
logger.info(f"generating examples from {filepath}")
|
73 |
+
_key = 0
|
74 |
+
|
75 |
+
with open(filepath, "r") as f:
|
76 |
+
reader = csv.DictReader(f, delimiter="\t")
|
77 |
+
for data in reader:
|
78 |
+
if data["Train/Dev/Test"].lower() != split:
|
79 |
+
continue
|
80 |
+
|
81 |
+
sentiment_score = int(data["Avg. Readers_Sentiment"])
|
82 |
+
if sentiment_score > 0:
|
83 |
+
label = "positive"
|
84 |
+
elif sentiment_score < 0:
|
85 |
+
label = "negative"
|
86 |
+
else:
|
87 |
+
label = "neutral"
|
88 |
+
|
89 |
+
if self.config.remove_neutral and label == "neutral":
|
90 |
+
continue
|
91 |
+
|
92 |
+
yield _key, {
|
93 |
+
"sentence": data["Sentence"],
|
94 |
+
"label": label,
|
95 |
+
}
|
96 |
+
_key += 1
|