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|
|
|
|
|
|
|
|
|
| 1 |
+
from pathlib import Path
|
| 2 |
+
from typing import Dict, List, Tuple
|
| 3 |
+
|
| 4 |
+
import datasets
|
| 5 |
+
import pandas as pd
|
| 6 |
+
from datasets.download.download_manager import DownloadManager
|
| 7 |
+
|
| 8 |
+
from seacrowd.utils import schemas
|
| 9 |
+
from seacrowd.utils.configs import SEACrowdConfig
|
| 10 |
+
from seacrowd.utils.constants import Licenses, Tasks
|
| 11 |
+
|
| 12 |
+
_CITATION = """
|
| 13 |
+
@article{SUTOYO2022108554,
|
| 14 |
+
title = {PRDECT-ID: Indonesian product reviews dataset for emotions classification tasks},
|
| 15 |
+
journal = {Data in Brief},
|
| 16 |
+
volume = {44},
|
| 17 |
+
pages = {108554},
|
| 18 |
+
year = {2022},
|
| 19 |
+
issn = {2352-3409},
|
| 20 |
+
doi = {https://doi.org/10.1016/j.dib.2022.108554},
|
| 21 |
+
url = {https://www.sciencedirect.com/science/article/pii/S2352340922007612},
|
| 22 |
+
author = {Rhio Sutoyo and Said Achmad and Andry Chowanda and Esther Widhi Andangsari and Sani M. Isa},
|
| 23 |
+
keywords = {Natural language processing, Text processing, Text mining, Emotions classification, Sentiment analysis},
|
| 24 |
+
abstract = {Recognizing emotions is vital in communication. Emotions convey
|
| 25 |
+
additional meanings to the communication process. Nowadays, people can
|
| 26 |
+
communicate their emotions on many platforms; one is the product review. Product
|
| 27 |
+
reviews in the online platform are an important element that affects customers’
|
| 28 |
+
buying decisions. Hence, it is essential to recognize emotions from the product
|
| 29 |
+
reviews. Emotions recognition from the product reviews can be done automatically
|
| 30 |
+
using a machine or deep learning algorithm. Dataset can be considered as the
|
| 31 |
+
fuel to model the recognizer. However, only a limited dataset exists in
|
| 32 |
+
recognizing emotions from the product reviews, particularly in a local language.
|
| 33 |
+
This research contributes to the dataset collection of 5400 product reviews in
|
| 34 |
+
Indonesian. It was carefully curated from various (29) product categories,
|
| 35 |
+
annotated with five emotions, and verified by an expert in clinical psychology.
|
| 36 |
+
The dataset supports an innovative process to build automatic emotion
|
| 37 |
+
classification on product reviews.}
|
| 38 |
+
}
|
| 39 |
+
"""
|
| 40 |
+
|
| 41 |
+
_LOCAL = False
|
| 42 |
+
_LANGUAGES = ["ind"]
|
| 43 |
+
_DATASETNAME = "prdect_id"
|
| 44 |
+
_DESCRIPTION = """
|
| 45 |
+
PRDECT-ID Dataset is a collection of Indonesian product review data annotated
|
| 46 |
+
with emotion and sentiment labels. The data were collected from one of the giant
|
| 47 |
+
e-commerce in Indonesia named Tokopedia. The dataset contains product reviews
|
| 48 |
+
from 29 product categories on Tokopedia that use the Indonesian language. Each
|
| 49 |
+
product review is annotated with a single emotion, i.e., love, happiness, anger,
|
| 50 |
+
fear, or sadness. The group of annotators does the annotation process to provide
|
| 51 |
+
emotion labels by following the emotions annotation criteria created by an
|
| 52 |
+
expert in clinical psychology. Other attributes related to the product review
|
| 53 |
+
are also extracted, such as Location, Price, Overall Rating, Number Sold, Total
|
| 54 |
+
Review, and Customer Rating, to support further research.
|
| 55 |
+
"""
|
| 56 |
+
|
| 57 |
+
_HOMEPAGE = "https://data.mendeley.com/datasets/574v66hf2v/1"
|
| 58 |
+
_LICENSE = Licenses.CC_BY_4_0.value
|
| 59 |
+
_URL = "https://data.mendeley.com/public-files/datasets/574v66hf2v/files/f258d159-c678-42f1-9634-edf091a0b1f3/file_downloaded"
|
| 60 |
+
|
| 61 |
+
_SUPPORTED_TASKS = [Tasks.SENTIMENT_ANALYSIS, Tasks.EMOTION_CLASSIFICATION]
|
| 62 |
+
_SOURCE_VERSION = "1.0.0"
|
| 63 |
+
_SEACROWD_VERSION = "2024.06.20"
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
class PrdectIDDataset(datasets.GeneratorBasedBuilder):
|
| 67 |
+
"""PRDECT-ID Dataset"""
|
| 68 |
+
|
| 69 |
+
SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
|
| 70 |
+
SEACROWD_VERSION = datasets.Version(_SEACROWD_VERSION)
|
| 71 |
+
|
| 72 |
+
SEACROWD_SCHEMA_NAME = "text"
|
| 73 |
+
|
| 74 |
+
BUILDER_CONFIGS = [
|
| 75 |
+
SEACrowdConfig(
|
| 76 |
+
name=f"{_DATASETNAME}_emotion_source",
|
| 77 |
+
version=SOURCE_VERSION,
|
| 78 |
+
description=f"{_DATASETNAME} source schema",
|
| 79 |
+
schema="source",
|
| 80 |
+
subset_id=f"{_DATASETNAME}_emotion",
|
| 81 |
+
),
|
| 82 |
+
SEACrowdConfig(
|
| 83 |
+
name=f"{_DATASETNAME}_sentiment_source",
|
| 84 |
+
version=SOURCE_VERSION,
|
| 85 |
+
description=f"{_DATASETNAME} source schema",
|
| 86 |
+
schema="source",
|
| 87 |
+
subset_id=f"{_DATASETNAME}_sentiment",
|
| 88 |
+
),
|
| 89 |
+
SEACrowdConfig(
|
| 90 |
+
name=f"{_DATASETNAME}_emotion_seacrowd_{SEACROWD_SCHEMA_NAME}",
|
| 91 |
+
version=SEACROWD_VERSION,
|
| 92 |
+
description=f"{_DATASETNAME} SEACrowd schema for emotion classification",
|
| 93 |
+
schema=f"seacrowd_{SEACROWD_SCHEMA_NAME}",
|
| 94 |
+
subset_id=f"{_DATASETNAME}_emotion",
|
| 95 |
+
),
|
| 96 |
+
SEACrowdConfig(
|
| 97 |
+
name=f"{_DATASETNAME}_sentiment_seacrowd_{SEACROWD_SCHEMA_NAME}",
|
| 98 |
+
version=SEACROWD_VERSION,
|
| 99 |
+
description=f"{_DATASETNAME} SEACrowd schema for sentiment analysis",
|
| 100 |
+
schema=f"seacrowd_{SEACROWD_SCHEMA_NAME}",
|
| 101 |
+
subset_id=f"{_DATASETNAME}_sentiment",
|
| 102 |
+
),
|
| 103 |
+
]
|
| 104 |
+
|
| 105 |
+
DEFAULT_CONFIG_NAME = f"{_DATASETNAME}_source"
|
| 106 |
+
CLASS_LABELS_EMOTION = ["Happy", "Sadness", "Anger", "Love", "Fear"]
|
| 107 |
+
CLASS_LABELS_SENTIMENT = ["Positive", "Negative"]
|
| 108 |
+
|
| 109 |
+
def _info(self) -> datasets.DatasetInfo:
|
| 110 |
+
if self.config.schema == "source":
|
| 111 |
+
features = datasets.Features(
|
| 112 |
+
{
|
| 113 |
+
"Category": datasets.Value("string"),
|
| 114 |
+
"Product Name": datasets.Value("string"),
|
| 115 |
+
"Location": datasets.Value("string"),
|
| 116 |
+
"Price": datasets.Value("int32"),
|
| 117 |
+
"Overall Rating": datasets.Value("float32"),
|
| 118 |
+
"Number Sold": datasets.Value("int32"),
|
| 119 |
+
"Total Review": datasets.Value("int32"),
|
| 120 |
+
"Customer Rating": datasets.Value("int32"),
|
| 121 |
+
"Customer Review": datasets.Value("string"),
|
| 122 |
+
"Sentiment": datasets.ClassLabel(names=self.CLASS_LABELS_SENTIMENT),
|
| 123 |
+
"Emotion": datasets.ClassLabel(names=self.CLASS_LABELS_EMOTION),
|
| 124 |
+
}
|
| 125 |
+
)
|
| 126 |
+
elif self.config.schema == "seacrowd_text":
|
| 127 |
+
if self.config.subset_id == f"{_DATASETNAME}_emotion":
|
| 128 |
+
features = schemas.text_features(label_names=self.CLASS_LABELS_EMOTION)
|
| 129 |
+
elif self.config.subset_id == f"{_DATASETNAME}_sentiment":
|
| 130 |
+
features = schemas.text_features(label_names=self.CLASS_LABELS_SENTIMENT)
|
| 131 |
+
else:
|
| 132 |
+
raise ValueError(f"Invalid subset: {self.config.subset_id}")
|
| 133 |
+
else:
|
| 134 |
+
raise ValueError(f"Schema '{self.config.schema}' is not defined.")
|
| 135 |
+
|
| 136 |
+
return datasets.DatasetInfo(
|
| 137 |
+
description=_DESCRIPTION,
|
| 138 |
+
features=features,
|
| 139 |
+
homepage=_HOMEPAGE,
|
| 140 |
+
license=_LICENSE,
|
| 141 |
+
citation=_CITATION,
|
| 142 |
+
)
|
| 143 |
+
|
| 144 |
+
def _split_generators(self, dl_manager: DownloadManager) -> List[datasets.SplitGenerator]:
|
| 145 |
+
"""Returns SplitGenerators."""
|
| 146 |
+
data_file = Path(dl_manager.download(_URL))
|
| 147 |
+
return [datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": data_file})]
|
| 148 |
+
|
| 149 |
+
def _generate_examples(self, filepath: Path) -> Tuple[int, Dict]:
|
| 150 |
+
"""Yield examples as (key, example) tuples"""
|
| 151 |
+
df = pd.read_csv(filepath, encoding="utf-8")
|
| 152 |
+
for idx, row in df.iterrows():
|
| 153 |
+
if self.config.schema == "source":
|
| 154 |
+
yield idx, dict(row)
|
| 155 |
+
elif self.config.schema == f"seacrowd_{self.SEACROWD_SCHEMA_NAME}":
|
| 156 |
+
if self.config.subset_id == f"{_DATASETNAME}_emotion":
|
| 157 |
+
yield idx, {"id": idx, "text": row["Customer Review"], "label": row["Emotion"]}
|
| 158 |
+
elif self.config.subset_id == f"{_DATASETNAME}_sentiment":
|
| 159 |
+
yield idx, {"id": idx, "text": row["Customer Review"], "label": row["Sentiment"]}
|
| 160 |
+
else:
|
| 161 |
+
raise ValueError(f"Invalid subset: {self.config.subset_id}")
|