智能聊天机器人——“有问必答”
【注】该项目已开源,开源地址为:链接,代码更新可能不及时。
第2章、《模式一:问候模式》
主窗体的布局如下图所示:
共九种功能模式,最下方为关闭窗口按钮。
点击问候模式按钮,进入问候模式:
下方为聊天窗口,点击发送按钮发送消息给聊天机器人。
示例问题:
1、My name is John.
2、我的名字是balabala。
3、Hi/Hello/Hey.
4、你好。
5、How are you?
6、你怎么样?
7、What is your name?
8、你的名字是什么?
9、歪比巴布
模块相关python代码
"""
greeting_bot_gui.py
问候模式的聊天机器人实现 Chatbot implementation in greeting mode
使用图形用户接口 Using the Graphical User Interface
作者:李 奕辰 Author: Yichen Li
"""
from PyQt5 import QtGui
# 导入相关模块 Import related modules
from common_trans_pairs import *
from nltk.chat.util import Chat, reflections
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QLabel, QTextEdit, QPushButton
# Greeting Mode 问候模式
# Define some pairs of input patterns and responses 定义一些输入问答对
pairs_greeting = [
# 1、用户(“我”)的名字
[
r"(.*)[Mm]y [Nn]ame [Ii]s(.*)", # 适配短语my name is(我的名字是)
["Hello %1, how can I assist you today?"]
],
[
"(.*)" + s_my + s_name + "(.*)", # 适配短语my name is(我的名字是)
["您好,今天我可以为您提供什么帮助?"]
],
# 2、你好
[
r"[Hh]i|[Hh]ello|[Hh]ey(.*)", # 适配单词Hi, Hello, Hey
["Hello!", "Hi there!", "Hey! How can I help you?"]
],
[
s_hello + "(.*)", # 适配单词Hi, Hello, Hey
["你好。", "别来无恙。", "嘿!我能帮你什么忙吗?"]
],
# 3、(你)怎么样
[
r"(.*)[Hh]ow [Aa]re [Yy]ou(.*)?", # 提取'how are you'(你怎么样),适配大小写习惯
[
"I'm just a bunch of python code, but thanks for asking!\n",
"Doing well, how about you?\n"
]
],
[
"(.*)" + s_how + "(.*)", # 提取'how are you'(你怎么样),适配大小写习惯
[
"我只是一堆python代码,但还是感谢您的询问!",
"还好吧,你呢?"]
],
# 4、用户询问chatbot名字
[
r"(.*)[Yy]our [Nn]ame(.*)", # 提取词组 your name(你的名字)
["I am a chatbot created using NLTK!\n"]
],
# TODO: 中文输入的问题?(已解决)
[
"(.*)" + s_your + "(.*)" + s_name + "(.*)",
["我是使用 NLTK 创建的聊天机器人。"]
],
# 5、戴夫问好
[
s_dave,
["歪比巴布,ruarourou"]
],
# 6、退出操作
[
r"quit",
["Bye! Take care.", "Goodbye!"]
],
[
s_quit,
["再见,保重。", "回见。"]
],
# 7、其它输入
[
r"(.*)",
["I'm not sure I understand what you mean. Can you rephrase that?\n"
"我不太明白你的意思。你能重新表述一下吗?"]
],
]
# Create a greeting chatbot instance 问候模式聊天机器人实例
chatbot_G = Chat(pairs_greeting, reflections)
class ChatbotApp_G(QWidget):
def __init__(self):
super().__init__()
# 字体设置
font = QtGui.QFont()
font.setFamily("Arial") # 括号里可以设置成自己想要的其它字体
font.setPointSize(18) # 括号里的数字可以设置成自己想要的字体大小
self.setWindowTitle("Chatbot(Greeting Mode) 【问候模式】")
self.setGeometry(100, 100, 400, 300)
# self.setFont()
# self.font().Bold()
self.layout = QVBoxLayout()
self.setFont(font)
self.chat_area = QTextEdit(self)
self.chat_area.setReadOnly(True)
self.chat_area.setText(
"In greeting mode, I can have a simple conversation with you, including greetings, small talk, etc.\n"
"在问候模式下,我可以和你进行简单的对话,包括问候、寒喧等等。\n")
self.layout.addWidget(self.chat_area)
self.input_area = QTextEdit(self)
self.layout.addWidget(self.input_area)
self.send_button = QPushButton("Send 发送", self)
self.send_button.clicked.connect(self.send_message)
self.layout.addWidget(self.send_button)
self.setLayout(self.layout)
def send_message(self):
user_input = self.input_area.toPlainText().strip()
if user_input:
self.chat_area.append("You (用户): " + user_input)
response = chatbot_G.respond(user_input)
self.chat_area.append("Greeting Bot (问候模式聊天机器人): " + response)
self.input_area.clear()
# if __name__ == "__main__":
def greeting_bot_start():
app = QApplication(sys.argv)
window = ChatbotApp_G()
window.show()
sys.exit(app.exec_())
在后续实践中发现,以下这个文件其实是多余的,但统一起见,使用这种翻译对的方法:
"""
文件名:common_trans_pairs.py
定义了一些常见翻译对 Some common translation pairs are defined
均使用utf-8编码 All use utf-8 encoding
作者:李 奕辰 Author: Yichen Li
"""
s_name = "名字"
s_name.encode('utf-8')
s_quit = "退出"
s_quit.encode('utf-8')
s_my = "我的"
s_my.encode('utf-8')
s_your = "你的"
s_your.encode('utf-8')
s_you = "你"
s_you.encode('utf-8')
s_hello = "你好"
s_hello.encode('utf-8')
s_how = "怎么样"
s_how.encode('utf-8')
s_tell = "讲"
s_tell.encode('utf-8')
s_joke = "笑话"
s_joke.encode('utf-8')
s_interesting = "有趣的"
s_interesting.encode('utf-8')
s_scary = "恐怖的"
s_scary.encode('utf-8')
s_story = "故事"
s_story.encode('utf-8')
s_dave = "歪比巴布"
s_dave.encode('utf-8')
s_con = "概念"
s_con.encode('utf-8')
s_fun = "职能"
s_fun.encode('utf-8')
s_char = "特性"
s_char.encode('utf-8')
s_theo = "理论"
s_theo.encode('utf-8')
s_ggcf = "改革春风吹满地"
s_ggcf.encode('utf-8')
s_dec = "决策"
s_dec.encode('utf-8')
s_met = "方法"
s_met.encode('utf-8')
s_plan = "计划"
s_plan.encode('utf-8')
s_for = "公式"
s_for.encode('utf-8')
s_func = "函数"
s_func.encode('utf-8')
s_org = "组织"
s_org.encode('utf-8')
s_str = "结构"
s_str.encode('utf-8')
s_lead = "领导"
s_lead.encode('utf-8')
s_use = "作用"
s_use.encode('utf-8')
s_mean = "含义"
s_mean.encode('utf-8')
s_mic = "微观"
s_mic.encode('utf-8')
s_mac = "宏观"
s_mic.encode('utf-8')
s_debt = "负债"
s_debt.encode('utf-8')
s_bus = "业务"
s_bus.encode('utf-8')
s_type = "风格类型"
s_type.encode('utf-8')
s_acc = "会计"
s_acc.encode('utf-8')
s_heart = "失恋"
s_heart.encode('utf-8')
s_comf = "安慰"
s_comf.encode('utf-8')
s_beau = "美丽的"
s_beau.encode('utf-8')
s_sentence = "句子"
s_sentence.encode('utf-8')
(持续更新中)