Use the Java SDK to create, query, update, and delete custom vocabularies for speech recognition.
Endpoint
The SDK uses the China (Beijing) endpoint by default. To switch to a different region, modify Constants.baseHttpApiUrl before initialization.
- Singapore
- China (Beijing)
https://{WorkspaceId}.ap-southeast-1.maas.aliyuncs.com/api/v1Replace {WorkspaceId} with your actual Workspace ID.Constants.baseHttpApiUrl before initialization:
- API keys differ across regions. Use the API key that matches your region.
- The region setting is global and affects all DashScope API calls.
VocabularyService
Package: com.alibaba.dashscope.audio.asr.vocabulary.VocabularyService
Description: Creates, queries, updates, and deletes custom vocabularies.
Constructor
Parameter | Type | Description |
|---|---|---|
apiKey | String | DashScope API key |
createVocabulary() - Create a custom vocabulary
Method signature:
Parameter | Type | Required | Description |
|---|---|---|---|
targetModel | String | Yes | The speech recognition model that uses this vocabulary. This value must match the model you specify when calling the speech recognition API. |
prefix | String | Yes | A custom prefix for the vocabulary. Only lowercase letters and digits are allowed, with a maximum length of 10 characters. |
vocabulary | JsonArray | Yes | The list of hotwords. Each JsonObject contains fields such as text, weight, and lang. For more information, see Hotword object structure. |
Type | Description |
|---|---|
Vocabulary | A custom vocabulary object that contains the vocabulary ID and other metadata. |
Exception | Description |
|---|---|
NoApiKeyException | The API key is empty. |
InputRequiredException | A required parameter is empty. |
listVocabulary() - List custom vocabularies
Method signature:
Parameter | Type | Required | Description |
|---|---|---|---|
prefix | String | No | The custom prefix of the vocabulary. When specified, only vocabularies with this prefix are returned. |
pageIndex | int | No | The page number, starting from 0. Default value: 0. |
pageSize | int | No | The number of entries per page. Default value: 10. |
Type | Description |
|---|---|
Vocabulary[] | An array of custom vocabulary objects. |
Field | Type | Description |
|---|---|---|
vocabularyId | String | The vocabulary ID. |
gmtCreate | String | The creation time. |
gmtModified | String | The last modification time. |
status | String | The status:
|
Exception | Description |
|---|---|
NoApiKeyException | The API key is empty. |
InputRequiredException | A required parameter is empty. |
queryVocabulary() - Query a custom vocabulary
Method signature:
Parameter | Type | Required | Description |
|---|---|---|---|
vocabularyId | String | Yes | The ID of the custom vocabulary to query. |
Type | Description |
|---|---|
Vocabulary | A |
Field | Type | Description |
|---|---|---|
vocabulary | JsonArray | The custom vocabulary content. |
targetModel | String | The speech recognition model that uses this vocabulary. This value must match the model you specify when calling the speech recognition API. |
gmtCreate | String | The creation time. |
gmtModified | String | The last modification time. |
status | String | The status:
|
Exception | Description |
|---|---|
NoApiKeyException | The API key is empty. |
InputRequiredException | A required parameter is empty. |
updateVocabulary() - Update a custom vocabulary
Method signature:
Parameter | Type | Required | Description |
|---|---|---|---|
vocabularyId | String | Yes | The ID of the vocabulary to update. |
vocabulary | JsonArray | Yes | The new vocabulary. This completely replaces the existing entries. |
Exception | Description |
|---|---|
NoApiKeyException | The API key is empty. |
InputRequiredException | A required parameter is empty. |
deleteVocabulary() - Delete a custom vocabulary
Method signature:
Parameter | Type | Required | Description |
|---|---|---|---|
vocabularyId | String | Yes | The ID of the vocabulary to delete. |
Exception | Description |
|---|---|
NoApiKeyException | The API key is empty. |
InputRequiredException | A required parameter is empty. |
Vocabulary class
Package: com.alibaba.dashscope.audio.asr.vocabulary.Vocabulary
Description: Stores the metadata and content of a custom vocabulary.
Methods
Method | Return type | Description |
|---|---|---|
getVocabularyId() | String | Returns the vocabulary ID. |
getTargetModel() | String | Returns the target model. |
getVocabulary() | JsonArray | Returns the custom vocabulary content. |
getStatus() | String | Returns the status. |
getGmtCreate() | String | Returns the creation time. |
getGmtModified() | String | Returns the last modified time. |
getData() | JsonObject | Returns the complete data in JSON format. |
Hotword object structure
Fields in each hotword JsonObject:
Field | Type | Required | Description |
|---|---|---|---|
text | String | Yes | The vocabulary entry text. The text language must be supported by the selected model. Supported languages vary by model. Use actual words rather than arbitrary character combinations to improve recognition accuracy. Maximum length: 15 characters for text that includes non-ASCII characters, or 7 space-separated words for ASCII-only text. |
weight | int | Yes | The vocabulary entry weight. Recommended value: 4. Valid values: 1 to 5. If recognition accuracy doesn't improve, increase the weight. An excessively high weight may reduce the recognition accuracy of other words. |
lang | String | No | The language code of the audio to be recognized. When set, the system improves recognition of vocabulary entries in the specified language. If you can't determine the language in advance, leave this parameter unset. The model detects the language automatically. Valid values (vary by model):
|