Kaldifst: Python Bindings for OpenFst

1.8.0 · active · verified Thu Apr 16

Kaldifst is a Python wrapper for the OpenFst library, a widely used C++ library for creating, manipulating, and performing operations on Finite State Transducers (FSTs) and Finite State Automata (FSAs). It is commonly used in speech recognition (e.g., Kaldi). As of version 1.8.0, it maintains an active release cadence, frequently updating with minor versions to address build fixes, performance improvements, and new features.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a simple FST from a string, print its representation, apply common FST operations like determinization and minimization, and find the shortest path within it. The `from_str` method provides a convenient way to define FSTs for testing or simple cases.

import kaldifst

# Create a simple Finite State Transducer (FST) from a string representation
# Format: from_state to_state input_label output_label weight
# Final states are specified by 'state_id final_weight'
fst = kaldifst.Fst.from_str("""
0 1 a a 0.5
1 2 b b 1.0
2 0.0
""")

print("Initial FST:")
print(fst)

# Perform operations like determinization and minimization
# Note: Many kaldifst functions modify the FST in-place
kaldifst.det_and_minimize(fst)
print("\nDeterminized and minimized FST:")
print(fst)

# Find the shortest path in the FST
shortest_path_fst = kaldifst.shortestpath(fst)
print("\nShortest path FST:")
print(shortest_path_fst)

view raw JSON →