Datasets:
Create sentiment_digikala_snappfood.py
Browse files
sentiment_digikala_snappfood.py
ADDED
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import csv
|
2 |
+
import datasets
|
3 |
+
from datasets.tasks import TextClassification
|
4 |
+
|
5 |
+
|
6 |
+
_DESCRIPTION = """\
|
7 |
+
Sentiment analysis dataset extracted and labeled from Digikala and Snapp Food comments
|
8 |
+
"""
|
9 |
+
|
10 |
+
_DOWNLOAD_URLS = [
|
11 |
+
"https://huggingface.co/datasets/hezar-ai/sentiment_digikala_snappfood/blob/main/sentiment_digikala_snappfood_train.csv",
|
12 |
+
"https://huggingface.co/datasets/hezar-ai/sentiment_digikala_snappfood/blob/main/sentiment_digikala_snappfood_test.csv"
|
13 |
+
]
|
14 |
+
|
15 |
+
|
16 |
+
class SentimentDigikalaSnappfoodConfig(datasets.BuilderConfig):
|
17 |
+
"""BuilderConfig for SentimentMixedV1"""
|
18 |
+
|
19 |
+
def __init__(self, **kwargs):
|
20 |
+
"""BuilderConfig for SentimentMixedV1.
|
21 |
+
Args:
|
22 |
+
**kwargs: keyword arguments forwarded to super.
|
23 |
+
"""
|
24 |
+
super(SentimentDigikalaSnappfoodConfig, self).__init__(version=datasets.Version("1.0.0", ""), **kwargs)
|
25 |
+
|
26 |
+
|
27 |
+
class SentimentDigikalaSnappfood(datasets.GeneratorBasedBuilder):
|
28 |
+
"""Sentiment analysis on Digikala/SnappFood comments"""
|
29 |
+
|
30 |
+
BUILDER_CONFIGS = [
|
31 |
+
SentimentDigikalaSnappfoodConfig(
|
32 |
+
name="plain_text",
|
33 |
+
description="Plain text",
|
34 |
+
)
|
35 |
+
]
|
36 |
+
|
37 |
+
def _info(self):
|
38 |
+
return datasets.DatasetInfo(
|
39 |
+
description=_DESCRIPTION,
|
40 |
+
features=datasets.Features(
|
41 |
+
{"text": datasets.Value("string"), "label": datasets.features.ClassLabel(names=["negative", "positive", "neutral"])}
|
42 |
+
),
|
43 |
+
supervised_keys=None,
|
44 |
+
homepage="https://huggingface.co/datasets/hezar-ai/sentiment_digikala_snappfood",
|
45 |
+
task_templates=[TextClassification(text_column="text", label_column="label")],
|
46 |
+
)
|
47 |
+
|
48 |
+
def _split_generators(self, dl_manager):
|
49 |
+
archive = dl_manager.download(_DOWNLOAD_URLS)
|
50 |
+
return [
|
51 |
+
datasets.SplitGenerator(
|
52 |
+
name=datasets.Split.TRAIN, gen_kwargs={"files": dl_manager.iter_archive(archive), "split": "train"}
|
53 |
+
),
|
54 |
+
datasets.SplitGenerator(
|
55 |
+
name=datasets.Split.TEST, gen_kwargs={"files": dl_manager.iter_archive(archive), "split": "test"}
|
56 |
+
),
|
57 |
+
]
|
58 |
+
|
59 |
+
def _generate_examples(self, filepath):
|
60 |
+
"""Generate examples."""
|
61 |
+
# For labeled examples, extract the label from the path.
|
62 |
+
label_mapping = {"negative": 0, "positive": 1, "neutral": 2}
|
63 |
+
with open(filepath, encoding="utf-8") as csv_file:
|
64 |
+
csv_reader = csv.reader(
|
65 |
+
csv_file, quotechar='"', delimiter=",", quoting=csv.QUOTE_ALL, skipinitialspace=True
|
66 |
+
)
|
67 |
+
for id_, row in enumerate(csv_reader):
|
68 |
+
text, label = row
|
69 |
+
label = label_mapping[label]
|
70 |
+
yield id_, {"text": text, "label": label}
|