-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasetclass.py
332 lines (277 loc) · 13.3 KB
/
datasetclass.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
# -*- coding: utf-8 -*-
"""
Created on Fri Feb 24 12:20:00 2017
@author: csten_000
"""
import matplotlib.pyplot as plt
from scipy.spatial.distance import cosine
import numpy as np
import os
import collections
class thedataset(object):
def __init__(self, trainx, trainy, testx, testy, validx, validy, lookup, uplook, count):
self.trainreviews = trainx
self.traintargets = trainy
self.testreviews = testx
self.testtargets = testy
self.validreviews = validx
self.validtargets = validy
self.lookup = lookup
self.uplook = uplook
self.ohnum = count+1 #len(lookup)
def add_wordvectors(self, wordvecs):
self.wordvecs = wordvecs
def showstringlenghts(self, whichones, percentage, printstuff): #if percentage is 1, its the maxlen of the entire dataset
lens = []
if whichones[0]:
for i in self.trainreviews:
lens.append(len(i))
if whichones[1]:
for i in self.testreviews:
lens.append(len(i))
if whichones[2]:
for i in self.validreviews:
lens.append(len(i))
bins = np.arange(0, 1001, 50) #bins = np.arange(0, max(lens), 75)
if printstuff:
plt.xlim([min(lens)-5, 1000+5]) #plt.xlim([min(lens)-5, max(lens)+5])
plt.hist(lens, bins=bins, alpha=0.5)
plt.title('Lenghts of the strings')
plt.show()
lens.sort()
return lens[(round(len(lens)*percentage))-1]
#TODO: vielleicht nicht mittem im satz brechen lassen?
def shortendata(self, whichones, percentage, lohnenderstring, printstuff, embedding_size):
maxlen = self.showstringlenghts(whichones,percentage,printstuff) #75% of data has a maxlength of 312, soo...
if printstuff:
print("Shortening the Strings...")
print("Max.length: ",self.showstringlenghts(whichones,1,False))
print("Amount: ", len(self.trainreviews) if whichones[0] else 0 + len(self.testreviews) if whichones[1] else 0 + len(self.validreviews) if whichones[2] else 0)
if whichones[0]:
i = 0
while True:
if len(self.trainreviews[i]) > maxlen:
if len(self.trainreviews[i][maxlen+1:]) > lohnenderstring:
self.trainreviews.append(self.trainreviews[i][maxlen+1:])
self.traintargets.append(self.traintargets[i])
self.trainreviews[i] = self.trainreviews[i][:maxlen]
if len(self.trainreviews[i]) < lohnenderstring:
del(self.trainreviews[i])
del(self.traintargets[i])
else: #wichtig! wenn er es löscht, hat das danach jetzt den bereits genutzten index -> don't increase!
i = i+1
if i >= len(self.trainreviews):
break
if whichones[1]:
i = 0
while True:
if len(self.testreviews[i]) > maxlen:
if len(self.testreviews[i][maxlen+1:]) > lohnenderstring:
self.testreviews.append(self.testreviews[i][maxlen+1:])
self.testtargets.append(self.testtargets[i])
self.testreviews[i] = self.testreviews[i][:maxlen]
if len(self.testreviews[i]) < lohnenderstring:
del(self.testreviews[i])
del(self.testtargets[i])
else: #wichtig! wenn er es löscht, hat das danach jetzt den bereits genutzten index -> don't increase!
i = i+1
if i >= len(self.testreviews):
break
if whichones[2]:
i = 0
while True:
if len(self.validreviews[i]) > maxlen:
if len(self.validreviews[i][maxlen+1:]) > lohnenderstring:
self.validreviews.append(self.validreviews[i][maxlen+1:])
self.validtargets.append(self.validtargets[i])
self.validreviews[i] = self.validreviews[i][:maxlen]
if len(self.validreviews[i]) < lohnenderstring:
del(self.validreviews[i])
del(self.validtargets[i])
else: #wichtig! wenn er es löscht, hat das danach jetzt den bereits genutzten index -> don't increase!
i = i+1
if i >= len(self.validreviews):
break
if printstuff:
print("to.....")
print(self.showstringlenghts(whichones,percentage,True))
print("Amount: ", len(self.trainreviews) if whichones[0] else 0 + len(self.testreviews) if whichones[1] else 0 + len(self.validreviews) if whichones[2] else 0)
print("to.....")
if whichones[0]:
for i in range(len(self.trainreviews)):
if len(self.trainreviews[i]) < maxlen:
diff = maxlen - len(self.trainreviews[i])
self.trainreviews[i].extend([self.ohnum]*diff)
if whichones[1]:
for i in range(len(self.testreviews)):
if len(self.testreviews[i]) < maxlen:
diff = maxlen - len(self.testreviews[i])
self.testreviews[i].extend([self.ohnum]*diff)
if whichones[2]:
for i in range(len(self.validreviews)):
if len(self.validreviews[i]) < maxlen:
diff = maxlen - len(self.validreviews[i])
self.validreviews[i].extend([self.ohnum]*diff)
if printstuff: print(self.showstringlenghts(whichones,percentage,True))
try:
self.lookup["<END>"]
except KeyError:
self.lookup["<END>"] = self.ohnum
self.uplook[self.ohnum] = "<END>"
self.ohnum += 1 #nur falls noch kein end-token drin ist+1!
if hasattr(self, 'wordvecs'): #falls man es NACH word2vec ausführt
self.wordvecs = np.append(self.wordvecs,np.transpose(np.transpose([[0]*embedding_size])),axis=0)
self.maxlenstring = maxlen
return maxlen
def closeones(self, indices):
for i in indices:
top_k = 5 # number of nearest neighbors
dists = np.zeros(self.wordvecs.shape[0])
for j in range(len(self.wordvecs)):
dists[j] = cosine(self.wordvecs[i],self.wordvecs[j])
dists[i] = float('inf')
clos = np.argsort(dists)[:top_k]
return [self.uplook[i] for i in clos]
def printcloseones(self, word):
print("Close to '",word.replace(" ",""),"': ",self.closeones([self.lookup[word]]))
def prepareback(self, str):
str = str.replace(" <comma>", ",")
str = str.replace(" <colon>", ":")
str = str.replace(" <openBracket>", "(")
str = str.replace(" <closeBracket>", ")")
str = str.replace(" <dots>", "...")
str = str.replace(" <dot>", ".")
str = str.replace(" <semicolon>", ";")
str = str.replace("<quote>", '"')
str = str.replace(" <question>", "?")
str = str.replace(" <exclamation>", "!")
str = str.replace(" <hyphen> ","-")
str = str.replace(" <END>", "")
str = str.replace(" <SuperQuestion>", "???")
str = str.replace(" <SuperExclamation>", "!!!")
return str
def showarating(self,number):
array = [self.uplook[i] for i in self.trainreviews[number]]
str = ' '.join(array)
str = self.prepareback(str)
return str
def read_words_as_id(self, where): #pendant to _file_to_word_ids
result = []
for string in where:
for word in string:
result.append(word)
result.append("1")
return result
def read_words(self, where): #pendant to _read_words
result = []
for string in where:
for word in string:
result.append(self.uplook[word])
result.append("<EOS>")
return result
def return_all(self, only_positive=False): #pendant to ptb_raw_data
if only_positive:
tmptr = [self.trainreviews[i] for i in range(len(self.traintargets)) if self.traintargets[i]==1]
trains = self.read_words_as_id(tmptr)
tmpte = [self.testreviews[i] for i in range(len(self.testtargets)) if self.testtargets[i]==1]
tests = self.read_words_as_id(tmpte)
tmpva = [self.validreviews[i] for i in range(len(self.validtargets)) if self.validtargets[i]==1]
valids = self.read_words_as_id(tmpva)
else:
trains = self.read_words_as_id(self.trainreviews)
tests = self.read_words_as_id(self.testreviews)
valids = self.read_words_as_id(self.validreviews)
return trains, tests, valids, self.ohnum
def build_vocab(self): #pendant to _build_vocab
return self.lookup
#TODO: es kann sein dass das hier in der selben reihenfolge sein muss wie die zahlen!
def get_vocab(self): #pendant to get_vocab
return list(self.lookup.keys())
def grammar_iterator(self, raw_data, batch_size, num_steps):
raw_data = np.array(raw_data, dtype=np.int32)
data_len = len(raw_data)
batch_len = data_len // batch_size
data = np.zeros([batch_size, batch_len], dtype=np.int32)
for i in range(batch_size):
data[i] = raw_data[batch_len * i:batch_len * (i + 1)]
epoch_size = (batch_len - 1) // num_steps
if epoch_size == 0:
raise ValueError("epoch_size == 0, decrease batch_size or num_steps")
for i in range(epoch_size):
x = data[:, i*num_steps:(i+1)*num_steps]
y = data[:, i*num_steps+1:(i+1)*num_steps+1]
yield (x, y)
## first we create, save & load the words as indices.
def make_dataset(whichsets = [True, True, True], config=None):
assert os.path.exists(config.setpath)
allwords = {}
wordcount = 2
datasets = [config.TRAINNAME, config.TESTNAME, config.VALIDATIONNAME]
#first we look how often each word occurs, to delete single occurences.
for currset in range(3):
if whichsets[currset]:
with open(config.setpath+datasets[currset]+".txt", encoding="utf8") as infile:
string = []
for line in infile:
words = line.split()
for word in words:
string.append(word)
#now we delete single occurences.
count = []
count2 = []
count.extend(collections.Counter(string).most_common(999999999))
for elem in count:
if elem[1] > 1:
count2.append(elem[0])
print("Most common words:")
print(count[0:5])
#now we make a dictionary, mapping words to their indices
for currset in range(3):
if whichsets[currset]:
with open(config.setpath+datasets[currset]+".txt", encoding="utf8") as infile:
for line in infile:
words = line.split()
for word in words:
if not word in allwords:
if word in count2: #words that only occur once don't count.
allwords[word] = wordcount
wordcount = wordcount +1
#print(allwords)
#the token for single occurences is "<UNK>", the one for end of sentence (if needed) is reserved to 1
allwords["<UNK>"] = 0
allwords["<EOS>"] = 1
reverse_dictionary = dict(zip(allwords.values(), allwords.keys()))
#now we make every ratings-string to an array of the respective numbers.
ratings = [[],[],[]]
for currset in range(3):
if whichsets[currset]:
with open(config.setpath+datasets[currset]+".txt", encoding="utf8") as infile:
for line in infile:
words = line.split()
currentrating = []
if len(words) > 1:
for word in words:
try:
currentrating.append(allwords[word])
except KeyError:
currentrating.append(allwords["<UNK>"])
ratings[currset].append(currentrating)
#also, we make an array of the ratings
ratetargets = [[],[],[]]
for currset in range(3):
if whichsets[currset]:
with open(config.setpath+datasets[currset]+"-target.txt", encoding="utf8") as infile:
for line in infile:
if config.is_for_trump:
ratetargets[currset].append(int(line))
else:
if int(line) < 5:
ratetargets[currset].append(0)
else:
ratetargets[currset].append(1)
#we made a dataset! :)
datset = thedataset(ratings[0], ratetargets[0],
ratings[1], ratetargets[1],
ratings[2], ratetargets[2],
allwords, reverse_dictionary, wordcount)
return datset