File: fsg_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 (37 lines) | stat: -rw-r--r-- 1,410 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
#!/usr/bin/python

import unittest
from pocketsphinx import LogMath, FsgModel, Decoder


class FsgTest(unittest.TestCase):
    def testfsg(self):
        lmath = LogMath()
        fsg = FsgModel("simple_grammar", lmath, 1.0, 10)
        fsg.word_add("hello")
        fsg.word_add("world")
        print(fsg.word_id("world"))
        self.assertEqual(fsg.word_id("world"), 1)
        self.assertEqual(fsg.word_add("world"), 1)

        fsg.add_silence("<sil>", 1, 0.5)

        decoder = Decoder()
        fsg = decoder.create_fsg("mygrammar",
                                 start_state=0, final_state=3,
                                 transitions=[(0, 1, 0.75, "hello"),
                                              (0, 1, 0.25, "goodbye"),
                                              (1, 2, 0.75, "beautiful"),
                                              (1, 2, 0.25, "cruel"),
                                              (2, 3, 1.0, "world")])
        self.assertTrue(fsg.accept("hello beautiful world"))
        self.assertTrue(fsg.accept("hello cruel world"))
        self.assertTrue(fsg.accept("goodbye beautiful world"))
        self.assertTrue(fsg.accept("goodbye cruel world"))
        self.assertFalse(fsg.accept("goodbye world"))
        self.assertFalse(fsg.accept("hello world"))
        self.assertFalse(fsg.accept("hello dead parrot"))


if __name__ == "__main__":
    unittest.main()