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 内置logging 使用详细介绍

    Python 内置logging 使用详细介绍

    提供日志记录的接口和众多处理模块,供用户存储各种格式的日志,帮助调试程序或者记录程序运行过程中的输出信息,这篇文章主要介绍了Python 内置logging 使用讲解,需要的朋友可以参考下
    2022-07-07
  • python os.path模块使用方法介绍

    python os.path模块使用方法介绍

    os.path 模块是系统路径操作模块,但实际的原理可以把它认为是处理包含斜杠("/")和反斜杠("\")字符串的模块,其中,斜杠("/")是 linux 系统下的路径分隔符,和反斜杠("\")是 windows 系统下的路径分隔符
    2022-08-08
  • pygame实现成语填空游戏

    pygame实现成语填空游戏

    这篇文章主要介绍了pygame实现成语填空游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-10-10
  • Python基于scapy实现修改IP发送请求的方法示例

    Python基于scapy实现修改IP发送请求的方法示例

    这篇文章主要介绍了Python基于scapy实现修改IP发送请求的方法,涉及Python网络编程中使用scapy操作IP的相关实现技巧,需要的朋友可以参考下
    2017-07-07
  • python3.6+opencv3.4实现鼠标交互查看图片像素

    python3.6+opencv3.4实现鼠标交互查看图片像素

    这篇文章主要为大家详细介绍了python3.6+opencv3.4实现鼠标交互查看图片像素,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-02-02
  • pygame实现简单五子棋游戏

    pygame实现简单五子棋游戏

    这篇文章主要为大家详细介绍了pygame实现简单五子棋游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下<BR>
    2022-01-01
  • 关于Python 中的时间处理包datetime和arrow的方法详解

    关于Python 中的时间处理包datetime和arrow的方法详解

    这篇文章主要介绍了关于Python 中的时间处理包datetime和arrow的相关知识,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-03-03
  • 浅谈Python里面小数点精度的控制

    浅谈Python里面小数点精度的控制

    今天小编就为大家分享一篇浅谈Python里面小数点精度的控制,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-07-07
  • Python数据分析Pandas Dataframe排序操作

    Python数据分析Pandas Dataframe排序操作

    这篇文章主要介绍了Python数据分析Pandas Dataframe排序操作,数据的排序是比较常用的操作,DataFrame 的排序分为两种,一种是对索引进行排序,另一种是对值进行排序,接下来就分别都介绍一下,需要的小伙伴可以参考一下
    2022-05-05
  • Flask框架工厂函数用法实例分析

    Flask框架工厂函数用法实例分析

    这篇文章主要介绍了Flask框架工厂函数用法,结合实例形式分析了Flask框架工厂函数定义、应用及相关操作注意事项,需要的朋友可以参考下
    2019-05-05

最新评论