|
import csv |
|
import datasets |
|
from datasets.tasks import TextClassification |
|
|
|
|
|
_DESCRIPTION = """\ |
|
Sentiment analysis dataset extracted and labeled from Digikala and Snapp Food comments |
|
""" |
|
|
|
_DOWNLOAD_URLS = [ |
|
"https://huggingface.co/datasets/hezar-ai/sentiment_digikala_snappfood/blob/main/sentiment_digikala_snappfood_train.csv", |
|
"https://huggingface.co/datasets/hezar-ai/sentiment_digikala_snappfood/blob/main/sentiment_digikala_snappfood_test.csv" |
|
] |
|
|
|
|
|
class SentimentDigikalaSnappfoodConfig(datasets.BuilderConfig): |
|
"""BuilderConfig for SentimentMixedV1""" |
|
|
|
def __init__(self, **kwargs): |
|
"""BuilderConfig for SentimentMixedV1. |
|
Args: |
|
**kwargs: keyword arguments forwarded to super. |
|
""" |
|
super(SentimentDigikalaSnappfoodConfig, self).__init__(version=datasets.Version("1.0.0", ""), **kwargs) |
|
|
|
|
|
class SentimentDigikalaSnappfood(datasets.GeneratorBasedBuilder): |
|
"""Sentiment analysis on Digikala/SnappFood comments""" |
|
|
|
BUILDER_CONFIGS = [ |
|
SentimentDigikalaSnappfoodConfig( |
|
name="plain_text", |
|
description="Plain text", |
|
) |
|
] |
|
|
|
def _info(self): |
|
return datasets.DatasetInfo( |
|
description=_DESCRIPTION, |
|
features=datasets.Features( |
|
{"text": datasets.Value("string"), "label": datasets.features.ClassLabel(names=["negative", "positive", "neutral"])} |
|
), |
|
supervised_keys=None, |
|
homepage="https://huggingface.co/datasets/hezar-ai/sentiment_digikala_snappfood", |
|
task_templates=[TextClassification(text_column="text", label_column="label")], |
|
) |
|
|
|
def _split_generators(self, dl_manager): |
|
archive = dl_manager.download(_DOWNLOAD_URLS) |
|
return [ |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TRAIN, gen_kwargs={"files": dl_manager.iter_archive(archive), "split": "train"} |
|
), |
|
datasets.SplitGenerator( |
|
name=datasets.Split.TEST, gen_kwargs={"files": dl_manager.iter_archive(archive), "split": "test"} |
|
), |
|
] |
|
|
|
def _generate_examples(self, filepath): |
|
"""Generate examples.""" |
|
|
|
label_mapping = {"negative": 0, "positive": 1, "neutral": 2} |
|
with open(filepath, encoding="utf-8") as csv_file: |
|
csv_reader = csv.reader( |
|
csv_file, quotechar='"', delimiter=",", quoting=csv.QUOTE_ALL, skipinitialspace=True |
|
) |
|
for id_, row in enumerate(csv_reader): |
|
text, label = row |
|
label = label_mapping[label] |
|
yield id_, {"text": text, "label": label} |