解析科大讯飞 语音识别 返回的语义结果

返回的json数据

{
	"code": "0",
	"data": [{
		"sub": "iat",
		"text": {
			"sn": 1,
			"ls": false,
			"bg": 0,
			"ed": 0,
			"ws": [{
				"bg": 1,
				"cw": [{
					"sc": 0,
					"w": "订"
				}]
			}, {
				"bg": 1,
				"cw": [{
					"sc": 0,
					"w": "今天"
				}]
			}, {
				"bg": 1,
				"cw": [{
					"sc": 0,
					"w": "中午"
				}]
			}, {
				"bg": 1,
				"cw": [{
					"sc": 0,
					"w": "十"
				}]
			}, {
				"bg": 1,
				"cw": [{
					"sc": 0,
					"w": "二"
				}]
			}, {
				"bg": 1,
				"cw": [{
					"sc": 0,
					"w": "点"
				}]
			}, {
				"bg": 1,
				"cw": [{
					"sc": 0,
					"w": "的"
				}]
			}, {
				"bg": 1,
				"cw": [{
					"sc": 0,
					"w": "闹钟"
				}]
			}]
		}
	}, {
		"sub": "iat",
		"text": {
			"sn": 2,
			"ls": true,
			"bg": 0,
			"ed": 0,
			"ws": [{
				"bg": 0,
				"cw": [{
					"sc": 0,
					"w": ""
				}]
			}]
		}
	}, {
		"sub": "nlp",
		"intent": {}
	}, {
		"sub": "nlp",
		"intent": {
			"answer": {
				"text": "好的,今天中午十二点我会提醒您"
			},
			"dialog_stat": "dataInvalid",
			"rc": 0,
			"save_history": true,
			"semantic": [{
				"intent": "CREATE",
				"slots": [{
					"name": "datetime",
					"normValue": "{\"datetime\":\"2018-06-12T12:00:00\",\"suggestDatetime\":\"2018-06-12T12:00:00\"}",
					"value": "今天中午十二点"
				}, {
					"name": "name",
					"value": "clock"
				}]
			}],
			"service": "scheduleX",
			"sid": "ara00179888@dx15c20e7aa62d000100",
			"state": {
				"fg::scheduleX::default::clockFinished": {
					"datetime.INTERVAL": "1",
					"datetime.date": "1",
					"datetime.time": "1",
					"name": "1",
					"operation": "1",
					"state": "clockFinished"
				}
			},
			"text": "订今天中午十二点的闹钟",
			"used_state": {
				"datetime.INTERVAL": "1",
				"datetime.date": "1",
				"datetime.time": "1",
				"name": "1",
				"operation": "1",
				"state": "clockFinished",
				"state_key": "fg::scheduleX::default::clockFinished"
			},
			"uuid": "atn02d2fe4a@dx00070e7aa62da11001"
		}
	}],
	"desc": "success",
	"sid": "ara00179888@dx15c20e7aa62d000100"
}

解析出 answer 和datetime


def GetAnswer():
    r = requests.post(URL, headers=buildHeader(), data=readFile(FILE_PATH))
    print( r.content)
    text = json.loads(r.content)
    intent=''
    normValue=''
    semantic=''
    semantic_slots=''
    semantic_intent = ''
    for item in text['data']:
        if item.get("intent") != None:
            intent = item.get("intent")
            #break
    if not intent:
        return
    #遍历 semantic 数组
    for item_sem in intent['semantic']:
        if item_sem.get("slots")!=None:
            semantic_slots=item_sem.get("slots")
        if item_sem.get("intent") != None:
            semantic_intent=item_sem.get("intent")
    #遍历slots 数组
    for item_slot in semantic_slots:
        if item_slot.get("normValue")!=None:
            normValue=item_slot.get("normValue")
    answer = intent["answer"]
    dicValue=eval(normValue)
    datatime=dicValue['datetime']
    suggestDatetime=dicValue['suggestDatetime']
    re_answer=answer["text"]
    #打印解析结果
    print re_answer
    print datatime
    print suggestDatetime
    return re_answer

json越来越流行,通过python获取到json格式的字符串后,可以通过eval函数转换成dict格式:

>>> a='{"name":"yct","age":10}'

>>> eval(a)
{'age': 10, 'name': 'yct'}

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐