Use the Java SDK to create, query, update, and delete custom vocabularies for speech recognition.
User guide: Custom hotwords.
Set the base URL before creating the service:
Package:
Create a custom vocabulary.
Returns:
List custom vocabularies filtered by prefix.
Returns:
Query details of a specific custom vocabulary.
Returns:
Update a custom vocabulary. This completely replaces the existing entries.
Returns:
Delete a custom vocabulary.
Returns:
Each entry in the
Prerequisites
- An API key configured as the
DASHSCOPE_API_KEYenvironment variable - The latest DashScope SDK
Service URL
Set the base URL before creating the service:
VocabularyService class
Package: com.alibaba.dashscope.audio.asr.vocabulary.VocabularyService
Manages the lifecycle of custom vocabularies (create, list, query, update, delete).
Constructor
| Parameter | Type | Required | Description |
|---|---|---|---|
| apiKey | String | Yes | Your DashScope API key. |
createVocabulary()
Create a custom vocabulary.
| Parameter | Type | Required | Description |
|---|---|---|---|
| targetModel | String | Yes | The speech recognition model that uses this vocabulary. 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, max 10 characters. |
| vocabulary | JsonArray | Yes | The vocabulary entries. See Hotword object structure. |
Vocabulary — use getVocabularyId() to retrieve the created vocabulary ID.
listVocabulary()
List custom vocabularies filtered by prefix.
| Parameter | Type | Required | Description |
|---|---|---|---|
| prefix | String | Yes | Filter by vocabulary prefix. |
| pageIndex | int | No | Page number, starting from 0. Default: 0. |
| pageSize | int | No | Number of entries per page. Default: 10. |
Vocabulary[] — each element has the following methods:
| Method | Return type | Description |
|---|---|---|
| getVocabularyId() | String | The vocabulary ID. |
| getGmtCreate() | String | The creation time. |
| getGmtModified() | String | The last modification time. |
| getStatus() | String | OK: Ready. UNDEPLOYED: Not available. |
queryVocabulary()
Query details of a specific custom vocabulary.
| Parameter | Type | Required | Description |
|---|---|---|---|
| vocabularyId | String | Yes | The ID of the custom vocabulary to query. |
Vocabulary — has the following methods:
| Method | Return type | Description |
|---|---|---|
| getVocabularyId() | String | The vocabulary ID. |
| getTargetModel() | String | The speech recognition model. |
| getVocabulary() | JsonArray | The vocabulary entries. |
| getStatus() | String | OK: Ready. UNDEPLOYED: Not available. |
| getGmtCreate() | String | The creation time. |
| getGmtModified() | String | The last modification time. |
| getData() | JsonObject | The complete data in JSON format. |
updateVocabulary()
Update a custom vocabulary. This completely replaces the existing entries.
| Parameter | Type | Required | Description |
|---|---|---|---|
| vocabularyId | String | Yes | The ID of the vocabulary to update. |
| vocabulary | JsonArray | Yes | The new vocabulary entries. See Hotword object structure. |
void
deleteVocabulary()
Delete a custom vocabulary.
| Parameter | Type | Required | Description |
|---|---|---|---|
| vocabularyId | String | Yes | The ID of the vocabulary to delete. |
void
Hotword object structure
Each entry in the vocabulary JsonArray has the following fields:
| Field | Type | Required | Description |
|---|---|---|---|
| text | String | Yes | The vocabulary entry text. The text language must be supported by the selected model. Use actual words rather than arbitrary character combinations. 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. Valid values vary by model. Fun-ASR: zh (Chinese), en (English), ja (Japanese). |