有没有办法在pocketsphinx python中返回整个字典条目(word + phoneme)?

时间:2017-02-28 05:31:47

标签: python speech-recognition text-to-speech pocketsphinx

这是我的代码:

#!/usr

/bin/env python
import os

import sphinxbase as sb
import pocketsphinx as ps

MODELDIR = 'deps/pocketsphinx/model'
DATADIR = 'deps/pocketsphinx/test/data'

# Create a decoder with certain model
config = ps.Decoder.default_config()
config.set_string('-hmm', os.path.join(MODELDIR, 'en-us/en-us'))
config.set_string('-lm', os.path.join(MODELDIR, 'en-us/en-us.lm.bin'))
config.set_string('-dict', os.path.join(MODELDIR, 'en-us/cmudict-en-us.dict'))
decoder = ps.Decoder(config)

# Decode streaming data.
decoder.start_utt()
stream = open(os.path.join(DATADIR, 'hello_world.wav'), 'rb')
while True:
    buf = stream.read(1024)
    if buf:
        decoder.process_raw(buf, False, False)
    else:
        break
decoder.end_utt()
stream.close()
print('Best hypothesis segments:', [seg.word for seg in decoder.seg()])

我在this stack overflow question

中找到了

音频文件hello_world.wav正确输出:'最佳假设片段:hello world'

这是我的问题。我正在查找位于以下内容的发音字典文件:/Library/Python/2.7/site-packages/speech_recognition/pocketsphinx-data/en-US,它似乎是将英文单词映射到音素。

'hello'和'world'的条目是:

hello HH AH L OW 
world W ER L D

我想从字典中返回整行。所以,不要只是'你好',我想'你好HH AH L OW'。有没有办法做到这一点?

1 个答案:

答案 0 :(得分:2)

获得结果后,您可以查找单词的发音:

 print ('Best hypothesis segments: ', [(seg.word, decoder.lookup_word(seg.word)) for seg in decoder.seg()])