File size: 2,716 Bytes
9f71ebc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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."""
        # 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}