Skip to main content
Custom hotwords

Custom hotwords Java SDK

Use the Java SDK to create, query, update, and delete custom vocabularies for speech recognition.

User guide: Custom hotwords.

Prerequisites

Service URL

Set the base URL before creating the service:
import com.alibaba.dashscope.utils.Constants;

Constants.baseHttpApiUrl = "https://dashscope-intl.aliyuncs.com/api/v1";

VocabularyService class

Package: com.alibaba.dashscope.audio.asr.vocabulary.VocabularyService Manages the lifecycle of custom vocabularies (create, list, query, update, delete).

Constructor

public VocabularyService(String apiKey)
ParameterTypeRequiredDescription
apiKeyStringYesYour DashScope API key.

createVocabulary()

Create a custom vocabulary.
public Vocabulary createVocabulary(String targetModel, String prefix,
                                   JsonArray vocabulary)
    throws NoApiKeyException, InputRequiredException
ParameterTypeRequiredDescription
targetModelStringYesThe speech recognition model that uses this vocabulary. Must match the model you specify when calling the speech recognition API.
prefixStringYesA custom prefix for the vocabulary. Only lowercase letters and digits are allowed, max 10 characters.
vocabularyJsonArrayYesThe vocabulary entries. See Hotword object structure.
Returns: Vocabulary — use getVocabularyId() to retrieve the created vocabulary ID.

listVocabulary()

List custom vocabularies filtered by prefix.
public Vocabulary[] listVocabulary(String prefix)
    throws NoApiKeyException, InputRequiredException

public Vocabulary[] listVocabulary(String prefix, int pageIndex, int pageSize)
    throws NoApiKeyException, InputRequiredException
ParameterTypeRequiredDescription
prefixStringYesFilter by vocabulary prefix.
pageIndexintNoPage number, starting from 0. Default: 0.
pageSizeintNoNumber of entries per page. Default: 10.
Returns: Vocabulary[] — each element has the following methods:
MethodReturn typeDescription
getVocabularyId()StringThe vocabulary ID.
getGmtCreate()StringThe creation time.
getGmtModified()StringThe last modification time.
getStatus()StringOK: Ready. UNDEPLOYED: Not available.

queryVocabulary()

Query details of a specific custom vocabulary.
public Vocabulary queryVocabulary(String vocabularyId)
    throws NoApiKeyException, InputRequiredException
ParameterTypeRequiredDescription
vocabularyIdStringYesThe ID of the custom vocabulary to query.
Returns: Vocabulary — has the following methods:
MethodReturn typeDescription
getVocabularyId()StringThe vocabulary ID.
getTargetModel()StringThe speech recognition model.
getVocabulary()JsonArrayThe vocabulary entries.
getStatus()StringOK: Ready. UNDEPLOYED: Not available.
getGmtCreate()StringThe creation time.
getGmtModified()StringThe last modification time.
getData()JsonObjectThe complete data in JSON format.

updateVocabulary()

Update a custom vocabulary. This completely replaces the existing entries.
public void updateVocabulary(String vocabularyId, JsonArray vocabulary)
    throws NoApiKeyException, InputRequiredException
ParameterTypeRequiredDescription
vocabularyIdStringYesThe ID of the vocabulary to update.
vocabularyJsonArrayYesThe new vocabulary entries. See Hotword object structure.
Returns: void

deleteVocabulary()

Delete a custom vocabulary.
public void deleteVocabulary(String vocabularyId)
    throws NoApiKeyException, InputRequiredException
ParameterTypeRequiredDescription
vocabularyIdStringYesThe ID of the vocabulary to delete.
Returns: void

Hotword object structure

Each entry in the vocabulary JsonArray has the following fields:
FieldTypeRequiredDescription
textStringYesThe 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.
weightintYesThe 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.
langStringNoThe 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).