-
Notifications
You must be signed in to change notification settings - Fork 25
/
chatbot.py
55 lines (40 loc) · 1.36 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
### pip3 install nltk
import nltk
from nltk.stem.lancaster import LancasterStemmer
stemmer = LancasterStemmer()
import numpy
import tensorflow as tf
from tensorflow.keras import layers, models
import random
import json
import pickle
with open("chatbot_intents.json") as file:
data = json.load(file)
with open("chatbot_data.pickle", "rb") as f:
words, labels, training, output = pickle.load(f)
# Load Model
model = models.load_model('models/chatbot_dnn.h5')
def bag_of_words(s, words):
bag = [0 for _ in range(len(words))]
s_words = nltk.word_tokenize(s)
s_words = [stemmer.stem(word.lower()) for word in s_words]
for se in s_words:
for i, w in enumerate(words):
if w == se:
bag[i] = 1
return numpy.array(bag)
def chat():
print("Start talking with the bot (type quit to stop)!")
while True:
inp = input("You: ")
if inp.lower() == "quit":
break
results = model.predict(numpy.array([bag_of_words(inp, words)]))
print('confidence: '+str(numpy.max(results)*100))
results_index = numpy.argmax(results)
tag = labels[results_index]
for tg in data["intents"]:
if tg['tag'] == tag:
responses = tg['responses']
print(random.choice(responses))
chat()