economy-watchers-survey-evaluation / economy-watchers-survey-evaluation.py
retarfi's picture
fix
9f40fc2 verified
raw
history blame contribute delete
No virus
5.17 kB
import datasets
from datasets import Dataset, load_dataset
# TODO: Add BibTeX citation
# Find for instance the citation on arxiv or on the dataset repo/website
_CITATION = """\
TBA
"""
_DESCRIPTION = """\
Dataset from the Economy Watchers Survey for use as evaluation tasks
"""
_HOMEPAGE = "https://github.com/retarfi/economy-watchers-survey"
_LICENSE = "CC-BY 4.0"
VERSION = datasets.Version("0.1.0")
EWS_REPO_NAME = "retarfi/economy-watchers-survey"
LABEL_REASON_OTHER: str = "それ以外"
class EconomyWatchersSurveyConfig(datasets.BuilderConfig):
def __init__(self, **kwargs):
self.ews_version = kwargs.pop("ews_version", None)
super(EconomyWatchersSurveyConfig, self).__init__(**kwargs)
class EconomyWatchersSurveyDataset(datasets.GeneratorBasedBuilder):
BUILDER_CONFIG_CLASS = EconomyWatchersSurveyConfig
BUILDER_CONFIGS = [
EconomyWatchersSurveyConfig(
name="sentiment", version=VERSION, description="Sentiment analysis"
),
EconomyWatchersSurveyConfig(
name="domain", version=VERSION, description="Domain classification"
),
EconomyWatchersSurveyConfig(
name="reason",
version=VERSION,
description="Classification of reason to decision of sentiment",
),
]
def _info(self):
feat_label: datasets.ClassLabel
if self.config.name == "sentiment":
feat_label = datasets.ClassLabel(
num_classes=5, names=["×", "▲", "□", "○", "◎"]
)
elif self.config.name == "domain":
feat_label = datasets.ClassLabel(
num_classes=3, names=["家計動向", "企業動向", "雇用"]
)
elif self.config.name == "reason":
feat_label = datasets.ClassLabel(
num_classes=12,
names=[
"来客数の動き",
"販売量の動き",
"お客様の様子",
"受注量や販売量の動き",
"単価の動き",
"取引先の様子",
"求人数の動き",
"競争相手の様子",
"受注価格や販売価格の動き",
"周辺企業の様子",
"求職者数の動き",
LABEL_REASON_OTHER,
],
)
else:
raise NotImplementedError(self.config.name)
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=datasets.Features(
{
"text": datasets.Value("string"),
"label": feat_label,
"id": datasets.Value("string"),
}
),
homepage=_HOMEPAGE,
license=_LICENSE,
citation=_CITATION,
)
def _split_generators(
self, dl_manager: datasets.DownloadManager
) -> list[datasets.SplitGenerator]:
return [
datasets.SplitGenerator(
name=datasets.Split.TRAIN, gen_kwargs={"split": "train"}
),
datasets.SplitGenerator(
name=datasets.Split.VALIDATION, gen_kwargs={"split": "validation"}
),
datasets.SplitGenerator(
name=datasets.Split.TEST, gen_kwargs={"split": "test"}
),
]
def _generate_examples(self, split: str):
ds_current: Dataset = load_dataset(
EWS_REPO_NAME, "current", revision=self.config.ews_version, split=split
)
if self.config.name == "reason":
ds_current = ds_current.filter(lambda example: example["判断の理由"] is not None)
else:
ds_future: Dataset = load_dataset(
EWS_REPO_NAME, "future", revision=self.config.ews_version, split=split
)
ds_current = datasets.concatenate_datasets(
[
ds_current.remove_columns("判断の理由"),
ds_future.rename_columns(
{
"景気の先行き判断": "景気の現状判断",
"景気の先行きに対する判断理由": "追加説明及び具体的状況の説明",
}
),
]
)
for example in ds_current:
str_label: str
if self.config.name == "sentiment":
str_label = example["景気の現状判断"]
elif self.config.name == "domain":
str_label = example["関連"]
elif self.config.name == "reason":
str_label = example["判断の理由"]
if str_label not in self.info.features["label"].names:
str_label = LABEL_REASON_OTHER
yield example["id"], {
"id": example["id"],
"text": example["追加説明及び具体的状況の説明"],
"label": self.info.features["label"].str2int(str_label),
}