All Products
Search
Document Center

Intelligent Speech Interaction:Use an SDK to specify the ID of a business-specific hotword vocabulary

更新时间:Apr 13, 2022

This topic describes how to use an SDK to specify the ID of your business-specific hotword vocabulary in the short sentence recognition, real-time speech recognition, and recording file recognition services.

If you use the Intelligent Speech Interaction console to create and associate hotwords with the appkey of a project, you do not need to specify a hotword vocabulary in your code. If you use the Alibaba Cloud pctowap open platform (POP) API to create business-specific hotwords, you must specify the ID of the hotword vocabulary in the SDK.

Short sentence recognition

In the short sentence recognition service, you must set the vocabulary_id advanced parameter to specify the ID of your business-specific hotword vocabulary.

SDK for Java

Note

Before you begin, make sure that you know how to use the SDK for Java. For more information, see SDK for Java.

The SDK does not provide a 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 recognizer object of the SpeechRecognizer class and before you call the start method, call the addCustomedParam method to specify the ID of your business-specific hotword vocabulary. For more information, see the following sample code:

SpeechRecognizer recognizer = new SpeechRecognizer(client, getRecognizerListener());
... // Other settings are omitted.
recognizer.addCustomedParam("vocabulary_id", "The ID of your business-specific hotword vocabulary");
...

SDK for iOS

Note

Before you begin, make sure that you know how to use the SDK for iOS. For more information, see NUI SDK for iOS.

You can call the setParams method of the RequestParam class to specify the ID of your business-specific hotword vocabulary.

...
NSMutableDictionary *userParams = [[NSMutableDictionary alloc]init];
[userParams setValue:@"The ID of your business-specific hotword vocabulary" forKey:@"vocabulary_id"];
[_recognizeRequestParam setParams:userParams];
...

SDK for Android

Note

Before you begin, make sure that you know how to use the SDK for Android. For more information, see NUI SDK for Android.

You can call the setParams method of the SpeechRecognizer class to specify the ID of your business-specific hotword vocabulary.

...
String userParamString = "{\"vocabulary_id\":\"The ID of your business-specific hotword vocabulary\"}";
speechRecognizer.setParams(userParamString);

RESTful API

Note

Before you begin, make sure that you know how to use the RESTful API. For more information, see RESTful API.

You can append the vocabulary_id parameter to other request parameters in the HTTP request. The following sample code shows how to set the vocabulary_id parameter in Java. If you use another programming language, set the vocabulary_id parameter in a similar way.

String url = "http://nls-gateway.ap-southeast-1.aliyuncs.com/stream/v1/asr";
String request = url;
request = request + "? appkey=" + appkey;
request = request + "&vocabulary_id=" + "The ID of your business-specific hotword vocabulary";
...

Real-time speech recognition

In the real-time speech recognition service, you must set the vocabulary_id advanced parameter to specify the ID of your business-specific hotword vocabulary.

SDK for Java

Note

Before you begin, make sure that you know how to use the SDK for Java. For more information, see SDK for Java.

The SDK does not provide a 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 transcriber object of the SpeechTranscriber class and before you call the start method, call the addCustomedParam method to specify the ID of your business-specific hotword vocabulary. For more information, see the following sample code:

SpeechTranscriber transcriber = new SpeechTranscriber(client, getTranscriberListener());
... // Other settings are omitted.
transcriber.addCustomedParam("vocabulary_id", "The ID of your business-specific hotword vocabulary");
...

SDK for C++

Note

Before you begin, make sure that you know how to use the SDK for C++. For more information, see SDK for C++.

The SDK does not provide a method to set the vocabulary_id parameter. You can set the parameter by calling the setPayloadParam method of the SpeechTranscriberRequest class.

/**
 * @brief Set a parameter.
 * @param value A string in JSON map format.
 * @return A value of 0 is returned if the parameter is set. Otherwise, a value of -1 is returned.
 */
int setPayloadParam(const char value);

After you create the request object of the SpeechTranscriberRequest class and before you call the start method, call the setPayloadParam method to specify the ID of your business-specific hotword vocabulary. For more information, see the following sample code:

SpeechTranscriberRequest* request = NlsClient::getInstance()->createTranscriberRequest(callback);
... // Other settings are omitted.
request->setPayloadParam("{\"vocabulary_id\":\"The ID of your business-specific hotword vocabulary\"}");
...

SDK for iOS

Note

Before you begin, make sure that you know how to use the SDK for iOS. For more information, see SDK for iOS.

You can call the setParams method of the RequestParam class to specify the ID of your business-specific hotword vocabulary.

...
NSMutableDictionary *userParams = [[NSMutableDictionary alloc]init];
[userParams setValue:@"The ID of your business-specific hotword vocabulary" forKey:@"vocabulary_id"];
[_transRequestParam setParams:userParams];
...

SDK for Android

Note

Before you begin, make sure that you know how to use the SDK for Android. For more information, see SDK for Android.

You can call the setParams method of the SpeechTranscriber class to specify the ID of your business-specific hotword vocabulary.

...
String userParamString = "{\"vocabulary_id\":\"The ID of your business-specific hotword vocabulary\"}";
speechTranscriber.setParams(userParamString);

Recording file recognition

In the recording file recognition service, you must set the vocabulary_id advanced parameter to specify the ID of your business-specific hotword vocabulary.

Note

Before you begin, make sure that you know how to use the recording file recognition API. For more information, see API reference.

You can set the vocabulary_id parameter in JSON format to the body of the HTTP request in the same way as setting other request parameters. The following sample code shows how to set the vocabulary_id parameter in JSON format:

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

The following sample code shows how to set parameters in the SDK for Java. If you use another programming language, set parameters in a similar way.

CommonRequest postRequest = new CommonRequest();
 ... // Other settings are omitted.
/**
 * Set request parameters in JSON format to the body of the HTTP request.
 */
JSONObject taskObject = new JSONObject();
// Set the appkey of your project.
taskObject.put(KEY_APP_KEY, appKey);
// Set the URL of the recording file to be recognized.
taskObject.put(KEY_FILE_LINK, "https://aliyun-nls.oss-cn-hangzhou.aliyuncs.com/asr/fileASR/examples/nls-sample-16k.wav");
taskObject.put("vocabulary_id", "The ID of your business-specific hotword vocabulary");
String task = taskObject.toJSONString();
System.out.println(task);
// Set the request body, which includes request parameters in a JSON-formatted string.
postRequest.putBodyParameter(KEY_TASK, task);
...