|
1 | 1 | pocketsphinx-python |
2 | 2 | =================== |
3 | 3 |
|
4 | | -Python interface to CMU SphinxBase and PocketSphinx libraries |
| 4 | +Python interface to CMU SphinxBase and PocketSphinx libraries created with SWIG. |
| 5 | +Pocketsphinx packages include python support, however, it is based on Automake and |
| 6 | +not well supported on Windows. |
| 7 | + |
| 8 | +This package provides module created with Python distutils setup and can be more |
| 9 | +portable. |
5 | 10 |
|
6 | 11 | Supported Platforms |
7 | 12 | ------------------- |
@@ -63,16 +68,35 @@ cd pocketsphinx-python |
63 | 68 | sudo python setup.py install |
64 | 69 | ``` |
65 | 70 |
|
66 | | -Import |
67 | | ------- |
| 71 | +Basic usage |
| 72 | +----------- |
68 | 73 |
|
69 | 74 | ```python |
70 | | -try: |
71 | | - # Python 2.x |
72 | | - from sphinxbase import Config |
73 | | - from pocketsphinx import Decoder |
74 | | -except ImportError: |
75 | | - # Python 3.x |
76 | | - from sphinxbase.sphinxbase import Config |
77 | | - from pocketsphinx.pocketsphinx import Decoder |
| 75 | +from os import environ, path |
| 76 | + |
| 77 | +from pocketsphinx.pocketsphinx import * |
| 78 | +from sphinxbase.sphinxbase import * |
| 79 | + |
| 80 | +MODELDIR = "pocketsphinx/model" |
| 81 | +DATADIR = "pocketsphinx/test/data" |
| 82 | + |
| 83 | +# Create a decoder with certain model |
| 84 | +config = Decoder.default_config() |
| 85 | +config.set_string('-hmm', path.join(MODELDIR, 'en-us/en-us')) |
| 86 | +config.set_string('-lm', path.join(MODELDIR, 'en-us/en-us.lm.dmp')) |
| 87 | +config.set_string('-dict', path.join(MODELDIR, 'en-us/cmudict-en-us.dict')) |
| 88 | +decoder = Decoder(config) |
| 89 | + |
| 90 | +# Decode streaming data. |
| 91 | +decoder = Decoder(config) |
| 92 | +decoder.start_utt() |
| 93 | +stream = open(path.join(DATADIR, 'goforward.raw'), 'rb') |
| 94 | +while True: |
| 95 | + buf = stream.read(1024) |
| 96 | + if buf: |
| 97 | + decoder.process_raw(buf, False, False) |
| 98 | + else: |
| 99 | + break |
| 100 | +decoder.end_utt() |
| 101 | +print ('Best hypothesis segments: ', [seg.word for seg in decoder.seg()]) |
78 | 102 | ``` |
0 commit comments