Python NLP开发之实现聊天机器人
作者:爱看书的小沐 时间:2021-12-21 18:04:48
1、简介
AI 聊天机器人使用自然语言处理 (NLP) 来帮助用户通过文本、图形或语音与 Web 服务或应用进行交互。聊天机器人可以理解自然人类语言、模拟人类对话并运行简单的自动化任务。此外,AI 聊天机器人还利用预测智能和分析技术来了解用户的偏好,并利用了解的这些信息来提供建议和预测需求
AI 聊天机器人用于各种渠道,例如消息传递应用、移动应用、网站、电话线路和支持语音的应用。它们可以针对不同目的而开发,无论是仅处理一些简单的命令,还是充当复杂的数字助理和交互式代理。AI 聊天机器人可以是大型应用程序的一部分,也可以完全独立存在
2、代码测试
2.1 open.drea.cc
import requests
import json
while True:
msg = input('我:')
sess = requests.get(
('https://open.drea.cc/bbsapi/chat/get?keyWord=' + msg + '&userName=type%3Dbbs'))
js = sess.text
js = json.loads(js)
print('微梦机器人:', js['data']['reply'])
2.2 api.ruyi.ai
import requests
import json
while True:
msg = input('我:')
sess = requests.get(('http://api.ruyi.ai/v1/message?app_key=your_key&user_id=test123&q=' + msg))
js = sess.text
js = json.loads(js)
js = js['result']['intents']
js = js[0]['result']['text']
print('海知机器人:', js)
2.3 route.showapi.com
import requests
import json
while True:
msg = input('我:')
sess = requests.get(
('http://route.showapi.com/60-27?&showapi_appid=102421&userid=userid&showapi_sign=your_key&info=' + msg))
js = sess.text
js = json.loads(js)
print('万维易源机器人:', js['showapi_res_body']['text'])
2.4 api.binstd.com
import requests
import json
while True:
msg = input('我:')
sess = requests.get(
('https://api.binstd.com/iqa/query?appkey=your_key&question=' + msg))
js = sess.text
js = json.loads(js)
print('进制机器人:', js['result']['content'])
2.5 api.jisuapi.com
import requests
import json
while True:
msg = input('我:')
sess = requests.get(('https://api.jisuapi.com/iqa/query?appkey=c0e4885a90566a7b&question=' + msg))
js = sess.text
js = json.loads(js)
print('极速机器人:', js['result']['content'])
2.6 api.fanyi.baidu.com
import requests
import string
import time
import hashlib
import json
#init
api_url = "http://api.fanyi.baidu.com/api/trans/vip/translate"
my_appid = 'your_key'
cyber = 'your_cyber '
lower_case = list(string.ascii_lowercase)
def requests_for_dst(word):
#init salt and final_sign
salt = str(time.time())[:10]
final_sign = str(my_appid)+word+salt+cyber
final_sign = hashlib.md5(final_sign.encode("utf-8")).hexdigest()
#区别en,zh构造请求参数
if list(word)[0] in lower_case:
paramas = {
'q': word,
'from': 'en',
'to': 'zh',
'appid': '%s' % my_appid,
'salt': '%s' % salt,
'sign': '%s' % final_sign
}
my_url = api_url+'?appid=' + \
str(my_appid)+'&q='+word+'&from='+'en'+'&to=' + \
'zh'+'&salt='+salt+'&sign='+final_sign
else:
paramas = {
'q': word,
'from': 'zh',
'to': 'en',
'appid': '%s' % my_appid,
'salt': '%s' % salt,
'sign': '%s' % final_sign
}
my_url = api_url+'?appid=' + \
str(my_appid)+'&q='+word+'&from='+'zh'+'&to=' + \
'en'+'&salt='+salt+'&sign='+final_sign
response = requests.get(api_url, params=paramas).content
content = str(response, encoding="utf-8")
json_reads = json.loads(content)
print('小度机器人:这句话翻译为中文是',json_reads['trans_result'][0]['dst'])
while True:
word = input("我: ")
requests_for_dst(word)
2.7 aiml
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.learn("startup.xml")
alice.respond('LOAD ALICE')
while True:
message = input("我:")
if("exit" == message):
exit()
response = alice.respond(message)
print('AIML机器人:',response)
2.8 api.tianapi.com
import requests
import json
while True:
msg = input('我:')
sess = requests.get(('http://api.tianapi.com/txapi/robot/index?key=your_key&question=' + msg))
js = sess.text
js = json.loads(js)
print('天行机器人:', js['newslist'])
2.9 nlp.xiaoi.com
import urllib.request,re
while True:
msg = input("我:")
msg = urllib.parse.quote(msg)
link = urllib.request.urlopen(
"http://nlp.xiaoi.com/robot/webrobot?&callback=__webrobot_processMsg&data=%7B%22sessionId%22%3A%22ff725c236e5245a3ac825b2dd88a7501%22%2C%22robotId%22%3A%22webbot%22%2C%22userId%22%3A%227cd29df3450745fbbdcf1a462e6c58e6%22%2C%22body%22%3A%7B%22content%22%3A%22" + msg + "%22%7D%2C%22type%22%3A%22txt%22%7D")
html_doc = link.read().decode()
reply_list = re.findall(r'\"content\":\"(.+?)\\r\\n\"', html_doc)
print("小i机器人:" + reply_list[-1])
2.10 api.qingyunke.com
import requests,json
while True:
msg = input("我:")
js = requests.get("http://api.qingyunke.com/api.php",{'key': 'free', 'appid': 0, 'msg': msg})
js.encoding = 'utf8'
js = js.json()
print('青云客机器人:', js['content'])
2.11 api.ownthink.com
import requests,json
while True:
msg = input('我:')
sess = requests.get(('https://api.ownthink.com/bot?spoken=' + msg))
js = sess.text
js = json.loads(js)
print('思知机器人:', js['data']['info']['text'])
来源:https://blog.csdn.net/hhy321/article/details/130547183
标签:Python,聊天,机器人
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
灵活调用xsl来解析xml文档(js异步)
2008-09-05 17:12:00
Centos6.x服务器配置jdk+tomcat+mysql环境(jsp+mysql)
2023-06-14 12:14:13
![](https://img.aspxhome.com/file/2023/1/67221_0s.png)
触手生春【4.13】CSS中的伪元素选择符
2008-11-11 13:10:00
![](https://img.aspxhome.com/file/UploadPic/200811/11/o2008113223147-93s.jpg)
基于网格的网页设计概念及实际应用案例
2010-03-30 14:59:00
![](https://img.aspxhome.com/file/UploadPic/20103/30/1052570-42s.jpg)
python的random和time模块详解
2023-07-27 18:16:27
![](https://img.aspxhome.com/file/2023/5/61215_0s.png)
关于数据库的alter table操作和索引概念
2009-12-15 21:57:00
asp入门之字符串函数介绍示例
2008-11-04 20:18:00
浅谈pytorch池化maxpool2D注意事项
2023-07-14 15:02:30
经典的退出浏览器弹窗代码
2008-07-30 12:48:00
SQL Server 2008主要功能在兼容性上的问题
2009-03-25 16:43:00
MIME Type 引出的两难困境
2011-06-14 09:45:41
Asp下Access无法更新的解决方法
2012-12-04 20:19:35
SQL Server如何保证可空字段中非空值唯一
2011-02-24 16:44:00
可以随便改别人的网页的代码
2008-03-25 12:54:00
用好Frontpage中的各种回车
2008-02-21 14:33:00
淘宝CSS框架研究(1):Reset CSS(八卦篇)
2009-03-31 12:58:00
P3P 和 跨域 (cross-domain) cookie 访问(读取和设置)
2011-04-02 10:42:00
Golang库插件注册加载机制的问题
2023-06-24 04:25:59
php在linux环境中如何使用redis详解
2023-06-14 23:38:30
JS+ASP实现无刷新新闻列表方法
2007-08-22 12:44:00