前提条件
● 确保已按照配置Java环境配置完毕。
● 确保已存在待识别的音频文件。如果需要请在下载的SDK压缩包中获取示例音 频。
初始化 Client
初始化AsrCustomizationClient,其参数包括AuthInfo和SisConfig。
表5-1 AuthInfo 参数名称 是否
必选
参数类型 描述
ak 是 String 用户的ak,可参考AK/SK认证。
sk 是 String 用户的sk,可参考AK/SK认证。
region 是 String 区域,如cn-north-4,参考终端节点。
projectId 是 String 项目ID,同region一一对应,参考获取项目ID。
endpoint 否 String 终端节点,具体请参考地区和终端节点。一般使用 默认即可。
表5-2 SisConfig 参数名称 是否
必选
参数类 型
描述
connecti onTimeo ut
否 Integer 连接超时,默认10000,单位ms。
readTim
eout 否 Integer 读取超时,默认10000,单位ms。
请求参数
请求类为AsrCustomShortRequest,详见表5-3。
表5-3 AsrCustomShortRequest
参数名称 是否
必选
参数类 型
描述
data 是 String 本地音频文件经过Base64编码后的字符串,音频文 件时长不超过1min。
audioForm
at 是 String 音频格式,具体信息请参见《API参考》中一句话识
别章节。
property 是 String 属性字符串,语言_采样率_模型,如
chinese_8k_common。具体信息请参见《API参 考》中一句话识别章节。
addPunc 否 String 表示是否在识别结果中添加标点,取值为yes 、 no,默认no。
digitNorm 否 String 表示是否将语音中的数字识别为阿拉伯数字,取值 为yes 、 no,默认为yes。
vocabulary
Id 否 String 热词表id,不使用则不填写。
创建热词表请参考《API参考》中创建热词表章节。
needWord
Info 否 String 表示是否在识别结果中输出分词结果信息,取值为
“yes”和“no”,默认为“no”。
响应参数
响应类为AsrCustomShortResponse,详见表5-4。
表5-4 AsrCustomShortResponse
参数名 是否必选 参数类型 说明
trace_id 是 String 服务内部的令牌,可用于在日志中追 溯具体流程,调用失败无此字段。
在某些错误情况下可能没有此令牌字 符串。
result 是 Object 调用成功表示识别结果,调用失败时
无此字段。请参考表5-5。
表5-5 Result
参数名 是否必选 参数类型 说明
text 是 String 调用成功表示识别出的内容。
score 是 Float 调用成功表示识别出的置信度,取值
范围:0~1。
word_info 否 Array of
objects 分词信息列表。
表5-6 Word_info 数据结构
参数名 是否必选 参数类型 说明
start_time 否 Integer 起始时间 end_time 否 Integer 结束时间
word 否 String 分词
代码示例
import com.huawei.sis.bean.SisConfig;
import com.huawei.sis.bean.SisConstant;
import com.huawei.sis.bean.request.AsrCustomShortRequest;
import com.huawei.sis.bean.response.AsrCustomShortResponse;
import com.huawei.sis.bean.AuthInfo;
import com.huawei.sis.client.AsrCustomizationClient;
import com.huawei.sis.exception.SisException;
import com.huawei.sis.util.IOUtils;
import java.util.List;
import com.huawei.sis.util.JsonUtils;
/** * 一句话识别
* * Copyright 2021 Huawei Technologies Co.,Ltd.
*/public class AsrCustomizationDemo {
private static final int MAX_POLLING_NUMS = 1000;
private String ak = "";
private String sk = "";
private String region = ""; // 区域,如cn-north-1、cn-north-4
private String projectId = ""; // 项目id。登录管理控制台,鼠标移动到右上角的用户名上,在下拉列表中选择我 的凭证,在项目列表中查看项目id。多项目时,展开“所属区域”,从“项目ID”列获取子项目ID。
// 一句话识别参数
private String path = ""; // 音频文件路径,如D:/test.wav等,sdk会将音频文件转化为base64编码 private String pathAudioFormat = ""; // 文件格式,如wav等
private String pathProperty = ""; // 属性字符串,language_sampleRate_domain, 如chinese_8k_common
/**
* 设置一句话识别参数,所有参数均有默认值,不配置也可使用 * * @param request 一句话识别请求
*/
private void setShortParameter(AsrCustomShortRequest request) { // 设置是否添加标点,默认是no
request.setAddPunc("yes");
// 设置是否将语音中的数字转写为阿拉伯数字,yes或no,默认yes request.setDigitNorm("no");
} /**
* 定义config,所有参数可选,设置超时时间等。
* * @return SisConfig */
private SisConfig getConfig() { SisConfig config = new SisConfig();
// 设置连接超时,默认10000ms
config.setConnectionTimeout(SisConstant.DEFAULT_CONNECTION_TIMEOUT);
// 设置读取超时,默认10000ms
config.setReadTimeout(SisConstant.DEFAULT_READ_TIMEOUT);
// 设置代理, 一定要确保代理可用才启动此设置。 代理初始化也可用不加密的代理,new ProxyHostInfo(host, port);
// ProxyHostInfo proxy = new ProxyHostInfo(host, port, username, password);
// config.setProxy(proxy);
return config;
}
/**
* 一句话识别demo */
private void shortDemo() { try {
// 1. 初始化AsrCustomizationClient
// 定义authInfo,根据ak,sk,region,projectId
AuthInfo authInfo = new AuthInfo(ak, sk, region, projectId);
// 设置config,主要与超时有关 SisConfig config = getConfig();
// 根据authInfo和config,构造AsrCustomizationClient
AsrCustomizationClient asr = new AsrCustomizationClient(authInfo, config);
// 2. 配置请求
String data = IOUtils.getEncodeDataByPath(path);
AsrCustomShortRequest request = new AsrCustomShortRequest(data, pathAudioFormat, pathProperty);
// 设置请求参数,所有参数均为可选 setShortParameter(request);
// 3. 发送请求,获取响应
AsrCustomShortResponse response = asr.getAsrShortResponse(request);
// 打印结果
System.out.println(JsonUtils.obj2Str(response, true));
} catch (SisException e) { e.printStackTrace();
System.out.println("error_code:" + e.getErrorCode() + "\nerror_msg:" + e.getErrorMsg());
} }
public static void main(String[] args) {
AsrCustomizationDemo demo = new AsrCustomizationDemo();
demo.shortDemo();
} }