|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +from os import environ, path |
| 4 | +from pocketsphinx.pocketsphinx import * |
| 5 | +from sphinxbase.sphinxbase import * |
| 6 | + |
| 7 | +MODELDIR = "../../../model" |
| 8 | +DATADIR = "../../../test/data" |
| 9 | + |
| 10 | +# Create a decoder with certain model |
| 11 | +config = Decoder.default_config() |
| 12 | +config.set_string('-hmm', path.join(MODELDIR, 'en-us/en-us')) |
| 13 | +config.set_string('-lm', path.join(MODELDIR, 'en-us/en-us.lm.bin')) |
| 14 | +config.set_string('-dict', path.join(DATADIR, 'defective.dic')) |
| 15 | +config.set_boolean('-mmap', False) |
| 16 | +decoder = Decoder(config) |
| 17 | + |
| 18 | +decoder.start_utt() |
| 19 | +stream = open(path.join(DATADIR, 'goforward.raw'), 'rb') |
| 20 | +while True: |
| 21 | + buf = stream.read(1024) |
| 22 | + if buf: |
| 23 | + decoder.process_raw(buf, False, False) |
| 24 | + else: |
| 25 | + break |
| 26 | +decoder.end_utt() |
| 27 | +print ('Decoding with default settings:', decoder.hyp().hypstr) |
| 28 | + |
| 29 | +# Load "turtle" language model and decode again. |
| 30 | +lm = NGramModel(config, decoder.get_logmath(), path.join(DATADIR, 'turtle.lm.bin')) |
| 31 | +print (lm.prob(['you'])) |
| 32 | +print (lm.prob(['are','you'])) |
| 33 | +print (lm.prob(['you', 'are', 'what'])) |
| 34 | +print (lm.prob(['lost', 'are', 'you'])) |
| 35 | + |
| 36 | +decoder.set_lm('turtle', lm) |
| 37 | +decoder.set_search('turtle') |
| 38 | +decoder.start_utt() |
| 39 | +stream = open(path.join(DATADIR, 'goforward.raw'), 'rb') |
| 40 | +while True: |
| 41 | + buf = stream.read(1024) |
| 42 | + if buf: |
| 43 | + decoder.process_raw(buf, False, False) |
| 44 | + else: |
| 45 | + break |
| 46 | +decoder.end_utt() |
| 47 | + |
| 48 | +print ('Decoding with "turtle" language:', decoder.hyp().hypstr) |
| 49 | + |
| 50 | +## The word 'meters' isn't in the loaded dictionary. |
| 51 | +## Let's add it manually. |
| 52 | +decoder.add_word('foobie', 'F UW B IY', False) |
| 53 | +decoder.add_word('meters', 'M IY T ER Z', True) |
| 54 | +decoder.start_utt() |
| 55 | +stream = open(path.join(DATADIR, 'goforward.raw'), 'rb') |
| 56 | +while True: |
| 57 | + buf = stream.read(1024) |
| 58 | + if buf: |
| 59 | + decoder.process_raw(buf, False, False) |
| 60 | + else: |
| 61 | + break |
| 62 | +decoder.end_utt() |
| 63 | +print ('Decoding with customized language:', decoder.hyp().hypstr) |
0 commit comments