holylovenia commited on
Commit
923ae04
·
1 Parent(s): afbe5c0

Upload barasa.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. barasa.py +161 -0
barasa.py ADDED
@@ -0,0 +1,161 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from pathlib import Path
2
+ from typing import List
3
+
4
+ import datasets
5
+ import pandas as pd
6
+ import codecs
7
+ from collections import namedtuple
8
+
9
+ from nusacrowd.utils import schemas
10
+ from nusacrowd.utils.configs import NusantaraConfig
11
+ from nusacrowd.utils.constants import DEFAULT_NUSANTARA_VIEW_NAME, DEFAULT_SOURCE_VIEW_NAME, Tasks
12
+
13
+ _DATASETNAME = "barasa"
14
+ _SOURCE_VIEW_NAME = DEFAULT_SOURCE_VIEW_NAME
15
+ _UNIFIED_VIEW_NAME = DEFAULT_NUSANTARA_VIEW_NAME
16
+
17
+ _LANGUAGES = ["ind"] # We follow ISO639-3 langauge code (https://iso639-3.sil.org/code_tables/639/data)
18
+ _LOCAL = False
19
+ _CITATION = """\
20
+ @inproceedings{baccianella-etal-2010-sentiwordnet,
21
+ title = "{S}enti{W}ord{N}et 3.0: An Enhanced Lexical Resource for Sentiment Analysis and Opinion Mining",
22
+ author = "Baccianella, Stefano and
23
+ Esuli, Andrea and
24
+ Sebastiani, Fabrizio",
25
+ booktitle = "Proceedings of the Seventh International Conference on Language Resources and Evaluation ({LREC}'10)",
26
+ month = may,
27
+ year = "2010",
28
+ address = "Valletta, Malta",
29
+ publisher = "European Language Resources Association (ELRA)",
30
+ url = "http://www.lrec-conf.org/proceedings/lrec2010/pdf/769_Paper.pdf",
31
+ abstract = "In this work we present SENTIWORDNET 3.0, a lexical resource explicitly devised for supporting sentiment classification and opinion mining applications. SENTIWORDNET 3.0 is an improved version of SENTIWORDNET 1.0, a lexical resource publicly available for research purposes, now currently licensed to more than 300 research groups and used in a variety of research projects worldwide. Both SENTIWORDNET 1.0 and 3.0 are the result of automatically annotating all WORDNET synsets according to their degrees of positivity, negativity, and neutrality. SENTIWORDNET 1.0 and 3.0 differ (a) in the versions of WORDNET which they annotate (WORDNET 2.0 and 3.0, respectively), (b) in the algorithm used for automatically annotating WORDNET, which now includes (additionally to the previous semi-supervised learning step) a random-walk step for refining the scores. We here discuss SENTIWORDNET 3.0, especially focussing on the improvements concerning aspect (b) that it embodies with respect to version 1.0. We also report the results of evaluating SENTIWORDNET 3.0 against a fragment of WORDNET 3.0 manually annotated for positivity, negativity, and neutrality; these results indicate accuracy improvements of about 20{\%} with respect to SENTIWORDNET 1.0.",
32
+ }
33
+
34
+ @misc{moeljadi_2016,
35
+ title={Neocl/Barasa: Indonesian SentiWordNet},
36
+ url={https://github.com/neocl/barasa},
37
+ journal={GitHub},
38
+ author={Moeljadi, David},
39
+ year={2016}, month={Mar}
40
+ }
41
+ """
42
+
43
+ _DESCRIPTION = """\
44
+ The Barasa dataset is an Indonesian SentiWordNet for sentiment analysis.
45
+ For each term, the pair (POS,ID) uniquely identifies a WordNet (3.0) synset and there are PosScore and NegScore to show the positivity and negativity of the term.
46
+ The objectivity score can be calculated as: ObjScore = 1 - (PosScore + NegScore).
47
+ """
48
+
49
+ _HOMEPAGE = "https://github.com/neocl/barasa"
50
+
51
+ _LICENSE = "MIT"
52
+
53
+ _URLs = {
54
+ "senti_wordnet": "https://github.com/neocl/barasa/raw/master/data/SentiWordNet_3.0.0_20130122.txt",
55
+ "tab": "https://github.com/neocl/barasa/raw/55f669ca3e417e7fa8d0ebafb67700b9c9eeff1d/data/wn-msa-all.tab",
56
+ }
57
+
58
+ _SUPPORTED_TASKS = [Tasks.SENTIMENT_ANALYSIS]
59
+
60
+ _SOURCE_VERSION = "1.0.0"
61
+ _NUSANTARA_VERSION = None
62
+
63
+ class Barasa(datasets.GeneratorBasedBuilder):
64
+
65
+ BUILDER_CONFIGS = [
66
+ NusantaraConfig(
67
+ name="barasa_source",
68
+ version=datasets.Version(_SOURCE_VERSION),
69
+ description="Barasa source schema",
70
+ schema="source",
71
+ subset_id="barasa",
72
+ ),
73
+ ]
74
+
75
+ DEFAULT_CONFIG_NAME = "barasa_source"
76
+
77
+ def _info(self):
78
+ if self.config.schema == "source":
79
+ features = datasets.Features(
80
+ {
81
+ "index": datasets.Value("string"),
82
+ "synset": datasets.Value("string"),
83
+ "PosScore": datasets.Value("float32"),
84
+ "NegScore": datasets.Value("float32"),
85
+ "language": datasets.Value("string"),
86
+ "goodness": datasets.Value("string"),
87
+ "lemma": datasets.Value("string"),
88
+ }
89
+ )
90
+
91
+ return datasets.DatasetInfo(
92
+ description=_DESCRIPTION,
93
+ features=features,
94
+ homepage=_HOMEPAGE,
95
+ license=_LICENSE,
96
+ citation=_CITATION,
97
+ )
98
+
99
+ def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
100
+ sentiWordnet_tsv_path = Path(dl_manager.download_and_extract(_URLs["senti_wordnet"]))
101
+ tab_path = Path(dl_manager.download_and_extract(_URLs["tab"]))
102
+ data_files = {
103
+ "sentiWordnet": sentiWordnet_tsv_path,
104
+ "tab": tab_path,
105
+ }
106
+
107
+ return [
108
+ datasets.SplitGenerator(
109
+ name=datasets.Split.TRAIN,
110
+ gen_kwargs={"filepath": [data_files["sentiWordnet"], data_files["tab"]]},
111
+ ),
112
+ ]
113
+
114
+ def _generate_examples(self, filepath: Path):
115
+ lines = self.gen_barasa(filepath[0], filepath[1])
116
+
117
+ if self.config.schema == "source":
118
+ for i, row in enumerate(lines):
119
+ synset, language, goodness, lemma, PosScore, NegScore = row.split('\t')[:6]
120
+ PosScore = float(PosScore)
121
+ NegScore = float(NegScore)
122
+ ex = {
123
+ "index": i,
124
+ "synset": synset,
125
+ "PosScore": PosScore,
126
+ "NegScore": NegScore,
127
+ "language": language,
128
+ "goodness": goodness,
129
+ "lemma": lemma,
130
+ }
131
+ yield i, ex
132
+ else:
133
+ raise ValueError(f"Invalid config: {self.config.name}")
134
+
135
+ def gen_barasa(self, SENTI_WORDNET_FILE, BAHASA_WORDNET_FILE):
136
+ SynsetInfo = namedtuple('SynsetInfo', ['synset', 'pos', 'neg'])
137
+ LemmaInfo = namedtuple('LemmaInfo', ['lemma', 'pos', 'neg'])
138
+
139
+ SYNSET_SCORE = {}
140
+ LEMMA_SCORE = {}
141
+
142
+ with codecs.open(SENTI_WORDNET_FILE, encoding='utf-8', mode='r') as SentiWN:
143
+ for line in SentiWN.readlines():
144
+ if line.startswith('#') or len(line.strip()) == 0: # ignore comments
145
+ continue
146
+ # strip off end-of-line, then split
147
+ pos, snum, pscore, nscore, lemma, definition = line.strip().split('\t')
148
+ synset = '%s-%s' % (snum, pos)
149
+ SYNSET_SCORE[synset] = SynsetInfo(synset, pscore, nscore)
150
+
151
+ newlines = []
152
+ with codecs.open(BAHASA_WORDNET_FILE, encoding='utf-8', mode='r') as BahasaWN:
153
+ for line in BahasaWN.readlines():
154
+ synset, lang, goodness, lemma = line.strip().split('\t')
155
+ if synset in SYNSET_SCORE:
156
+ sscore = SYNSET_SCORE[synset]
157
+ LEMMA_SCORE[lemma] = LemmaInfo(lemma, sscore.pos, sscore.neg)
158
+ newline = ("%s\t" * 6) % (synset, lang, goodness, lemma, sscore.pos, sscore.neg)
159
+ newlines.append(newline)
160
+
161
+ return newlines