2022.11.22 flaks项目开发-简单聊天机器人

介绍:

1.简单聊天机器人功能:
    1.在输入框中输入你的语句,机器人会自动回答你的问题
2.static文件夹保存静态文件
  templates文件夹保存html代码
  sample.py是程序源代码及程序入口
3.涉及到chatterbot,js,html,py

项目结构:
                              2022.11.22 flaks项目开发-简单聊天机器人_第1张图片

 知识点:
1.开发聊天机器人主要用到的是chatterbot包,安装应该选择1.0.1版本,最新版运行起来会报错

pip install chatterbot==1.0.1
pip install chatterbot-corpus
pip install scrapy

2.引入chatterbot并使用

from chatterbot import ChatBot
from chatterbot.trainers import ChatterBotCorpusTrainer

english_bot = ChatBot("Chatterbot", storage_adapter="chatterbot.storage.SQLStorageAdapter")
trainer = ChatterBotCorpusTrainer(english_bot)
trainer.train("chatterbot.corpus.english")

                创建了机器人对象,指定了数据库,指定了训练的语料包

3.与聊天机器人对话需要用到html及javascript

py代码(用来接收返回来的消息):

@app.route("/get")
def get_bot_response():
	userText = request.args.get("msg")
	return str(english_bot.get_response(userText))

html代码(用来在屏幕上显示消息并返回给py代码):
 

   function getBotResponse()
   {
        var rawText = $("#textInput").val();
        var userHtml = '

' + rawText + '

'; $("#textInput").val(""); $("#chatbox").append(userHtml); document.getElementById('userInput').scrollIntoView({block:'start',behaviour:'smooth'}); $.get("/get", {msg:rawText}).done(function(data){ var botHtml = '

' + data + '

'; $("#chatbox").append(botHtml); document.getElementById('userInput').scrollIntoView({block:'start', behaviour:'smooth'}); }); } $("#textInput").keypress(function(e){ if(e.which ==13){ getBotResponse();} }); $("#buttonInput").click(function() { getBotResponse(); });

                        .val()是获取数值,scrollintoview是显示在屏幕上,keypress是获取判断输入的                           键值

4.可以以自己创建yml文件的方式人为为机器人输入语料库

categories:
  - conversations
conversations:
- - Good Evening.
  - Good eveing.
- - Hello.
  - Hey how are you?
- - I am fine but feeling ·bored.
  - ohh you want to play a game?
- - no i want to see a movie.
  - ok shall i give your favouite move list?
- - yes.
  - okay done enjoy the movie.

问题:

1.出现了AttributeError: module 'chatterbot.storage' has no attribute 'SQLStorageAdapte'的错误,

        这里是拼错了,应该用chatterbot.storage.SQLStorageAdapter

2.出现AttributeError: module 'time' has no attribute 'clock,是因为py3.8以后就不支持time.clock方         法了,只能切换回3.7版本

3.OSError: [E050] Can't find model 'en'. It doesn't seem to be a shortcut link, a Python package or a valid path to a data directory.的错误
pycharm还安装不上
只能手动安装了

4.yaml.scanner.ScannerError: while scanning a simple key
  in "data/data.yml", line 3, column 1
could not find expected ':'
  in "data/data.yml", line 4, column 1
的错误
发现是yml语句中:是中文格式,所以错误,修改后继续执行文件

5.flask启动后一直在转圈
 * Restarting with stat
[nltk_data] Downloading package averaged_perceptron_tagger to
[nltk_data]     C:\Users\HP\AppData\Roaming\nltk_data...
[nltk_data]   Unzipping taggers\averaged_perceptron_tagger.zip.
[nltk_data] Downloading package punkt to
[nltk_data]     C:\Users\HP\AppData\Roaming\nltk_data...
[nltk_data] Error downloading 'punkt' from
pycharm卡住了,在控制台点一下enter健就好

6.chatterbot.chatterbot.ChatBot.ChatBotException: Either a statement object or a "text" keyword argument is required. Neither was provided.又爆了这个错误
是javascript语句没写对的原因

你可能感兴趣的:(机器人,python)