Initial import to Tizen
[profile/ivi/sphinxbase.git] / python / sb_test.py
1 #!/usr/bin/env python
2
3 import sphinxbase
4 import sys
5
6 lm = sphinxbase.NGramModel("../test/unit/test_ngram/100.arpa.DMP")
7 for ng in lm.mgrams(0):
8     print ng.log_prob, ng.log_bowt
9 lm.write("100.arpa.DMP")
10 lm.casefold(sphinxbase.UPPER)
11 lm.write("100.arpa")
12
13 hc = sphinxbase.HuffCode((("foo", 42), ("bar", 4), ("baz", 5), ("quux", 6), ("argh", 225), ("hurf", 15001), ("burf", 3), ("blatz", 2), ("unf", 87), ("woof", 1003)))
14 hc.dump(sys.stdout)
15 data, bits = hc.encode(("hurf", "burf", "blatz", "unf", "woof"))
16 dstr = "".join([("%02x" % ord(b)) for b in data])
17 print "encoding", ("hurf", "burf", "blatz", "unf", "woof"), "=>", (dstr, bits)
18 print "decoded to", hc.decode(data)
19
20 hc.write("foo.huff")
21 hc = sphinxbase.HuffCode(infile="foo.huff")
22 data, bits = hc.encode(("hurf", "burf", "blatz", "unf", "woof"))
23 dstr = "".join([("%02x" % ord(b)) for b in data])
24 print "encoding", ("hurf", "burf", "blatz", "unf", "woof"), "=>", (dstr, bits)
25 print "decoded to", hc.decode(data)
26
27 hc.attach("foo.bin", "wb")
28 hc.encode_to_file(("hurf", "burf", "blatz", "unf", "woof"))
29 hc.encode_to_file(("burf", "blatz", "woof", "unf", "woof"))
30 hc.detach()
31
32 hc.attach("foo.bin", "rb")
33 syms = []
34 while True:
35     sym = hc.decode_from_file()
36     if sym == None:
37         break
38     syms.append(sym)
39 print "decoded", syms
40 hc.detach()