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.")