溫馨提示×

使用python實現機器人聊天功能

養魚的貓咪
186
2021-03-08 10:36:15
欄目: 編程語言

使用python實現機器人聊天功能

利用python實現一個機器人聊天功能,具體方法如下:

import aiml

import sys

import os

def get_module_dir(name):

print("module", sys.modules[name])

path = getattr(sys.modules[name], 'file', None)

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對象

alice.learn("startup.xml")

alice.respond('LOAD ALICE')

while True:

message = input("Enter your message >> ")

if("exit" == message):

exit()

response = alice.respond(message) # 機器人應答

print(response)

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女