Python - Speech Recognition time offsets
Question: I am trying to do speech recognition using python. In addition to this, I need
to get the times of beginning and end of each word.

I would rather use a free library that can deal with this. I've heard that
Sphinx is able to do this but I couldn't find any examples (for python
anyway).

I would appreciate any help or suggestions.


Answer: Something like this:

    
    
    from os import environ, path
    
    from pocketsphinx.pocketsphinx import *
    from sphinxbase.sphinxbase import *
    
    MODELDIR = "../../../model"
    DATADIR = "../../../test/data"
    
    config = Decoder.default_config()
    config.set_string('-hmm', path.join(MODELDIR, 'en-us/en-us'))
    config.set_string('-lm', path.join(MODELDIR, 'en-us/en-us.lm.bin'))
    config.set_string('-dict', path.join(MODELDIR, 'en-us/cmudict-en-us.dict'))
    config.set_string('-logfn', '/dev/null')
    decoder = Decoder(config)
    
    stream = open(path.join(DATADIR, 'goforward.raw'), 'rb')
    
    in_speech_bf = False
    decoder.start_utt()
    while True:
        buf = stream.read(1024)
        if buf:
            decoder.process_raw(buf, False, False)
            if decoder.get_in_speech() != in_speech_bf:
                in_speech_bf = decoder.get_in_speech()
                if not in_speech_bf:
                    decoder.end_utt()
                    print ('Result:', decoder.hyp().hypstr)
                    print ([(seg.word, seg.prob, seg.start_frame, seg.end_frame) for seg in decoder.seg()])
                    decoder.start_utt()
        else:
            break
    decoder.end_utt()
    

More examples
[here](https://github.com/cmusphinx/pocketsphinx/blob/master/swig/python/test/).

