Function rustfst::utils::acceptor

source ·
pub fn acceptor<W: Semiring, F: MutableFst<W>>(labels: &[Label], weight: W) -> F
Expand description

Turns a list of labels into a linear acceptor (FST with the same labels for both input and output). The only accepted path in the acceptor will be labels.

§Example

use rustfst::fst_traits::{CoreFst, MutableFst, ExpandedFst};
use rustfst::fst_impls::VectorFst;
use rustfst::semirings::{ProbabilityWeight, Semiring};
use rustfst::utils::acceptor;
use rustfst::Tr;

let labels = vec![32, 43, 21];

let fst : VectorFst<ProbabilityWeight> = acceptor(&labels, ProbabilityWeight::one());

assert_eq!(fst.num_states(), 4);

// The acceptor function produces the same FST as the following code

let mut fst_ref = VectorFst::new();
let s1 = fst_ref.add_state();
let s2 = fst_ref.add_state();
let s3 = fst_ref.add_state();
let s4 = fst_ref.add_state();

fst_ref.set_start(s1).unwrap();
fst_ref.set_final(s4, ProbabilityWeight::one()).unwrap();

fst_ref.add_tr(s1, Tr::new(labels[0], labels[0], ProbabilityWeight::one(), s2)).unwrap();
fst_ref.add_tr(s2, Tr::new(labels[1], labels[1], ProbabilityWeight::one(), s3)).unwrap();
fst_ref.add_tr(s3, Tr::new(labels[2], labels[2], ProbabilityWeight::one(), s4)).unwrap();

assert_eq!(fst, fst_ref);