123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- # -*- coding: utf-8 -*-
- """
- 百度的语音识别API.
- dev_pid:
- - 1936: 普通话远场
- - 1536:普通话(支持简单的英文识别)
- - 1537:普通话(纯中文识别)
- - 1737:英语
- - 1637:粤语
- - 1837:四川话
- 要使用本模块, 首先到 yuyin.baidu.com 注册一个开发者账号,
- 之后创建一个新应用, 然后在应用管理的"查看key"中获得 API Key 和 Secret Key
- 填入 config.xml 中.
- ...
- baidu_yuyin:
- appid: '9670645'
- api_key: 'qg4haN8b2bGvFtCbBGqhrmZy'
- secret_key: '585d4eccb50d306c401d7df138bb02e7'
- ...
- """
- from __future__ import division
- from ..__load__ import *
- from .base import Base
- # 需要百度SDK
- from aip import AipSpeech
- class Baidu(Base):
- def __init__(self):
- super(self.__class__, self).__init__(True)
- def setting(self, appid, api_key, secret_key, dev_pid=1936, voice=2, per=1, lan='zh', **args):
- self.engine = AipSpeech(appid, api_key, secret_key)
- self.dev_pid = dev_pid
- self.per = per
- self.lan = lan
- self.voice = voice
- def asr(self, fp):
- pcm = getPcmFromWav(fp)
- res = self.engine.asr(pcm, 'pcm', 16000, {
- 'dev_pid': self.dev_pid,
- })
- if res['err_no'] == 0:
- res['result'] = ''.join(res['result'])
- return self.arrLog(1, res['result'])
- else:
- res['err_msg'] = ''.join(res['err_msg'])
- return self.arrLog(2, res['err_msg'])
- def tts(self, phrase):
- result = self.engine.synthesis(phrase, self.lan, self.voice, {
- 'vol': 5,
- 'spd': 3,
- 'pit': 8,
- 'per': self.per
- });
- # 识别正确返回语音二进制 错误则返回dict 参照下面错误码
- if not isinstance(result, dict):
- tmpfile = writeTempFile(result, '.mp3')
- return self.ttsLog(1, tmpfile)
- else:
- return self.ttsLog(2, '')
- #使用百度 UNIT 进行 NLU 解析
- def nlu(self, query, service_id):
- return self.sdk.getUnit(query, service_id, self.api_key, self.secret_key)
- """
- 提取意图
- :param parsed: 解析结果
- :return: 意图数组
- """
- def getIntent(self, parsed):
- return self.sdk.getIntent(parsed)
- """
- 判断是否包含某个意图
- :param parsed: UNIT 解析结果
- :param intent: 意图的名称
- :return: True: 包含; False: 不包含
- """
- def hasIntent(self, parsed, intent):
- return self.sdk.hasIntent(parsed, intent)
- """
- 提取某个意图的所有词槽
- :param parsed: UNIT 解析结果
- :param intent: 意图的名称
- :return: 词槽列表。你可以通过 name 属性筛选词槽,
- 再通过 normalized_word 属性取出相应的值
- """
- def getSlots(self, parsed, intent):
- return self.sdk.getSlots(parsed, intent)
- """
- 找出命中某个词槽的内容
- :param parsed: UNIT 解析结果
- :param intent: 意图的名称
- :param name: 词槽名
- :return: 命中该词槽的值的列表。
- """
- def getSlotWords(self, parsed, intent, name):
- return self.sdk.getSlotWords(parsed, intent, name)
- """
- 提取 UNIT 的回复文本
- :param parsed: UNIT 解析结果
- :param intent: 意图的名称
- :return: UNIT 的回复文本
- """
- def getSay(self, parsed, intent):
- return self.sdk.getSay(parsed, intent)
|