File: pypocketsphinx_test.py

package info (click to toggle)
pocketsphinx 5.0.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 51,236 kB
  • sloc: ansic: 54,519; python: 2,438; sh: 566; cpp: 410; perl: 342; yacc: 93; lex: 50; makefile: 30
file content (207 lines) | stat: -rw-r--r-- 6,958 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#!/usr/bin/env python3

"""Test the various use cases for the old abandoned
python-pocketsphinx module and its simple classes.  We don't support
the truly useless parts of the API like defaulting to "goforward.raw"
as the input, and some results have changed, other than that it should
be compatible.
"""

import os
from pocketsphinx import Pocketsphinx, AudioFile, NGramModel, Jsgf
from unittest import TestCase, main

MODELDIR = os.path.join(os.path.dirname(__file__), "../../model")
DATADIR = os.path.join(os.path.dirname(__file__), "../../test/data")


class TestAudioFile(TestCase):
    def test_audiofile_raw(self):
        hypothesis = ""
        for phrase in AudioFile(audio_file=os.path.join(DATADIR, "goforward.raw")):
            hypothesis = str(phrase)
        self.assertEqual(hypothesis, "go forward ten meters")


class TestRawDecoder(TestCase):
    def setUp(self):
        self.ps = Pocketsphinx(
            hmm=os.path.join(MODELDIR, "en-us/en-us"),
            lm=os.path.join(MODELDIR, "en-us/en-us.lm.bin"),
            dict=os.path.join(MODELDIR, "en-us/cmudict-en-us.dict"),
        )
        self.ps.decode(os.path.join(DATADIR, "goforward.raw"))

    def test_raw_decoder_lookup_word(self):
        self.assertEqual(self.ps.lookup_word("hello"), "HH AH L OW")
        self.assertEqual(self.ps.lookup_word("abcdf"), None)

    def test_raw_decoder_hypothesis(self):
        self.assertEqual(self.ps.hypothesis(), "go forward ten years")
        self.assertEqual(self.ps.score(), -8237)
        self.assertAlmostEqual(self.ps.confidence(), 0.01, 3)

    def test_raw_decoder_segments(self):
        self.assertEqual(
            self.ps.segments(),
            ["<s>", "go", "forward", "ten", "years", "</s>"],
        )

    def test_raw_decoder_best_hypothesis(self):
        self.assertEqual(
            self.ps.best(),
            [
                ("go forward ten years", -28492),
                ("go forward ten meters", -28547),
                ("go for word ten meters", -29079),
                ("go forward ten liters", -29084),
                ("go forward ten leaders", -29098),
                ("go forward can meters", -29174),
                ("go for word ten years", -29216),
                ("go forward ten readers", -29254),
                ("go for work ten meters", -29259),
                ("go forward can leaders", -29261),
            ],
        )


class TestCepDecoder(TestCase):
    def test_cep_decoder_hypothesis(self):
        ps = Pocketsphinx(
            hmm=os.path.join(MODELDIR, "en-us/en-us"),
            lm=os.path.join(MODELDIR, "en-us/en-us.lm.bin"),
            dict=os.path.join(MODELDIR, "en-us/cmudict-en-us.dict"),
            verbose=True,
        )
        with open(os.path.join(DATADIR, "goforward.mfc"), "rb") as f:
            with ps.start_utterance():
                f.read(4)
                buf = f.read(13780)
                ps.process_cep(buf, False, True)
        self.assertEqual(ps.hypothesis(), "go forward ten meters")
        self.assertEqual(ps.score(), -7103)
        self.assertEqual(ps.probability(), -33134)


class TestJsgf(TestCase):
    def test_jsgf(self):
        ps = Pocketsphinx(
            hmm=os.path.join(MODELDIR, "en-us/en-us"),
            lm=os.path.join(DATADIR, "turtle.lm.bin"),
            dic=os.path.join(DATADIR, "turtle.dic"),
        )

        # Decoding with 'turtle' language model
        ps.decode(os.path.join(DATADIR, "goforward.raw"))
        self.assertEqual(ps.hypothesis(), "go forward ten meters")

        # Switch to JSGF grammar
        jsgf = Jsgf(os.path.join(DATADIR, "goforward.gram"))
        rule = jsgf.get_rule("goforward.move2")
        fsg = jsgf.build_fsg(rule, ps.get_logmath(), 7.5)
        ps.add_fsg("goforward", fsg)
        ps.activate_search("goforward")

        # Decoding with 'goforward' grammar
        ps.decode(os.path.join(DATADIR, "goforward.raw"))
        self.assertEqual(ps.hypothesis(), "go forward ten meters")


class TestKws(TestCase):
    def test_kws(self):
        segments = []
        for phrase in AudioFile(
                os.path.join(DATADIR, "goforward.raw"),
                lm=None,
                keyphrase="forward",
                kws_threshold=1e20,
        ):
            segments = phrase.segments(detailed=True)
        self.assertEqual(segments, [("forward", -706, 63, 121)])

    def test_kws_badapi(self):
        segments = []
        for phrase in AudioFile(
            audio_file=os.path.join(DATADIR, "goforward.raw"),
            lm=False,  # Make sure this still works
            keyphrase="forward",
            kws_threshold=1e20,
        ):
            segments = phrase.segments(detailed=True)
        self.assertEqual(segments, [("forward", -706, 63, 121)])


class TestLm(TestCase):
    def test_lm(self):
        ps = Pocketsphinx(
            hmm=os.path.join(MODELDIR, "en-us/en-us"),
            lm=os.path.join(MODELDIR, "en-us/en-us.lm.bin"),
            dic=os.path.join(DATADIR, "defective.dic"),
        )

        # Decoding with 'defective' dictionary
        ps.decode(os.path.join(DATADIR, "goforward.raw"))
        self.assertEqual(ps.hypothesis(), "")

        # Switch to 'turtle' language model
        turtle_lm = os.path.join(DATADIR, "turtle.lm.bin")
        lm = NGramModel(ps.get_config(), ps.get_logmath(), turtle_lm)
        ps.add_lm("turtle", lm)
        ps.activate_search("turtle")

        # Decoding with 'turtle' language model
        ps.decode(os.path.join(DATADIR, "goforward.raw"))
        self.assertEqual(ps.hypothesis(), "")

        # The word 'meters' isn't in the loaded dictionary
        # Let's add it manually
        ps.add_word("foobie", "F UW B IY", False)
        ps.add_word("meters", "M IY T ER Z", True)

        # Decoding with 'turtle' language model
        ps.decode(os.path.join(DATADIR, "goforward.raw"))
        self.assertEqual(ps.hypothesis(), "foobie meters meters")


class TestPhoneme(TestCase):
    def setUp(self):
        self.ps = Pocketsphinx(
            allphone=os.path.join(MODELDIR, "en-us/en-us-phone.lm.bin"),
            lw=2.0,
            pip=0.3,
            beam=1e-200,
            pbeam=1e-20,
        )
        self.ps.decode(os.path.join(DATADIR, "goforward.raw"))

    def test_phoneme_hypothesis(self):
        self.assertEqual(
            self.ps.hypothesis(), "SIL G OW F AO R D T AE N NG IY ZH ER S SIL"
        )

    def test_phoneme_best_phonemes(self):
        self.assertEqual(
            self.ps.segments(),
            [
                "SIL",
                "G",
                "OW",
                "F",
                "AO",
                "R",
                "D",
                "T",
                "AE",
                "N",
                "NG",
                "IY",
                "ZH",
                "ER",
                "S",
                "SIL",
            ],
        )


if __name__ == "__main__":
    main(verbosity=2)