日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python可以帮机器人编程吗_Python如何实现机器人聊天

發(fā)布時間:2023/12/2 python 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python可以帮机器人编程吗_Python如何实现机器人聊天 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

今天午休的時候,無意之中看了一篇博客,名字叫Python實現(xiàn)機器人,感覺挺有的意思的。

于是用其寫了一個簡單的Python聊天,源碼如下所示:

# -*- coding: utf-8 -*-

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() # 創(chuàng)建機器人alice對象

alice.learn("startup.xml") # 加載...\\botdata\\alice\\startup.xml

alice.respond('LOAD ALICE') # 加載...\\botdata\\alice目錄下的語料庫

while True:

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

if("exit" == message):

exit()

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

print(response)

注意:如果出現(xiàn)某某模塊找不到的時候,記得使用pip安裝對應的模塊。

效果圖如下所示:

唯一美中不足的是英文,不過沒關系,國內(nèi)有圖靈機器人。

代碼如下所示:

from urllib.request import urlopen,Request

from urllib.error import URLError

from urllib.parse import urlencode

import json

class TuringChatMode(object):

"""this mode base on turing robot"""

def __init__(self):

# API接口地址

self.turing_url = 'http://www.tuling123.com/openapi/api?'

def get_turing_text(self,text):

''' 請求方式: HTTP POST

請求參數(shù): 參數(shù) 是否必須 長度 說明

key 必須 32 APIkey

info 必須 1-32 請求內(nèi)容,編碼方式為"utf-8"

userid 必須 32 MAC地址或ID

'''

turing_url_data = dict(

key = 'fcbf9efe277e493993e889eabca5b331',

info = text,

userid = '60-14-B3-BA-E1-4D',

)

# print("The things to Request is:",self.turing_url + urlencode(turing_url_data))

self.request = Request(self.turing_url + urlencode(turing_url_data))

# print("The result of Request is:",self.request)

try:

w_data = urlopen(self.request)

# print("Type of the data from urlopen:",type(w_data))

# print("The data from urlopen is:",w_data)

except URLError:

raise IndexError("No internet connection available to transfer txt data")

# 如果發(fā)生網(wǎng)絡錯誤,斷言提示沒有可用的網(wǎng)絡連接來傳輸文本信息

except:

raise KeyError("Server wouldn't respond (invalid key or quota has been maxed out)")

# 其他情況斷言提示服務相應次數(shù)已經(jīng)達到上限

response_text = w_data.read().decode('utf-8')

# print("Type of the response_text :",type(response_text))

# print("response_text :",response_text)

json_result = json.loads(response_text)

# print("Type of the json_result :",type(json_result))

return json_result['text']

if __name__ == '__main__':

print("Now u can type in something & input q to quit")

turing = TuringChatMode()

while True:

msg = input("\nMaster:")

if msg == 'q':

exit("u r quit the chat !") # 設定輸入q,退出聊天。

else:

turing_data = turing.get_turing_text(msg)

print("Robot:",turing_data)

效果圖如下:

可能由于機器人智能太低了,有點答非所問。

編程的世界是有趣的,你去探索,你會發(fā)現(xiàn)很多有意思的事情。

以上就是Python如何實現(xiàn)機器人聊天的詳細內(nèi)容,更多關于python 實現(xiàn)機器人聊天的資料請關注自學編程網(wǎng)其它相關文章!

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅持創(chuàng)作打卡瓜分現(xiàn)金大獎

總結

以上是生活随笔為你收集整理的python可以帮机器人编程吗_Python如何实现机器人聊天的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。