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/hezarai/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.""" label2id = self.info.features[self.info.task_templates[0].label_column].str2int with open(filepath, encoding="utf-8") as csv_file: csv_reader = csv.reader( csv_file, quotechar='"', skipinitialspace=True ) # skip the first row if your csv file has a header row next(csv_reader, None) for id_, row in enumerate(csv_reader): text, label = row label = label2id(label) yield id_, {"text": text, "label": label}