-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle_data_SB1.py
311 lines (228 loc) · 8.41 KB
/
handle_data_SB1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# -*- coding: utf-8 -*-
import xml.etree.ElementTree as ET
import numpy as np
import itertools
import collections
import string
import unicodedata
import sys
# Fix absolute path
data_file = '/Users/mrkwse/Documents/University/NLPR/OA/Data/ABSA16_Laptops_Train_SB1_v2.xml'
def load_data(data_file):
tree = ET.parse(data_file)
root = tree.getroot()
input_text = []
output_labels = []
meta = {'max_word_count': 0, 'max_string_length': 0}
# Review = Review in root = Reviews
for review in root:
# sentence_data = []
#
for sentence in review.findall('sentences/sentence'):
for text in sentence.findall('text'):
input_text.append(text.text)
if len(text.text) > meta['max_string_length']:
meta['max_string_length'] = len(text.text)
if len(text.text.split(' ')) > meta['max_word_count']:
meta['max_word_count'] = len(text.text.split(' '))
if len(sentence.findall('Opinions/Opinion')) == 0:
output_labels.append([["NULL#NULL"]])
else:
labels = []
for opinion in sentence.findall('Opinions/Opinion'):
op_atts = []
op_atts.append(opinion.attrib['category'])
op_atts.append(opinion.attrib['polarity'])
labels.append(op_atts)
output_labels.append(labels)
return [input_text, output_labels, meta]
def remove_outlying_labels(output_labels):
# pruned_labels = output_labels
label_list = {"OTHER#OTHER": 0}
for element in output_labels:
for quality in element:
if quality[0] in label_list:
label_list[quality[0]] += 1
else:
label_list[quality[0]] = 1
# print label_list
xx = 0
while xx < len(output_labels):
yy = 0
while yy < len(output_labels[xx]):
if label_list[output_labels[xx][yy][0]] < 20:
output_labels[xx][yy][0] = "OTHER#OTHER"
yy += 1
xx += 1
return output_labels
def binary_labels(output_labels, return_index=False, label_list=None):
"""
Format label data to be binary arrays.
"""
# Populate label list if required, otherwise input is used (e.g. for
# evaluationd data to follow same format as training)
if label_list == None:
label_list = ["OTHER#OTHER"]
for element in output_labels:
for quality in element:
if quality[0] not in label_list and quality[0] != "NULL#NULL":
label_list.append(quality[0])
labels_binary = []
empty_label = []
for element in label_list:
empty_label.append(0)
# TODO: Array of single aspect variable arrays.
for sentence in output_labels:
labels_binary.append(empty_label[:])
for aspect in sentence:
if aspect[0] in label_list:
labels_binary[-1][label_list.index(aspect[0])] = 1
elif aspect[0] == "NULL#NULL":
labels_binary[-1] = empty_label[:]
else:
labels_binary[-1][label_list.index("OTHER#OTHER")] = 1
# label_index[quality[0]] = label_index['max'] + 1
# label_index['max'] += 1
# labels_binary[-1][label_index[quality[0]]] = 1
if return_index:
# label list acts as a lookup incase of printing classification results
return np.array(labels_binary), label_list
else:
return np.array(labels_binary)
def binary_sentiment(output_labels, return_index=False):
sentiment_index = ['positive', 'conflict', 'negative']
binary_sentiment = []
empty_label = [0, 0, 0]
for element in output_labels:
binary_sentiment.append(empty_label[:])
for example in element:
if example[1] in sentiment_index:
binary_sentiment[-1][sentiment_index.index(example[1])] = 1
else:
raise Exception('Mysterious 4th sentiment class')
if return_index:
return np.array(binary_sentiment), sentiment_index
else:
return np.array(binary_sentiment)
def binary_combined(output_labels, return_index=False):
binary_array = []
# Setup sentiment index and empty array
sentiment_index = ['positive', 'negative', 'other']
binary_labels = []
empty_sentiment = [0, 0, 0]
# Setup aspect index and empty array
label_list = []
for element in output_labels:
for quality in element:
if quality[0] not in label_list:
label_list.append(quality[0])
labels_binary = []
empty_label = []
for element in label_list:
empty_label.append(0)
combined_empty = [empty_label[:], empty_sentiment[:]]
for review in output_labels:
element = []
for aspect in review:
example = [empty_label[:], empty_sentiment[:]]
# Probably if/except these
example[0][label_list.index(aspect[0])] = 1
if aspect[1] == 'neutral' or 'conflict':
example[1][sentiment_index.index('other')] = 1
else:
example[1][sentiment_index.index(aspect[1])] = 1
element.append(example)
binary_array.append(element)
# z = np.array(binary_array)
# print z.shape
return np.array(binary_array)
# def binary_eval(output_labels, label_list):
def return_batches(data, batch_size, num_epochs, shuffle=True):
data = np.array(data)
data_size = len(data)
num_batches_per_epoch = int((len(data)-1)/batch_size) + 1
for epoch in range(num_epochs):
if shuffle:
shuffle_indices = np.random.permutation(np.arange(data_size))
shuffled_data = data[shuffle_indices]
else:
shuffled_data = data
for batch_num in range(num_batches_per_epoch):
start_index = batch_num * batch_size
end_index = min((batch_num + 1) * batch_size, data_size)
yield shuffled_data[start_index:end_index]
# http://stackoverflow.com/questions/34293875/how-to-remove-punctuation-marks-from-a-string-in-python-3-x-using-translate
translator = str.maketrans('','', string.punctuation)
def word_lists(text):
output = []
for sentence in text:
sentence = sentence.replace(u'\xa0', u' ')
output.append(sentence.translate(translator).split(' '))
return output
def vocabulary_transform(text, max_length=None):
words = word_lists(text)
word_counts = collections.Counter(itertools.chain(*words))
vocabulary = [x[0] for x in word_counts.most_common()]
vocabulary = list(sorted(vocabulary))
vocabulary.append('</NULL>')
# vocabulary = {x: i for i, x in enumerate(vocabulary_inv)}
#
# max_i = max(vocabulary[x] for x in vocabulary)
# vocabulary['</NULL>'] = max_i + 1
return [vocabulary]
# FIXME TODO FIXME TODO FIXME TODO PADDING
def build_input_data(sentences, vocabulary, meta, pad=True):
training_data = []
for sentence in sentences:
sen_data = []
sentence = sentence.replace(u'\xa0', u' ')
sentence = sentence.translate(translator)
for word in sentence.split(' '):
if word in vocabulary:
sen_data.append(vocabulary.index(word))
else:
sen_data.append(vocabulary.index('</NULL>'))
yy = len(sentence.split(' '))
while yy < meta['max_word_count']:
sen_data.append(vocabulary.index('</NULL>'))
yy += 1
training_data.append(sen_data)
# training_data = np.array([[vocabulary[word] for word in sentence] for sentence in sentences])
return np.array(training_data)
# x, y = load_data(data_file)
#
# alt_labels(y)
# x,y,z = load_data(data_file)
# print y
# print remove_outlying_labels(y)
#
# yi = binary_labels(y)
#
# # print yi
# print len(x)
# print len(y)
# print len(yi)
#
# binary_combined(y)
### FIXME
# if 0:
# print(input_text)
#
# print(output_labels)
#
# print(count_label)
# print(count_text)
#
# print(max_length)
#
#
# for key, value in sorted(label_count.iteritems(), key=lambda (k,v): (v,k)):
# print "%s: %s" % (key, value)
#
#
# for key, value in sorted(cat_count.iteritems(), key=lambda (k,v): (v,k)):
# print "%s: %s" % (key, value)
#
#
# for key, value in sorted(sub_type.iteritems(), key=lambda (k,v): (v,k)):
# print "%s: %s" % (key, value)