iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Python NLP开发之实现聊天机器人
  • 849
分享到

Python NLP开发之实现聊天机器人

Python 官方文档:入门教程 => 点击学习

摘要

目录1、简介2、代码测试2.1 open.drea.cc2.2 api.ruyi.ai2.3 route.showapi.com2.4 api.binstd.com2.5 api.j

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'])

以上就是python NLP开发之实现聊天机器人的详细内容,更多关于Python聊天机器人的资料请关注编程网其它相关文章!

--结束END--

本文标题: Python NLP开发之实现聊天机器人

本文链接: https://www.lsjlt.com/news/212816.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作