java sdk 使用说明
功能介绍
kws(关键词服务)的java sdk同asr实时长语音sdk。针对kws的功能,主要提供了关键词表增删改查的util类,以及用来设置keyword_list_id(关键词表id)的参数。
SDK下载地址
javasdk
相关接口说明
- 关键词表维护接口:om.alibaba.idst.nls.utils.KwsMngUtil
该类中包含了所有增删改查的静态方法,调用方式参见下面代码示例。
- 关键词表id设置接口:public void setKeyWordListId(String keyWordListId)
该方法位于NlsRequest中,NlsRequest的其它方法说明参见实时asr java sdk文档,调用方式参加下面代码示例。
关键词表维护代码示例
- public static void main(String[] args) {
- String akId = args[0];
- String akSecret = args[1];
- String json = "{\n"
- + " \"keywords\": [\n"
- + " {\"keyword\":\"张三\"},\n"
- + " {\"keyword\":\"李四\"}\n"
- + " ]\n"
- + "}";
- String updateJson = "{\n"
- + " \"keywords\": [\n"
- + " {\"keyword\":\"张四\"},\n"
- + " {\"keyword\":\"李三\"}\n"
- + " ]\n"
- + "}";
- String response = KwsMngUtil.create(akId, akSecret, json);
- logger.info("create response:{}", response);
- String keywordListId = (String)JSONPath.read(response, "$.keyword_list_id");
- response=KwsMngUtil.update(akId, akSecret, keywordListId,updateJson);
- logger.info("update response:{}", response);
- response = KwsMngUtil.get(akId, akSecret, keywordListId);
- logger.info("get response:{}", response);
- response = KwsMngUtil.delete(akId, akSecret, keywordListId);
- logger.info("delete :{}, response:{}", keywordListId, response);
- }
关键词识别代码示例
- package com.alibaba.idst.nls;
- import com.alibaba.fastjson.JSON;
- import com.alibaba.idst.nls.event.NlsEvent;
- import com.alibaba.idst.nls.event.NlsListener;
- import com.alibaba.idst.nls.protocol.NlsRequest;
- import com.alibaba.idst.nls.protocol.NlsResponse;
- import java.io.File;
- import java.io.FileInputStream;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- /**
- * Created by songsong.sss on 16/6/22.
- */
- public class RealtimeDemo implements NlsListener {
- private NlsClient client = new NlsClient();
- private static final String asrSC = "pcm";
- static Logger logger = LoggerFactory.getLogger(RealtimeDemo.class);
- public String filePath = "";
- public String appKey = "";
- String ak_id = "";
- String ak_secret = "";
- public RealtimeDemo() {
- }
- public void shutDown() {
- logger.debug("close NLS client manually!");
- client.close();
- logger.debug("demo done");
- }
- public void start() {
- logger.debug("init Nls client...");
- client.init();
- }
- public void hearIt() {
- logger.debug("open audio file...");
- FileInputStream fis = null;
- try {
- File file = new File(filePath);
- fis = new FileInputStream(file);
- } catch (Exception e) {
- e.printStackTrace();
- }
- if(fis != null) {
- logger.debug("create NLS future");
- try {
- NlsRequest req = new NlsRequest();
- req.setAppkey(appKey);
- req.setFormat(asrSC);
- req.setResponseMode("streaming");
- req.setSampleRate(16000);
- //用户根据[热词文档](~~49179~~) 设置自定义热词。
- //通过设置VocabularyId调用热词。
- //req.setVocabularyId("");
- //设置关键词库ID 使用时请修改为自定义的词库ID
- req.setKeyWordListId("c1391f1c1f1b4002936893c6d97592f3");
- // the id and the id secret
- req.authorize(ak_id, ak_secret);
- NlsFuture future = client.createNlsFuture(req,this);
- logger.debug("call NLS service");
- byte[] b = new byte[8000];
- int len = 0;
- while((len = fis.read(b)) > 0) {
- future.sendVoice(b, 0, len);
- Thread.sleep(200);
- }
- logger.debug("send finish signal!");
- future.sendFinishSignal();
- logger.debug("main thread enter waiting .");
- future.await(100000);
- } catch(Exception e) {
- e.printStackTrace();
- }
- logger.debug("calling NLS service end");
- }
- }
- @Override
- public void onMessageReceived(NlsEvent e) {
- NlsResponse response = e.getResponse();
- response.getFinish();
- if (response.result != null) {
- logger.debug("status code = {},get finish is {},get recognize result: {}",
- response.getStatusCode(), response.getFinish(), response.getResult());
- if (response.getQuality() != null) {
- logger.info("Sentence {} is over. Get ended sentence recognize result: {}, voice quality is {}",
- response.result.getSentence_id(), response.getResult(), JSON
- .toJSONString(response.getQuality()));
- }
- } else {
- logger.info(JSON.toJSONString(response));
- }
- }
- @Override
- public void onOperationFailed(NlsEvent e) {
- logger.error("status code is {}, on operation failed: {}",e.getResponse().getStatusCode(),e.getErrorMessage());
- }
- @Override
- public void onChannelClosed(NlsEvent e) {
- logger.debug("on websocket closed.");
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- RealtimeDemo lun = new RealtimeDemo();
- logger.info("start ....");
- if (args.length < 4) {
- logger.debug("RealtimeDemo need params: <app-key> <Id> <Secret> <opu-file> ");
- System.exit(-1);
- }
- lun.appKey = args[0];
- lun.ak_id = args[1];
- lun.ak_secret = args[2];
- lun.filePath = args[3];
- lun.start();
- lun.hearIt();
- lun.shutDown();
- }
- }
关键词识别结果说明
- 如果没有设置keyword_list_id,即没有调用req.setKeyWordListId,则返回结果与实时asr返回结果一样;
- 如果设置了keyword_list_id,没有命中,返回json形式为:{
- "version": "1.0",
- "request_id": "ee621386178e4ae4a2d8171e6c31a4bf",
- "status_code": 200,
- "finish": 0,
- "result": {
- "sentence_id": 1,
- "begin_time": 300,
- "end_time": 3600,
- "status_code": 0
- }
- }
如果设置了keyword_list_id,且有命中,返回json形式为:
- {
- "version": "1.0",
- "request_id": "ee621386178e4ae4a2d8171e6c31a4bf",
- "status_code": 200,
- "finish": 0,
- "result": {
- "sentence_id": 1,
- "begin_time": 300,
- "end_time": 3600,
- "status_code": 0,
- "keywords":[
- {"keyword":"张三", "confidence":0.5,"start_time": 1, "end_time": 10},
- {"keyword":"李四", "confidence":0.6,"start_time": 1, "end_time": 10}
- ]
- }
- }
keywords参数说明