开发者社区> 问答> 正文

java sdk是什么?



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文档,调用方式参加下面代码示例。

关键词表维护代码示例

<pre style='background: rgb(246, 246, 246); font: 12px/1.6 "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Helvetica, monospace, monospace; margin: 0px 0px 16px; padding: 10px; outline: 0px; border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; overflow: auto; word-spacing: 0px; white-space: pre-wrap; word-wrap: break-word; box-sizing: border-box; orphans: 2; widows: 2; font-size-adjust: none; font-stretch: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;' prettyprinted?="" linenums="">
  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.     }


关键词识别代码示例

<pre style='background: rgb(246, 246, 246); font: 12px/1.6 "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Helvetica, monospace, monospace; margin: 0px 0px 16px; padding: 10px; outline: 0px; border-radius: 3px; border: 1px solid rgb(221, 221, 221); color: rgb(51, 51, 51); text-transform: none; text-indent: 0px; letter-spacing: normal; overflow: auto; word-spacing: 0px; white-space: pre-wrap; word-wrap: break-word; box-sizing: border-box; orphans: 2; widows: 2; font-size-adjust: none; font-stretch: normal; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;' prettyprinted?="" linenums="">
  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形式为:<pre style='background: rgb(246, 246, 246); font: 12px/1.6 "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Helvetica, monospace, monospace; margin: 0px 0px 16px; padding: 10px; outline: 0px; border-radius: 3px; border: 1px solid rgb(221, 221, 221); overflow: auto; white-space: pre-wrap; word-wrap: break-word; box-sizing: border-box; font-size-adjust: none; font-stretch: normal;' prettyprinted?="" linenums="">{
  3. "version": "1.0",
  4. "request_id": "ee621386178e4ae4a2d8171e6c31a4bf",
  5. "status_code": 200,
  6. "finish": 0,
  7. "result": {
  8.      "sentence_id": 1,
  9.      "begin_time": 300,
  10.      "end_time": 3600,
  11.      "status_code": 0
  12. }
  13. }
如果设置了keyword_list_id,且有命中,返回json形式为:<pre style='background: rgb(246, 246, 246); font: 12px/1.6 "YaHei Consolas Hybrid", Consolas, "Meiryo UI", "Malgun Gothic", "Segoe UI", "Trebuchet MS", Helvetica, monospace, monospace; margin: 0px 0px 16px; padding: 10px; outline: 0px; border-radius: 3px; border: 1px solid rgb(221, 221, 221); overflow: auto; white-space: pre-wrap; word-wrap: break-word; box-sizing: border-box; font-size-adjust: none; font-stretch: normal;' prettyprinted?="" linenums="">
  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参数说明
字段类型说明
keywordString查询出来的关键词
confidencefloat匹配度,范围0-1
start_timeLong开始时间,单位ms
end_timeLong结束时间,单位ms

展开
收起
nicenelly 2017-10-25 15:16:16 2127 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
从 SDK 到编解码:视频直播架构解析 立即下载
跨平台的云服务SDK需要什么 立即下载
一个跨平台的云服务SDK需要什么 立即下载