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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
"""
|
| 17 |
+
Code-mixed sentiment analysis of Indonesian language and Javanese language
|
| 18 |
+
using Lexicon based approach
|
| 19 |
+
|
| 20 |
+
Nowadays mixing one language with another language either in spoken or written
|
| 21 |
+
communication has become a common practice for bilingual speakers in daily
|
| 22 |
+
conversation as well as in social media. Lexicon based approach is one of the
|
| 23 |
+
approaches in extracting the sentiment analysis. This study is aimed to compare
|
| 24 |
+
two lexicon models which are SentiNetWord and VADER in extracting the polarity
|
| 25 |
+
of the code-mixed sentences in Indonesian language and Javanese language. 3,963
|
| 26 |
+
tweets were gathered from two accounts that provide code-mixed tweets.
|
| 27 |
+
Pre-processing such as removing duplicates, translating to English, filter
|
| 28 |
+
special characters, transform lower case and filter stop words were conducted
|
| 29 |
+
on the tweets. Positive and negative word score from lexicon model was then
|
| 30 |
+
calculated using simple mathematic formula in order to classify the polarity.
|
| 31 |
+
By comparing with the manual labelling, the result showed that SentiNetWord
|
| 32 |
+
perform better than VADER in negative sentiments. However, both of the lexicon
|
| 33 |
+
model did not perform well in neutral and positive sentiments. On overall
|
| 34 |
+
performance, VADER showed better performance than SentiNetWord. This study
|
| 35 |
+
showed that the reason for the misclassified was that most of Indonesian
|
| 36 |
+
language and Javanese language consist of words that were considered as
|
| 37 |
+
positive in both Lexicon model.
|
| 38 |
+
|
| 39 |
+
[nusantara_schema_name] = (text, t2t)
|
| 40 |
+
"""
|
| 41 |
+
from pathlib import Path
|
| 42 |
+
from typing import Dict, List, Tuple
|
| 43 |
+
|
| 44 |
+
import datasets
|
| 45 |
+
import pandas as pd
|
| 46 |
+
|
| 47 |
+
from nusacrowd.utils import schemas
|
| 48 |
+
from nusacrowd.utils.configs import NusantaraConfig
|
| 49 |
+
from nusacrowd.utils.constants import Tasks
|
| 50 |
+
|
| 51 |
+
_CITATION = """\
|
| 52 |
+
@article{Tho_2021,
|
| 53 |
+
doi = {10.1088/1742-6596/1869/1/012084},
|
| 54 |
+
url = {https://doi.org/10.1088/1742-6596/1869/1/012084},
|
| 55 |
+
year = 2021,
|
| 56 |
+
month = {apr},
|
| 57 |
+
publisher = {{IOP} Publishing},
|
| 58 |
+
volume = {1869},
|
| 59 |
+
number = {1},
|
| 60 |
+
pages = {012084},
|
| 61 |
+
author = {C Tho and Y Heryadi and L Lukas and A Wibowo},
|
| 62 |
+
title = {Code-mixed sentiment analysis of Indonesian language and Javanese language using Lexicon based approach},
|
| 63 |
+
journal = {Journal of Physics: Conference Series},
|
| 64 |
+
abstract = {Nowadays mixing one language with another language either in
|
| 65 |
+
spoken or written communication has become a common practice for bilingual
|
| 66 |
+
speakers in daily conversation as well as in social media. Lexicon based
|
| 67 |
+
approach is one of the approaches in extracting the sentiment analysis. This
|
| 68 |
+
study is aimed to compare two lexicon models which are SentiNetWord and VADER
|
| 69 |
+
in extracting the polarity of the code-mixed sentences in Indonesian language
|
| 70 |
+
and Javanese language. 3,963 tweets were gathered from two accounts that
|
| 71 |
+
provide code-mixed tweets. Pre-processing such as removing duplicates,
|
| 72 |
+
translating to English, filter special characters, transform lower case and
|
| 73 |
+
filter stop words were conducted on the tweets. Positive and negative word
|
| 74 |
+
score from lexicon model was then calculated using simple mathematic formula
|
| 75 |
+
in order to classify the polarity. By comparing with the manual labelling,
|
| 76 |
+
the result showed that SentiNetWord perform better than VADER in negative
|
| 77 |
+
sentiments. However, both of the lexicon model did not perform well in
|
| 78 |
+
neutral and positive sentiments. On overall performance, VADER showed better
|
| 79 |
+
performance than SentiNetWord. This study showed that the reason for the
|
| 80 |
+
misclassified was that most of Indonesian language and Javanese language
|
| 81 |
+
consist of words that were considered as positive in both Lexicon model.}
|
| 82 |
+
}
|
| 83 |
+
"""
|
| 84 |
+
|
| 85 |
+
_DATASETNAME = "code_mixed_jv_id"
|
| 86 |
+
|
| 87 |
+
_DESCRIPTION = """\
|
| 88 |
+
Sentiment analysis and machine translation data for Javanese and Indonesian.
|
| 89 |
+
"""
|
| 90 |
+
|
| 91 |
+
_HOMEPAGE = "https://iopscience.iop.org/article/10.1088/1742-6596/1869/1/012084"
|
| 92 |
+
|
| 93 |
+
_LICENSE = "cc_by_3.0"
|
| 94 |
+
|
| 95 |
+
_URLS = {
|
| 96 |
+
_DATASETNAME: "https://docs.google.com/spreadsheets/d/1mq2VyPEDfXl7K6p5TbRPsaefYwkuy7RQ/export?format=csv&gid=356398080",
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
_SUPPORTED_TASKS = [Tasks.SENTIMENT_ANALYSIS, Tasks.MACHINE_TRANSLATION]
|
| 100 |
+
|
| 101 |
+
_SOURCE_VERSION = "1.0.0"
|
| 102 |
+
|
| 103 |
+
_NUSANTARA_VERSION = "1.0.0"
|
| 104 |
+
|
| 105 |
+
_LANGUAGES = ['jav', 'ind']
|
| 106 |
+
_LOCAL = False
|
| 107 |
+
|
| 108 |
+
LANGUAGES_COLUMNS = {
|
| 109 |
+
"id": ("text_ind", "text_jav"),
|
| 110 |
+
"jv": ("text_jav", "text_ind"),
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
class CodeMixedSenti(datasets.GeneratorBasedBuilder):
|
| 115 |
+
"""Code-mixed sentiment analysis for Indonesian and Javanese."""
|
| 116 |
+
|
| 117 |
+
SOURCE_VERSION = datasets.Version(_SOURCE_VERSION)
|
| 118 |
+
NUSANTARA_VERSION = datasets.Version(_NUSANTARA_VERSION)
|
| 119 |
+
|
| 120 |
+
BUILDER_CONFIGS = [
|
| 121 |
+
NusantaraConfig(
|
| 122 |
+
name="code_mixed_jv_id_source",
|
| 123 |
+
version=SOURCE_VERSION,
|
| 124 |
+
description="code_mixed_jv_id source schema for Javanese and Indonesian",
|
| 125 |
+
schema="source",
|
| 126 |
+
subset_id="code_mixed_source",
|
| 127 |
+
),
|
| 128 |
+
NusantaraConfig(
|
| 129 |
+
name="code_mixed_jv_id_jv_nusantara_text",
|
| 130 |
+
version=NUSANTARA_VERSION,
|
| 131 |
+
description="code_mixed_jv_id nusantara_text schema for Javanese",
|
| 132 |
+
schema="nusantara_text",
|
| 133 |
+
subset_id="code_mixed_jv",
|
| 134 |
+
),
|
| 135 |
+
NusantaraConfig(
|
| 136 |
+
name="code_mixed_jv_id_id_nusantara_text",
|
| 137 |
+
version=NUSANTARA_VERSION,
|
| 138 |
+
description="code_mixed_jv_id nusantara_text schema for Indonesian",
|
| 139 |
+
schema="nusantara_text",
|
| 140 |
+
subset_id="code_mixed_id",
|
| 141 |
+
),
|
| 142 |
+
NusantaraConfig(
|
| 143 |
+
name="code_mixed_jv_id_nusantara_t2t",
|
| 144 |
+
version=NUSANTARA_VERSION,
|
| 145 |
+
description="code_mixed_jv_id nusantara_t2t schema for Javanese and Indonesian",
|
| 146 |
+
schema="nusantara_t2t",
|
| 147 |
+
subset_id="code_mixed_jv_id",
|
| 148 |
+
)
|
| 149 |
+
]
|
| 150 |
+
|
| 151 |
+
DEFAULT_CONFIG_NAME = "code_mixed_id_jv_source"
|
| 152 |
+
|
| 153 |
+
def _info(self) -> datasets.DatasetInfo:
|
| 154 |
+
if self.config.schema == "source":
|
| 155 |
+
features = datasets.Features({
|
| 156 |
+
"text_jav": datasets.Value("string"),
|
| 157 |
+
"text_ind": datasets.Value("string"),
|
| 158 |
+
"label": datasets.Value("int32")
|
| 159 |
+
})
|
| 160 |
+
elif self.config.schema == "nusantara_text":
|
| 161 |
+
features = schemas.text_features(["-1", "0", "1"])
|
| 162 |
+
elif self.config.schema == "nusantara_t2t":
|
| 163 |
+
features = schemas.text2text_features
|
| 164 |
+
|
| 165 |
+
return datasets.DatasetInfo(description=_DESCRIPTION, features=features, homepage=_HOMEPAGE, license=_LICENSE, citation=_CITATION,)
|
| 166 |
+
|
| 167 |
+
def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
|
| 168 |
+
"""Returns SplitGenerators."""
|
| 169 |
+
url = _URLS[_DATASETNAME]
|
| 170 |
+
path = dl_manager.download_and_extract(url)
|
| 171 |
+
return [
|
| 172 |
+
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"filepath": path, "split": "train"}),
|
| 173 |
+
]
|
| 174 |
+
|
| 175 |
+
def _generate_examples(self, filepath: Path, split: str) -> Tuple[int, Dict]:
|
| 176 |
+
df = pd.read_csv(filepath,
|
| 177 |
+
skiprows=1,
|
| 178 |
+
names=["text_jav", "label", "text_ind"])
|
| 179 |
+
if self.config.schema == "source":
|
| 180 |
+
i = 0
|
| 181 |
+
for row in df.itertuples():
|
| 182 |
+
ex = {"text_jav": row.text_jav, "text_ind": row.text_ind, "label": row.label}
|
| 183 |
+
yield i, ex
|
| 184 |
+
i += 1
|
| 185 |
+
elif self.config.schema == "nusantara_text":
|
| 186 |
+
prefix_length = len(_DATASETNAME)
|
| 187 |
+
start = prefix_length + 1
|
| 188 |
+
end = prefix_length + 1 + 2
|
| 189 |
+
language = self.config.name[start:end]
|
| 190 |
+
keep_column, drop_column = LANGUAGES_COLUMNS[language]
|
| 191 |
+
df = df.drop(columns=[drop_column]).rename(columns={keep_column: "text"})
|
| 192 |
+
i = 0
|
| 193 |
+
for row in df.itertuples():
|
| 194 |
+
ex = {"id": str(i), "text": row.text, "label": str(row.label)}
|
| 195 |
+
yield i, ex
|
| 196 |
+
i += 1
|
| 197 |
+
elif self.config.schema == "nusantara_t2t":
|
| 198 |
+
i = 0
|
| 199 |
+
for row in df.itertuples():
|
| 200 |
+
ex = {"id": str(i), "text_1": row.text_jav, "text_2": row.text_ind, "text_1_name": "jav", "text_2_name": "ind"}
|
| 201 |
+
yield i, ex
|
| 202 |
+
i += 1
|
| 203 |
+
|
| 204 |
+
if __name__ == "__main__":
|
| 205 |
+
datasets.load_dataset(__file__)
|