python英文聊天机器人(亲测可用)
生活随笔
收集整理的這篇文章主要介紹了
python英文聊天机器人(亲测可用)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
python英文聊天機器人(親測可用)
本文參考https://www.cnblogs.com/youcong/p/10462924.html
第一步:
下載所需包
pip install aiml出現Successfully installed aiml-0.9.2即安裝成功
第二步:
編寫代碼
導入包:
import sys import os import aiml獲得語料庫目錄:
def _get_module_dir(name):if os.environ.get('show-info', False):print('module', sys.modules[name])path = getattr(sys.modules[name], '__file__', None)if os.environ.get('show-info', False):print(path)if not path:raise AttributeError(f'module {name} has not attribute __file__')return os.path.dirname(os.path.abspath(path))創建機器人:
_alice_path = _get_module_dir('aiml') + '\\botdata\\alice' os.chdir(_alice_path) _alice = aiml.Kernel() _alice.learn("startup.xml") _alice.respond('LOAD ALICE')定義聊天函數:
def english_chat(message: str):return _alice.respond(message)第三步:
如果你按上代碼執行,可能看到如下起飛(我已經起飛)
報錯信息是說time包不支持clock函數了(我的python是3.8)
解決方案:
找到Kernel.py,我的Kernel.py在
F:\anaconda3\lib\site-packages\aiml下。將第335行
改為:
start = time.time()然后把第356行
print( "done (%.2f seconds)" % (time.clock() - start) )改為:
print( "done (%.2f seconds)" % (time.time() - start) )最后附上全部代碼:
import sys import os import warnings try:import aiml except (AttributeError, ImportError):print('''If you want to use english chat robot, please update lib aiml's Kernel.py line 335 "start = time.time()" to: "start = time.clock()" and line 356 "print( "done (%.2f seconds)" % (time.clock() - start) )" to: "print("done (%.2f seconds)" % (time.time() - start))"''')warnings.warn('aiml module is wrong')sys.exit(1)def _get_module_dir(name):if os.environ.get('show-info', False):print('module', sys.modules[name])path = getattr(sys.modules[name], '__file__', None)if os.environ.get('show-info', False):print(path)if not path:raise AttributeError('module %s has not attribute __file__' % name)return os.path.dirname(os.path.abspath(path))_alice_path = _get_module_dir('aiml') + '\\botdata\\alice' os.chdir(_alice_path) _alice = aiml.Kernel() _alice.learn("startup.xml") _alice.respond('LOAD ALICE')def english_chat(message: str):return _alice.respond(message)if __name__ == '__main__':while True:msg = input('Please input, input q to quit: ')if msg == 'q':sys.exit(0)print('Robot:', english_chat(msg))總結
以上是生活随笔為你收集整理的python英文聊天机器人(亲测可用)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 猜水果—Java
- 下一篇: 加载磊科336usb无线网卡驱动到s3c