101 lines
4.7 KiB
Python
Executable File
101 lines
4.7 KiB
Python
Executable File
# coding=utf-8
|
||
import os
|
||
import time
|
||
import sys
|
||
import urllib
|
||
import json
|
||
import requests
|
||
#import xmltodict
|
||
|
||
# Demo示例,需要在具体项目具体场景下自行实现相关调用逻辑
|
||
|
||
class XiaoMiNlu:
|
||
def __init__(self):
|
||
self.nlpUrl = 'http://39.106.157.134:8110/chatbot/chat/ivr?'
|
||
self.appId = 402
|
||
def makeUrl(self, sessionId, asrResult, asrBeginTime, asrEndTime, emotionValue, vendorParams):
|
||
#http://39.106.157.134:8110/chatbot/chat/ivr?AppId=402&Utterance=%E4%BD%A0%E5%A5%BD&VendorParam={%22index%22:%221%22}&SessionId=ee8a166cd1cc4bab
|
||
url = self.nlpUrl + "AppId=" + str(self.appId) + "&Utterance=" + urllib.quote(asrResult)
|
||
if sessionId is not None and sessionId != "":
|
||
url = url + "&SessionId=" + sessionId
|
||
if vendorParams is not None and vendorParams != "":
|
||
#按需将 k1=v1;k2=v2;k3=v3的形式转成json格式(云小蜜调用要求)
|
||
items = vendorParams.split(';')
|
||
d = dict()
|
||
for item in items:
|
||
key,value = item.split('=')
|
||
d[key] = value
|
||
url = url + "&VendorParam=" + json.dumps(d)
|
||
return url
|
||
|
||
def request(self, ssionId, asrResult, asrBeginTime, asrEndTime, emotionValue, vendorParams):
|
||
url = self.makeUrl(ssionId, asrResult, asrBeginTime, asrEndTime, emotionValue, vendorParams)
|
||
req = requests.get(url)
|
||
if(req.status_code != 200):
|
||
print req.text
|
||
print "ERROR url(%s), request Fail(%d)" % (url, req.status_code)
|
||
return ""
|
||
'''
|
||
{
|
||
"SessionId": "123f",
|
||
"MessageId": "601762b2951e4a2381e26ec0a2703fc7",
|
||
"Messages": [
|
||
{
|
||
"Type": "Text",
|
||
"Text": {
|
||
"Content": "{\"INTEL_CUST_HEAD\":{\"RESP_DESC\":\"\",\"APP_ID\":\" \",\"RESP_CODE\":\"\",\"TIMESTAMP\":\"\",\"TRANS_ID\":\"\"},\"INTEL_CUST_BODY\":{\"RESP_DESC\":\"\",\"OPERTYPE\":\"0\",\"RESP_CODE\":\"0000\",\"BROADCAST1\":\"engine \",\"INTERRUPT1\":\"\"}}",
|
||
"AnswerSource": "ChitChat"
|
||
}
|
||
}
|
||
]
|
||
}
|
||
'''
|
||
# 按需对NLU的返回进行解析,获取需要关注的内容并打包为xml格式返回:此处示例只获取"Messages->Text->Content"字段对应的值
|
||
print "result = ", req.text
|
||
jss = json.loads(req.text.encode('utf-8'))
|
||
reply = ""
|
||
if(jss.has_key('Messages') and jss['Messages'][0].has_key('Text')):
|
||
result = jss['Messages'][0]['Text']
|
||
if result.has_key('Content'):
|
||
reply = result['Content'].encode('utf-8')
|
||
|
||
# 把 json格式转为xml格式(MRCPSERVER和IVR交互时只有XML格式)
|
||
reply_json = json.loads(reply)
|
||
#print type(reply_json), reply_json
|
||
#xmlstr = xmltodict.unparse(reply_json, full_document=False)
|
||
xmlstr = "<f>fff</f>"
|
||
#print "xmlstr = ", xmlstr
|
||
return xmlstr
|
||
|
||
def nluRequest(sessionId, asrResult, asrBeginTime, asrEndTime, emotionValue, vendorParams):
|
||
print "python nluRequest = ", sessionId, asrResult, asrBeginTime, asrEndTime, emotionValue, vendorParams
|
||
''' 需要NLU服务关注asrResult的取值范围:
|
||
NO-MTACH : 检测到了语音(vad检测到了起始点),但ASR并没有得到有效的结果,这种一般是客户声音过小、背景音过大、声音有能量但无法听到实际内容导致
|
||
NO-INPUT-TIMEOUT : 无话超时,也即:用户侧没有说法,全是静音
|
||
ASR-ERROR : 调用ASR进行识别,可能需要NLU服务进行兜底处理
|
||
其他情况 : 用户语音的ASR识别结果
|
||
'''
|
||
if asrResult == "NO-MTACH":
|
||
print "check speech, but no valid result!"
|
||
elif asrResult == "NO-INPUT-TIMEOUT":
|
||
print "check no speech, user may be not speak!"
|
||
elif asrResult == "ASR-ERROR":
|
||
print "call ASR failure, you should hold this!"
|
||
else:
|
||
print "check speech, and recognized success: ", asrResult
|
||
|
||
#time.sleep(1)
|
||
# 注意:一定要返回如下格式
|
||
# 返回<k1>fff</k1>类似结构,该结构即是xml格式(去掉xml头))
|
||
return "<key>value</key>"
|
||
nlu = XiaoMiNlu()
|
||
return nlu.request(sessionId, asrResult, asrBeginTime, asrEndTime, emotionValue, vendorParams)
|
||
|
||
if __name__ == '__main__':
|
||
print nluRequest("123f", "你好", 1234, 4569, 5, "ke=1;phone=123;callid=zxcvb"), "\n"
|
||
print nluRequest("123f", "WELCOME", 1234, 4569, 0, "ke=1"), "\n"
|
||
print nluRequest("123f", "NO-MTACH", 1234, 4569, 0, "ke=1"), "\n"
|
||
print nluRequest("123f", "NO-INPUT-TIMEOUT", 1234, 1, 4569, "ke=1"), "\n"
|
||
print nluRequest("123f", "ASR-ERROR", 1234, 4569, 0, ""), "\n"
|
||
print "Game Over"
|