Datasets:

Languages:
Vietnamese
ArXiv:
License:
holylovenia commited on
Commit
93ee7a3
·
verified ·
1 Parent(s): 003f4ba

Upload vlsp2016_sa.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. vlsp2016_sa.py +181 -0
vlsp2016_sa.py ADDED
@@ -0,0 +1,181 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2022 The HuggingFace Datasets Authors and the current dataset script contributor.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+
16
+ import os
17
+ import re
18
+ from pathlib import Path
19
+ from typing import Dict, List, Tuple
20
+
21
+ import datasets
22
+
23
+ from seacrowd.utils import schemas
24
+ from seacrowd.utils.configs import SEACrowdConfig
25
+ from seacrowd.utils.constants import Licenses, Tasks
26
+
27
+ _CITATION = """\
28
+ @article{nguyen2018vlsp,
29
+ title={VLSP shared task: sentiment analysis},
30
+ author={Nguyen, Huyen TM and Nguyen, Hung V and Ngo, \
31
+ Quyen T and Vu, Luong X and Tran, Vu Mai and Ngo, Bach X and Le, Cuong A},
32
+ journal={Journal of Computer Science and Cybernetics},
33
+ volume={34},
34
+ number={4},
35
+ pages={295--310},
36
+ year={2018}
37
+ }
38
+ """
39
+ _DATASETNAME = "vlsp2016_sa"
40
+
41
+ _DESCRIPTION = """\
42
+ The SA-VLSP2016 dataset were collected from three source sites which are tinhte.vn, \
43
+ vnexpress.net and Facebook, and used for the sentiment analysis task. The data consists \
44
+ of comments of technical articles on those sites. Each comment is given one of \
45
+ four labels: POS (positive), NEG (negative), NEU (neutral) and USELESS (filter-out).
46
+ """
47
+
48
+ _HOMEPAGE = "https://vlsp.org.vn/resources-vlsp2016"
49
+ _LANGUAGES = ["vie"]
50
+
51
+ _LICENSE = Licenses.CC_BY_NC_SA_4_0.value
52
+ _LOCAL = True
53
+
54
+ _URLS = {}
55
+
56
+ _SUPPORTED_TASKS = [Tasks.SENTIMENT_ANALYSIS]
57
+
58
+ _SOURCE_VERSION = "1.0.0"
59
+
60
+ _SEACROWD_VERSION = "2024.06.20"
61
+ _TAGS = ["POS", "NEG", "NEU"]
62
+
63
+
64
+ class VLSP2016SADataset(datasets.GeneratorBasedBuilder):
65
+ """The SA-VLSP2016 dataset, used for sentiment analysis, comprises comments from technical \
66
+ articles on tinhte.vn, vnexpress.net, and Facebook, each labeled as positive, negative, neutral, or filter-out."""
67
+
68
+ SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
69
+ SEACROWD_VERSION = datasets.Version(_SEACROWD_VERSION)
70
+ SEACROWD_SCHEMA_NAME = "text"
71
+
72
+ BUILDER_CONFIGS = [
73
+ SEACrowdConfig(
74
+ name=f"{_DATASETNAME}_source",
75
+ version=SOURCE_VERSION,
76
+ description=f"{_DATASETNAME} source schema",
77
+ schema="source",
78
+ subset_id=f"{_DATASETNAME}",
79
+ ),
80
+ SEACrowdConfig(
81
+ name=f"{_DATASETNAME}_seacrowd_{SEACROWD_SCHEMA_NAME}",
82
+ version=SEACROWD_VERSION,
83
+ description=f"{_DATASETNAME} SEACrowd schema",
84
+ schema=f"seacrowd_{SEACROWD_SCHEMA_NAME}",
85
+ subset_id=f"{_DATASETNAME}",
86
+ ),
87
+ SEACrowdConfig(
88
+ name=f"{_DATASETNAME}_tokenized_seacrowd_{SEACROWD_SCHEMA_NAME}",
89
+ version=SEACROWD_VERSION,
90
+ description=f"{_DATASETNAME} SEACrowd schema",
91
+ schema=f"seacrowd_{SEACROWD_SCHEMA_NAME}",
92
+ subset_id=f"{_DATASETNAME}_tokenized",
93
+ ),
94
+ ]
95
+
96
+ DEFAULT_CONFIG_NAME = f"{_DATASETNAME}_source"
97
+
98
+ def _info(self) -> datasets.DatasetInfo:
99
+ if self.config.schema == "source":
100
+ features = datasets.Features(
101
+ {
102
+ "id": datasets.Value("string"),
103
+ "text": datasets.Value("string"),
104
+ "label": datasets.ClassLabel(names=_TAGS),
105
+ }
106
+ )
107
+
108
+ elif self.config.schema == f"seacrowd_{self.SEACROWD_SCHEMA_NAME}":
109
+ features = schemas.text_features(_TAGS)
110
+
111
+ return datasets.DatasetInfo(
112
+ description=_DESCRIPTION,
113
+ features=features,
114
+ homepage=_HOMEPAGE,
115
+ license=_LICENSE,
116
+ citation=_CITATION,
117
+ )
118
+
119
+ def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
120
+ """Returns SplitGenerators."""
121
+ if self.config.data_dir is None:
122
+ raise ValueError("This is a local dataset. Please pass the data_dir kwarg to load_dataset.")
123
+ else:
124
+ data_dir = self.config.data_dir
125
+
126
+ return [
127
+ datasets.SplitGenerator(
128
+ name=datasets.Split.TRAIN,
129
+ # Whatever you put in gen_kwargs will be passed to _generate_examples
130
+ gen_kwargs={
131
+ "filepath": os.path.join(data_dir, "SA2016-training_data"),
132
+ "split": "train",
133
+ },
134
+ ),
135
+ datasets.SplitGenerator(
136
+ name=datasets.Split.VALIDATION,
137
+ gen_kwargs={
138
+ "filepath": os.path.join(data_dir, "SA2016-TestData-Ans"),
139
+ "split": "dev",
140
+ },
141
+ ),
142
+ ]
143
+
144
+ def _generate_examples(self, filepath: Path, split: str) -> Tuple[int, Dict]:
145
+ if split == "dev":
146
+ if self.config.schema in ["source", f"seacrowd_{self.SEACROWD_SCHEMA_NAME}"]:
147
+ labelfile = "test_raw_ANS.txt"
148
+ elif self.config.schema == f"seacrowd_{self.SEACROWD_SCHEMA_NAME}_tokenized":
149
+ labelfile = "test_tokenized_ANS.txt"
150
+
151
+ with open(os.path.join(filepath, labelfile)) as file:
152
+ data = file.read()
153
+
154
+ pattern = re.compile("(?P<sentence>.+)\n(?P<label>(POS|NEG|NEU))\n")
155
+
156
+ if self.config.schema in ["source", f"seacrowd_{self.SEACROWD_SCHEMA_NAME}", f"seacrowd_{self.SEACROWD_SCHEMA_NAME}_tokenized"]:
157
+ for i, match in enumerate(pattern.finditer(data)):
158
+ yield i, {"id": i, "text": match.group("sentence").replace("\xa0", " "), "label": match.group("label")}
159
+
160
+ else:
161
+ labeltext = {"POS": [], "NEG": [], "NEU": []}
162
+ if self.config.schema in ["source", f"seacrowd_{self.SEACROWD_SCHEMA_NAME}"]:
163
+ positive = "SA-training_positive.txt"
164
+ negative = "SA-training_negative.txt"
165
+ neutral = "SA-training_neutral.txt"
166
+ elif self.config.schema == f"seacrowd_{self.SEACROWD_SCHEMA_NAME}_tokenized":
167
+ positive = "train_positive_tokenized.txt"
168
+ negative = "train_negative_tokenized.txt"
169
+ neutral = "train_neutral_tokenized.txt"
170
+
171
+ for labelsplit, labelfile in zip(labeltext.keys(), [positive, negative, neutral]):
172
+ with open(os.path.join(filepath, labelfile)) as file:
173
+ data = file.read()
174
+ labeltext[labelsplit] = data.split("\n\n")[:-1]
175
+
176
+ if self.config.schema in ["source", f"seacrowd_{self.SEACROWD_SCHEMA_NAME}", f"seacrowd_{self.SEACROWD_SCHEMA_NAME}_tokenized"]:
177
+ idcounter = 0
178
+ for label, sentences in labeltext.items():
179
+ for sentence in sentences:
180
+ yield idcounter, {"id": idcounter, "text": sentence, "label": label}
181
+ idcounter = idcounter + 1