All Products
Search
Document Center

Specify an extended hotword vocabulary through the SDK 2.0

Last Updated: May 22, 2020

Specify extended hotwords managed by using POP API operations in the SDK

If you set extended hotwords for a project in the console, the hotwords are bound to the appkey of the project. You do not need to specify the extended hotword vocabulary in the client code. If you create an extended hotword vocabulary by using the corresponding pctowap open platform (POP) API operation, you need to call the corresponding SDK method to specify the ID of the vocabulary. This topic describes how to set extended hotwords for the short sentence recognition, real-time speech recognition, and recording file recognition services.

Short sentence recognition

In the short sentence recognition service, you need to specify the advanced parameter vocabulary_id to specify the ID of the extended hotword vocabulary.

Java SDK 2.0

For more information about how to use the Java SDK, see Java SDK 2.0 for short sentence recognition. The SDK does not provide a method to set the vocabulary_id parameter. You can set the parameter through the addCustomedParam method of the SpeechRecognizer class.

  1. public void addCustomedParam(String key, Object value);

In this demo, call the addCustomedParam method to specify the extended hotword vocabulary after creating the recognizer object of the SpeechRecognizer class and before calling the start method.

  1. SpeechRecognizer recognizer = new SpeechRecognizer(client, getRecognizerListener());
  2. ······· // Ignore other configurations.
  3. recognizer.addCustomedParam("vocabulary_id", "The ID of your extended hotword vocabulary");
  4. ·······

iOS SDK 2.0

For more information about how to use the iOS SDK, see iOS SDK 2.0 for short sentence recognition.You can call the setParams method of the RequestParam object to specify the ID of the hotword vocabulary.

  1. ......
  2. NSMutableDictionary *userParams = [[NSMutableDictionary alloc]init];
  3. [userParams setValue:@"The ID of your extended hotword vocabulary" forKey:@"vocabulary_id"];
  4. [_recognizeRequestParam setParams:userParams];
  5. ......

Android SDK 2.0

For more information about how to use the Android SDK, see Android SDK 2.0 for short sentence recognition.You can call the setParams method of the SpeechRecognizer object to specify the ID of the hotword vocabulary.

  1. ......
  2. String userParamString = "{\"vocabulary_id\":\"The ID of your extended hotword vocabulary\"}";
  3. speechRecognizer.setParams(userParamString);

RESTful API 2.0

For more information about how to use the RESTful API, see RESTful API 2.0 for short sentence recognition. You can add the vocabulary_id parameter to other request parameters as an HTTP request parameter. The following Java demo shows how to set the request parameters:

  1. String url = "http://nls-slp.ap-southeast-1.aliyuncs.com/stream/v1/asr";
  2. String request = url;
  3. request = request + "?appkey=" + appkey;
  4. request = request + "&vocabulary_id=" + "The ID of your extended hotword vocabulary";
  5. ·····

The procedure for setting the vocabulary_id parameter in the demos of other programming languages is the same as that in the Java demo.

Real-time speech recognition

In the real-time speech recognition service, you need to specify the advanced parameter vocabulary_id to specify the ID of the extended hotword vocabulary.

Java SDK

For more information about how to use the Java SDK, see Java SDK 2.0 for real-time speech recognition. The SDK does not provide a method to set the vocabulary_id parameter. You can set the parameter through the addCustomedParam method of the SpeechTranscriber class.

  1. public void addCustomedParam(String key, Object value);

In this demo, call the addCustomedParam method to specify the extended hotword vocabulary after creating the transcriber object of the SpeechRecognizer class and before calling the start method.

  1. SpeechTranscriber transcriber = new SpeechTranscriber(client, getTranscriberListener());
  2. ······· // Ignore other configurations.
  3. transcriber.addCustomedParam("vocabulary_id", "The ID of your extended hotword vocabulary");
  4. ······

iOS SDK 2.0

For more information about how to use the iOS SDK, see iOS SDK 2.0 for real-time speech recognition.You can call the setParams method of the RequestParam object to specify the ID of the hotword vocabulary.

  1. ......
  2. NSMutableDictionary *userParams = [[NSMutableDictionary alloc]init];
  3. [userParams setValue:@"The ID of your extended hotword vocabulary" forKey:@"vocabulary_id"];
  4. [_transRequestParam setParams:userParams];
  5. ......

Android SDK 2.0

For more information about how to use the Android SDK, see Android SDK 2.0 for real-time speech recognition.You can call the setParams method of the SpeechTranscriber object to specify the ID of the hotword vocabulary.

  1. ......
  2. String userParamString = "{\"vocabulary_id\":\"The ID of your extended hotword vocabulary\"}";
  3. speechTranscriber.setParams(userParamString);

Recording file recognition

In the recording file recognition service, you need to specify the advanced parameter vocabulary_id to specify the ID of the extended hotword vocabulary.

For more information about how to use the recording file recognition service, see the API description of the recording file recognition service. Same as other request parameters, the vocabulary_id parameter can be set in JSON format to the body of the HTTP request.

  1. {
  2. "app_key": "The appkey of your project",
  3. "vocabulary_id": "The ID of your extended hotword vocabulary",
  4. "file_link": "https://aliyun-nls.oss-cn-hangzhou.aliyuncs.com/asr/fileASR/examples/nls-sample-16k.wav"
  5. }

The following Java demo shows how to set the request parameters:

  1. CommonRequest postRequest = new CommonRequest();
  2. ······· // Ignore other configurations.
  3. /**
  4. * Set parameters for the recording file recognition request and add request parameters to the request body as a JSON-formatted string.
  5. */
  6. JSONObject taskObject = new JSONObject();
  7. // The appkey of the project.
  8. taskObject.put(KEY_APP_KEY, appKey);
  9. // The URL of the recording file to be recognized.
  10. taskObject.put(KEY_FILE_LINK, "https://aliyun-nls.oss-cn-hangzhou.aliyuncs.com/asr/fileASR/examples/nls-sample-16k.wav");
  11. taskObject.put("vocabulary_id", "The ID of your extended hotword vocabulary");
  12. String task = taskObject.toJSONString();
  13. System.out.println(task);
  14. // The request body, which includes request parameters in a JSON-formatted string.
  15. postRequest.putBodyParameter(KEY_TASK, task);
  16. ·······

The procedure for setting the vocabulary_id parameter in the demos of other programming languages is the same as that in the Java demo.