Creating FSTs from strings

In this tutorial, we demonstrate how to create the following FST from strings.

Hint

You can read the strings from some files.

quick-start-2.svg

Fig. 2 quick-start-2.svg

Listing 2 Create an FST from strings.
 1#!/usr/bin/env python3
 2
 3import graphviz
 4
 5import kaldifst
 6
 7
 8def main():
 9    s = """
10        0 1 a x 0.5
11        0 1 b y 1.5
12        1 2 c z 2.5
13        2 3.5
14    """
15    isym = kaldifst.SymbolTable.from_str(
16        """
17        a 1
18        b 2
19        c 3
20    """
21    )
22
23    osym = kaldifst.SymbolTable.from_str(
24        """
25        x 1
26        y 2
27        z 3
28    """
29    )
30
31    fst = kaldifst.compile(
32        s=s,
33        acceptor=False,
34        isymbols=isym,
35        osymbols=osym,
36        keep_isymbols=True,
37        keep_osymbols=True,
38    )
39    fst.write("binary-2.fst")
40
41    fst_dot = kaldifst.draw(fst, acceptor=False, portrait=True)
42    source = graphviz.Source(fst_dot)
43    source.render(outfile="quick-start-2.svg")
44
45
46if __name__ == "__main__":
47    main()