-
Notifications
You must be signed in to change notification settings - Fork 2
/
chatbot.py
154 lines (123 loc) · 5.38 KB
/
chatbot.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
from keras.models import Model
from keras.layers import Input, LSTM, Dense, Embedding
from keras.preprocessing.sequence import pad_sequences
import numpy as np
import nltk
import os
import sys
import zipfile
import urllib
HIDDEN_UNITS = 256
WHITELIST = 'abcdefghijklmnopqrstuvwxyz1234567890?.,'
GLOVE_EMBEDDING_SIZE = 100
GLOVE_MODEL = "glove_model/glove.6B.100d.txt"
DATA_SET_NAME = 'gunthercox'
def in_white_list(_word):
for char in _word:
if char in WHITELIST:
return True
return False
def reporthook(block_num, block_size, total_size):
read_so_far = block_num * block_size
if total_size > 0:
percent = read_so_far * 1e2 / total_size
s = "\r%5.1f%% %*d / %d" % (
percent, len(str(total_size)), read_so_far, total_size)
sys.stderr.write(s)
if read_so_far >= total_size: # near the end
sys.stderr.write("\n")
else: # total size is unknown
sys.stderr.write("read %d\n" % (read_so_far,))
def load_glove():
word2em = {}
# file = open(GLOVE_MODEL, mode='rt', encoding='utf8')
file = open(GLOVE_MODEL, mode='rt')
for line in file:
words = line.strip().split()
word = words[0]
embeds = np.array(words[1:], dtype=np.float32)
word2em[word] = embeds
file.close()
return word2em
class GunthercoxWordGloveChatBot(object):
model = None
encoder_model = None
decoder_model = None
target_word2idx = None
target_idx2word = None
max_decoder_seq_length = None
max_encoder_seq_length = None
num_decoder_tokens = None
word2em = None
def __init__(self):
self.word2em = load_glove()
print(len(self.word2em))
print(self.word2em['start'])
self.target_word2idx = np.load(
'models/word-glove-target-word2idx.npy').item()
self.target_idx2word = np.load(
'models/word-glove-target-idx2word.npy').item()
context = np.load('models/word-glove-context.npy').item()
self.max_encoder_seq_length = context['encoder_max_seq_length']
self.max_decoder_seq_length = context['decoder_max_seq_length']
self.num_decoder_tokens = context['num_decoder_tokens']
encoder_inputs = Input(shape=(None, GLOVE_EMBEDDING_SIZE), name='encoder_inputs')
encoder_lstm = LSTM(units=HIDDEN_UNITS, return_state=True, name="encoder_lstm")
encoder_outputs, encoder_state_h, encoder_state_c = encoder_lstm(encoder_inputs)
encoder_states = [encoder_state_h, encoder_state_c]
decoder_inputs = Input(shape=(None, GLOVE_EMBEDDING_SIZE), name='decoder_inputs')
decoder_lstm = LSTM(units=HIDDEN_UNITS, return_sequences=True, return_state=True, name='decoder_lstm')
decoder_outputs, _, _ = decoder_lstm(decoder_inputs, initial_state=encoder_states)
decoder_dense = Dense(self.num_decoder_tokens, activation='softmax', name='decoder_dense')
decoder_outputs = decoder_dense(decoder_outputs)
self.model = Model([encoder_inputs, decoder_inputs], decoder_outputs)
self.model.load_weights('models/word-glove-weights.h5')
self.model.compile(optimizer='rmsprop', loss='categorical_crossentropy')
self.encoder_model = Model(encoder_inputs, encoder_states)
decoder_state_inputs = [Input(shape=(HIDDEN_UNITS,)), Input(shape=(HIDDEN_UNITS,))]
decoder_outputs, state_h, state_c = decoder_lstm(decoder_inputs, initial_state=decoder_state_inputs)
decoder_states = [state_h, state_c]
decoder_outputs = decoder_dense(decoder_outputs)
self.decoder_model = Model([decoder_inputs] + decoder_state_inputs, [decoder_outputs] + decoder_states)
def reply(self, input_text):
input_seq = []
input_emb = []
for word in nltk.word_tokenize(input_text.lower()):
if not in_white_list(word):
continue
emb = np.zeros(shape=GLOVE_EMBEDDING_SIZE)
if word in self.word2em:
emb = self.word2em[word]
input_emb.append(emb)
input_seq.append(input_emb)
input_seq = pad_sequences(input_seq, self.max_encoder_seq_length)
states_value = self.encoder_model.predict(input_seq)
target_seq = np.zeros((1, 1, GLOVE_EMBEDDING_SIZE))
target_seq[0, 0, :] = self.word2em['start']
target_text = ''
target_text_len = 0
terminated = False
while not terminated:
output_tokens, h, c = self.decoder_model.predict([target_seq] + states_value)
sample_token_idx = np.argmax(output_tokens[0, -1, :])
sample_word = self.target_idx2word[sample_token_idx]
target_text_len += 1
if sample_word != 'start' and sample_word != 'end':
target_text += ' ' + sample_word
if sample_word == 'end' or target_text_len >= self.max_decoder_seq_length:
terminated = True
target_seq = np.zeros((1, 1, GLOVE_EMBEDDING_SIZE))
if sample_word in self.word2em:
target_seq[0, 0, :] = self.word2em[sample_word]
states_value = [h, c]
return target_text.strip()
def interactive(self):
user_chat = ''
while user_chat != 'exit':
user_chat = input('You : ')
print('Bot : ', self.reply(user_chat))
def main():
model = GunthercoxWordGloveChatBot()
model.interactive()
if __name__ == '__main__':
main()