-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
09b004d
commit 95774ab
Showing
18 changed files
with
1,024 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
51
docs/source/python_api/code/convert_nbest_to_vector/ex1.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
118
docs/source/python_api/code/convert_nbest_to_vector/ex2.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.