-
Notifications
You must be signed in to change notification settings - Fork 0
/
model_functs.py
49 lines (35 loc) · 1.47 KB
/
model_functs.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
from keras.preprocessing.sequence import pad_sequences
import pickle
from tqdm import tqdm
import re
with open('tokenizers/shakes_tokenizer.pickle', 'rb') as handle:
tokenizer = pickle.load(handle)
def generate_text(seed_text, next_words, model, max_sequence_len):
for _ in tqdm(range(next_words)):
token_list = tokenizer.texts_to_sequences([seed_text])[0]
token_list = pad_sequences([token_list], maxlen=max_sequence_len-1, padding='pre')
predicted = model.predict_classes(token_list, verbose=0)
output_word = ""
for word,index in tokenizer.word_index.items():
if index == predicted:
output_word = word
break
seed_text += " "+output_word
return seed_text.title()
def suggestions(seed_text, model, max_sequence_len):
generated=''
while True:
token_list = tokenizer.texts_to_sequences([seed_text])[0]
token_list = pad_sequences([token_list], maxlen=max_sequence_len-1, padding='pre')
predicted = model.predict_classes(token_list, verbose=0)
output_word = ""
for word,index in tokenizer.word_index.items():
if index == predicted:
output_word = word
break
seed_text += " "+output_word
generated+=' '+output_word
if(len(generated.split(' '))>5):
if re.search('[^a-zA-Z|^\s]',generated)!=None:
break
return generated