python - 如何在 CMU 发音词典中查找某个 arpabet 发音是否存在?
问题描述
给定一个编码随机话语的 arpabet 令牌列表,我希望能够查看 arpabet 字符串是否实际上在 CMU 发音字典中。如果是,我还想要它在字典中匹配的单词。
有什么办法可以在 python 中做到这一点?
解决方案
不确定您是想在 Python 中使用 CMU Sphinx 还是自己来做这件事,但无论如何我都想出了一个合理的解决方案。这是代码:
#!/usr/bin/env python3
with open('dic.dict') as f:
cmu_dict = {}
for entry in f:
tokens = []
for t in entry.split():
tokens.append(t)
cmu_dict[tokens[0]] = tokens[1:] # index dict by word, value are phonemes
my_arpabets_list = [
['P', 'AH', 'L', 'IY', 'S', 'M', 'IH', 'N'],
['D', 'IH', 'L', 'AH', 'T', 'EY', 'SH', 'AH', 'N'],
]
for arpabet_tokens in my_arpabets_list:
found = False
for word, pronunciation in cmu_dict.items():
if pronunciation == arpabet_tokens:
print('match: %s %s' % (word, ' '.join(pronunciation)))
found = True
break
if not found:
print('error: could not find a word for tokens %s' % arpabet_tokens)
通过执行代码,我可以获得以下输出:
$ ./read.py
match: policemen P AH L IY S M IH N
error: could not find a word for tokens ['D', 'IH', 'L', 'AH', 'T', 'EY', 'SH', 'AH', 'N']
假设您安装了 Pocketsphinx,您可以将文件替换dic.dict
为与软件包一起安装的英语语言的默认字典以/usr/local/share/pocketsphinx/model/en-us/cmudict-en-us.dict
对其进行测试。
推荐阅读
- vba - CallByName 与 MsgBox 一起使用,但在分配值时不可用(“需要对象”错误)
- python - Numpy数组:如何逐行检查前X个值是否有效?
- sql-server - SQL Server 2016 遇到内存不足或会话超过服务器中允许的最大值
- javascript - 用php编写js文件时删除换行符
- javascript - 动态开放街图
- spring - 来自 liquibase 配置的 JPA 实体类生成器
- javascript - (jQuery) 在移动设备上调用函数,但 jQuery 动画不起作用
- node.js - Node JS Promise 拒绝异常
- javascript - 将 React 组件的 HTML 发送到服务器
- windows - 允许使用 TouchInjection windows 8+ api 进行屏幕外触摸?