#fst #graph #transducer #acceptor #shortest-path


Library for constructing, combining, optimizing, and searching weighted finite-state transducers (FSTs)

22 releases

new 0.8.1 Jun 24, 2021
0.8.0 Dec 16, 2020
0.7.4 Oct 12, 2020
0.5.0 Feb 4, 2020
0.1.7 Oct 28, 2018

#20 in Science

Download history 210/week @ 2021-02-25 183/week @ 2021-03-04 360/week @ 2021-03-11 187/week @ 2021-03-18 329/week @ 2021-03-25 310/week @ 2021-04-01 329/week @ 2021-04-08 287/week @ 2021-04-15 305/week @ 2021-04-22 249/week @ 2021-04-29 257/week @ 2021-05-06 268/week @ 2021-05-13 285/week @ 2021-05-20 353/week @ 2021-05-27 376/week @ 2021-06-03 453/week @ 2021-06-10

1,323 downloads per month


27K SLoC


rustc >= 1.41.0 Native Linux test status Current version Documentation License: MIT/Apache-2.0

Rust implementation of Weighted Finite States Transducers.

Rustfst is a library for constructing, combining, optimizing, and searching weighted finite-state transducers (FSTs). Weighted finite-state transducers are automata where each transition has an input label, an output label, and a weight. The more familiar finite-state acceptor is represented as a transducer with each transition's input and output label equal. Finite-state acceptors are used to represent sets of strings (specifically, regular or rational sets); finite-state transducers are used to represent binary relations between pairs of strings (specifically, rational transductions). The weights can be used to represent the cost of taking a particular transition.

FSTs have key applications in speech recognition and synthesis, machine translation, optical character recognition, pattern matching, string processing, machine learning, information extraction and retrieval among others. Often a weighted transducer is used to represent a probabilistic model (e.g., an n-gram model, pronunciation model). FSTs can be optimized by determinization and minimization, models can be applied to hypothesis sets (also represented as automata) or cascaded by finite-state composition, and the best results can be selected by shortest-path algorithms.



Implementation heavily inspired from Mehryar Mohri's, Cyril Allauzen's and Michael Riley's work :


use anyhow::Result;
use rustfst::prelude::*;
use rustfst::algorithms::determinize::{DeterminizeType, determinize};
use rustfst::algorithms::rm_epsilon::rm_epsilon;
use std::sync::Arc;

fn main() -> Result<()> {
    // Creates a empty wFST
    let mut fst = VectorFst::<TropicalWeight>::new();

    // Add some states
    let s0 = fst.add_state();
    let s1 = fst.add_state();
    let s2 = fst.add_state();

    // Set s0 as the start state

    // Add a transition from s0 to s1
    fst.add_tr(s0, Tr::new(3, 5, 10.0, s1))?;

    // Add a transition from s0 to s2
    fst.add_tr(s0, Tr::new(5, 7, 18.0, s2))?;

    // Set s1 and s2 as final states
    fst.set_final(s1, 31.0)?;
    fst.set_final(s2, 45.0)?;

    // Iter over all the paths in the wFST
    for p in fst.paths_iter() {
         println!("{:?}", p);

    // A lot of operations are available to modify/optimize the FST.
    // Here are a few examples :

    // - Remove useless states.
    connect(&mut fst)?;

    // - Optimize the FST by merging states with the same behaviour.
    minimize(&mut fst, true)?;

    // - Copy all the input labels in the output.
    project(&mut fst, ProjectType::ProjectInput);

    // - Remove epsilon transitions.
    rm_epsilon(&mut fst)?;

    // - Compute an equivalent FST but deterministic.
    fst = determinize(Arc::new(fst), DeterminizeType::DeterminizeFunctional)?;



A big number of algorithms are already implemented. The main one missing is the Composition.

Benchmark with OpenFST

I did a benchmark some time ago on almost every linear fst algorithm and compared the results with OpenFst. You can find the results here :

Spoiler alert: Rustfst is faster on all those algorithms 😅

For the other algorithms, i'm finishing the implementation then will do another round of benchmarks.

At the moment, the main algorithm missing in Rustfst is the composition that will be implemented shortly. All the important algorithms like minimization, determinization ... are already implemented but not benched and thus not (necessarily) optimized.


The documentation of the last released version is available here : https://docs.rs/rustfst


Licensed under either of

at your option.


Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.


~93K SLoC