
# 一句话识别Http接口
#### 前提条件
- 确保已按照[配置Java环境](https://support.huaweicloud.com/sdkreference-sis/sis_05_0041.html)配置完毕。
- 确保已存在待识别的音频文件。如果需要请在下载的SDK压缩包中获取示例音频。
- 请参考[SDK（websocket）](https://support.huaweicloud.com/sdkreference-sis/sis_05_0004.html)获取最新版本SDK包。
 
#### 初始化Client
初始化AsrCustomizationClient，其参数包括AuthInfo和SisConfig。
表1AuthInfo 
| 参数名称      | 是否必选 | 参数类型   | 描述                                                                                     |
|:---|:---|:---|:---|
| ak        | 是    | String | 用户的ak，可参考[AK/SK认证](https://support.huaweicloud.com/sdkreference-sis/sis_05_0003.html)。 |
| sk        | 是    | String | 用户的sk，可参考[AK/SK认证](https://support.huaweicloud.com/sdkreference-sis/sis_05_0003.html)。 |
| region    | 是    | String | 区域，如cn-north-4，参考[终端节点](https://support.huaweicloud.com/api-sis/sis_03_0004.html)。     |
| projectId | 是    | String | 项目ID，同region一一对应，参考[获取项目ID](https://support.huaweicloud.com/api-sis/sis_03_0008.html)。 |
| endpoint  | 否    | String | 终端节点，具体请参考[终端节点](https://support.huaweicloud.com/api-sis/sis_03_0004.html)。一般使用默认即可。   |
   
表2SisConfig 
| 参数名称              | 是否必选 | 参数类型    | 描述                 |
|:---|:---|:---|:---|
| connectionTimeout | 否    | Integer | 连接超时，默认10000，单位ms。 |
| readTimeout       | 否    | Integer | 读取超时，默认10000，单位ms。 |
   
#### 请求参数
请求类为AsrCustomShortRequest，详见[表3]。
 表3AsrCustomShortRequest 
| 参数名称               | 是否必选 | 参数类型   | 描述                                                                                                                                                          |
|:---|:---|:---|:---|
| data               | 是    | String | 本地音频文件经过Base64编码后的字符串，音频文件时长不超过1min。                                                                                                                        |
| audioFormat        | 是    | String | 音频格式，具体信息请参见《API参考》中[一句话识别](https://support.huaweicloud.com/api-sis/sis_03_0040.html)章节。                                                                    |
| property           | 是    | String | 属性字符串，语言_采样率_模型，如chinese_16k_general。具体信息请参见《API参考》中[一句话识别](https://support.huaweicloud.com/api-sis/sis_03_0040.html)章节。                                    |
| addPunc            | 否    | String | 表示是否在识别结果中添加标点，取值为yes 、 no，默认no。                                                                                                                            |
| digitNorm          | 否    | String | 表示是否将语音中的数字识别为阿拉伯数字，取值为yes 、 no，默认为yes。                                                                                                                     |
| vocabularyId       | 否    | String | 热词表id，不使用则不填写。 创建热词表请参考《API参考》中[创建热词表](https://support.huaweicloud.com/api-sis/sis_03_0071.html)章节。                           |
| needWordInfo       | 否    | String | 表示是否在识别结果中输出分词结果信息，取值为"yes"和"no"，默认为"no"。                                                                                                                   |
| autoLanguageDetect | 否    | String | 表示是否开启普通话、上海话、四川话、粤语、英文自动识别，取值为"yes"和"no"，默认为"no"。如果开启此开关，且传入音频时长不低于1.5秒，后端服务会依据用户的输入音频自动选择对应语种的模型（property）进行识别。注意，语种的判定并非绝对正确，所以用户如果已经明确所属语种/方言，不建议打开此开关。 |
   
#### 响应参数
响应类为AsrCustomShortResponse，详见[表4]。调用失败处理方法请参见[错误码](https://support.huaweicloud.com/api-sis/sis_03_0045.html)。
 表4AsrCustomShortResponse 
| 参数名     | 是否必选 | 参数类型   | 说明                                                                                                 |
|:---|:---|:---|:---|
| traceId | 是    | String | 服务内部的令牌，可用于在日志中追溯具体流程，调用失败无此字段。 在某些错误情况下可能没有此令牌字符串。 |
| result  | 是    | Object | 调用成功表示识别结果，调用失败时无此字段。请参考[表5]。                                     |
   
 表5Result 
| 参数名      | 是否必选 | 参数类型             | 说明                       |
|:---|:---|:---|:---|
| text     | 是    | String           | 调用成功表示识别出的内容。            |
| score    | 是    | Float            | 调用成功表示识别出的置信度，取值范围：0\~1。 |
| wordInfo | 否    | Array of objects | 分词信息列表。                  |
   
表6Word_info 数据结构 
| 参数名       | 是否必选 | 参数类型    | 说明    |
|:---|:---|:---|:---|
| startTime | 否    | Integer | 起始时间。 |
| endTime   | 否    | Integer | 结束时间。 |
| word      | 否    | String  | 分词。   |
   
#### 代码示例
如下示例仅供参考，最新代码请前往[SDK（websocket）](https://support.huaweicloud.com/sdkreference-sis/sis_05_0004.html)章节获取并运行。
```
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 SLEEP_TIME = 500;
  private static final int MAX_POLLING_NUMS = 1000;
  // 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险，建议在配置文件或者环境变量中密文存放，使用时解密，确保安全；
  // 本示例以ak和sk保存在环境变量中来实现身份验证为例，运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和HUAWEICLOUD_SDK_SK。
  private String ak = System.getenv("HUAWEICLOUD_SDK_AK");
  private String sk = System.getenv("HUAWEICLOUD_SDK_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 = "chinese_16k_general";     // 属性字符串，language_sampleRate_domain, 16k模型推荐使用chinese_16k_general
  /**
   * 设置一句话识别参数，所有参数均有默认值，不配置也可使用
   *
   * @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);
      // 设置企业id, 可选
      // Map<String, String> headers = OKHttpClientUtils.getJsonHeaders();
      // headers.put(SisConstant.ENTERPRISE_PROJECT_ID_KEY, "your enterprise_id");
      // AsrCustomShortResponse response = asr.getAsrShortResponse(headers, 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();
  }
}
```
