This topic describes how to set business-specific hotwords for short sentence recognition, real-time speech recognition, and recording file recognition in software development kit (SDK) examples.
Overview
Business-specific hotword vocabularies that you configure in the console are attached to a project's appkey. You do not need to set them in your code.
For business-specific hotword vocabularies trained using the POP API, you must set the vocabulary ID in the SDK.
SDK settings for hotwords have a higher priority and overwrite console settings.
The POP API supports only the creation of business-specific hotword vocabularies. To activate a vocabulary, you must call an SDK method in your client code to set the business-specific hotword vocabulary ID.
Short sentence recognition
For short sentence recognition, use the advanced parameter vocabulary_id to specify the business-specific hotword vocabulary ID.
Java SDK
First, understand the basic usage of the Java SDK. For more information, see Java SDK.
The SDK does not provide a dedicated method to set the vocabulary_id parameter. You can set the parameter by calling the addCustomedParam method of the SpeechRecognizer class:
public void addCustomedParam(String key, Object value);After you create the SpeechRecognizer object `recognizer` and before you call the `start` method, call this method to set the business-specific hotwords:
SpeechRecognizer recognizer = new SpeechRecognizer(client, getRecognizerListener());
... // Other settings are omitted.
recognizer.addCustomedParam("vocabulary_id", "Your business-specific hotword vocabulary ID");
...iOS SDK
First, understand the basic usage of the iOS SDK. For more information, see iOS SDK.
Set the hotword ID using the setParams method of the RequestParam object.
...
NSMutableDictionary *userParams = [[NSMutableDictionary alloc]init];
[userParams setValue:@"Your business-specific hotword vocabulary ID" forKey:@"vocabulary_id"];
[_recognizeRequestParam setParams:userParams];
...Android SDK
First, understand the basic usage of the Android SDK. For more information, see Android SDK.
Set the hotword ID using the setParams method of the SpeechRecognizer object.
...
String userParamString = "{\"vocabulary_id\":\"Your business-specific hotword vocabulary ID\"}";
speechRecognizer.setParams(userParamString);RESTful API
First, understand the basic usage of the RESTful API. For more information, see RESTful API.
Append the vocabulary_id parameter to the other request parameters in the HTTP request. The following Java example shows how to do this. The approach is similar for other languages.
String url = "http://nls-gateway.cn-shanghai.aliyuncs.com/stream/v1/asr";
String request = url;
request = request + "?appkey=" + appkey;
request = request + "&vocabulary_id=" + "Your business-specific hotword vocabulary ID";
...Real-time speech recognition
For real-time speech recognition, use the advanced parameter vocabulary_id to specify the business-specific hotword vocabulary ID.
Java SDK
First, understand the basic usage of the Java SDK. For more information, see Java SDK.
The SDK does not provide a dedicated method to set the vocabulary_id parameter. You can set the parameter by calling the addCustomedParam method of the SpeechTranscriber class:
public void addCustomedParam(String key, Object value);After you create the SpeechTranscriber object `transcriber` and before you call the `start` method, call this method to set the business-specific hotwords:
SpeechTranscriber transcriber = new SpeechTranscriber(client, getTranscriberListener());
... // Other settings are omitted.
transcriber.addCustomedParam("vocabulary_id", "Your business-specific hotword vocabulary ID");
...C++ SDK
First, understand the basic usage of the C++ SDK. For more information, see C++ SDK.
The SDK does not provide a dedicated method to set the vocabulary_id parameter. You can set the parameter by calling the setPayloadParam method of the SpeechTranscriberRequest class:
/**
* @brief Parameter settings
* @param value A string in JSON map format.
* @return Returns 0 on success or -1 on failure.
*/
int setPayloadParam(const char value);After you create the SpeechTranscriberRequest object `request` and before you call the `start` method, call this method to set the business-specific hotwords:
SpeechTranscriberRequest* request = NlsClient::getInstance()->createTranscriberRequest(callback);
... // Other settings are omitted.
request->setPayloadParam("{\"vocabulary_id\":\"Your business-specific hotword vocabulary ID\"}");
...iOS SDK
First, understand the basic usage of the iOS SDK. For more information, see iOS SDK.
Set the hotword ID using the setParams method of the RequestParam object.
...
NSMutableDictionary *userParams = [[NSMutableDictionary alloc]init];
[userParams setValue:@"Your business-specific hotword vocabulary ID" forKey:@"vocabulary_id"];
[_transRequestParam setParams:userParams];
...Android SDK
First, understand the basic usage of the Android SDK. For more information, see Android SDK.
Set the hotword ID using the setParams method of the SpeechTranscriber object.
...
String userParamString = "{\"vocabulary_id\":\"Your business-specific hotword vocabulary ID\"}";
speechTranscriber.setParams(userParamString);Recording file recognition
For recording file recognition, use the advanced parameter vocabulary_id to specify the business-specific hotword vocabulary ID.
First, understand the basic usage of the recording file recognition API. For more information, see API reference.
You can set the vocabulary_id parameter to a JSON string in the body of the HTTP request. The JSON format is as follows.
{
"app_key": "Your project appkey", // To get an Appkey, go to the console: https://nls-portal.console.aliyun.com/applist
"vocabulary_id": "Your business-specific hotword vocabulary ID",
"file_link": "https://aliyun-nls.oss-cn-hangzhou.aliyuncs.com/asr/fileASR/examples/nls-sample-16k.wav"
}
The following example demonstrates how to set parameters using the Java SDK. The method is similar for SDKs in other languages.
CommonRequest postRequest = new CommonRequest();
... // Other settings are omitted.
/**
* Set request parameters as a JSON string in the request body.
*/
JSONObject taskObject = new JSONObject();
// Set the appkey. To get an Appkey, go to the console: https://nls-portal.console.aliyun.com/applist
taskObject.put(KEY_APP_KEY, appKey);
// Set the audio file access URL
taskObject.put(KEY_FILE_LINK, "https://aliyun-nls.oss-cn-hangzhou.aliyuncs.com/asr/fileASR/examples/nls-sample-16k.wav");
taskObject.put("vocabulary_id", "Your business-specific hotword vocabulary ID");
String task = taskObject.toJSONString();
System.out.println(task);
// Set the preceding JSON string as the body parameter
postRequest.putBodyParameter(KEY_TASK, task);
...