### Install kaldifst from source Source: https://k2-fsa.github.io/kaldifst/install/index.html Build and install kaldifst from source. Clones the repository, changes into the directory, and runs setup.py install. ```bash git clone https://github.com/k2-fsa/kaldifst cd kaldifst python3 setup.py install ``` -------------------------------- ### Create an FST with kaldifst.StdVectorFst and visualize it Source: https://k2-fsa.github.io/kaldifst/quick_start/quick-start.html Builds a small weighted finite-state transducer with kaldifst.StdVectorFst, adding states, arcs, and symbol tables, then writes it to binary.fst, reads it back, and renders a Graphviz visualization to quick-start.svg. Requires the kaldifst and graphviz packages. The example prints the FST and its dot representation, and the final SVG matches the figure shown at the start of the section. ```python #!/usr/bin/env python3 import graphviz import kaldifst def main(): # A vector FST is a general mutable FST fst = kaldifst.StdVectorFst() # Adds state 0 to the initially empty FST and make it the start state. s0 = fst.add_state() # 1st state will be state 0 (returned by add_state) assert s0 == 0 fst.start = 0 # set the start state to 0 # Adds two arcs exiting state 0. # Arc constructor args: ilabel, olabel, weight, dest state ID. fst.add_arc( state=0, arc=kaldifst.StdArc(ilabel=1, olabel=1, weight=0.5, nextstate=1), ) fst.add_arc( state=0, arc=kaldifst.StdArc(ilabel=2, olabel=2, weight=1.5, nextstate=1), ) # Adds state 1 and its arc. fst.add_state() fst.add_arc( state=1, arc=kaldifst.StdArc(ilabel=3, olabel=3, weight=2.5, nextstate=2), ) # Adds state 2 and set its final weight. fst.add_state() fst.set_final(state=2, weight=3.5) # 1st arg is state ID, 2nd arg weight # Add an input symbol table isym = kaldifst.SymbolTable() isym.add_symbol(symbol="a", key=1) isym.add_symbol("b", 2) isym.add_symbol("c", 3) fst.input_symbols = isym # Add an output symbol table osym = kaldifst.SymbolTable() osym.add_symbol("x", 1) osym.add_symbol("y", 2) osym.add_symbol("z", 3) fst.output_symbols = osym # We can save this FST to a file with fst.write(filename="binary.fst") # We can read it back fst2 = kaldifst.StdVectorFst.read("binary.fst") print(fst2) print(fst2.to_str()) # Get a dot format of the FST for visualization fst_dot = kaldifst.draw(fst, acceptor=False, portrait=True) print(fst_dot) # You can use # https://dreampuf.github.io/GraphvizOnline # to visualize it and share the link to others # Alternatively, you can use graphviz to visualize it source = graphviz.Source(fst_dot) source.render(outfile="quick-start.svg") # Done. quick-start.svg is the figure we showed at the start of this section if __name__ == "__main__": main() ``` -------------------------------- ### Check kaldifst version Source: https://k2-fsa.github.io/kaldifst/install/index.html Verify the installation by importing kaldifst and printing its version. Expected output is the installed version, e.g., 1.7.4. ```bash python3 -c "import kaldifst; print(kaldifst.__version__)" ``` -------------------------------- ### Install kaldifst with pip Source: https://k2-fsa.github.io/kaldifst/install/index.html Install kaldifst via pip. The first command installs the latest release from PyPI; the second upgrades from the Git repository. Both use --verbose to show detailed output. ```bash pip install --verbose kaldifst ``` ```bash pip install --verbose -U git+https://github.com/kaldifst/kaldifst.git ``` -------------------------------- ### Convert k2 transducer to OpenFst with k2_to_openfst Source: https://k2-fsa.github.io/kaldifst/convert-k2-to-openfst/convert-transducer.html Converts a k2 transducer to OpenFst using k2_to_openfst with olabels='aux_labels', then draws the result with kaldifst.draw. The example defines a transducer with labels and auxiliary labels, sets symbol tables, and renders both the k2 and OpenFst versions as SVG files. ```python #!/usr/bin/env python3 import graphviz import k2 from kaldifst.utils import k2_to_openfst import kaldifst def main(): s = """ 0 1 1 1 0.5 0 1 2 2 1.5 1 2 3 3 2.5 1 3 -1 -1 3.0 2 3 -1 -1 3.5 3 """ labels_sym = k2.SymbolTable.from_str(""" a 1 b 2 c 3 """) aux_labels_sym = k2.SymbolTable.from_str(""" x 1 y 2 z 3 """) fsa = k2.Fsa.from_str(s, acceptor=False) fsa.labels_sym = labels_sym fsa.aux_labels_sym = aux_labels_sym fsa.draw("k2-transducer.svg") fst = k2_to_openfst(fsa, olabels='aux_labels') fst_dot = kaldifst.draw(fst, acceptor=False, portrait=True) source = graphviz.Source(fst_dot) source.render(outfile="openfst-transducer.svg") if __name__ == "__main__": main() ``` -------------------------------- ### Create, print, and visualize a lattice Source: https://k2-fsa.github.io/kaldifst/lattice/intro.html Demonstrates the construction of a Lattice object, adding states and arcs with specific labels and weights, and exporting the result to a DOT format for visualization. ```python 1#!/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=2, weight=kaldifst.LatticeWeight(graph_cost=0.1, acoustic_cost=0.2), nextstate=s1, ), ) fst.add_arc( state=s0, arc=kaldifst.LatticeArc( ilabel=3, olabel=4, weight=kaldifst.LatticeWeight(graph_cost=0.3, acoustic_cost=0.4), nextstate=s2, ), ) fst.add_arc( state=s1, arc=kaldifst.LatticeArc( ilabel=5, olabel=6, weight=kaldifst.LatticeWeight(graph_cost=0.5, acoustic_cost=0.6), nextstate=s3, ), ) fst.add_arc( state=s2, arc=kaldifst.LatticeArc( ilabel=6, olabel=8, weight=kaldifst.LatticeWeight(graph_cost=0.7, acoustic_cost=0.8), nextstate=s3, ), ) fst.set_final(state=s3, weight=kaldifst.LatticeWeight.one) print(fst) # The above statement gives the following output """ 0 1 1 2 0.1,0.2 0 2 3 4 0.3,0.4 1 3 5 6 0.5,0.6 2 3 6 8 0.7,0.8 3 """ # Now we can draw it fst_dot = kaldifst.draw(fst, acceptor=False, portrait=True) print(fst_dot) # You can use # https://dreampuf.github.io/GraphvizOnline # to visualize it and share the link to others # Alternatively, you can use graphviz to visualize it source = graphviz.Source(fst_dot) source.render(outfile="lattice.svg") ``` -------------------------------- ### Convert k2 acceptor to OpenFst with k2_to_openfst Source: https://k2-fsa.github.io/kaldifst/convert-k2-to-openfst/convert-acceptor.html Converts a k2 acceptor FSA to OpenFst format using k2_to_openfst, then draws it with kaldifst. The acceptor is defined from a string with labels and weights; note the negative sign on arcs with label -1. Requires graphviz, k2, and kaldifst. ```python #!/usr/bin/env python3 import graphviz import k2 from kaldifst.utils import k2_to_openfst import kaldifst def main(): s = """ 0 1 1 0.5 0 1 2 1.5 1 2 3 2.5 1 3 -1 3.0 2 3 -1 3.5 3 """ sym = k2.SymbolTable.from_str( """ a 1 b 2 c 3 """ ) fsa = k2.Fsa.from_str(s, acceptor=True) fsa.labels_sym = sym fsa.draw("k2-acceptor.svg") fst = k2_to_openfst(fsa) fst_dot = kaldifst.draw(fst, acceptor=True, portrait=True) source = graphviz.Source(fst_dot) source.render(outfile="openfst-acceptor.svg") if __name__ == "__main__": main() ``` -------------------------------- ### Create an FST from strings Source: https://k2-fsa.github.io/kaldifst/quick_start/quick-start-2.html This script defines an FST structure, assigns input and output symbol tables, compiles the FST, saves it to a binary file, and renders a visualization as an SVG. ```python 1#!/usr/bin/env python3 import graphviz import kaldifst def main(): s = """ 0 1 a x 0.5 0 1 b y 1.5 1 2 c z 2.5 2 3.5 """ isym = kaldifst.SymbolTable.from_str( """ a 1 b 2 c 3 """ ) osym = kaldifst.SymbolTable.from_str( """ x 1 y 2 z 3 """ ) fst = kaldifst.compile( s=s, acceptor=False, isymbols=isym, osymbols=osym, keep_isymbols=True, keep_osymbols=True, ) fst.write("binary-2.fst") fst_dot = kaldifst.draw(fst, acceptor=False, portrait=True) source = graphviz.Source(fst_dot) source.render(outfile="quick-start-2.svg") if __name__ == "__main__": main() ``` -------------------------------- ### Generate ark/scp files with kaldifst.VectorFstWriter Source: https://k2-fsa.github.io/kaldifst/read-write-ark-scp/index.html Creates ark/scp files with kaldifst.VectorFstWriter. The script defines an acceptor and a transducer, writes them as lat1 and lat2 to lat.ark and lat.scp, then lists the generated files and shows how to inspect them with Kaldi's fstcopy command. ```python #!/usr/bin/env python3 import kaldifst def generate_acceptor(): s = """ 0 1 a 0.5 1 2 b 0.8 2 0.25 """ isym = kaldifst.SymbolTable.from_str( """ a 1 b 2 """ ) fsa = kaldifst.compile( s, acceptor=True, isymbols=isym, keep_isymbols=True, ) return fsa def generate_transducer(): s = """ 0 1 a A 0.5 1 2 b B 0.8 2 0.25 """ isym = kaldifst.SymbolTable.from_str( """ a 1 b 2 """ ) osym = kaldifst.SymbolTable.from_str( """ A 1 B 2 """ ) fst = kaldifst.compile( s, acceptor=False, isymbols=isym, osymbols=osym, keep_isymbols=True, keep_osymbols=True, ) return fst def test_writer(): fsa = generate_acceptor() fst = generate_transducer() wspecifier = f"ark,scp:lat.ark,lat.scp" with kaldifst.VectorFstWriter(wspecifier) as ko: ko.write("lat1", fsa) ko.write("lat2", fst) def main(): test_writer() if __name__ == "__main__": main() ``` ```shell $ ls -lh lat.scp lat.ark -rw-r--r-- 1 kuangfangjun root 452 Oct 8 23:18 lat.ark -rw-r--r-- 1 kuangfangjun root 32 Oct 8 23:18 lat.scp ``` ```shell $ fstcopy scp:lat.scp ark,t:- fstcopy scp:lat.scp ark,t:- lat1 0 1 a 1 0.5 1 2 b 2 0.8 2 0.25 lat2 0 1 a A 0.5 1 2 b B 0.8 2 0.25 LOG (fstcopy[5.5.1035~1-3dd90]:main():fstcopy.cc:71) Copied 2 FSTs. ``` -------------------------------- ### Uninstall kaldifst with pip Source: https://k2-fsa.github.io/kaldifst/install/index.html Uninstall kaldifst using pip. ```bash pip uninstall kaldifst ``` -------------------------------- ### Read ark/scp files sequentially with kaldifst.SequentialVectorFstReader Source: https://k2-fsa.github.io/kaldifst/read-write-ark-scp/index.html Reads ark/scp files sequentially with kaldifst.SequentialVectorFstReader. The script iterates over the reader, asserts each value is a kaldifst.StdVectorFst, and prints the key and FST text. The output shows the two FSTs lat1 and lat2 with their arcs. ```python #!/usr/bin/env python3 import kaldifst def test_sequential_reader(): rspecifier = f"scp:lat.scp" with kaldifst.SequentialVectorFstReader(rspecifier) as ki: for key, value in ki: assert isinstance(value, kaldifst.StdVectorFst) print("key", key) print("value\n", str(value)) def main(): test_sequential_reader() if __name__ == "__main__": main() ``` ```text key lat1 value 0 1 a 1 0.5 1 2 b 2 0.8 2 0.25 key lat2 value 0 1 a A 0.5 1 2 b B 0.8 2 0.25 ``` -------------------------------- ### Read ark/scp files randomly with kaldifst.RandomAccessVectorFstReader Source: https://k2-fsa.github.io/kaldifst/read-write-ark-scp/index.html Reads FSTs from ark/scp files randomly with kaldifst.RandomAccessVectorFstReader. The script opens the reader with a scp specifier, checks membership for lat1 and lat2, and prints each FST by key. ```python #!/usr/bin/env python3 import kaldifst def test_random_access_reader(): rspecifier = f"scp:lat.scp" with kaldifst.RandomAccessVectorFstReader(rspecifier) as ki: assert "lat1" in ki assert "lat2" in ki lat1 = ki["lat1"] print("lat1", lat1) lat2 = ki["lat2"] print("lat2", lat2) def main(): test_random_access_reader() if __name__ == "__main__": main() ``` -------------------------------- ### Iterating and modifying an FST Source: https://k2-fsa.github.io/kaldifst/quick_start/quick-start-3.html This script reads 'binary.fst', iterates through its states and arcs, updates specific arc weights, and renders the modified FST to an SVG file. ```python 1#!/usr/bin/env python3 2 3import graphviz 4 5import kaldifst 6 7 8def main(): 9 fst = kaldifst.StdVectorFst.read("binary.fst") 10 for state in kaldifst.StateIterator(fst): 11 for arc in kaldifst.ArcIterator(fst, state): 12 13 # Note: We can change the attribute of the arc if we want 14 if arc.weight == 0.5: 15 arc.weight = 0.6 16 elif arc.weight == 1.5: 17 arc.weight = 1.8 18 elif arc.weight == 2.5: 19 arc.weight = 1.25 20 21 print(state, arc) 22 23 fst_dot = kaldifst.draw(fst, acceptor=False, portrait=True) 24 source = graphviz.Source(fst_dot) 25 source.render(outfile="quick-start-3.svg") 26 27 28if __name__ == "__main__": 29 main() 30""" 31Output of this file: 32 330 (ilabel: 1, olabel: 1, weight: 0.6, nextstate: 1) 340 (ilabel: 2, olabel: 2, weight: 1.8, nextstate: 1) 351 (ilabel: 3, olabel: 3, weight: 1.25, nextstate: 2) 36""" ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.