Skip to content

Instantly share code, notes, and snippets.

@Perlence
Created February 18, 2016 14:28
Show Gist options
  • Select an option

  • Save Perlence/482021283fecca48414a to your computer and use it in GitHub Desktop.

Select an option

Save Perlence/482021283fecca48414a to your computer and use it in GitHub Desktop.
Basic usage of PocketSphinx via Python
from __future__ import print_function
from os import path
from sphinxbase import sphinxbase # noqa
from pocketsphinx import pocketsphinx
MODELDIR = "pocketsphinx/model"
DATADIR = "pocketsphinx/test/data"
# Create a decoder with certain model
config = pocketsphinx.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'))
# Decode streaming data.
decoder = Decoder(config)
decoder.start_utt()
stream = open(path.join(DATADIR, 'goforward.raw'), 'rb')
while True:
buf = stream.read(1024)
if not buf:
break
decoder.process_raw(buf, False, False)
decoder.end_utt()
print('Best hypothesis segments: ', [seg.word for seg in decoder.seg()])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment