-
Notifications
You must be signed in to change notification settings - Fork 2
/
data.py
379 lines (303 loc) · 12.2 KB
/
data.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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
from nltk import word_tokenize
from collections import Counter
from nltk.corpus import stopwords
import string
import math
from keras.preprocessing.text import text_to_word_sequence
import pandas as pd
import pickle
import numpy as np
import os
import csv
import xml.etree.ElementTree as ET
#import html
import HTMLParser
#from html.parser import HTMLParser
import re
import nltk
nltk.download('stopwords')
stop = set(stopwords.words('english'))
def load_embedding_file(embed_file_name, word_set):
''' loads embedding file and returns a dictionary (word -> embedding) for the words existing in the word_set '''
embeddings = {}
with open(embed_file_name, 'r') as embed_file:
for line in embed_file:
content = line.strip().split()
word = content[0]
if word in word_set:
embedding = np.array(content[1:], dtype=float)
embeddings[word] = embedding
return embeddings
def get_dataset_resources(data_file_name, sent_word2idx, target_word2idx, word_set, max_sent_len):
''' updates word2idx and word_set '''
if len(sent_word2idx) == 0:
sent_word2idx["<pad>"] = 0
tech_reviews, food_reviews = load_and_clean()
text = np.array(tech_reviews['text'])
aspects = np.array(tech_reviews['aspect_term'])
t_sentences = np.array(map(lambda x, y: replace_with_token(x, y), text, aspects))
word_count = []
sent_word_count = []
target_count = []
words = []
sentence_words = []
target_words = []
for i in range(t_sentences.shape[0]):
sentence = t_sentences[i]
target = aspects[i].lower()
sentence.replace("$t$", "")
sentence = sentence.lower()
sentence_splitted = text_to_word_sequence(sentence)
target_splitted = text_to_word_sequence(target)
max_sent_len = max(max_sent_len, len(sentence_splitted))
sentence_words.extend(sentence_splitted)
target_words.extend([target])
words.extend(sentence_splitted + target_splitted)
sent_word_count.extend(Counter(sentence_words).most_common())
target_count.extend(Counter(target_words).most_common())
word_count.extend(Counter(words).most_common())
for word, _ in sent_word_count:
if word not in sent_word2idx:
sent_word2idx[word] = len(sent_word2idx)
for target, _ in target_count:
if target not in target_word2idx:
target_word2idx[target] = len(target_word2idx)
for word, _ in word_count:
if word not in word_set:
word_set[word] = 1
print('resources calculation finished')
return max_sent_len
def get_embedding_matrix(embeddings, sent_word2idx, target_word2idx, edim):
''' returns the word and target embedding matrix '''
word_embed_matrix = np.zeros([len(sent_word2idx), edim], dtype=float)
target_embed_matrix = np.zeros([len(target_word2idx), edim], dtype=float)
for word in sent_word2idx:
if word in embeddings:
word_embed_matrix[sent_word2idx[word]] = embeddings[word]
for target in target_word2idx:
for word in target:
if word in embeddings:
target_embed_matrix[target_word2idx[target]] += embeddings[word]
target_embed_matrix[target_word2idx[target]] /= max(1, len(target.split()))
print(type(word_embed_matrix))
return word_embed_matrix, target_embed_matrix
def load_and_clean():
# read into pandas csv
tech_reviews = pd.read_csv('data/data_1_train.csv', quoting=csv.QUOTE_NONE, error_bad_lines=False, skipinitialspace=True)
food_reviews = pd.read_csv('data/data_2_train.csv', quoting=csv.QUOTE_NONE, error_bad_lines=False)
# rename columns to remove whitespaces
tech_reviews.columns = ['example_id', 'text', 'aspect_term', 'term_location', 'class']
food_reviews.columns = ['example_id', 'text', 'aspect_term', 'term_location', 'class']
# replace _ with whitespace and [comma] with ,
tech_reviews['text'] = tech_reviews['text'].str.replace('_ ', '')
food_reviews['text'] = food_reviews['text'].str.replace('_ ', '')
tech_reviews['text'] = tech_reviews['text'].str.replace("\[comma\]", ',')
food_reviews['text'] = food_reviews['text'].str.replace("\[comma\]", ',')
print('tech_reviews shape: ' + str(tech_reviews.shape))
print('food_reviews shape: ' + str(food_reviews.shape))
return tech_reviews, food_reviews
def replace_with_token(sentence, aspect):
s = sentence.replace(aspect, '$t$')
return s
def get_dataset(data_file_name, sent_word2idx, target_word2idx, embeddings, MODE):
''' returns the dataset'''
sentence_list = []
location_list = []
target_list = []
polarity_list = []
tech_reviews, food_reviews = load_and_clean()
text = np.array(tech_reviews['text'])
aspects = np.array(tech_reviews['aspect_term'])
polarities = np.array(tech_reviews['class'])
t_sentences = np.array(map(lambda x, y: replace_with_token(x, y), text, aspects))
target_error_counter = 0
lower_bound = 0
upper_bound = text.shape[0]
print('lower_bound: ' + str(lower_bound))
print('upper_bound: ' + str(upper_bound))
# for i in range(t_sentences.shape[0]):
for i in range(lower_bound, upper_bound):
sentence = t_sentences[i].lower()
target = aspects[i].lower()
polarity = polarities[i] + 1
sent_words = text_to_word_sequence(sentence)
target_words = text_to_word_sequence(target)
target_location = -1
for idx, s in enumerate(sent_words):
if s == 't':
target_location = idx
if target_location == -1:
print(sentence)
print(sent_words)
print(target)
print(target_words)
for idx, s in enumerate(sent_words):
target_temp = target_words[0]
print(sent_words.index(target_temp))
print('target_location = -1')
target_error_counter += 1
is_included_flag = 1
id_tokenised_sentence = []
location_tokenised_sentence = []
for index, word in enumerate(sent_words):
if word == 't':
continue
try:
word_index = sent_word2idx[word]
except:
print(word)
print("id not found for word in the sentence")
exit()
location_info = abs(index - target_location)
if word in embeddings:
id_tokenised_sentence.append(word_index)
location_tokenised_sentence.append(location_info)
is_included_flag = 0
for word in target_words:
if word in embeddings:
is_included_flag = 1
break
try:
target_index = target_word2idx[target]
except:
print(target)
print("id not found for target")
exit()
if not is_included_flag:
print(sentence)
continue
sentence_list.append(id_tokenised_sentence)
location_list.append(location_tokenised_sentence)
target_list.append(target_index)
polarity_list.append(polarity)
print('target_error_counter: ' + str(target_error_counter))
return sentence_list, location_list, target_list, polarity_list
# PREDICT FUNCTIONS
def get_dataset_resources_test(data_filename, sent_word2idx, target_word2idx, word_set, max_sent_len):
''' updates word2idx and word_set '''
if len(sent_word2idx) == 0:
sent_word2idx["<pad>"] = 0
test_data = load_and_clean_test(data_filename)
text = np.array(test_data['text'])
aspects = np.array(test_data['aspect_term'])
t_sentences = np.array(map(lambda x, y: replace_with_token(x, y), text, aspects))
word_count = []
sent_word_count = []
target_count = []
words = []
sentence_words = []
target_words = []
for i in range(t_sentences.shape[0]):
sentence = t_sentences[i]
target = aspects[i].lower()
sentence.replace("$t$", "")
sentence = sentence.lower()
sentence_splitted = text_to_word_sequence(sentence)
target_splitted = text_to_word_sequence(target)
sentence = ' '.join(text_to_word_sequence(sentence))
target = ' '.join(text_to_word_sequence(target))
max_sent_len = max(max_sent_len, len(sentence_splitted))
sentence_words.extend(sentence_splitted)
target_words.extend([target])
words.extend(sentence_splitted + target_splitted)
sent_word_count.extend(Counter(sentence_words).most_common())
target_count.extend(Counter(target_words).most_common())
word_count.extend(Counter(words).most_common())
for word, _ in sent_word_count:
if word not in sent_word2idx:
sent_word2idx[word] = len(sent_word2idx)
for target, _ in target_count:
if target not in target_word2idx:
target_word2idx[target] = len(target_word2idx)
for word, _ in word_count:
if word not in word_set:
word_set[word] = 1
return max_sent_len
def get_dataset_test(data_filename, sent_word2idx, target_word2idx, embeddings):
''' returns the dataset'''
sentence_list = []
location_list = []
target_list = []
# polarity_list = []
test_data = load_and_clean_test(data_filename)
text = np.array(test_data['text'])
aspects = np.array(test_data['aspect_term'])
# polarities = np.array(test_data['class']) # no classes!
t_sentences = np.array(map(lambda x, y: replace_with_token(x, y), text, aspects))
target_error_counter = 0
lower_bound = 0
upper_bound = text.shape[0]
print('lower_bound: ' + str(lower_bound))
print('upper_bound: ' + str(upper_bound))
for i in range(lower_bound, upper_bound):
sentence = t_sentences[i].lower()
target = aspects[i].lower()
# polarity = polarities[i] + 1
sent_words = text_to_word_sequence(sentence)
target_words = text_to_word_sequence(target)
sentence = ' '.join(text_to_word_sequence(sentence))
target = ' '.join(text_to_word_sequence(target))
target_location = -1
for idx, s in enumerate(sent_words):
if s == 't':
target_location = idx
if target_location == -1:
#print(sentence)
#print(sent_words)
#print(target)
#print(target_words)
for idx, s in enumerate(sent_words):
target_temp = target_words[0]
print(sent_words.index(target_temp))
print('target_location: -1')
target_error_counter += 1
is_included_flag = 1
id_tokenised_sentence = []
location_tokenised_sentence = []
for index, word in enumerate(sent_words):
if word == 't':
continue
try:
word_index = sent_word2idx[word]
except:
print(word)
print("id not found for word in the sentence TEST")
exit()
location_info = abs(index - target_location)
if word in embeddings:
id_tokenised_sentence.append(word_index)
location_tokenised_sentence.append(location_info)
# is_included_flag = 0
# for word in target_words:
# if word in embeddings:
# is_included_flag = 1
# break
try:
target_index = target_word2idx[target]
except:
print(target)
print("id not found for target TEST")
exit()
# if not is_included_flag:
# print(sentence)
# continue
sentence_list.append(id_tokenised_sentence)
location_list.append(location_tokenised_sentence)
target_list.append(target_index)
# polarity_list.append(polarity)
print('target_error_counter: ' + str(target_error_counter))
return (sentence_list, location_list, target_list), test_data
def load_and_clean_test(dataset):
if dataset == 'tech':
PATH = 'data_1_test.csv'
else:
PATH = 'data_2_test.csv'
# read into pandas csv
test_data = pd.read_csv('data/' + PATH, error_bad_lines=False, skipinitialspace=True)
# rename columns to remove whitespaces
test_data.columns = ['example_id', 'text', 'aspect_term', 'term_location']
# replace _ with whitespace and [comma] with ,
test_data['text'] = test_data['text'].str.replace('_ ', '')
test_data['text'] = test_data['text'].str.replace("\[comma\]", ',')
print('test_data shape: ' + str(test_data.shape))
return test_data