Python 实现简单智能聊天机器人攻略
介绍
智能聊天机器人是指能够理解人类语言并进行智能回复的计算机程序,是自然语言处理(NLP)和人工智能(AI)技术的应用之一。Python 作为一种流行的编程语言,在实现智能聊天机器人方面表现出色。
本攻略将介绍如何使用 Python 实现一个简单的智能聊天机器人。
步骤
- 安装所需的 Python packages。
pip install nltk
pip install numpy
pip install tensorflow
pip install tflearn
- 导入所需的 packages。
import nltk
from nltk.stem.lancaster import LancasterStemmer
import numpy as np
import tflearn
import tensorflow as tf
import random
import json
import pickle
- 准备数据
准备一份包含了智能聊天机器人可能会遇到的所有问题及对应回答的 JSON 文件,是实现智能聊天机器人的最重要的一步。
- 对数据进行预处理
使用NLTK(自然语言工具包)进行文本的预处理,去除无用词汇,将文本转换为词汇列表。在预处理之后,将列表转换成按照每个词的出现次数统计的向量。
stemmer = LancasterStemmer()
def tokenize(sentence):
return nltk.word_tokenize(sentence)
def stem(word):
return stemmer.stem(word.lower())
def bag_of_words(tokenized_sentence, words):
sentence_words = [stem(word) for word in tokenized_sentence]
bag = np.zeros(len(words), dtype=np.float32)
for idx, w in enumerate(words):
if w in sentence_words:
bag[idx] = 1.0
return bag
- 构建机器学习模型
在预处理完成的基础上,将数据集分成输入和输出,使用 TensorFlow 和 tflearn 库在数据集上训练模型,并使用 pickle 序列化保存模型。
with open("intents.json") as file:
data = json.load(file)
words = []
labels = []
docs_x = []
docs_y = []
for intent in data["intents"]:
for pattern in intent["patterns"]:
tokenized_sentence = tokenize(pattern)
docs_x.append(bag_of_words(tokenized_sentence, words))
docs_y.append(intent["tag"])
if intent["tag"] not in labels:
labels.append(intent["tag"])
words = [stem(word) for word in words]
words = sorted(list(set(words)))
labels = sorted(labels)
training_data = []
output_data = []
out_empty = [0 for _ in range(len(labels))]
for idx, x in enumerate(docs_x):
training_data.append(x)
output_data.append(out_empty.copy())
output_data[idx][labels.index(docs_y[idx])] = 1
training_data = np.array(training_data)
output_data = np.array(output_data)
tf.reset_default_graph()
net = tflearn.input_data(shape=[None, len(training_data[0])])
net = tflearn.fully_connected(net, 8)
net = tflearn.fully_connected(net, 8)
net = tflearn.fully_connected(net, len(output_data[0]), activation="softmax")
net = tflearn.regression(net)
model = tflearn.DNN(net)
model.fit(training_data, output_data, n_epoch=1000, batch_size=8, show_metric=True)
model.save("model.tflearn")
pickle.dump({"words": words, "labels": labels, "training_data": training_data, "output_data": output_data}, open("data.pickle", "wb"))
- 完整的聊天机器人代码样例
import nltk
from nltk.stem.lancaster import LancasterStemmer
import numpy as np
import tflearn
import tensorflow as tf
import random
import json
import pickle
stemmer = LancasterStemmer()
def tokenize(sentence):
return nltk.word_tokenize(sentence)
def stem(word):
return stemmer.stem(word.lower())
def bag_of_words(tokenized_sentence, words):
sentence_words = [stem(word) for word in tokenized_sentence]
bag = np.zeros(len(words), dtype=np.float32)
for idx, w in enumerate(words):
if w in sentence_words:
bag[idx] = 1.0
return bag
def chat():
with open("intents.json") as file:
data = json.load(file)
with open("data.pickle", "rb") as f:
words, labels, training_data, output_data = pickle.load(f)
tf.reset_default_graph()
net = tflearn.input_data(shape=[None, len(training_data[0])])
net = tflearn.fully_connected(net, 8)
net = tflearn.fully_connected(net, 8)
net = tflearn.fully_connected(net, len(output_data[0]), activation="softmax")
net = tflearn.regression(net)
model = tflearn.DNN(net)
model.load("model.tflearn")
print("Let's chat! Enter 'quit' to exit")
while True:
user_input = input("You: ")
if user_input.lower() == 'quit':
break
tokenized_input = tokenize(user_input)
bag = bag_of_words(tokenized_input, words)
results = model.predict([bag])[0]
results_index = np.argmax(results)
tag = labels[results_index]
if results[results_index] > 0.7:
for intent in data["intents"]:
if intent["tag"] == tag:
response = random.choice(intent["responses"])
print("Bot: ", response)
else:
print("Bot: I'm not quite sure what you mean")
此时,聊天机器人已经构建完成,你可以运行 chat()
函数进行测试。
示例
以下是一个测试机器人的例子:
Let's chat! Enter 'quit' to exit
You: Hi there!
Bot: Hiya!
You: Can you help me with my order?
Bot: Of course, can you give me your order number?
You: Oh, I don't have it with me right now.
Bot: Sorry, I need your order number to help you.
You: Can you tell me about the return policy?
Bot: Our return policy is 30 days from the date of purchase. What item are you looking to return?
You: Thank you!
Bot: You're welcome!
如上例所示,智能聊天机器人可以理解并回答用户提出的关于订单、退货政策等问题,过程中表现出了不同程度的智能。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Python 实现简单智能聊天机器人 - Python技术站