-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprojectlib.py
executable file
·508 lines (369 loc) · 15.7 KB
/
projectlib.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
"""
Library for CharCNN Project
Jann Goschenhofer
Feb 20. 2018
"""
import scipy.io
import pandas
import numpy as np
import collections
import csv
import random
from keras.utils.np_utils import to_categorical
import keras
"""
Classic check function
"""
def check():
print("projectlib loaded fuji")
"""
convert text to matrix of character strings
input: text as string, alphabet as string, amount of chars per observation (paper: 1014)
output: ndarray with dimension (#alphabet, #maxchars, 1), aka a matrix
"""
def generate_one_hot(text, alphabet, maxChars):
# initialize empty ndarray with zeros only and depth 1
textRep = np.zeros(shape=(len(alphabet), maxChars, 1))
# cut text to maxChars
if len(text) > maxChars:
text = text[0:maxChars]
# loop over all chars in the text
for char_index in range(0, len(text)):
if text[char_index] in alphabet:
alpha_index = alphabet.find(text[char_index])
# rows = alphabet features, columns = characters
textRep[alpha_index][char_index][0] = 1
# in case of whitespace or unknown characters include 0-vector
# do nothing
return(textRep)
##########
########## encode review in embedding
##########
# helper function to get embedding vector for word
def getVec(word, embedding):
return embedding.loc[word].as_matrix()
def generate_glove_embed(text, embeddingPath, maxWords= 100):
embedding = pandas.read_table(embeddingPath, sep=" ", index_col=0, header=None, quoting=csv.QUOTE_NONE)
maxWords = 100
# initialize empty ndarray with zeros only and depth 1
textRep = np.zeros(shape=(embedding.shape[1], maxWords, 1))
# cut text into words
textList = text.split()
# cut words after
if len(textList) > maxWords:
textList = textList[0:maxWords]
# store vector for each word
for wordIndex in range(0, len(textList)):
# use only words that are in the embedding
if textList[wordIndex] in embedding.index:
textRep[:, wordIndex, 0] = getVec(word = textList[wordIndex], embedding = embedding)
# rescale to [0, 1]
min = embedding.values.min()
max = embedding.values.max()
textRep = (textRep - min) / (np.diff([min, max])[0])
return textRep
##########
########## Decode one hot encoding
##########
def decoder(onehotText, alphabet, maxChars = 1014):
# initialize string
# onehotText = encoded
a = str()
for colIndex in range(0, maxChars):
# only store alphabet index, if 1 is in column
if np.isin(1, onehotText[:, colIndex, 0]):
alphaIndex = np.where(onehotText[:, colIndex, 0] == 1)[0][0]
# check print
# print(colIndex, alphaIndex)
a = a + alphabet[alphaIndex]
# else add blank spacre
else:
a = a + " "
return(a)
##########
########## Decode one hot encoding for 2dim objects
##########
#TODO: integrate in above function!
def decoder2dim(onehotText, alphabet, maxChars = 1014):
# initialize string
# onehotText = encoded
a = str()
for colIndex in range(0, maxChars):
# only store alphabet index, if 1 is in column
if np.isin(1, onehotText[:, colIndex]):
alphaIndex = np.where(onehotText[:, colIndex] == 1)[0][0]
# check print
# print(colIndex, alphaIndex)
a = a + alphabet[alphaIndex]
# else add blank spacre
else:
a = a + " "
return(a)
##############
############## Encode user written review life
##############
def encodeReview(textInput, path_alphabet, maxChars):
data = pandas.DataFrame(pandas.Series(textInput))
alphabet = open(path_alphabet).read()
# generate data Matrix
#reviews = data.iloc[0:amountData, 0]
reviewRep = np.zeros(shape=(1, (maxChars * len(alphabet)), 1))
# stor.shape
for i in range(len(reviews)):
reviewRep[i, :, 0] = generate_one_hot(text=reviews[i],
alphabet=alphabet,
maxChars=maxChars).reshape(-1, len(alphabet) * maxChars, order="F")
# generate label vector
labels = data.iloc[0:amountData, 1]
return(reviewRep[:, :, 0], labels)
##############
############## Function that returns matrix representation and sentiment for x reviews
##############
def buildSet(path_data, path_alphabet, maxChars, amountData, batchStart = 0):
if batchStart != 0:
data = pandas.concat(pandas.read_csv(filepath_or_buffer=path_data, skiprows=batchStart, chunksize=amountData),
ignore_index=True)
else:
data = pandas.read_csv(filepath_or_buffer=path_data)
alphabet = open(path_alphabet).read()
# generate data Matrix
reviews = data.iloc[0:amountData, 0]
reviewRep = np.zeros(shape=(len(reviews), (maxChars * len(alphabet)), 1))
# stor.shape
for i in range(len(reviews)):
reviewRep[i, :, 0] = generate_one_hot(text=reviews[i],
alphabet=alphabet,
maxChars=maxChars).reshape(-1, len(alphabet) * maxChars, order="F")
# generate label vector
labels = data.iloc[0:amountData, 1]
return(reviewRep[:, :, 0], labels)
############## BUILD TEST DATA SET
############## Function that returns matrix representation and sentiment for x reviews FROM already read array
##############
# needed in the Data Generator
# Difference: outputs data in reshaped shape and readable for keras
# amount data is not needed anymore
# uses skiprows to skip the reading of some rows
def buildSetTest(path_data, path_alphabet, maxChars, skiprows = 0, amountData = 1000):
data = pandas.read_csv(filepath_or_buffer=path_data, skiprows=skiprows)
alphabet = open(path_alphabet).read()
lenAlpha = len(alphabet)
# generate data Matrix#
# amount data to work with
reviews = data.iloc[0:amountData, 0]
#print(len(reviews))
reviewRep = np.zeros(shape=(len(reviews), (maxChars * len(alphabet)), 1))
# stor.shape
for i in range(len(reviews)):
reviewRep[i, :, 0] = generate_one_hot(text=reviews[i],
alphabet=alphabet,
maxChars=maxChars).reshape(-1, len(alphabet) * maxChars, order="F")
# generate label vector
# hacky but ok
# TODO check error with dimensions!!
labels = data.iloc[0:amountData, 1]#
# return(reviewRep[:, :, 0].reshape(-1, lenAlpha, maxChars, 1), labels)
return(reviewRep[:, :, 0].reshape(-1, maxChars, lenAlpha), labels)
##############
############## Function that returns matrix representation and sentiment for x reviews FROM already read array
##############
# needed in the Data Generator
# Difference: outputs data in reshaped shape
# amount data is not needed anymore
# uses skiprows to skip the reading of some rows
def buildSetDG(path_data, path_alphabet, maxChars, skiprows = 0):
data = pandas.read_csv(filepath_or_buffer=path_data, skiprows=skiprows)
alphabet = open(path_alphabet).read()
lenAlpha = len(alphabet)
# generate data Matrix
reviews = data.iloc[:, 0]
reviewRep = np.zeros(shape=(len(reviews), (maxChars * len(alphabet)), 1))
# stor.shape
for i in range(len(reviews)):
reviewRep[i, :, 0] = generate_one_hot(text=reviews[i],
alphabet=alphabet,
maxChars=maxChars).reshape(-1, len(alphabet) * maxChars, order="F")
# generate label vector
labels = to_categorical(data.iloc[:, 1])
# changed for 2 net
return(reviewRep[:, :, 0].reshape(-1, maxChars, lenAlpha), labels)
##############
############## Function that returns matrix representation and sentiment for x reviews FROM already read array
##############
# needed in the Data Generator
# Difference: outputs data in reshaped shape
# amount data is not needed anymore
# uses skiprows to skip the reading of some rows
def buildSetDGGlove(path_data, embeddingPath, dimEmbed = 50, maxWords = 100, skiprows = 0):
data = pandas.read_csv(filepath_or_buffer=path_data, skiprows=skiprows)
# generate data Matrix
reviews = data.iloc[:, 0]
reviewRep = np.zeros(shape=(len(reviews), (maxWords * dimEmbed), 1))
# stor.shape
for i in range(len(reviews)):
reviewRep[i, :, 0] = generate_glove_embed(text=reviews[i],
embeddingPath=embeddingPath,
maxWords = maxWords).reshape(-1, dimEmbed * maxWords, order="F")
# generate label vector
labels = to_categorical(data.iloc[:, 1])
print("i build set")
# changed for 2 net
return(reviewRep[:, :, 0].reshape(-1, maxWords, dimEmbed), labels)
##############
############## Data Generator Object Class
##############
# excellent blog post: https://stanford.edu/~shervine/blog/keras-how-to-generate-data-on-the-fly.html
# adjusted a lot to pre-build buildSetDG
# we need shape (trainSize, lenAlpha, maxChars, 1) for input X
# dim_x = lenAlpha, dim_y = maxChars, batch_size = batch_size
# we do not have a dim z as we use 1-dimensional images
class DataGeneratorGlove(object):
# intitialize the whole vehicle
def __init__(self, dim_x = 32, dim_y = 32, batch_size = 32, shuffle = True, path_data = "bla", embeddingPath = "bla",
maxWords = 100, allIDs = range(0, 2)):
'Initialization'
self.dim_x = dim_x
self.dim_y = dim_y
self.batch_size = batch_size
self.shuffle = shuffle
self.path_data = path_data
self.embeddingPath = embeddingPath
self.maxWords = maxWords
self.allIDs = allIDs
# again: we do not need labels as opposed to the blog post
# listIDs are given to that homie during the model.fit_generator() call
def generate(self, list_IDs):
'Generates batches of samples'
# Infinite loop
while 1:
# Generate order of exploration of dataset
# to add randomness
indexes = self.__get_exploration_order(list_IDs)
# Generate batches
imax = int(len(indexes)/self.batch_size)
for i in range(imax):
# Find list of IDs
list_IDs_temp = [list_IDs[k] for k in indexes[i*self.batch_size:(i+1)*self.batch_size]]
# Generate data
X, y = self.__data_generation(list_IDs_temp)
yield X, y
# changes order of indices in the batches if shuffle is set true
# add randomness to data
def __get_exploration_order(self, list_IDs):
'Generates order of exploration'
# Find exploration order
indexes = np.arange(len(list_IDs))
if self.shuffle == True:
np.random.shuffle(indexes)
return indexes
# generates batches
# only need IDs
# IDs are beforehand shuffled by __get_exploration_order()
# we do not need the labels, automatically extracted by DG build
def __data_generation(self, list_IDs_temp):
'Generates data of batch_size samples'
# Generate data
# that is the FUNKY PART
# read only the lines from the review thingy that match the current list_IDs_temp
# aka skip all lines that do not match
# use np.setdiff1d to find the skip-IDs
deselectIDs = np.setdiff1d(self.allIDs, list_IDs_temp)
# check printer
# print("I start generating a data set")
X, y = buildSetDGGlove(path_data=self.path_data, embeddingPath=self.embeddingPath,
maxWords=self.maxWords, skiprows=deselectIDs)
print("i generate")
# return X, sparsify(y)
return X, y
##############
############## Data Generator Object Class
##############
# excellent blog post: https://stanford.edu/~shervine/blog/keras-how-to-generate-data-on-the-fly.html
# adjusted a lot to pre-build buildSetDG
# we need shape (trainSize, lenAlpha, maxChars, 1) for input X
# dim_x = lenAlpha, dim_y = maxChars, batch_size = batch_size
# we do not have a dim z as we use 1-dimensional images
class DataGenerator(object):
# intitialize the whole vehicle
def __init__(self, dim_x = 32, dim_y = 32, batch_size = 32, shuffle = True, path_data = "bla", path_alphabet = "bla",
maxChars = 1014, allIDs = range(0, 2)):
'Initialization'
self.dim_x = dim_x
self.dim_y = dim_y
self.batch_size = batch_size
self.shuffle = shuffle
self.path_data = path_data
self.path_alphabet = path_alphabet
self.maxChars = maxChars
self.allIDs = allIDs
# again: we do not need labels as opposed to the blog post
# listIDs are given to that homie during the model.fit_generator() call
def generate(self, list_IDs):
'Generates batches of samples'
# Infinite loop
while 1:
# Generate order of exploration of dataset
# to add randomness
indexes = self.__get_exploration_order(list_IDs)
# Generate batches
imax = int(len(indexes)/self.batch_size)
for i in range(imax):
# Find list of IDs
list_IDs_temp = [list_IDs[k] for k in indexes[i*self.batch_size:(i+1)*self.batch_size]]
# Generate data
X, y = self.__data_generation(list_IDs_temp)
yield X, y
# changes order of indices in the batches if shuffle is set true
# add randomness to data
def __get_exploration_order(self, list_IDs):
'Generates order of exploration'
# Find exploration order
indexes = np.arange(len(list_IDs))
if self.shuffle == True:
np.random.shuffle(indexes)
return indexes
# generates batches
# only need IDs
# IDs are beforehand shuffled by __get_exploration_order()
# we do not need the labels, automatically extracted by DG build
def __data_generation(self, list_IDs_temp):
'Generates data of batch_size samples'
# Generate data
# that is the FUNKY PART
# read only the lines from the review thingy that match the current list_IDs_temp
# aka skip all lines that do not match
# use np.setdiff1d to find the skip-IDs
deselectIDs = np.setdiff1d(self.allIDs, list_IDs_temp)
# check printer
# print("I start generating a data set")
X, y = buildSetDG(path_data=self.path_data, path_alphabet=self.path_alphabet,
maxChars=self.maxChars, skiprows=deselectIDs)
# return X, sparsify(y)
return X, y
##############
############## predicttion Wrapper for LIME
##############
def predictFromText(textInputList):
# basically same code as in detect_review
alphabetPath = "../alphabet.txt"
alphabet = open(alphabetPath).read()
maxChars = 1014
model = keras.models.load_model('../charCnn8Huge.h5')
# catch single string inputs and convert them to list
if textInputList.__class__ != list:
print("caught single string")
print(textInputList)
textInputList = [textInputList]
print(textInputList)
# list for predictions
predStorage = []
# loop through input list and predict
for textInput in textInputList:
print(textInput)
recodeText = generate_one_hot(text=textInput, alphabet=alphabet, maxChars=maxChars)
pred = model.predict(recodeText.transpose())
# control output of function
# print(str(textInput), "\n", pred)
predStorage.append(pred)
# convert to dxk ndarray
return (np.hstack(predStorage).reshape(-1, 2))