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

2023-05-19 08:05:13 开发 机器人 聊天

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

相关文章