Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Commit 39b4667

Browse files
committed
Tests moved here from the repository pocketsphinx
1 parent 52bf9fb commit 39b4667

File tree

9 files changed

+340
-0
lines changed

9 files changed

+340
-0
lines changed

test/Makefile.am

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
EXTRA_DIST = \
2+
config_test.py \
3+
continuous_test.py \
4+
decoder_test.py \
5+
kws_test.py \
6+
fsg_test.py \
7+
jsgf_test.py \
8+
lattice_test.py \
9+
lm_test.py

test/config_test.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/usr/bin/python
2+
3+
from os import environ, path
4+
5+
from pocketsphinx.pocketsphinx import *
6+
from sphinxbase.sphinxbase import *
7+
8+
#some dumb test for checking during developent
9+
10+
MODELDIR = "../../../model"
11+
12+
config = Decoder.default_config()
13+
14+
intval = 256
15+
floatval = 8000.0
16+
stringval = "~/pocketsphinx"
17+
boolval = True
18+
19+
# Check values that was previously set.
20+
s = config.get_float("-samprate")
21+
print ("Float: ",floatval ," ", s)
22+
config.set_float("-samprate", floatval)
23+
s = config.get_float("-samprate")
24+
print ("Float: ",floatval ," ", s)
25+
26+
s = config.get_int("-nfft")
27+
print ("Int:",intval, " ", s)
28+
config.set_int("-nfft", intval)
29+
s = config.get_int("-nfft")
30+
print ("Int:",intval, " ", s)
31+
32+
s = config.get_string("-rawlogdir")
33+
print ("String:",stringval, " ", s)
34+
config.set_string("-rawlogdir", stringval)
35+
s = config.get_string("-rawlogdir")
36+
print ("String:",stringval, " ", s)
37+
38+
s = config.get_boolean("-backtrace")
39+
print ("Boolean:", boolval, " ", s)
40+
config.set_boolean("-backtrace", boolval);
41+
s = config.get_boolean("-backtrace")
42+
print ("Boolean:", boolval, " ", s)
43+
44+
config.set_string_extra("-something12321", "abc")
45+
print config.get_string("-something12321")
46+

test/continuous_test.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#!/usr/bin/python
2+
3+
from os import environ, path
4+
5+
from pocketsphinx.pocketsphinx import *
6+
from sphinxbase.sphinxbase import *
7+
8+
MODELDIR = "../../../model"
9+
DATADIR = "../../../test/data"
10+
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(MODELDIR, 'en-us/cmudict-en-us.dict'))
15+
config.set_string('-logfn', '/dev/null')
16+
decoder = Decoder(config)
17+
18+
stream = open(path.join(DATADIR, 'goforward.raw'), 'rb')
19+
#stream = open('10001-90210-01803.wav', 'rb')
20+
21+
in_speech_bf = False
22+
decoder.start_utt()
23+
while True:
24+
buf = stream.read(1024)
25+
if buf:
26+
decoder.process_raw(buf, False, False)
27+
if decoder.get_in_speech() != in_speech_bf:
28+
in_speech_bf = decoder.get_in_speech()
29+
if not in_speech_bf:
30+
decoder.end_utt()
31+
print 'Result:', decoder.hyp().hypstr
32+
decoder.start_utt()
33+
else:
34+
break
35+
decoder.end_utt()

test/decoder_test.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/python
2+
3+
from os import environ, path
4+
5+
6+
from pocketsphinx.pocketsphinx import *
7+
from sphinxbase.sphinxbase import *
8+
9+
MODELDIR = "../../../model"
10+
DATADIR = "../../../test/data"
11+
12+
# Create a decoder with certain model
13+
config = Decoder.default_config()
14+
config.set_string('-hmm', path.join(MODELDIR, 'en-us/en-us'))
15+
config.set_string('-lm', path.join(MODELDIR, 'en-us/en-us.lm.bin'))
16+
config.set_string('-dict', path.join(MODELDIR, 'en-us/cmudict-en-us.dict'))
17+
18+
# Decode streaming data.
19+
decoder = Decoder(config)
20+
21+
print ("Pronunciation for word 'hello' is ", decoder.lookup_word("hello"))
22+
print ("Pronunciation for word 'abcdf' is ", decoder.lookup_word("abcdf"))
23+
24+
decoder.start_utt()
25+
stream = open(path.join(DATADIR, 'goforward.raw'), 'rb')
26+
while True:
27+
buf = stream.read(1024)
28+
if buf:
29+
decoder.process_raw(buf, False, False)
30+
else:
31+
break
32+
decoder.end_utt()
33+
34+
hypothesis = decoder.hyp()
35+
logmath = decoder.get_logmath()
36+
print ('Best hypothesis: ', hypothesis.hypstr, " model score: ", hypothesis.best_score, " confidence: ", logmath.exp(hypothesis.prob))
37+
38+
print ('Best hypothesis segments: ', [seg.word for seg in decoder.seg()])
39+
40+
# Access N best decodings.
41+
print ('Best 10 hypothesis: ')
42+
for best, i in zip(decoder.nbest(), range(10)):
43+
print (best.hypstr, best.score)
44+
45+
stream = open(path.join(DATADIR, 'goforward.mfc'), 'rb')
46+
stream.read(4)
47+
buf = stream.read(13780)
48+
decoder.start_utt()
49+
decoder.process_cep(buf, False, True)
50+
decoder.end_utt()
51+
hypothesis = decoder.hyp()
52+
print ('Best hypothesis: ', hypothesis.hypstr, " model score: ", hypothesis.best_score, " confidence: ", hypothesis.prob)

test/fsg_test.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/python
2+
3+
import sys
4+
5+
from sphinxbase.sphinxbase import LogMath
6+
from sphinxbase.sphinxbase import FsgModel
7+
8+
lmath = LogMath()
9+
fsg = FsgModel("simple_grammar", lmath, 1.0, 10)
10+
fsg.word_add("hello")
11+
fsg.word_add("world")
12+
print (fsg.word_id("world"))
13+
14+
fsg.add_silence("<sil>", 1, 0.5)
15+

test/jsgf_test.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/usr/bin/python
2+
3+
from os import environ, path
4+
from sys import stdout
5+
6+
from pocketsphinx.pocketsphinx import *
7+
from sphinxbase.sphinxbase import *
8+
9+
MODELDIR = "../../../model"
10+
DATADIR = "../../../test/data"
11+
12+
# Create a decoder with certain model
13+
config = Decoder.default_config()
14+
config.set_string('-hmm', path.join(MODELDIR, 'en-us/en-us'))
15+
config.set_string('-lm', path.join(DATADIR, 'turtle.lm.bin'))
16+
config.set_string('-dict', path.join(DATADIR, 'turtle.dic'))
17+
decoder = Decoder(config)
18+
19+
# Decode with lm
20+
decoder.start_utt()
21+
stream = open(path.join(DATADIR, 'goforward.raw'), 'rb')
22+
while True:
23+
buf = stream.read(1024)
24+
if buf:
25+
decoder.process_raw(buf, False, False)
26+
else:
27+
break
28+
decoder.end_utt()
29+
print ('Decoding with "turtle" language:', decoder.hyp().hypstr)
30+
31+
# Switch to JSGF grammar
32+
jsgf = Jsgf(path.join(DATADIR, 'goforward.gram'))
33+
rule = jsgf.get_rule('goforward.move2')
34+
fsg = jsgf.build_fsg(rule, decoder.get_logmath(), 7.5)
35+
fsg.writefile('goforward.fsg')
36+
37+
decoder.set_fsg("goforward", fsg)
38+
decoder.set_search("goforward")
39+
40+
decoder.start_utt()
41+
stream = open(path.join(DATADIR, 'goforward.raw'), 'rb')
42+
while True:
43+
buf = stream.read(1024)
44+
if buf:
45+
decoder.process_raw(buf, False, False)
46+
else:
47+
break
48+
decoder.end_utt()
49+
print ('Decoding with "goforward" grammar:', decoder.hyp().hypstr)

test/kws_test.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#!/usr/bin/python
2+
3+
import sys, os
4+
from pocketsphinx.pocketsphinx import *
5+
from sphinxbase.sphinxbase import *
6+
7+
8+
modeldir = "../../../model"
9+
datadir = "../../../test/data"
10+
11+
# Create a decoder with certain model
12+
config = Decoder.default_config()
13+
config.set_string('-hmm', os.path.join(modeldir, 'en-us/en-us'))
14+
config.set_string('-dict', os.path.join(modeldir, 'en-us/cmudict-en-us.dict'))
15+
config.set_string('-keyphrase', 'forward')
16+
config.set_float('-kws_threshold', 1e+20)
17+
18+
19+
# Open file to read the data
20+
stream = open(os.path.join(datadir, "goforward.raw"), "rb")
21+
22+
# Alternatively you can read from microphone
23+
# import pyaudio
24+
#
25+
# p = pyaudio.PyAudio()
26+
# stream = p.open(format=pyaudio.paInt16, channels=1, rate=16000, input=True, frames_per_buffer=1024)
27+
# stream.start_stream()
28+
29+
# Process audio chunk by chunk. On keyword detected perform action and restart search
30+
decoder = Decoder(config)
31+
decoder.start_utt()
32+
while True:
33+
buf = stream.read(1024)
34+
if buf:
35+
decoder.process_raw(buf, False, False)
36+
else:
37+
break
38+
if decoder.hyp() != None:
39+
print ([(seg.word, seg.prob, seg.start_frame, seg.end_frame) for seg in decoder.seg()])
40+
print ("Detected keyword, restarting search")
41+
decoder.end_utt()
42+
decoder.start_utt()

test/lattice_test.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/python
2+
3+
from os import environ, path
4+
5+
from pocketsphinx.pocketsphinx import *
6+
from sphinxbase.sphinxbase import *
7+
8+
MODELDIR = "../../../model"
9+
DATADIR = "../../../test/data"
10+
11+
# Create a decoder with certain model
12+
config = Decoder.default_config()
13+
config.set_string('-hmm', path.join(MODELDIR, 'en-us/en-us'))
14+
config.set_string('-lm', path.join(MODELDIR, 'en-us/en-us.lm.bin'))
15+
config.set_string('-dict', path.join(MODELDIR, 'en-us/cmudict-en-us.dict'))
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+
28+
decoder.get_lattice().write('goforward.lat')
29+
decoder.get_lattice().write_htk('goforward.htk')

test/lm_test.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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

Comments
 (0)