用户词典(单词注册・单词强调)
AmiVoice API 具有用户词典功能,当需要识别未被识别或难以识别的单词时, Hybrid 引擎可以进行"单词注册",而 End to End 引擎可以进行"单词强调"。
这里首先介绍一个可以快速尝试用户词典功能的步骤。
快速入门
要运行 Python 示例代码,需要安装 requests 库。 将以下代码保存为 quickstart.py 等文件名,
python -m pip install requests
然后使用以下命令运行:
python quickstart.py
根据环境不同,可能需要使用 python3 或 py 替代 python。
注册用户词典
使用用户词典注册 API 或通过 MyPage 进行用户词典注册。这里我们将在"会話_汎用"引擎(-a-general)中注册"とりぷるだぶる"这个发音,使其被识别为"WWW",如下所示:
- macOS / Linux
- Windows (PowerShell)
- Windows (命令提示符)
- MyPage
- Python
- 将
{API_key}替换为您账户的 API key。 - 将
{profile_id}替换为您想使用的 profileId。例如,如果您想使用test作为 profile ID,请在以下{profile_id}处填写test。
curl https://acp-api.amivoice.com/profilewords/-a-general/{profile_id} \
-H "Authorization: Bearer {API_key}" \
-H "Content-Type: application/json" \
-d '{"profilewords":[{"written":"WWW","spoken":"とりぷるだぶる"}]}' | jq
- 如果未安装
curl命令,请从 https://curl.se/ 下载适用于您操作系统的包,或使用包管理器安装 curl。 - 语音识别结果格式使用 Unicode 转义。上述命令使用
jq来美化响应。如果未安装jq,请尝试删除| jq部分后执行。可以从 https://stedolan.github.io/jq/ 下载适用于您操作系统的jq包,或使用包管理器安装。
- 将
{API_key}替换为您账户的 API key。 - 将
{profile_id}替换为您想使用的 profileId。例如,如果您想使用test作为 profileId,请在以下{profile_id}处填写test。
curl.exe https://acp-api.amivoice.com/profilewords/-a-general/{profile_id} `
-H "Authorization: Bearer {API_key}" `
-H "Content-Type: application/json" `
-d '{"profilewords":[{"written":"WWW","spoken":"とりぷるだぶる"}]}' | jq
- 在 PowerShell 中,
curl是Invoke-WebRequest的别名,因此请明确使用curl.exe。Windows 10 版本 1803 及更高版本默认包含curl.exe。如果未包含,请从 https://curl.se/ 安装。 - 语音识别结果格式使用 Unicode 转义。上述命令使用
jq来美化响应。如果未安装jq,请尝试删除| jq部分后执行。可以从 https://stedolan.github.io/jq/ 下载适用于您操作系统的jq包,或使用包管理器安装。
- 将
{API_key}替换为您账户的 API key。 - 将
{profile_id}替换为您想使用的 profileId。例如,如果您想使用test作为 profileId,请在以下{profile_id}处填写test。
curl https://acp-api.amivoice.com/profilewords/-a-general/{profile_id} ^
-H "Authorization: Bearer {API_key}" ^
-H "Content-Type: application/json" ^
-d "{""profilewords"":[{""written"":""WWW"",""spoken"":""とりぷるだぶる""}]}" | jq
- Windows 10 版本 1803 及更高版本默认包含
curl。如果未包含,请从 https://curl.se/ 安装。 - 在命令提示符中,不能使用
'(单引号)来指定 JSON 字符串,请使用"(双引号)。同时,内部使用的双引号需要转义(使用""或\")。 - 语音识别结果格式使用 Unicode 转义。上述命令使用
jq来美化响应。如果未安装jq,请尝试删除| jq部分后执行。可以从 https://stedolan.github.io/jq/ 下载适用于您操作系统的jq包,或使用包管理器安装。
在 MyPage 上的用户词典注册可以在"Recognition Rate Adjustment"页面内的"User Dictionary Registration"部分(可以通过此链接直接访问)进行。访问需要登录。
按照以下步骤直接输入并注册单词:
- 从引擎名称下拉菜单中选择 [-a-general],然后点击 [Confirm]。
- 从 [New Word Registration] 中选择 [Register New Words by Direct Input] 标签,在 [Written] 中输入"WWW",在 [Spoken] 中输入"とりぷるだぶる"。
- 点击 [Add] 按钮。
- 确认输入的单词显示在 [Registered Word List] 中即完成。
在 MyPage 上注册用户词典时,注册目标配置文件的 profileId 就是 Service ID。例如,如果 Service ID 是"user01",则 profileId 也是"user01"。
- 将
{API_key}替换为您账户的 API key。 - 将
{profile_id}替换为您想使用的 profileId。例如,如果您想使用test作为 profileId,请在以下{profile_id}处填写test。
import requests
import json
dgn = "-a-general" # 指定连接引擎名称
pid = "{profile_id}" # 请替换为您要使用的 profileId
api_key = "{API_key}" # 请替换为实际的 API key
endpoint = f"https://acp-api.amivoice.com/profilewords/{dgn}/{pid}"
headers = {
"Authorization": f"Bearer {api_key}",
"Content-Type": "application/json"
}
data = {
"profilewords": [
{"written": "WWW", "spoken": "とりぷるだぶる"}
]
}
# 发送 POST 请求
response = requests.post(endpoint, headers=headers, json=data)
# 检查响应状态码
if response.status_code == 200:
# 显示 JSON 响应
response_json = response.json()
print(json.dumps(response_json, indent=4, ensure_ascii=False))
else:
print(f"Error: {response.status_code}{response.text}")
准备音频文件
准备要转写的音频文件。可以直接使用以下示例音频(www.wav)。
关于支持的音频文件格式,请参阅音频格式说明。
执行语音识别
请执行以下操作。将 {API_key} 替换为您账户的 API key,将 {profile_id} 替换为要使用的配置文件的 profileId,将 www.wav 替换为要使用的音频文件的路径。
- curl (macOS / Linux)
- curl (Windows PowerShell)
- curl (Windows 命令提示符)
- Python
curl https://acp-api.amivoice.com/v1/recognize \
-F u={API_key} \
-F d="grammarFileNames=-a-general profileId=:{profile_id}" \
-F a=@www.wav | jq
- 如果未安装
curl命令,请从 https://curl.se/ 下载适用于您操作系统的包,或使用包管理器安装 curl。 - 语音识别结果格式使用 Unicode 转义。上述命令使用
jq来美化响应。如果未安装jq,请尝试删除| jq部分后执行。可以从 https://stedolan.github.io/jq/ 下载适用于您操作系统的jq包,或使用包管理器安装。
curl.exe https://acp-api.amivoice.com/v1/recognize `
-F u={API_key} `
-F d="grammarFileNames=-a-general profileId=:{profile_id}" `
-F a=@www.wav | jq
- 在 PowerShell 中,
curl是Invoke-WebRequest的别名,因此请明确使用curl.exe。Windows 10 版本 1803 及更高版本默认包含curl.exe。如果未包含,请从 https://curl.se/ 安装。 - 语音识别结果格式使用 Unicode 转义。上述命令使用
jq来美化响应。如果未安装jq,请尝试删除| jq部分后执行。可以从 https://stedolan.github.io/jq/ 下载适用于您操作系统的jq包,或使用包管理器安装。
curl https://acp-api.amivoice.com/v1/recognize ^
-F u={API_key} ^
-F d="grammarFileNames=-a-general profileId=:{profile_id}" ^
-F a=@www.wav | jq
- Windows 10 版本 1803 及以上已默认包含
curl。如果没有包含,请从 https://curl.se/ 安装。 - 语音识别结果格式是 Unicode 转义的。在上述命令中,我们使用
jq来格式化响应以便于查看。如果未安装jq,请尝试删除| jq部分执行。您可以从 https://stedolan.github.io/jq/ 下载适用于您的操作系统的jq包,或使用包管理器安装。
import requests
api_key = "{API_key}"
pid = "{profile_id}" # 例: test
with open("www.wav", "rb") as f:
response = requests.post(
"https://acp-api.amivoice.com/v1/recognize",
data={
"d": f"grammarFileNames=-a-general profileId=:{pid}",
"u": api_key
},
files={"a": f}
)
print(response.json())
确认结果
成功时将返回如下 JSON。text 字段包含转写结果。
{
"results": [
{
"tokens": [ ... ],
"confidence": 0.94,
"starttime": 500,
"endtime": 2084,
"tags": [],
"rulename": "",
"text": "WWW"
}
],
"utteranceid": "20260611/18/019eb5f848ac0a3013ba94cb_20260611_181648",
"text": "WWW",
"code": "",
"message": ""
}
有关响应内容的详细信息,请参阅语音识别结果。
下一步
对于 Hybrid 引擎,在默认状态下,语音识别引擎只能识别预先注册的单词。因此,如果想要识别引擎未知的专有名词等单词,就需要额外注册这些单词的信息。这个功能就是"单词注册"。
而对于 End to End 引擎,由于语音识别的机制与 Hybrid 引擎不同,它没有"预先注册的单词"这一概念。如果想要识别未被识别或难以识别的单词,需要通过提高该单词的出现频率来进行"单词强调"。
本节将按以下顺序进行解释:
有关 API 的参考,请参阅以下内容:
某些语言不支持用户词典。有关详细信息,请参阅 AmiVoice API 支持的语言。
AmiVoice Tech Blog 还介绍了用户词典的各种技巧。
- Register words that are not recognized by speech recognition! Tips for word registration in AmiVoice
- Explanation of "Classes" that can be selected with the word registration function of AmiVoice API (general-purpose engine)
- [For intermediate users] About automatic conversion of word spoken in AmiVoice
- AmiVoice's word registration API gives you more freedom in speech recognition!
- AmiVoice API Update: End-to-End ”Keyword Biasing” Feature