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

 更新时间:2023年05月08日 08:40:58   作者:爱看书的小沐  
这篇文章主要为大家介绍了Python如何实现聊天机器人,即使用自然语言处理 (NLP) 来帮助用户通过文本、图形或语音与 Web 服务或应用进行交互,感兴趣的可以了解一下

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聊天机器人的资料请关注脚本之家其它相关文章!

相关文章

  • Python之os模块案例详解

    Python之os模块案例详解

    这篇文章主要介绍了Python之os模块案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下
    2021-09-09
  • Python Playwright的使用详解

    Python Playwright的使用详解

    本篇博客为大家介绍一款新的自动化测试工具,效果类似 selenium,但是这个模块年轻。模块名称为 playwright-python,微软开源的,是针对 Python 语言的纯自动化工具,感兴趣的可以了解一下
    2022-10-10
  • Python 二叉树的层序建立与三种遍历实现详解

    Python 二叉树的层序建立与三种遍历实现详解

    这篇文章主要介绍了Python 二叉树的层序建立与三种遍历实现详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-07-07
  • python的中异常处理机制

    python的中异常处理机制

    这篇文章主要介绍了python的中异常处理的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-08-08
  • python实现多线程端口扫描

    python实现多线程端口扫描

    这篇文章主要为大家详细介绍了python实现多线程端口扫描,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-08-08
  • 浅谈python 调用open()打开文件时路径出错的原因

    浅谈python 调用open()打开文件时路径出错的原因

    这篇文章主要介绍了浅谈python 调用open()打开文件时路径出错的原因,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-06-06
  • Python 3.6 中使用pdfminer解析pdf文件的实现

    Python 3.6 中使用pdfminer解析pdf文件的实现

    这篇文章主要介绍了Python 3.6 中使用pdfminer解析pdf文件的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-09-09
  • Python3实现的简单三级菜单功能示例

    Python3实现的简单三级菜单功能示例

    这篇文章主要介绍了Python3实现的简单三级菜单功能,涉及Python用户交互以及针对json格式数据的遍历、读取、判断等相关操作技巧,需要的朋友可以参考下
    2019-03-03
  • PyTorch 成功安装验证的方法小结

    PyTorch 成功安装验证的方法小结

    这篇文章主要介绍了PyTorch 成功安装验证的方法小结,分享几种验证PyTorch是否安装成功的方法,确认PyTorch是否工作正常非常重要,可以避免后续的问题,需要的朋友可以参考下
    2023-11-11
  • 关于ResNeXt网络的pytorch实现

    关于ResNeXt网络的pytorch实现

    今天小编就为大家分享一篇关于ResNeXt网络的pytorch实现,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-01-01

最新评论