-
Notifications
You must be signed in to change notification settings - Fork 0
/
size.py
192 lines (118 loc) · 4.4 KB
/
size.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
from keras.models import Sequential
from keras.layers import LSTM, Dropout, Dense
from nltk import word_tokenize as tokenize
from gensim.models import KeyedVectors
import numpy as np
def load_model(path):
timesteps = 200
neurons = 300
batch_size = 64
epochs = 25
model = Sequential()
# model.add(LSTM(200, input_shape=(timesteps, neurons), return_sequences=True))
# model.add(Dropout(.2))
# model.add(Dense(2, activation='softmax'))
# model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
model.add(LSTM(200, input_shape=(timesteps, neurons), return_sequences=True))
model.add(Dropout(.2))
model.add(LSTM(200, return_sequences=True))
model.add(Dropout(.2))
model.add(Dense(2, activation='softmax'))
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
model.load_weights(path)
return model
def load_overall_model(path):
timesteps = 200
neurons = 300
batch_size = 64
epochs = 25
model = Sequential()
model.add(LSTM(200, input_shape=(timesteps, neurons), return_sequences=False))
model.add(Dropout(.2))
model.add(Dense(2, activation='softmax'))
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy'])
model.load_weights(path)
return model
a = """
Play Penalty Shootout and feel the thrill at the mark!Are you an ice cold goal getter and anticipating goalkeeper? Find out in this thrilling game of penalty shootouts, where you can become the matchwinner or the big loser!Right, left or straight throught the middle - it's your decision and your chanceto win the duel. It's not just a random corner, it's your opponents decision where to shoot or where to dive for the ball. Take up the challenge and play against real opponents! Winning games frequently? Get it on and show everybody that you are the best penalty scorer and the quickest goalkeeper of all times! Eternalize your name in the highscores! Play Penalty Shootout by dutyfarm.com
"""
def load_w2v():
_fname = "GoogleNews-vectors-negative300.bin"
w2vModel = KeyedVectors.load_word2vec_format(_fname, binary=True)
return w2vModel
w2v = load_w2v()
def prepare_text(text):
tokens = tokenize(text)
original = []
filteredTokens = []
for x in tokens:
if x in w2v.vocab:
filteredTokens.append(x)
original.append(0.0)
else:
original.append(None)
# for word in filteredTokens:
#
#
# output.append(w2v.word_vec(word))
output = list(map(lambda word: w2v.word_vec(word), filteredTokens))
if len(output) > 200:
output = output[:200]
else:
while len(output) < 200:
output.append([0.0] * 300)
return np.array([output]), original
def fix_holes(original, predictions):
predictions = predictions[0]
pred_index = 0
for i, spot in enumerate(original):
if spot is None:
original[i] = float((predictions[pred_index-1][0] + predictions[pred_index][0])/2)
else:
original[i] = float(predictions[pred_index][0])
pred_index += 1
return original
outs = []
model = load_model('weights4.h5')
def prepare_tokens(tokens):
original = []
filteredTokens = []
for x in tokens:
if x in w2v.vocab:
filteredTokens.append(x)
original.append(0.0)
else:
original.append(None)
# for word in filteredTokens:
#
#
# output.append(w2v.word_vec(word))
output = list(map(lambda word: w2v.word_vec(word), filteredTokens))
if len(output) > 200:
output = output[:200]
else:
while len(output) < 200:
output.append([0.0] * 300)
return np.array([output]), original
#overall_model = load_overall_model('weights4.h5')
def avg(array):
total = 0
count = 0
for i in array:
total += i
count += 1
return total/count
def predict(text):
tokens = tokenize(text)
i = 0
prep, origin = prepare_tokens(tokens[i:i+200])
pred = model.predict(prep)
origin = fix_holes(origin, pred)
while i*200 < len(origin):
p, o = prepare_tokens(tokens[i:i+200])
i += 1
pred = model.predict(p)
o = fix_holes(o, pred)
origin = origin + o
return origin, avg(origin)
#print(prepare_text(a), model.predict(pa))