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): train_path = dl_manager.download_and_extract(_TRAIN_DOWNLOAD_URL) test_path = dl_manager.download_and_extract(_TEST_DOWNLOAD_URL) return [ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": train_path}), datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"filepath": test_path}), ] def _generate_examples(self, filepath): """Generate examples.""" # For labeled examples, extract the label from the path. 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}