这里有一些事情掩盖了潜在的bug。
speech_recognition.recognize_sphinx()
只是几个CMUsphinx命令的包装器,可以找到
here
第746行。这个问题有点混乱,因此我们将重点关注下面的代码片段:
import speech_recognition as sr
import os
import pocketsphinx as ps
grammar = 'search.gram'
try:
language_directory = os.path.join(os.path.dirname(os.path.realpath(__file__)), "pocketsphinx-data", "en-US")
acoustic_parameters_directory = os.path.join(language_directory, "acoustic-model")
language_model_file = os.path.join(language_directory, "language-model.lm.bin")
phoneme_dictionary_file = os.path.join(language_directory, "pronounciation-dictionary.dict")
config = ps.Decoder.default_config()
config.set_string("-hmm",
acoustic_parameters_directory)
config.set_string("-lm", language_model_file)
config.set_string("-dict", phoneme_dictionary_file)
config.set_string("-logfn", os.devnull)
decoder = ps.Decoder(config)
grammar_path = os.path.abspath(os.path.dirname(grammar))
grammar_name = os.path.splitext(os.path.basename(grammar))[0]
fsg_path = "{0}/{1}.fsg".format(grammar_path, grammar_name)
if not os.path.exists(fsg_path):
jsgf = ps.Jsgf(grammar)
rule = jsgf.get_rule("{0}.{0}".format(grammar_name))
fsg = jsgf.build_fsg(rule, decoder.get_logmath(), 7.5)
fsg.writefile(fsg_path)
print('Successful JSFG to FSG conversion!!!')
decoder.set_fsg(grammar_name, fsg)
except Exception as e:
print('Ach no! {0}'.format(e))
finally:
os.remove('search.fsg')
运行该代码时,我们会发现错误出现了,但日志信息也被关闭了!打开后,大量文本会被丢弃到终端中,这可能会造成麻烦。在这种情况下,将其重新打开以发现。。。
...
ERROR: "fsg_search.c", line 141: The word 'perquisition' is missing in the dictionary
...
现在我们有进展了。这给我们留下了两个选择之一。首先我们可以扫描字典(
pocketsphinx.get_model_path()+'/cmudict-en-us.dict'
或类似的)来确定一个单词是否存在。然后我们可以决定是忽略这个词,还是将它添加到字典中。
添加到字典不一定是直接的。。。这取决于它与词典中其他单词的相似程度,你可能会侥幸逃脱。否则,您还必须重新培训模型。关于如何做到这一点,可以找到更好的解释
here
享受