全部产品
存储与CDN 数据库 安全 应用服务 数加·人工智能 数加·大数据基础服务 互联网中间件 视频服务 开发者工具 解决方案 物联网

java sdk

更新时间:2017-08-22 16:11:20

java sdk 使用说明

功能介绍

kws(关键词服务)的java sdk 同asr实时长语音sdk。针对kws的功能,主要提供了关键词表增删改查的util类,以及用来设置keyword_list_id(关键词表id)的参数。

SDK下载地址

java sdk

相关接口说明

  • 关键词表维护接口:om.alibaba.idst.nls.utils.KwsMngUtil

该类中包含了所有增删改查的静态方法,调用方式参见下面代码示例。

  • 关键词表id设置接口:public void setKeyWordListId(String keyWordListId)

该方法位于NlsRequest中,NlsRequest的其它方法说明参见实时asr java sdk文档,调用方式参加下面代码示例。

关键词表维护代码示例

  1. public static void main(String[] args) {
  2. String akId = args[0];
  3. String akSecret = args[1];
  4. String json = "{\n"
  5. + " \"keywords\": [\n"
  6. + " {\"keyword\":\"张三\"},\n"
  7. + " {\"keyword\":\"李四\"}\n"
  8. + " ]\n"
  9. + "}";
  10. String updateJson = "{\n"
  11. + " \"keywords\": [\n"
  12. + " {\"keyword\":\"张四\"},\n"
  13. + " {\"keyword\":\"李三\"}\n"
  14. + " ]\n"
  15. + "}";
  16. String response = KwsMngUtil.create(akId, akSecret, json);
  17. logger.info("create response:{}", response);
  18. String keywordListId = (String)JSONPath.read(response, "$.keyword_list_id");
  19. response=KwsMngUtil.update(akId, akSecret, keywordListId,updateJson);
  20. logger.info("update response:{}", response);
  21. response = KwsMngUtil.get(akId, akSecret, keywordListId);
  22. logger.info("get response:{}", response);
  23. response = KwsMngUtil.delete(akId, akSecret, keywordListId);
  24. logger.info("delete :{}, response:{}", keywordListId, response);
  25. }

关键词识别代码示例

  1. package com.alibaba.idst.nls;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.idst.nls.event.NlsEvent;
  4. import com.alibaba.idst.nls.event.NlsListener;
  5. import com.alibaba.idst.nls.protocol.NlsRequest;
  6. import com.alibaba.idst.nls.protocol.NlsResponse;
  7. import java.io.File;
  8. import java.io.FileInputStream;
  9. import org.slf4j.Logger;
  10. import org.slf4j.LoggerFactory;
  11. /**
  12. * Created by songsong.sss on 16/6/22.
  13. */
  14. public class RealtimeDemo implements NlsListener {
  15. private NlsClient client = new NlsClient();
  16. private static final String asrSC = "pcm";
  17. static Logger logger = LoggerFactory.getLogger(RealtimeDemo.class);
  18. public String filePath = "";
  19. public String appKey = "";
  20. String ak_id = "";
  21. String ak_secret = "";
  22. public RealtimeDemo() {
  23. }
  24. public void shutDown() {
  25. logger.debug("close NLS client manually!");
  26. client.close();
  27. logger.debug("demo done");
  28. }
  29. public void start() {
  30. logger.debug("init Nls client...");
  31. client.init();
  32. }
  33. public void hearIt() {
  34. logger.debug("open audio file...");
  35. FileInputStream fis = null;
  36. try {
  37. File file = new File(filePath);
  38. fis = new FileInputStream(file);
  39. } catch (Exception e) {
  40. e.printStackTrace();
  41. }
  42. if(fis != null) {
  43. logger.debug("create NLS future");
  44. try {
  45. NlsRequest req = new NlsRequest();
  46. req.setAppkey(appKey);
  47. req.setFormat(asrSC);
  48. req.setResponseMode("streaming");
  49. req.setSampleRate(16000);
  50. //用户根据[热词文档](~~49179~~) 设置自定义热词。
  51. //通过设置VocabularyId调用热词。
  52. //req.setVocabularyId("");
  53. //设置关键词库ID 使用时请修改为自定义的词库ID
  54. req.setKeyWordListId("c1391f1c1f1b4002936893c6d97592f3");
  55. // the id and the id secret
  56. req.authorize(ak_id, ak_secret);
  57. NlsFuture future = client.createNlsFuture(req,this);
  58. logger.debug("call NLS service");
  59. byte[] b = new byte[8000];
  60. int len = 0;
  61. while((len = fis.read(b)) > 0) {
  62. future.sendVoice(b, 0, len);
  63. Thread.sleep(200);
  64. }
  65. logger.debug("send finish signal!");
  66. future.sendFinishSignal();
  67. logger.debug("main thread enter waiting .");
  68. future.await(100000);
  69. } catch(Exception e) {
  70. e.printStackTrace();
  71. }
  72. logger.debug("calling NLS service end");
  73. }
  74. }
  75. @Override
  76. public void onMessageReceived(NlsEvent e) {
  77. NlsResponse response = e.getResponse();
  78. response.getFinish();
  79. if (response.result != null) {
  80. logger.debug("status code = {},get finish is {},get recognize result: {}",
  81. response.getStatusCode(), response.getFinish(), response.getResult());
  82. if (response.getQuality() != null) {
  83. logger.info("Sentence {} is over. Get ended sentence recognize result: {}, voice quality is {}",
  84. response.result.getSentence_id(), response.getResult(), JSON
  85. .toJSONString(response.getQuality()));
  86. }
  87. } else {
  88. logger.info(JSON.toJSONString(response));
  89. }
  90. }
  91. @Override
  92. public void onOperationFailed(NlsEvent e) {
  93. logger.error("status code is {}, on operation failed: {}",e.getResponse().getStatusCode(),e.getErrorMessage());
  94. }
  95. @Override
  96. public void onChannelClosed(NlsEvent e) {
  97. logger.debug("on websocket closed.");
  98. }
  99. /**
  100. * @param args
  101. */
  102. public static void main(String[] args) {
  103. RealtimeDemo lun = new RealtimeDemo();
  104. logger.info("start ....");
  105. if (args.length < 4) {
  106. logger.debug("RealtimeDemo need params: <app-key> <Id> <Secret> <opu-file> ");
  107. System.exit(-1);
  108. }
  109. lun.appKey = args[0];
  110. lun.ak_id = args[1];
  111. lun.ak_secret = args[2];
  112. lun.filePath = args[3];
  113. lun.start();
  114. lun.hearIt();
  115. lun.shutDown();
  116. }
  117. }

关键词识别结果说明

  1. 如果没有设置keyword_list_id,即没有调用req.setKeyWordListId,则返回结果与实时asr返回结果一样;
  2. 如果设置了keyword_list_id,没有命中,返回json形式为:
    1. {
    2. "version": "1.0",
    3. "request_id": "ee621386178e4ae4a2d8171e6c31a4bf",
    4. "status_code": 200,
    5. "finish": 0,
    6. "result": {
    7. "sentence_id": 1,
    8. "begin_time": 300,
    9. "end_time": 3600,
    10. "status_code": 0
    11. }
    12. }
  3. 如果设置了keyword_list_id,且有命中,返回json形式为:
    1. {
    2. "version": "1.0",
    3. "request_id": "ee621386178e4ae4a2d8171e6c31a4bf",
    4. "status_code": 200,
    5. "finish": 0,
    6. "result": {
    7. "sentence_id": 1,
    8. "begin_time": 300,
    9. "end_time": 3600,
    10. "status_code": 0,
    11. "keywords":[
    12. {"keyword":"张三", "confidence":0.5"start_time": 1, "end_time": 10},
    13. {"keyword":"李四", "confidence":0.6"start_time": 1, "end_time": 10}
    14. ]
    15. }
    16. }
    keywords参数说明
字段 类型 说明
keyword String 查询出来的关键词
confidence float 匹配度,范围0-1
start_time Long 开始时间,单位ms
end_time Long 结束时间,单位ms
本文导读目录