
作者:Shivashish Thkaur
pip install tensorflow keras pickle nltk
NLU(自然语言理解):机器理解人类语言的能力。
NLG(自然语言生成):机器生成类似于人类书面句子的文本的能力。
Train_chatbot.py——在这个文件里,我们将构建和训练深度学习模型,该模型可以分类和识别用户对聊天机器人的要求。
Gui_Chatbot.py——这个文件是用来构建图形用户界面、与聊天机器人聊天的。
Intents.json——这个意图文件有我们即将用于训练模型的所有数据,它包含一批标签及其相应的模式和响应。
Chatbot_model.h5——这是一个分层数据格式文件,其中存储了训练模型的权重和架构。
Classes.pkl——pickle文件可用于存储所有的标签名,以便对我们预测消息进行分类。
Words.pkl——这个文件包含模型的词汇表中所有的特有词语(unique words)。
步骤1. 导入库并加载数据
1import numpy as np
2from keras.models import Sequential
3from keras.layers import Dense, Activation, Dropout
4from keras.optimizers import SGD
5import random
6
7import nltk
8from nltk.stem import WordNetLemmatizer
9lemmatizer = WordNetLemmatizer()
10import json
11import pickle
12
13intents_file = open('intents.json').read()
14intents = json.loads(intents_file)
步骤2. 预处理数据
1words=[]
2classes = []
3documents = []
4ignore_letters = ['!', '?', ',', '.']
5
6for intent in intents['intents']:
7 for pattern in intent['patterns']:
8 #tokenize each word
9 word = nltk.word_tokenize(pattern)
10 words.extend(word)
11 #add documents in the corpus
12 documents.append((word, intent['tag']))
13 # add to our classes list
14 if intent['tag'] not in classes:
15 classes.append(intent['tag'])
16
17print(documents)
1# lemmaztize and lower each word and remove duplicates
2words = [lemmatizer.lemmatize(w.lower()) for w in words if w not in ignore_letters]
3words = sorted(list(set(words)))
4# sort classes
5classes = sorted(list(set(classes)))
6# documents = combination between patterns and intents
7print (len(documents), "documents")
8# classes = intents
9print (len(classes), "classes", classes)
10# words = all words, vocabulary
11print (len(words), "unique lemmatized words", words)
12
13pickle.dump(words,open('words.pkl','wb'))
14pickle.dump(classes,open('classes.pkl','wb'))
步骤3. 创建训练和测试数据
1# create the training data
2training = []
3# create empty array for the output
4output_empty = [0] * len(classes)
5# training set, bag of words for every sentence
6for doc in documents:
7 # initializing bag of words
8 bag = []
9 # list of tokenized words for the pattern
10 word_patterns = doc[0]
11 # lemmatize each word - create base word, in attempt to represent related words
12 word_patterns = [lemmatizer.lemmatize(word.lower()) for word in word_patterns]
13 # create the bag of words array with 1, if word is found in current pattern
14 for word in words:
15 bag.append(1) if word in word_patterns else bag.append(0)
16
17 # output is a '0' for each tag and '1' for current tag (for each pattern)
18 output_row = list(output_empty)
19 output_row[classes.index(doc[1])] = 1
20 training.append([bag, output_row])
21# shuffle the features and make numpy array
22random.shuffle(training)
23training = np.array(training)
24# create training and testing lists. X - patterns, Y - intents
25train_x = list(training[:,0])
26train_y = list(training[:,1])
27print("Training data is created")
步骤4. 训练模型
1# deep neural networds model
2model = Sequential()
3model.add(Dense(128, input_shape=(len(train_x[0]),), activation='relu'))
4model.add(Dropout(0.5))
5model.add(Dense(64, activation='relu'))
6model.add(Dropout(0.5))
7model.add(Dense(len(train_y[0]), activation='softmax'))
8
9# Compiling model. SGD with Nesterov accelerated gradient gives good results for this model
10sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
11model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
12
13#Training and saving the model
14hist = model.fit(np.array(train_x), np.array(train_y), epochs=200, batch_size=5, verbose=1)
15model.save('chatbot_model.h5', hist)
16
17print("model is created")
步骤5. 与聊天机器人交互
Python
1import nltk
2from nltk.stem import WordNetLemmatizer
3lemmatizer = WordNetLemmatizer()
4import pickle
5import numpy as np
6
7from keras.models import load_model
8model = load_model('chatbot_model.h5')
9import json
10import random
11intents = json.loads(open('intents.json').read())
12words = pickle.load(open('words.pkl','rb'))
13classes = pickle.load(open('classes.pkl','rb'))
14
15def clean_up_sentence(sentence):
16 # tokenize the pattern - splitting words into array
17 sentence_words = nltk.word_tokenize(sentence)
18 # stemming every word - reducing to base form
19 sentence_words = [lemmatizer.lemmatize(word.lower()) for word in sentence_words]
20 return sentence_words
21# return bag of words array: 0 or 1 for words that exist in sentence
22
23def bag_of_words(sentence, words, show_details=True):
24 # tokenizing patterns
25 sentence_words = clean_up_sentence(sentence)
26 # bag of words - vocabulary matrix
27 bag = [0]*len(words)
28 for s in sentence_words:
29 for i,word in enumerate(words):
30 if word == s:
31 # assign 1 if current word is in the vocabulary position
32 bag[i] = 1
33 if show_details:
34 print ("found in bag: %s" % word)
35 return(np.array(bag))
36
37def predict_class(sentence):
38 # filter below threshold predictions
39 p = bag_of_words(sentence, words,show_details=False)
40 res = model.predict(np.array([p]))[0]
41 ERROR_THRESHOLD = 0.25
42 results = [[i,r] for i,r in enumerate(res) if r>ERROR_THRESHOLD]
43 # sorting strength probability
44 results.sort(key=lambda x: x[1], reverse=True)
45 return_list = []
46 for r in results:
47 return_list.append({"intent": classes[r[0]], "probability": str(r[1])})
48 return return_list
49
50def getResponse(ints, intents_json):
51 tag = ints[0]['intent']
52 list_of_intents = intents_json['intents']
53 for i in list_of_intents:
54 if(i['tag']== tag):
55 result = random.choice(i['responses'])
56 break
57 return result
58
59#Creating tkinter GUI
60import tkinter
61from tkinter import *
62
63def send():
64 msg = EntryBox.get("1.0",'end-1c').strip()
65 EntryBox.delete("0.0",END)
66
67 if msg != '':
68 ChatBox.config(state=NORMAL)
69 ChatBox.insert(END, "You: " + msg + '\n\n')
70 ChatBox.config(foreground="#446665", font=("Verdana", 12 ))
71
72 ints = predict_class(msg)
73 res = getResponse(ints, intents)
74
75 ChatBox.insert(END, "Bot: " + res + '\n\n')
76
77 ChatBox.config(state=DISABLED)
78 ChatBox.yview(END)
79
80root = Tk()
81root.title("Chatbot")
82root.geometry("400x500")
83root.resizable(width=FALSE, height=FALSE)
84
85#Create Chat window
86ChatBox = Text(root, bd=0, bg="white", height="8", width="50", font="Arial",)
87
88ChatBox.config(state=DISABLED)
89
90#Bind scrollbar to Chat window
91scrollbar = Scrollbar(root, command=ChatBox.yview, cursor="heart")
92ChatBox['yscrollcommand'] = scrollbar.set
93
94#Create Button to send message
95SendButton = Button(root, font=("Verdana",12,'bold'), text="Send", width="12", height=5,
96 bd=0, bg="#f9a602", activebackground="#3c9d9b",fg='#000000',
97 command= send )
98
99#Create the box to enter message
100EntryBox = Text(root, bd=0, bg="white",width="29", height="5", font="Arial")
101#EntryBox.bind("<Return>", send)
102
103#Place all components on the screen
104scrollbar.place(x=376,y=6, height=386)
105ChatBox.place(x=6,y=6, height=386, width=370)
106EntryBox.place(x=128, y=401, height=90, width=265)
107SendButton.place(x=6, y=401, height=90)
108
109root.mainloop()

