import csv import datasets from datasets.tasks import TextClassification _DESCRIPTION = """\ Sentiment analysis dataset extracted and labeled from Digikala and Snapp Food comments """ _DOWNLOAD_URLS = { "train": "https://huggingface.co/datasets/hezarai/sentiment-dksf/raw/main/sentiment_dksf_train.csv", "test": "https://huggingface.co/datasets/hezarai/sentiment-dksf/raw/main/sentiment_dksf_test.csv" } class SentimentDKSF(datasets.GeneratorBasedBuilder): """Sentiment analysis on Digikala/SnappFood comments""" 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-dksf", task_templates=[TextClassification(text_column="text", label_column="label")], ) def _split_generators(self, dl_manager): train_path = dl_manager.download_and_extract(_DOWNLOAD_URLS["train"]) test_path = dl_manager.download_and_extract(_DOWNLOAD_URLS["test"]) 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.""" label_mapping = {"negative": 0, "positive": 1, "neutral": 2} with open(filepath, encoding="utf-8") as csv_file: csv_reader = csv.reader( csv_file, quotechar='"', skipinitialspace=True ) for id_, row in enumerate(csv_reader): text, label = row label = label_mapping[label] yield id_, {"text": text, "label": label}