Datasets:
fix for ver2 dataset (#3)
Browse files- tests/wrime_test.py +25 -0
- wrime.py +79 -102
tests/wrime_test.py
CHANGED
@@ -27,3 +27,28 @@ def test_load_dataset(
|
|
27 |
assert dataset["train"].num_rows == expected_train_num_rows # type: ignore
|
28 |
assert dataset["validation"].num_rows == expected_val_num_rows # type: ignore
|
29 |
assert dataset["test"].num_rows == expected_test_num_rows # type: ignore
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
assert dataset["train"].num_rows == expected_train_num_rows # type: ignore
|
28 |
assert dataset["validation"].num_rows == expected_val_num_rows # type: ignore
|
29 |
assert dataset["test"].num_rows == expected_test_num_rows # type: ignore
|
30 |
+
|
31 |
+
writer_readers = [
|
32 |
+
"writer",
|
33 |
+
"reader1",
|
34 |
+
"reader2",
|
35 |
+
"reader3",
|
36 |
+
"avg_readers",
|
37 |
+
]
|
38 |
+
expected_keys = ["sentence", "user_id", "datetime"] + writer_readers
|
39 |
+
|
40 |
+
for split in ["train", "validation", "test"]:
|
41 |
+
split_dataset = dataset[split] # type: ignore
|
42 |
+
|
43 |
+
for data in split_dataset:
|
44 |
+
assert len(data.keys()) == len(expected_keys)
|
45 |
+
for expected_key in expected_keys:
|
46 |
+
assert expected_key in data.keys()
|
47 |
+
|
48 |
+
for k in writer_readers:
|
49 |
+
if dataset_name == "ver1":
|
50 |
+
assert len(data[k]) == 8 # 8 感情強度
|
51 |
+
elif dataset_name == "ver2":
|
52 |
+
assert len(data[k]) == 8 + 1 # 8 感情強度 + 1 感情極性
|
53 |
+
else:
|
54 |
+
raise ValueError(f"Invalid dataset version: {dataset_name}")
|
wrime.py
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
import logging
|
2 |
-
from typing import TypedDict
|
3 |
|
4 |
import datasets as ds
|
5 |
import pandas as pd
|
@@ -60,15 +60,42 @@ def _fix_typo_in_dataset(df: pd.DataFrame) -> pd.DataFrame:
|
|
60 |
return df
|
61 |
|
62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
def _load_tsv(tsv_path: str) -> pd.DataFrame:
|
64 |
logger.info(f"Load TSV file from {tsv_path}")
|
65 |
df = pd.read_csv(tsv_path, delimiter="\t")
|
66 |
|
|
|
67 |
df = _fix_typo_in_dataset(df)
|
|
|
68 |
|
69 |
return df
|
70 |
|
71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
class WrimeDataset(ds.GeneratorBasedBuilder):
|
73 |
BUILDER_CONFIGS = [
|
74 |
ds.BuilderConfig(
|
@@ -83,64 +110,18 @@ class WrimeDataset(ds.GeneratorBasedBuilder):
|
|
83 |
),
|
84 |
]
|
85 |
|
86 |
-
def
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
"fear": ds.Value("uint8"),
|
99 |
-
"disgust": ds.Value("uint8"),
|
100 |
-
"trust": ds.Value("uint8"),
|
101 |
-
},
|
102 |
-
"reader1": {
|
103 |
-
"joy": ds.Value("uint8"),
|
104 |
-
"sadness": ds.Value("uint8"),
|
105 |
-
"anticipation": ds.Value("uint8"),
|
106 |
-
"surprise": ds.Value("uint8"),
|
107 |
-
"anger": ds.Value("uint8"),
|
108 |
-
"fear": ds.Value("uint8"),
|
109 |
-
"disgust": ds.Value("uint8"),
|
110 |
-
"trust": ds.Value("uint8"),
|
111 |
-
},
|
112 |
-
"reader2": {
|
113 |
-
"joy": ds.Value("uint8"),
|
114 |
-
"sadness": ds.Value("uint8"),
|
115 |
-
"anticipation": ds.Value("uint8"),
|
116 |
-
"surprise": ds.Value("uint8"),
|
117 |
-
"anger": ds.Value("uint8"),
|
118 |
-
"fear": ds.Value("uint8"),
|
119 |
-
"disgust": ds.Value("uint8"),
|
120 |
-
"trust": ds.Value("uint8"),
|
121 |
-
},
|
122 |
-
"reader3": {
|
123 |
-
"joy": ds.Value("uint8"),
|
124 |
-
"sadness": ds.Value("uint8"),
|
125 |
-
"anticipation": ds.Value("uint8"),
|
126 |
-
"surprise": ds.Value("uint8"),
|
127 |
-
"anger": ds.Value("uint8"),
|
128 |
-
"fear": ds.Value("uint8"),
|
129 |
-
"disgust": ds.Value("uint8"),
|
130 |
-
"trust": ds.Value("uint8"),
|
131 |
-
},
|
132 |
-
"avg_readers": {
|
133 |
-
"joy": ds.Value("uint8"),
|
134 |
-
"sadness": ds.Value("uint8"),
|
135 |
-
"anticipation": ds.Value("uint8"),
|
136 |
-
"surprise": ds.Value("uint8"),
|
137 |
-
"anger": ds.Value("uint8"),
|
138 |
-
"fear": ds.Value("uint8"),
|
139 |
-
"disgust": ds.Value("uint8"),
|
140 |
-
"trust": ds.Value("uint8"),
|
141 |
-
},
|
142 |
-
}
|
143 |
-
)
|
144 |
return ds.DatasetInfo(
|
145 |
description=_DESCRIPTION,
|
146 |
features=features,
|
@@ -149,14 +130,27 @@ class WrimeDataset(ds.GeneratorBasedBuilder):
|
|
149 |
citation=_CITATION,
|
150 |
)
|
151 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
152 |
def _split_generators(self, dl_manager: ds.DownloadManager):
|
153 |
wrime_datasets = dl_manager.download_and_extract(_URLS)
|
154 |
major_version_name = f"ver{self.config.version.major}" # type: ignore
|
155 |
|
156 |
wrime_df = _load_tsv(tsv_path=wrime_datasets[major_version_name])
|
157 |
-
tng_wrime_df = wrime_df[wrime_df["
|
158 |
-
dev_wrime_df = wrime_df[wrime_df["
|
159 |
-
tst_wrime_df = wrime_df[wrime_df["
|
160 |
|
161 |
return [
|
162 |
ds.SplitGenerator(
|
@@ -173,51 +167,34 @@ class WrimeDataset(ds.GeneratorBasedBuilder):
|
|
173 |
),
|
174 |
]
|
175 |
|
176 |
-
def
|
177 |
-
self,
|
178 |
-
df: pd.DataFrame,
|
179 |
-
):
|
180 |
for i in range(len(df)):
|
181 |
row_df = df.iloc[i]
|
182 |
|
183 |
example_dict = {
|
184 |
-
"sentence": row_df["
|
185 |
-
"user_id": row_df["
|
186 |
-
"datetime": row_df["
|
187 |
}
|
188 |
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
"surprise": row_df["Writer_Surprise"],
|
194 |
-
"anger": row_df["Writer_Anger"],
|
195 |
-
"fear": row_df["Writer_Fear"],
|
196 |
-
"disgust": row_df["Writer_Disgust"],
|
197 |
-
"trust": row_df["Writer_Trust"],
|
198 |
-
}
|
199 |
-
|
200 |
-
for reader_num in range(1, 4):
|
201 |
-
example_dict[f"reader{reader_num}"] = {
|
202 |
-
"joy": row_df[f"Reader{reader_num}_Joy"],
|
203 |
-
"sadness": row_df[f"Reader{reader_num}_Sadness"],
|
204 |
-
"anticipation": row_df[f"Reader{reader_num}_Anticipation"],
|
205 |
-
"surprise": row_df[f"Reader{reader_num}_Surprise"],
|
206 |
-
"anger": row_df[f"Reader{reader_num}_Anger"],
|
207 |
-
"fear": row_df[f"Reader{reader_num}_Fear"],
|
208 |
-
"disgust": row_df[f"Reader{reader_num}_Disgust"],
|
209 |
-
"trust": row_df[f"Reader{reader_num}_Trust"],
|
210 |
}
|
211 |
-
|
212 |
-
example_dict["avg_readers"] = {
|
213 |
-
"joy": row_df["Avg. Readers_Joy"],
|
214 |
-
"sadness": row_df["Avg. Readers_Sadness"],
|
215 |
-
"anticipation": row_df["Avg. Readers_Anticipation"],
|
216 |
-
"surprise": row_df["Avg. Readers_Surprise"],
|
217 |
-
"anger": row_df["Avg. Readers_Anger"],
|
218 |
-
"fear": row_df["Avg. Readers_Fear"],
|
219 |
-
"disgust": row_df["Avg. Readers_Disgust"],
|
220 |
-
"trust": row_df["Avg. Readers_Trust"],
|
221 |
-
}
|
222 |
-
|
223 |
yield i, example_dict
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import logging
|
2 |
+
from typing import Final, List, TypedDict
|
3 |
|
4 |
import datasets as ds
|
5 |
import pandas as pd
|
|
|
60 |
return df
|
61 |
|
62 |
|
63 |
+
def _convert_column_name(df: pd.DataFrame) -> pd.DataFrame:
|
64 |
+
|
65 |
+
# ['Sentence', 'UserID', 'Datetime', 'Train/Dev/Test', 'Writer_Joy', ...]
|
66 |
+
# -> ['sentence', 'userid', 'datetime', 'train/dev/test', 'writer_joy', ...]
|
67 |
+
df.columns = df.columns.str.lower()
|
68 |
+
|
69 |
+
# ['avg. readers_joy', 'avg. readers_sadness', 'avg. readers_anticipation', ...]
|
70 |
+
# -> ['avg_readers_joy', 'avg_readers_sadness', 'avg_readers_anticipation', ...]
|
71 |
+
df.columns = df.columns.str.replace(". ", "_")
|
72 |
+
|
73 |
+
return df
|
74 |
+
|
75 |
+
|
76 |
def _load_tsv(tsv_path: str) -> pd.DataFrame:
|
77 |
logger.info(f"Load TSV file from {tsv_path}")
|
78 |
df = pd.read_csv(tsv_path, delimiter="\t")
|
79 |
|
80 |
+
# some preprocessing
|
81 |
df = _fix_typo_in_dataset(df)
|
82 |
+
df = _convert_column_name(df)
|
83 |
|
84 |
return df
|
85 |
|
86 |
|
87 |
+
EIGHT_EMOTIONS: Final[List[str]] = [
|
88 |
+
"joy",
|
89 |
+
"sadness",
|
90 |
+
"anticipation",
|
91 |
+
"surprise",
|
92 |
+
"anger",
|
93 |
+
"fear",
|
94 |
+
"disgust",
|
95 |
+
"trust",
|
96 |
+
]
|
97 |
+
|
98 |
+
|
99 |
class WrimeDataset(ds.GeneratorBasedBuilder):
|
100 |
BUILDER_CONFIGS = [
|
101 |
ds.BuilderConfig(
|
|
|
110 |
),
|
111 |
]
|
112 |
|
113 |
+
def __info(self, emotions: List[str]) -> ds.DatasetInfo:
|
114 |
+
features_dict = {
|
115 |
+
"sentence": ds.Value("string"),
|
116 |
+
"user_id": ds.Value("string"),
|
117 |
+
"datetime": ds.Value("string"),
|
118 |
+
}
|
119 |
+
|
120 |
+
readers = [f"reader{i}" for i in range(1, 4)] + ["avg_readers"]
|
121 |
+
for k in ["writer"] + readers:
|
122 |
+
features_dict[k] = {emotion: ds.Value("int8") for emotion in emotions} # type: ignore
|
123 |
+
features = ds.Features(features_dict)
|
124 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
125 |
return ds.DatasetInfo(
|
126 |
description=_DESCRIPTION,
|
127 |
features=features,
|
|
|
130 |
citation=_CITATION,
|
131 |
)
|
132 |
|
133 |
+
def _info(self) -> ds.DatasetInfo:
|
134 |
+
|
135 |
+
if self.config.version.major == 1: # type: ignore
|
136 |
+
# Ver.1: 80人の筆者から収集した43,200件の投稿に感情強度をラベル付け
|
137 |
+
return self.__info(emotions=EIGHT_EMOTIONS)
|
138 |
+
|
139 |
+
elif self.config.version.major == 2: # type: ignore
|
140 |
+
# Ver.2: 60人の筆者から収集した35,000件の投稿(Ver.1のサブセット)に感情極性を追加でラベル付け
|
141 |
+
return self.__info(emotions=EIGHT_EMOTIONS + ["sentiment"])
|
142 |
+
|
143 |
+
else:
|
144 |
+
raise ValueError(f"Invalid dataset version: {self.config.version}")
|
145 |
+
|
146 |
def _split_generators(self, dl_manager: ds.DownloadManager):
|
147 |
wrime_datasets = dl_manager.download_and_extract(_URLS)
|
148 |
major_version_name = f"ver{self.config.version.major}" # type: ignore
|
149 |
|
150 |
wrime_df = _load_tsv(tsv_path=wrime_datasets[major_version_name])
|
151 |
+
tng_wrime_df = wrime_df[wrime_df["train/dev/test"] == "train"]
|
152 |
+
dev_wrime_df = wrime_df[wrime_df["train/dev/test"] == "dev"]
|
153 |
+
tst_wrime_df = wrime_df[wrime_df["train/dev/test"] == "test"]
|
154 |
|
155 |
return [
|
156 |
ds.SplitGenerator(
|
|
|
167 |
),
|
168 |
]
|
169 |
|
170 |
+
def __generate_examples(self, df: pd.DataFrame, emotions: List[str]):
|
|
|
|
|
|
|
171 |
for i in range(len(df)):
|
172 |
row_df = df.iloc[i]
|
173 |
|
174 |
example_dict = {
|
175 |
+
"sentence": row_df["sentence"],
|
176 |
+
"user_id": row_df["userid"],
|
177 |
+
"datetime": row_df["datetime"],
|
178 |
}
|
179 |
|
180 |
+
readers = [f"reader{i}" for i in range(1, 4)] + ["avg_readers"]
|
181 |
+
for k in ["writer"] + readers:
|
182 |
+
example_dict[k] = {
|
183 |
+
emotion: row_df[f"{k}_{emotion}"] for emotion in emotions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
184 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
185 |
yield i, example_dict
|
186 |
+
|
187 |
+
def _generate_examples(self, df: pd.DataFrame): # type: ignore[override]
|
188 |
+
|
189 |
+
if self.config.version.major == 1: # type: ignore
|
190 |
+
yield from self.__generate_examples(
|
191 |
+
df,
|
192 |
+
emotions=EIGHT_EMOTIONS,
|
193 |
+
)
|
194 |
+
elif self.config.version.major == 2: # type: ignore
|
195 |
+
yield from self.__generate_examples(
|
196 |
+
df,
|
197 |
+
emotions=EIGHT_EMOTIONS + ["sentiment"],
|
198 |
+
)
|
199 |
+
else:
|
200 |
+
raise ValueError(f"Invalid dataset version: {self.config.version}")
|