Upload SemEval2014.py
Browse files- SemEval2014.py +189 -0
SemEval2014.py
ADDED
@@ -0,0 +1,189 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 lingual SemEval2014 Task5 Reviews Corpus"""
|
17 |
+
|
18 |
+
import datasets
|
19 |
+
|
20 |
+
_CITATION = """\
|
21 |
+
@article{2014SemEval,
|
22 |
+
title={SemEval-2014 Task 4: Aspect Based Sentiment Analysis},
|
23 |
+
author={ Pontiki, M. and D Galanis and Pavlopoulos, J. and Papageorgiou, H. and Manandhar, S. },
|
24 |
+
journal={Proceedings of International Workshop on Semantic Evaluation at},
|
25 |
+
year={2014},
|
26 |
+
}
|
27 |
+
"""
|
28 |
+
|
29 |
+
_LICENSE = """\
|
30 |
+
Please click on the homepage URL for license details.
|
31 |
+
"""
|
32 |
+
|
33 |
+
_DESCRIPTION = """\
|
34 |
+
A collection of SemEval2014 specifically designed to aid research in Aspect Based Sentiment Analysis.
|
35 |
+
"""
|
36 |
+
|
37 |
+
_CONFIG = [
|
38 |
+
|
39 |
+
# restaurants domain
|
40 |
+
"restaurants",
|
41 |
+
# laptops domain
|
42 |
+
"laptops",
|
43 |
+
]
|
44 |
+
|
45 |
+
_VERSION = "0.0.1"
|
46 |
+
|
47 |
+
_HOMEPAGE_URL = "https://alt.qcri.org/semeval2014/task4/index.php?id=data-and-tools"
|
48 |
+
_DOWNLOAD_URL = "https://raw.githubusercontent.com/YaxinCui/ABSADataset/main/SemEval2014Task4/{split}/{domain}_{split}.xml"
|
49 |
+
|
50 |
+
|
51 |
+
class SemEval2014Config(datasets.BuilderConfig):
|
52 |
+
"""BuilderConfig for SemEval2014Config."""
|
53 |
+
|
54 |
+
def __init__(self, _CONFIG, **kwargs):
|
55 |
+
super(SemEval2014Config, self).__init__(version=datasets.Version(_VERSION, ""), **kwargs),
|
56 |
+
self.configs = _CONFIG
|
57 |
+
|
58 |
+
|
59 |
+
class SemEval2014(datasets.GeneratorBasedBuilder):
|
60 |
+
"""The lingual Amazon Reviews Corpus"""
|
61 |
+
|
62 |
+
BUILDER_CONFIGS = [
|
63 |
+
SemEval2014Config(
|
64 |
+
name="All",
|
65 |
+
_CONFIG=_CONFIG,
|
66 |
+
description="A collection of SemEval2014 specifically designed to aid research in lingual Aspect Based Sentiment Analysis.",
|
67 |
+
)
|
68 |
+
] + [
|
69 |
+
SemEval2014Config(
|
70 |
+
name=config,
|
71 |
+
_CONFIG=[config],
|
72 |
+
description=f"{config} of SemEval2014 specifically designed to aid research in Aspect Based Sentiment Analysis",
|
73 |
+
)
|
74 |
+
for config in _CONFIG
|
75 |
+
]
|
76 |
+
|
77 |
+
BUILDER_CONFIG_CLASS = SemEval2014Config
|
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 |
+
'aspectTerms': [
|
86 |
+
{'from': datasets.Value(dtype='string'),
|
87 |
+
'polarity': datasets.Value(dtype='string'),
|
88 |
+
'term': datasets.Value(dtype='string'),
|
89 |
+
'to': datasets.Value(dtype='string')}
|
90 |
+
],
|
91 |
+
'aspectCategories': [
|
92 |
+
{'category': datasets.Value(dtype='string'),
|
93 |
+
'polarity': datasets.Value(dtype='string')}
|
94 |
+
],
|
95 |
+
'domain': datasets.Value(dtype='string'),
|
96 |
+
'sentenceId': datasets.Value(dtype='string')
|
97 |
+
}
|
98 |
+
),
|
99 |
+
supervised_keys=None,
|
100 |
+
license=_LICENSE,
|
101 |
+
homepage=_HOMEPAGE_URL,
|
102 |
+
citation=_CITATION,
|
103 |
+
)
|
104 |
+
|
105 |
+
def _split_generators(self, dl_manager):
|
106 |
+
|
107 |
+
train_urls = [_DOWNLOAD_URL.format(split="train", domain=config) for config in self.config.configs]
|
108 |
+
dev_urls = [_DOWNLOAD_URL.format(split="trial", domain=config) for config in self.config.configs]
|
109 |
+
test_urls = [_DOWNLOAD_URL.format(split="test", domain=config) for config in self.config.configs]
|
110 |
+
|
111 |
+
train_paths = dl_manager.download_and_extract(train_urls)
|
112 |
+
dev_paths = dl_manager.download_and_extract(dev_urls)
|
113 |
+
test_paths = dl_manager.download_and_extract(test_urls)
|
114 |
+
|
115 |
+
return [
|
116 |
+
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={"file_paths": train_paths, "domain_list": self.config.configs}),
|
117 |
+
datasets.SplitGenerator(name=datasets.Split.VALIDATION, gen_kwargs={"file_paths": dev_paths, "domain_list": self.config.configs}),
|
118 |
+
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"file_paths": test_paths, "domain_list": self.config.configs}),
|
119 |
+
]
|
120 |
+
|
121 |
+
def _generate_examples(self, file_paths, domain_list):
|
122 |
+
row_count = 0
|
123 |
+
assert len(file_paths)==len(domain_list)
|
124 |
+
|
125 |
+
for i in range(len(file_paths)):
|
126 |
+
file_path, domain = file_paths[i], domain_list[i]
|
127 |
+
semEvalDataset = SemEvalXMLDataset(file_path, domain)
|
128 |
+
|
129 |
+
for example in semEvalDataset.SentenceWithOpinions:
|
130 |
+
yield row_count, example
|
131 |
+
row_count += 1
|
132 |
+
|
133 |
+
from xml.dom.minidom import parse
|
134 |
+
|
135 |
+
class SemEvalXMLDataset():
|
136 |
+
def __init__(self, file_name, domain):
|
137 |
+
# 获得SentenceWithOpinions,一个List包含(reviewId, sentenceId, text, Opinions)
|
138 |
+
|
139 |
+
self.SentenceWithOpinions = []
|
140 |
+
self.xml_path = file_name
|
141 |
+
|
142 |
+
self.sentenceXmlList = parse(self.xml_path).getElementsByTagName('sentence')
|
143 |
+
|
144 |
+
for sentenceXml in self.sentenceXmlList:
|
145 |
+
|
146 |
+
sentenceId = sentenceXml.getAttribute("id")
|
147 |
+
if len(sentenceXml.getElementsByTagName("text")[0].childNodes) < 1:
|
148 |
+
# skip no reviews part
|
149 |
+
continue
|
150 |
+
text = sentenceXml.getElementsByTagName("text")[0].childNodes[0].nodeValue
|
151 |
+
|
152 |
+
aspectTermsXLMList = sentenceXml.getElementsByTagName("aspectTerm")
|
153 |
+
aspectTerms = []
|
154 |
+
for opinionXml in aspectTermsXLMList:
|
155 |
+
# some text maybe have no opinion
|
156 |
+
term = opinionXml.getAttribute("term")
|
157 |
+
polarity = opinionXml.getAttribute("polarity")
|
158 |
+
from_ = opinionXml.getAttribute("from")
|
159 |
+
to = opinionXml.getAttribute("to")
|
160 |
+
aspectTermDict = {
|
161 |
+
"term": term,
|
162 |
+
"polarity": polarity,
|
163 |
+
"from": from_,
|
164 |
+
"to": to
|
165 |
+
}
|
166 |
+
aspectTerms.append(aspectTermDict)
|
167 |
+
|
168 |
+
# 从小到大排序
|
169 |
+
aspectTerms.sort(key=lambda x: x["from"])
|
170 |
+
|
171 |
+
aspectCategoriesXmlList = sentenceXml.getElementsByTagName("aspectCategory")
|
172 |
+
aspectCategories = []
|
173 |
+
for aspectCategoryXml in aspectCategoriesXmlList:
|
174 |
+
category = aspectCategoryXml.getAttribute("category")
|
175 |
+
polarity = aspectCategoryXml.getAttribute("polarity")
|
176 |
+
aspectCategoryDict = {
|
177 |
+
"category": category,
|
178 |
+
"polarity": polarity
|
179 |
+
}
|
180 |
+
aspectCategories.append(aspectCategoryDict)
|
181 |
+
|
182 |
+
self.SentenceWithOpinions.append({
|
183 |
+
"text": text,
|
184 |
+
"aspectTerms": aspectTerms,
|
185 |
+
"aspectCategories": aspectCategories,
|
186 |
+
"domain": domain,
|
187 |
+
"sentenceId": sentenceId
|
188 |
+
}
|
189 |
+
)
|