-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtest-openfst.py
52 lines (50 loc) · 1.7 KB
/
test-openfst.py
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
import unittest
import openfst
class narray(unittest.TestCase):
def testAddString(self):
fst = openfst.StdVectorFst()
fst.AddString("hello")
fst.AddString("world")
def testRead(self):
fst = openfst.StdVectorFst()
fst.AddString("hello")
fst.AddString("world")
fst.Write("_test.fst")
fst2 = openfst.Read("_test.fst")
openfst.Verify(fst2)
# might check for equivalence here
def testAddGetString(self):
fst = openfst.StdVectorFst()
fst.AddString("hello")
assert "hello"==openfst.GetString(fst)
def testAddGetWString(self):
fst = openfst.StdVectorFst()
fst.AddWString(u"hello")
fst.Write("temp.fst")
assert u"hello"==openfst.WGetString(fst)
def testFinal(self):
fst = openfst.StdVectorFst()
s = [fst.AddState() for i in range(4)]
fst.SetStart(s[0])
fst.SetFinal(s[3],73.0)
for i in range(3):
fst.AddArc(s[i],10+i,20+i,90+i,s[i+1])
assert fst.IsFinal(s[3])
assert abs(fst.FinalWeight(s[3])-73.0)<1e-10
def testTranslation(self):
input = openfst.StdVectorFst()
input.AddString("aaba")
fst = openfst.StdVectorFst()
fst.AddTranslation("a","A")
fst.AddTranslation("b","B")
openfst.ClosureStar(fst)
result = openfst.StdVectorFst()
openfst.Compose(input,fst,result)
shortest = openfst.StdVectorFst()
openfst.ProjectOutput(result)
openfst.RmEpsilon(result)
openfst.ShortestPath(result,shortest,1)
shortest.Write("temp.fst")
assert "AABA"==openfst.GetString(shortest)
if __name__ == "__main__":
unittest.main()