Yaxin commited on
Commit
2c764ad
·
1 Parent(s): 32929a0

Create new file

Browse files
Files changed (1) hide show
  1. SemEval2015Task12Raw.py +180 -0
SemEval2015Task12Raw.py ADDED
@@ -0,0 +1,180 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2020 HuggingFace Datasets Authors.
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
+ """The SemEval2015 Task12 Reviews Corpus"""
17
+
18
+ import datasets
19
+
20
+ _CITATION = """\
21
+ @inproceedings{pontiki2015semeval,
22
+ title={Semeval-2015 task 12: Aspect based sentiment analysis},
23
+ author={Pontiki, Maria and Galanis, Dimitrios and Papageorgiou, Harris and Manandhar, Suresh and Androutsopoulos, Ion},
24
+ booktitle={Proceedings of the 9th international workshop on semantic evaluation (SemEval 2015)},
25
+ pages={486--495},
26
+ year={2015}
27
+ }
28
+ """
29
+
30
+ _LICENSE = """\
31
+ Please click on the homepage URL for license details.
32
+ """
33
+
34
+ _DESCRIPTION = """\
35
+ A collection of SemEval2015 specifically designed to aid research in Aspect Based Sentiment Analysis.
36
+ """
37
+
38
+ _CONFIG = [
39
+ # restaruants Domain
40
+ "restaurants",
41
+ # Consumer Electronics Domain
42
+ "laptops"
43
+ ]
44
+
45
+ _VERSION = "0.0.1"
46
+
47
+ _HOMEPAGE_URL = "https://alt.qcri.org/semeval2015/task12/index.php?id=data-and-tools/"
48
+ _DOWNLOAD_URL = "https://raw.githubusercontent.com/YaxinCui/ABSADataset/main/SemEval2015Task12Corrected/{split}/{domain}_{split}.xml"
49
+
50
+
51
+ class SemEval2015Task12RawConfig(datasets.BuilderConfig):
52
+ """BuilderConfig for SemEval2015Config."""
53
+
54
+ def __init__(self, _CONFIG, **kwargs):
55
+ super(SemEval2015Task12RawConfig, self).__init__(version=datasets.Version(_VERSION, ""), **kwargs),
56
+ self.configs = _CONFIG
57
+
58
+
59
+ class SemEval2015Task12Raw(datasets.GeneratorBasedBuilder):
60
+ """The lingual Amazon Reviews Corpus"""
61
+
62
+ BUILDER_CONFIGS = [
63
+ SemEval2015Task12RawConfig(
64
+ name="All",
65
+ _CONFIG=_CONFIG,
66
+ description="A collection of SemEval2015 specifically designed to aid research in lingual Aspect Based Sentiment Analysis.",
67
+ )
68
+ ] + [
69
+ SemEval2015Task12RawConfig(
70
+ name=config,
71
+ _CONFIG=[config],
72
+ description=f"{config} of SemEval2015 specifically designed to aid research in lingual Aspect Based Sentiment Analysis",
73
+ )
74
+ for config in _CONFIG
75
+ ]
76
+
77
+ BUILDER_CONFIG_CLASS = SemEval2015Task12RawConfig
78
+ DEFAULT_CONFIG_NAME = "All"
79
+
80
+ def _info(self):
81
+ return datasets.DatasetInfo(
82
+ description=_DESCRIPTION,
83
+ features=datasets.Features(
84
+ {'text': datasets.Value(dtype='string'),
85
+ 'opinions': [
86
+ {'category': datasets.Value(dtype='string'),
87
+ 'from': datasets.Value(dtype='string'),
88
+ 'polarity': datasets.Value(dtype='string'),
89
+ 'target': datasets.Value(dtype='string'),
90
+ 'to': datasets.Value(dtype='string')}
91
+ ],
92
+ 'domain': datasets.Value(dtype='string'),
93
+ 'reviewId': datasets.Value(dtype='string'),
94
+ 'sentenceId': datasets.Value(dtype='string')
95
+ }
96
+ ),
97
+ supervised_keys=None,
98
+ license=_LICENSE,
99
+ homepage=_HOMEPAGE_URL,
100
+ citation=_CITATION,
101
+ )
102
+
103
+ def _split_generators(self, dl_manager):
104
+
105
+ train_urls = [_DOWNLOAD_URL.format(split="train", domain=config) for config in self.config.configs]
106
+ dev_urls = [_DOWNLOAD_URL.format(split="trial", domain=config) for config in self.config.configs]
107
+ test_urls = [_DOWNLOAD_URL.format(split="test", domain=config) for config in self.config.configs]
108
+
109
+ train_paths = dl_manager.download_and_extract(train_urls)
110
+ dev_paths = dl_manager.download_and_extract(dev_urls)
111
+ test_paths = dl_manager.download_and_extract(test_urls)
112
+
113
+ return [
114
+ datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"file_paths": train_paths, "domain_list": self.config.configs}),
115
+ datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"file_paths": dev_paths, "domain_list": self.config.configs}),
116
+ datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"file_paths": test_paths, "domain_list": self.config.configs}),
117
+ ]
118
+
119
+ def _generate_examples(self, file_paths, domain_list):
120
+ row_count = 0
121
+ assert len(file_paths)==len(domain_list)
122
+
123
+ for i in range(len(file_paths)):
124
+ file_path, domain = file_paths[i], domain_list[i]
125
+ semEvalDataset = SemEvalXMLDataset(file_path, domain)
126
+
127
+ for example in semEvalDataset.SentenceWithOpinions:
128
+
129
+ yield row_count, example
130
+ row_count += 1
131
+
132
+
133
+ # 输入:xlm文件的文件路径
134
+ # 输出:一个DataSet,每个样例包含[reviewid, sentenceId, text, UniOpinions]
135
+ # 每个样例包含的Opinion,是一个列表,包含的是单个Opinion的详情
136
+
137
+ from xml.dom.minidom import parse
138
+
139
+ class SemEvalXMLDataset():
140
+ def __init__(self, file_name, domain):
141
+ # 获得SentenceWithOpinions,一个List包含(reviewId, sentenceId, text, Opinions)
142
+
143
+ self.SentenceWithOpinions = []
144
+ self.xml_path = file_name
145
+
146
+ self.sentenceXmlList = parse(self.xml_path).getElementsByTagName('sentence')
147
+
148
+ for sentenceXml in self.sentenceXmlList:
149
+ reviewId = sentenceXml.getAttribute("id").split(':')[0]
150
+ sentenceId = sentenceXml.getAttribute("id")
151
+ if len(sentenceXml.getElementsByTagName("text")[0].childNodes) < 1:
152
+ # skip no reviews part
153
+ continue
154
+ text = sentenceXml.getElementsByTagName("text")[0].childNodes[0].nodeValue
155
+ OpinionXmlList = sentenceXml.getElementsByTagName("Opinion")
156
+ Opinions = []
157
+ for opinionXml in OpinionXmlList:
158
+ # some text maybe have no opinion
159
+ target = opinionXml.getAttribute("target")
160
+ category = opinionXml.getAttribute("category")
161
+ polarity = opinionXml.getAttribute("polarity")
162
+ from_ = opinionXml.getAttribute("from")
163
+ to = opinionXml.getAttribute("to")
164
+
165
+ opinionDict = {
166
+ "target": target,
167
+ "category": category,
168
+ "polarity": polarity,
169
+ "from": from_,
170
+ "to": to
171
+ }
172
+ Opinions.append(opinionDict)
173
+
174
+ self.SentenceWithOpinions.append({
175
+ "text": text,
176
+ "opinions": Opinions,
177
+ "domain": domain,
178
+ "reviewId": reviewId,
179
+ "sentenceId": sentenceId
180
+ })