Introduction to Lattice

Arcs in a Lattice are of type LatticeArc, whose weight is of type LatticeWeight.

A LatticeWeight has two parts:

  • value1: It contains the graph cost

  • value2: It contains the acoustic cost

The following code demonstrates how to create and visualize a lattice.

Listing 9 Create, print, and visualize a lattice.
 1#!/usr/bin/env python3
 2
 3import graphviz
 4
 5import kaldifst
 6
 7fst = kaldifst.Lattice()
 8s0 = fst.add_state()
 9s1 = fst.add_state()
10s2 = fst.add_state()
11s3 = fst.add_state()
12
13fst.start = s0
14fst.add_arc(
15    state=s0,
16    arc=kaldifst.LatticeArc(
17        ilabel=1,
18        olabel=2,
19        weight=kaldifst.LatticeWeight(graph_cost=0.1, acoustic_cost=0.2),
20        nextstate=s1,
21    ),
22)
23
24fst.add_arc(
25    state=s0,
26    arc=kaldifst.LatticeArc(
27        ilabel=3,
28        olabel=4,
29        weight=kaldifst.LatticeWeight(graph_cost=0.3, acoustic_cost=0.4),
30        nextstate=s2,
31    ),
32)
33
34fst.add_arc(
35    state=s1,
36    arc=kaldifst.LatticeArc(
37        ilabel=5,
38        olabel=6,
39        weight=kaldifst.LatticeWeight(graph_cost=0.5, acoustic_cost=0.6),
40        nextstate=s3,
41    ),
42)
43
44fst.add_arc(
45    state=s2,
46    arc=kaldifst.LatticeArc(
47        ilabel=6,
48        olabel=8,
49        weight=kaldifst.LatticeWeight(graph_cost=0.7, acoustic_cost=0.8),
50        nextstate=s3,
51    ),
52)
53
54fst.set_final(state=s3, weight=kaldifst.LatticeWeight.one)
55
56print(fst)
57# The above statement gives the following output
58"""
590      1      1      2      0.1,0.2
600      2      3      4      0.3,0.4
611      3      5      6      0.5,0.6
622      3      6      8      0.7,0.8
633
64"""
65
66# Now we can draw it
67fst_dot = kaldifst.draw(fst, acceptor=False, portrait=True)
68print(fst_dot)
69# You can use
70# https://dreampuf.github.io/GraphvizOnline
71# to visualize it and share the link to others
72
73# Alternatively, you can use graphviz to visualize it
74source = graphviz.Source(fst_dot)
75source.render(outfile="lattice.svg")
lattice.svg

Fig. 9 lattice.svg generated from the above code.

1:2/0.1,0.2 means:

  • The input label is 1

  • The output label is 2

  • The graph cost is 0.1

  • The acoustic cost is 0.2