/
fstutils.py
69 lines (62 loc) · 1.89 KB
/
fstutils.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# -*- coding: utf-8 -*-
from pylab import *
import openfst
from openfst import StdVectorFst as FST
from openfst import LogVectorFst as LFST
ASCII = openfst.SymbolTable("ASCII")
for i in range(127):
if i==0:
ASCII.AddSymbol("ϵ",i)
elif i<=32:
ASCII.AddSymbol("$%02x"%i,i)
else:
ASCII.AddSymbol(chr(i),i)
def minimize(fst):
dfst = FST()
openfst.Determinize(fst,dfst)
openfst.Minimize(dfst)
return dfst
def log_minimize(fst):
dfst = LFST()
openfst.Determinize(fst,dfst)
openfst.Minimize(dfst)
return dfst
def show_fst(fst):
import pydot,pylab
graph = pydot.Dot(rankdir="LR")
isyms = fst.InputSymbols()
if not isyms: isyms = ASCII
osyms = fst.OutputSymbols()
if not osyms: osyms = ASCII
for s in range(fst.NumStates()):
if s==fst.Start():
n = pydot.Node("%d"%s,shape="box")
graph.add_node(n)
if fst.IsFinal(s):
l = '"'
l += "%d"%s # node id
if fst.Final(s).Value()!=0.0: # optional non-zero accept cost
l += "/%s"%fst.Final(s).Value()
l += '"'
n = pydot.Node("%d"%s,label=l,penwidth="3")
graph.add_node(n)
for t in range(fst.NumArcs(s)):
a = fst.GetArc(s,t)
l = '"'
l += '%s'%isyms.Find(a.ilabel)
if a.olabel!=a.ilabel: l += ":%s"%osyms.Find(a.olabel)
v = a.weight.Value()
if v!=0.0: l += "/%s"%v
l += '"'
n = a.nextstate
e = pydot.Edge("%d"%s,"%d"%n,label=l)
graph.add_edge(e)
graph.write_png("/tmp/_test.png")
pylab.gca().set_xticks([]); pylab.gca().set_yticks([])
pylab.clf()
pylab.imshow(pylab.imread("/tmp/_test.png"))
def fstsize(fst):
edges = 0
for s in range(fst.NumStates()):
edges += fst.NumArcs(s)
return fst.NumStates(),edges