Convert acceptors

This section demonstrates how to convert an acceptor from k2 to OpenFst.

k2-acceptor.svg

Fig. 5 An acceptor in k2.

openfst-acceptor.svg

Fig. 6 The above acceptor converted to OpenFst. Pay attention to the negative sign for numbers on the arcs.

Listing 4 Convert an acceptor from k2 to OpenFst
 1#!/usr/bin/env python3
 2
 3import graphviz
 4import k2
 5from kaldifst.utils import k2_to_openfst
 6
 7import kaldifst
 8
 9
10def main():
11    s = """
12    0 1 1 0.5
13    0 1 2 1.5
14    1 2 3 2.5
15    1 3 -1 3.0
16    2 3 -1 3.5
17    3
18    """
19    sym = k2.SymbolTable.from_str(
20        """
21        a 1
22        b 2
23        c 3
24    """
25    )
26    fsa = k2.Fsa.from_str(s, acceptor=True)
27    fsa.labels_sym = sym
28    fsa.draw("k2-acceptor.svg")
29
30    fst = k2_to_openfst(fsa)
31
32    fst_dot = kaldifst.draw(fst, acceptor=True, portrait=True)
33    source = graphviz.Source(fst_dot)
34    source.render(outfile="openfst-acceptor.svg")
35
36
37if __name__ == "__main__":
38    main()