Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add lattice-to-nbest #59

Merged
merged 3 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions docs/source/python_api/code/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ all:
$(MAKE) -C minimize_encoded
$(MAKE) -C reverse
$(MAKE) -C rmepsilon
$(MAKE) -C shortest_path
$(MAKE) -C scale_lattice
$(MAKE) -C convert_nbest_to_vector
$(MAKE) -C lattice_to_nbest

clean:
$(MAKE) -C add_self_loops clean
Expand All @@ -35,3 +39,6 @@ clean:
$(MAKE) -C reverse clean
$(MAKE) -C rmepsilon clean
$(MAKE) -C shortest_path clean
$(MAKE) -C scale_lattice clean
$(MAKE) -C convert_nbest_to_vector clean
$(MAKE) -C lattice_to_nbest clean
7 changes: 7 additions & 0 deletions docs/source/python_api/code/convert_nbest_to_vector/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

all:
python3 ./ex1.py
python3 ./ex2.py

clean:
$(RM) *.svg *.gv
51 changes: 51 additions & 0 deletions docs/source/python_api/code/convert_nbest_to_vector/ex1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env python3

import graphviz

import kaldifst

s1 = """
0 1 a 0.1
0 2 b 0.1
1 3 c 0.4
1 3 d 0.2
2 3 c 0.3
2 3 d 0.2
3 0
"""

sym1 = kaldifst.SymbolTable(name="sym1")
sym1.add_symbol("eps", 0)
sym1.add_symbol("a", 1)
sym1.add_symbol("b", 2)
sym1.add_symbol("c", 3)
sym1.add_symbol("d", 4)

a = kaldifst.compile(s=s1, acceptor=True, isymbols=sym1)
a.input_symbols = sym1

a_dot = kaldifst.draw(a, acceptor=True, portrait=True)
a_source = graphviz.Source(a_dot)
a_source.render(outfile="vector-fst.svg")

nbest_3 = kaldifst.shortest_path(a, n=3)
nbest_3_dot = kaldifst.draw(nbest_3, acceptor=True, portrait=True)
nbest_3_source = graphviz.Source(nbest_3_dot)
nbest_3_source.render(outfile="vector-fst-3best.svg")

nbest_list = kaldifst.convert_nbest_to_vector(nbest_3)
for b in nbest_list:
b.input_symbols = a.input_symbols
b.output_symbols = a.output_symbols

nbest_list_0_dot = kaldifst.draw(nbest_list[0], acceptor=True, portrait=True)
nbest_list_0_source = graphviz.Source(nbest_list_0_dot)
nbest_list_0_source.render(outfile="vector-fst-3best-0.svg")

nbest_list_1_dot = kaldifst.draw(nbest_list[1], acceptor=True, portrait=True)
nbest_list_1_source = graphviz.Source(nbest_list_1_dot)
nbest_list_1_source.render(outfile="vector-fst-3best-1.svg")

nbest_list_2_dot = kaldifst.draw(nbest_list[2], acceptor=True, portrait=True)
nbest_list_2_source = graphviz.Source(nbest_list_2_dot)
nbest_list_2_source.render(outfile="vector-fst-3best-2.svg")
118 changes: 118 additions & 0 deletions docs/source/python_api/code/convert_nbest_to_vector/ex2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#!/usr/bin/env python3

import graphviz

import kaldifst

fst = kaldifst.Lattice()
s0 = fst.add_state()
s1 = fst.add_state()
s2 = fst.add_state()
s3 = fst.add_state()

fst.start = s0
fst.add_arc(
state=s0,
arc=kaldifst.LatticeArc(
ilabel=1,
olabel=1,
weight=kaldifst.LatticeWeight(graph_cost=0.02, acoustic_cost=0.08),
nextstate=s1,
),
)

fst.add_arc(
state=s0,
arc=kaldifst.LatticeArc(
ilabel=2,
olabel=2,
weight=kaldifst.LatticeWeight(graph_cost=0.03, acoustic_cost=0.07),
nextstate=s2,
),
)

fst.add_arc(
state=s1,
arc=kaldifst.LatticeArc(
ilabel=3,
olabel=3,
weight=kaldifst.LatticeWeight(graph_cost=0.1, acoustic_cost=0.3),
nextstate=s3,
),
)

fst.add_arc(
state=s1,
arc=kaldifst.LatticeArc(
ilabel=4,
olabel=4,
weight=kaldifst.LatticeWeight(graph_cost=0.15, acoustic_cost=0.05),
nextstate=s3,
),
)

fst.add_arc(
state=s2,
arc=kaldifst.LatticeArc(
ilabel=3,
olabel=3,
weight=kaldifst.LatticeWeight(graph_cost=0.15, acoustic_cost=0.15),
nextstate=s3,
),
)

fst.add_arc(
state=s2,
arc=kaldifst.LatticeArc(
ilabel=4,
olabel=4,
weight=kaldifst.LatticeWeight(graph_cost=0.05, acoustic_cost=0.15),
nextstate=s3,
),
)

fst.set_final(state=s3, weight=kaldifst.LatticeWeight.one)


sym1 = kaldifst.SymbolTable(name="sym1")
sym1.add_symbol("eps", 0)
sym1.add_symbol("a", 1)
sym1.add_symbol("b", 2)
sym1.add_symbol("c", 3)
sym1.add_symbol("d", 4)

sym2 = kaldifst.SymbolTable(name="sym2")
sym2.add_symbol("eps", 0)
sym2.add_symbol("A", 1)
sym2.add_symbol("B", 2)
sym2.add_symbol("C", 3)
sym2.add_symbol("D", 4)

fst.input_symbols = sym1
fst.output_symbols = sym2

fst_dot = kaldifst.draw(fst, acceptor=False, portrait=True)
fst_source = graphviz.Source(fst_dot)
fst_source.render(outfile="lattice.svg")

nbest_3 = kaldifst.shortest_path(fst, n=3)
nbest_3_dot = kaldifst.draw(nbest_3, acceptor=False, portrait=True)
nbest_3_source = graphviz.Source(nbest_3_dot)
nbest_3_source.render(outfile="lattice-3best.svg")

nbest_list = kaldifst.convert_nbest_to_vector(nbest_3)
for b in nbest_list:
b.input_symbols = fst.input_symbols
b.output_symbols = fst.output_symbols

nbest_list_0_dot = kaldifst.draw(nbest_list[0], acceptor=True, portrait=True)
nbest_list_0_source = graphviz.Source(nbest_list_0_dot)
nbest_list_0_source.render(outfile="lattice-3best-0.svg")

nbest_list_1_dot = kaldifst.draw(nbest_list[1], acceptor=True, portrait=True)
nbest_list_1_source = graphviz.Source(nbest_list_1_dot)
nbest_list_1_source.render(outfile="lattice-3best-1.svg")

nbest_list_2_dot = kaldifst.draw(nbest_list[2], acceptor=True, portrait=True)
nbest_list_2_source = graphviz.Source(nbest_list_2_dot)
nbest_list_2_source.render(outfile="lattice-3best-2.svg")
7 changes: 7 additions & 0 deletions docs/source/python_api/code/lattice_to_nbest/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

all:
python3 ./ex1.py
python3 ./ex2.py

clean:
$(RM) *.svg *.gv
46 changes: 46 additions & 0 deletions docs/source/python_api/code/lattice_to_nbest/ex1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/usr/bin/env python3

import graphviz

import kaldifst

s1 = """
0 1 a 0.1
0 2 b 0.1
1 3 c 0.4
1 3 d 0.2
2 3 c 0.3
2 3 d 0.2
3 0
"""

sym1 = kaldifst.SymbolTable(name="sym1")
sym1.add_symbol("eps", 0)
sym1.add_symbol("a", 1)
sym1.add_symbol("b", 2)
sym1.add_symbol("c", 3)
sym1.add_symbol("d", 4)

a = kaldifst.compile(s=s1, acceptor=True, isymbols=sym1)
a.input_symbols = sym1

a_dot = kaldifst.draw(a, acceptor=True, portrait=True)
a_source = graphviz.Source(a_dot)
a_source.render(outfile="vector-fst.svg")

nbest_list = kaldifst.lattice_to_nbest(a, n=3)
for b in nbest_list:
b.input_symbols = a.input_symbols
b.output_symbols = a.output_symbols

nbest_list_0_dot = kaldifst.draw(nbest_list[0], acceptor=True, portrait=True)
nbest_list_0_source = graphviz.Source(nbest_list_0_dot)
nbest_list_0_source.render(outfile="vector-fst-3best-0.svg")

nbest_list_1_dot = kaldifst.draw(nbest_list[1], acceptor=True, portrait=True)
nbest_list_1_source = graphviz.Source(nbest_list_1_dot)
nbest_list_1_source.render(outfile="vector-fst-3best-1.svg")

nbest_list_2_dot = kaldifst.draw(nbest_list[2], acceptor=True, portrait=True)
nbest_list_2_source = graphviz.Source(nbest_list_2_dot)
nbest_list_2_source.render(outfile="vector-fst-3best-2.svg")
113 changes: 113 additions & 0 deletions docs/source/python_api/code/lattice_to_nbest/ex2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#!/usr/bin/env python3

import graphviz

import kaldifst

fst = kaldifst.Lattice()
s0 = fst.add_state()
s1 = fst.add_state()
s2 = fst.add_state()
s3 = fst.add_state()

fst.start = s0
fst.add_arc(
state=s0,
arc=kaldifst.LatticeArc(
ilabel=1,
olabel=1,
weight=kaldifst.LatticeWeight(graph_cost=0.02, acoustic_cost=0.08),
nextstate=s1,
),
)

fst.add_arc(
state=s0,
arc=kaldifst.LatticeArc(
ilabel=2,
olabel=2,
weight=kaldifst.LatticeWeight(graph_cost=0.03, acoustic_cost=0.07),
nextstate=s2,
),
)

fst.add_arc(
state=s1,
arc=kaldifst.LatticeArc(
ilabel=3,
olabel=3,
weight=kaldifst.LatticeWeight(graph_cost=0.1, acoustic_cost=0.3),
nextstate=s3,
),
)

fst.add_arc(
state=s1,
arc=kaldifst.LatticeArc(
ilabel=4,
olabel=4,
weight=kaldifst.LatticeWeight(graph_cost=0.15, acoustic_cost=0.05),
nextstate=s3,
),
)

fst.add_arc(
state=s2,
arc=kaldifst.LatticeArc(
ilabel=3,
olabel=3,
weight=kaldifst.LatticeWeight(graph_cost=0.15, acoustic_cost=0.15),
nextstate=s3,
),
)

fst.add_arc(
state=s2,
arc=kaldifst.LatticeArc(
ilabel=4,
olabel=4,
weight=kaldifst.LatticeWeight(graph_cost=0.05, acoustic_cost=0.15),
nextstate=s3,
),
)

fst.set_final(state=s3, weight=kaldifst.LatticeWeight.one)


sym1 = kaldifst.SymbolTable(name="sym1")
sym1.add_symbol("eps", 0)
sym1.add_symbol("a", 1)
sym1.add_symbol("b", 2)
sym1.add_symbol("c", 3)
sym1.add_symbol("d", 4)

sym2 = kaldifst.SymbolTable(name="sym2")
sym2.add_symbol("eps", 0)
sym2.add_symbol("A", 1)
sym2.add_symbol("B", 2)
sym2.add_symbol("C", 3)
sym2.add_symbol("D", 4)

fst.input_symbols = sym1
fst.output_symbols = sym2

fst_dot = kaldifst.draw(fst, acceptor=False, portrait=True)
fst_source = graphviz.Source(fst_dot)
fst_source.render(outfile="lattice.svg")

nbest_list = kaldifst.lattice_to_nbest(fst, n=3)
for b in nbest_list:
b.input_symbols = fst.input_symbols
b.output_symbols = fst.output_symbols

nbest_list_0_dot = kaldifst.draw(nbest_list[0], acceptor=True, portrait=True)
nbest_list_0_source = graphviz.Source(nbest_list_0_dot)
nbest_list_0_source.render(outfile="lattice-3best-0.svg")

nbest_list_1_dot = kaldifst.draw(nbest_list[1], acceptor=True, portrait=True)
nbest_list_1_source = graphviz.Source(nbest_list_1_dot)
nbest_list_1_source.render(outfile="lattice-3best-1.svg")

nbest_list_2_dot = kaldifst.draw(nbest_list[2], acceptor=True, portrait=True)
nbest_list_2_source = graphviz.Source(nbest_list_2_dot)
nbest_list_2_source.render(outfile="lattice-3best-2.svg")
7 changes: 7 additions & 0 deletions docs/source/python_api/code/scale_lattice/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

all:
python3 ./ex1.py
python3 ./ex2.py

clean:
$(RM) *.svg *.gv
Loading
Loading