Use the POP API to create, retrieve, update, delete, and list business-specific hotword vocabularies from a client application, without configuring them in the console. Each vocabulary contains a set of business-related hotwords and their weights.
API operations
The following operations use HTTPS POST requests and API version 2018-11-20. Place operation-specific parameters in the request body. The example uses CommonRequest from Alibaba Cloud SDK for Java to sign and send RPC requests.
|
Operation |
Purpose |
|
|
Creates a vocabulary and returns a |
|
|
Retrieves a vocabulary and its word weights by ID. |
|
|
Updates the name, description, and complete set of word weights. |
|
|
Deletes a vocabulary by ID. |
|
|
Lists vocabularies by page, without word weights. |
The region is ap-southeast-1 and the endpoint is nls-slp.ap-southeast-1.aliyuncs.com.
Limitations
By default, each account can create up to 10 business-specific hotword vocabularies.
|
Constraint |
Maximum |
|
Hotwords per vocabulary |
128 |
|
Chinese-only hotword |
10 Chinese characters |
|
English-only hotword |
5 English words |
|
Mixed Chinese and English hotword |
10 Chinese characters and English letters in total |
Hotwords must use UTF-8 encoding and must not contain punctuation or special characters. Each hotword weight must be an integer from −6 to 5.
-
A positive weight increases the likelihood that the hotword is recognized. A negative weight decreases it.
-
A weight of −6 suppresses recognition of the hotword as much as possible.
-
A commonly used weight is 2. If the effect is insufficient, increase the weight appropriately. Excessively high weights may reduce recognition accuracy for other words.
Sample code
The example creates, retrieves, updates, and lists vocabularies, then deletes only the vocabulary it created. It does not delete other vocabularies returned by the list operation.
Add dependencies and configure credentials
The example uses aliyun-java-sdk-core 3.7.1 and fastjson 1.2.83. For SDK setup, see Get started. For generic request usage, see Generic calls.
The Alibaba Cloud SDK for Java core library must be version 3.5.0 or later. Versions 4.0.0 and later also require their corresponding third-party dependencies.
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>3.7.1</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
</dependency>
Set ALIYUN_AK_ID and ALIYUN_AK_SECRET to the AccessKey ID and AccessKey secret, respectively. The code reads credentials from these environment variables; no command-line arguments are required.
Call the API
After the update, WordWeights contains only watermelon, demonstrating full replacement. The example creates one vocabulary, so the account must have an available vocabulary slot before execution.
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.profile.DefaultProfile;
import java.util.UUID;
public class AsrVocabPopApiDemo {
private static final String REGION_ID = "ap-southeast-1";
private static final String DOMAIN = "nls-slp.ap-southeast-1.aliyuncs.com";
private final IAcsClient client;
public AsrVocabPopApiDemo(String accessKeyId, String accessKeySecret) {
client = new DefaultAcsClient(DefaultProfile.getProfile(
REGION_ID, accessKeyId, accessKeySecret));
}
private CommonRequest request(String action) {
CommonRequest request = new CommonRequest();
request.setDomain(DOMAIN);
request.setProtocol(ProtocolType.HTTPS);
request.setVersion("2018-11-20");
request.setMethod(MethodType.POST);
request.setAction(action);
return request;
}
private JSONObject execute(CommonRequest request) {
CommonResponse response;
try {
response = client.getCommonResponse(request);
} catch (ClientException e) {
throw new IllegalStateException("Request failed: " + e.getErrCode());
}
JSONObject result = JSONObject.parseObject(response.getData());
if (response.getHttpStatus() != 200) {
throw new IllegalStateException("HTTP " + response.getHttpStatus()
+ ", Code=" + result.getString("Code")
+ ", Message=" + result.getString("Message")
+ ", RequestId=" + result.getString("RequestId"));
}
return result;
}
public String createAsrVocab(String name, String description, JSONObject words) {
CommonRequest request = request("CreateAsrVocab");
request.putBodyParameter("Name", name);
request.putBodyParameter("Description", description);
request.putBodyParameter("WordWeights", words.toJSONString());
return execute(request).getString("VocabId");
}
public JSONObject getAsrVocab(String vocabId) {
CommonRequest request = request("GetAsrVocab");
request.putBodyParameter("Id", vocabId);
return execute(request).getJSONObject("Vocab");
}
public void updateAsrVocab(String vocabId, String name,
String description, JSONObject words) {
CommonRequest request = request("UpdateAsrVocab");
request.putBodyParameter("Id", vocabId);
request.putBodyParameter("Name", name);
request.putBodyParameter("Description", description);
request.putBodyParameter("WordWeights", words.toJSONString());
execute(request);
}
public JSONObject listAsrVocab(int pageNumber, int pageSize) {
CommonRequest request = request("ListAsrVocab");
request.putBodyParameter("PageNumber", pageNumber);
request.putBodyParameter("PageSize", pageSize);
return execute(request).getJSONObject("Page");
}
public void deleteAsrVocab(String vocabId) {
CommonRequest request = request("DeleteAsrVocab");
request.putBodyParameter("Id", vocabId);
execute(request);
}
private static String requiredEnv(String name) {
String value = System.getenv(name);
if (value == null || value.trim().isEmpty()) {
throw new IllegalArgumentException("Set environment variable " + name);
}
return value;
}
public static void main(String[] args) {
AsrVocabPopApiDemo demo = new AsrVocabPopApiDemo(
requiredEnv("ALIYUN_AK_ID"), requiredEnv("ALIYUN_AK_SECRET"));
String name = "example_vocab_" + UUID.randomUUID().toString();
JSONObject words = new JSONObject();
words.put("apple", 3);
words.put("watermelon", 3);
String vocabId = demo.createAsrVocab(name, "Example vocabulary", words);
System.out.println("Created vocabulary: " + vocabId);
try {
System.out.println("Vocabulary: " + demo.getAsrVocab(vocabId));
JSONObject updatedWords = new JSONObject();
updatedWords.put("watermelon", 2);
demo.updateAsrVocab(vocabId, name, "Updated vocabulary", updatedWords);
System.out.println("Updated vocabulary: " + demo.getAsrVocab(vocabId));
JSONObject page = demo.listAsrVocab(1, 10);
System.out.println("Total vocabularies: " + page.getIntValue("TotalItems"));
} finally {
demo.deleteAsrVocab(vocabId);
System.out.println("Deleted example vocabulary: " + vocabId);
}
}
}
Parameter reference
Create a vocabulary
Call CreateAsrVocab to create a vocabulary.
Request parameters
|
Parameter |
Type |
Required |
Description |
|
|
String |
Yes |
The vocabulary name. |
|
|
String |
Yes |
A JSON object serialized as a string. Each key is a hotword of type String, and each value is a weight of type Int. Example: |
|
|
String |
No |
The vocabulary description. |
Response parameters
A successful request returns HTTP 200 with a JSON object in the response body.
|
Parameter |
Type |
Description |
|
|
String |
The request ID. |
|
|
String |
The vocabulary ID. Use this ID to retrieve, update, or delete the vocabulary. |
Retrieve a vocabulary
Call GetAsrVocab to retrieve a vocabulary.
Request parameters
|
Parameter |
Type |
Required |
Description |
|
|
String |
Yes |
The |
Response parameters
|
Parameter |
Type |
Description |
|
|
String |
The request ID. |
|
|
Object |
The vocabulary object, with the following fields. |
Vocab fields
|
Field |
Type |
Description |
|
|
String |
The vocabulary ID, which is the same as the |
|
|
String |
The vocabulary name. |
|
|
String |
The vocabulary description. |
|
|
Int |
The size of the compiled vocabulary. |
|
|
String |
The MD5 value of the compiled vocabulary. |
|
|
String |
The creation time. |
|
|
String |
The last update time. |
|
|
Map |
The hotwords and their weights. |
Update a vocabulary
Call UpdateAsrVocab to update the name, description, and word weights. WordWeights replaces all existing hotwords and weights; it does not append to them. Include all hotwords to retain in the request.
Request parameters
|
Parameter |
Type |
Required |
Description |
|
|
String |
Yes |
The vocabulary ID. |
|
|
String |
Yes |
The updated vocabulary name. |
|
|
String |
Yes |
The complete set of updated word weights, in the same format as for vocabulary creation. |
|
|
String |
No |
The updated vocabulary description. |
Response parameters
A successful request returns HTTP 200. The RequestId field (String) in the JSON response contains the request ID.
Delete a vocabulary
Call DeleteAsrVocab to delete a vocabulary.
Request parameters
|
Parameter |
Type |
Required |
Description |
|
|
String |
Yes |
The ID of the vocabulary to delete. |
Response parameters
A successful request returns HTTP 200. The RequestId field (String) in the JSON response contains the request ID.
List vocabularies
Call ListAsrVocab to retrieve vocabularies by page. Results are sorted by update time in descending order. Hotwords and weights are omitted to reduce the response size.
Request parameters
|
Parameter |
Type |
Required |
Description |
|
|
Int |
No |
The page number. Default: 1. Must be greater than 0. |
|
|
Int |
No |
The number of vocabularies per page. Default: 10. Set this parameter to a value from 10 to 100. |
Response parameters
|
Parameter |
Type |
Description |
|
|
String |
The request ID. |
|
|
Object |
The paginated result, with the following fields. |
Page fields
|
Field |
Type |
Description |
|
|
List<Vocab> |
An array of vocabulary objects. Each object has the |
|
|
Int |
The current page number. |
|
|
Int |
The number of vocabularies per page. |
|
|
Int |
The total number of pages, including any partial last page. |
|
|
Int |
The total number of vocabularies. |
Error codes
If a request fails, the HTTP status is not 200. The JSON response body contains Code, Message, and RequestId. Use the error code and message to troubleshoot the request.
|
Error code |
Description and solution |
|
|
The vocabulary does not exist. Check whether the ID is correct or the vocabulary was deleted. |
|
|
A parameter is invalid. Use |
|
|
The vocabulary count exceeds the limit. By default, each account can create up to 10 vocabularies. |
|
|
A vocabulary error occurred, such as a compilation failure. Check the submitted content based on |
|
|
The required |
|
|
The required |
|
Pagination error code |
Description and solution |
|
|
The page number is invalid. Set |
|
|
The page size is invalid. Set |