chatterbot(聊天机器人)库安装及实例测试

educative.io 有一门编程一小时课程,叫做 Build Your Own Chatbot in Python

课程简单介绍了AI的历史、机器人三定律、自然语言处理和AI的流行趋势,提供了AI calculator和house AI的运行环境。

但是,要运行自己的chatbot,还是需要安装chatterbot模块。(python版本:3.8.8 64-bit)

常规pip安装方法pip install chatterbot在安装spacy模块时出错;

spacy官网给出的conda方法:

conda install -c conda-forge spacy
python -m spacy download en_core_web_sm

依然无法解决问题。

StackOverflow上搜索有找到相关问题:

https://stackoverflow.com/questions/44925395/error-while-installing-chatterbot?r=SearchResults

方法1:pip install chatterbot==1.0.4

方法2:https://github.com/gunthercox/ChatterBot/archive/master.zip
下载源文件解压,cmd切换到该文件路径,使用python setup.py install,如果不行可尝试管理员权限。

我先尝试了方法2,成功安装1.1.0版本,但是在D:\anaconda3\Lib\site-packages 中找不到chatter文件夹,于是卸载重装了1.0.4版本。

运行ai_calculator.py,出现nltk_data找不到包的问题:

nltk官网有相关参考,文件可以在https://github.com/nltk/nltk_data/的packages里找,注意将各个压缩包解压放在以上searched in的任一文件夹下。

虽然一直存在nltk_data连接不上的问题

等了一会儿后,还是成功运行了:

另一个对话bot也是等了一会儿后成功运行:

附ai_calculator.py代码:

from chatterbot import ChatBot

# naming the ChatBot calculator
# using mathematical evaluation logic
# the calculator AI will not learn with the user input
Bot = ChatBot(name = 'Calculator',
                read_only = True,                  
                logic_adapters = ["chatterbot.logic.MathematicalEvaluation"],                 
                storage_adapter = "chatterbot.storage.SQLStorageAdapter")
    

# clear the screen and start the calculator
print('\033c')
print("Hello, I am a calculator. How may I help you?")
while (True):
    # take the input from the user
    user_input = input("me: ")
    
    # check if the user has typed quit to exit the prgram   
    if user_input.lower() == 'quit':
        print("Exiting")
        break

    # otherwise, evaluate the user input
    # print invalid input if the AI is unable to comprehend the input
    try:
        response = Bot.get_response(user_input)
        print("Calculator:", response)
    except:
        print("Calculator: Please enter valid input.")
posted @ 2021-06-12 22:42  ikventure  阅读(1987)  评论(0编辑  收藏  举报