Files
banban/talkingq-url/test/update_hotword_weights.py
2026-03-24 15:04:36 +08:00

51 lines
1.8 KiB
Python

import asyncio
from aliyun_hotword import hotword_manager
async def update_hotword_weights(hotword_weights: dict = None, default_weight: int = None):
"""更新指定热词的权重
Args:
hotword_weights: 字典,键为热词文本,值为新的权重值
default_weight: 如果提供,将所有热词权重设为此值
"""
vocabulary_id = await hotword_manager.get_or_create_default_vocabulary()
print(f"获取到热词表ID: {vocabulary_id}")
vocabulary = await hotword_manager.query_vocabulary(vocabulary_id)
hotwords = vocabulary.get('vocabulary', [])
updated = False
if default_weight is not None:
for hotword in hotwords:
old_weight = hotword['weight']
hotword['weight'] = default_weight
print(f"更新热词 '{hotword.get('text')}' 权重: {old_weight} -> {default_weight}")
updated = True
elif hotword_weights:
for hotword in hotwords:
if hotword.get('text') in hotword_weights:
old_weight = hotword['weight']
hotword['weight'] = hotword_weights[hotword.get('text')]
print(f"更新热词 '{hotword.get('text')}' 权重: {old_weight} -> {hotword['weight']}")
updated = True
if updated:
await hotword_manager.update_vocabulary(vocabulary_id, hotwords)
print("热词表权重更新成功")
else:
print("未找到需要更新的热词")
if __name__ == "__main__":
asyncio.run(update_hotword_weights(default_weight=4))
"""
weight_updates = {
"学姐": 200,
"懒羊羊": 150,
"喜羊羊": 180,
"Miniso": 120
}
asyncio.run(update_hotword_weights(hotword_weights=weight_updates))
"""