Library for constructing, combining, optimizing, and searching weighted finite-state transducers (FSTs). Re-implementation of OpenFst in Rust.
Project description
Rustfst
Rust
Python
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.
Overview
For a basic example see the section below.
Some simple and commonly encountered types of FSTs can be easily
created with the macro [fst
] or the functions
acceptor
and
transducer
.
For more complex cases you will likely start with the
VectorFst
type, which will be imported
in the [prelude
] along with most everything else you need.
VectorFst<TropicalWeight>
corresponds
directly to the OpenFST StdVectorFst
, and can be used to load
its files using read
or
read_text
.
Because "iteration" over an FST can mean many different things,
there are a variety of different iterators. To iterate over state
IDs you may use
states_iter
, while to
iterate over transitions out of a state, you may use
get_trs
. Since it is common to
iterate over both, this can be done using
fst_iter
or
fst_into_iter
. It
is also very common to iterate over paths accepted by an FST,
which can be done with
paths_iter
, and as a convenience
for generating text,
string_paths_iter
.
Alternately, in the case of a linear FST, you may retrieve the
only possible path with
decode_linear_fst
.
Note that iterating over paths is not the same thing as finding
the shortest path or paths, which is done with
shortest_path
(for a single path)
or
shortest_path_with_config
(for N-shortest paths).
For the complete list of algorithms, see the [algorithms
] module.
You may now be wondering, especially if you have previously used
such linguist-friendly tools as
pyfoma, "what if I just want
to transduce some text???" The unfriendly answer is that
rustfst is a somewhat lower-level library, designed for
implementing things like speech recognizers. The somewhat more
helpful answer is that you would do this by constructing an
acceptor
for your input, which you will
compose
with a
transducer
, then
project
the result to itsoutput, and finally
iterate over the paths in
the resulting FST.
References
Implementation heavily inspired from Mehryar Mohri's, Cyril Allauzen's and Michael Riley's work :
- Weighted automata algorithms
- The design principles of a weighted finite-state transducer library
- OpenFst: A general and efficient weighted finite-state transducer library
- Weighted finite-state transducers in speech recognition
The API closely resembles that of OpenFST, with some
simplifications and changes to make it more idiomatic in Rust, notably
the use of Tr
instead of Arc
. See Differences fromOpenFST for more information.
Example
use anyhow::Result;
use rustfst::prelude::*;
use rustfst::algorithms::determinize::{DeterminizeType, determinize};
use rustfst::algorithms::rm_epsilon::rm_epsilon;
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
fst.set_start(s0)?;
// 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)?;
// - 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(&fst)?;
Ok(())
}
Differences from OpenFST
Here is a non-exhaustive list of ways in which Rustfst's API differs from OpenFST:
- The default epsilon symbol is
<eps>
and not<epsilon>
. - Functions and methods follow Rust naming conventions,
e.g.
add_state
rather thanAddState
, but are otherwise mostly equivalent, except that: - Transitions are called
Tr
and notArc
, becauseArc
has a rather different and well-established meaning in Rust, and rustfst uses it (std::sync::Arc
, that is) to reference-count symbol tables. All associated functions also usetr
. - Final states are not indicated by a final weight of
zero
. You can test for finality usingis_final
, andfinal_weight
returns an [Option
]. This requires some care when converting OpenFST code. - Transitions can be accessed directly as a slice rather than requiring an iterator.
- Semiring operations are expressed as plain old methods rather
than strange C++ things. So write
w1.plus(w2)
rather thanPlus(w1, w2)
, for instance. - Weights have in-place operations for ⊕
(
plus_assign
) and ⊗ (times_assign
). - Most of the type aliases (which would be trait aliases in Rust) such
as
StdArc
,StdFst
, and so forth, are missing, but type inference allows us to avoid explicit type arguments in most cases, such as when calling [Tr::new
], for instance. - State IDs are unsigned, with [
NO_STATE_ID
] used for a missing value. They are also 32 bits by default (presumably, 4 billion states is enough for most applications). This means you must take care to cast them to [usize
] when using them as indices, and vice-versa, preferably checking for overflows - Symbol IDs are also unsigned and 32-bits, with [
NO_LABEL
] used for a missing value. - Floating-point weights are not generic, so are always single-precision.
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 😅
Documentation
The documentation of the last released version is available here : https://docs.rs/rustfst
Release process
- Use the script
update_version.sh
to update the version of every package. - Push
- Push a new tag with the prefix
rustfst-v
Example :
./update_version.sh 0.9.1-alpha.6
git commit -am "Release 0.9.1-alpha.6"
git push
git tag -a rustfst-v0.9.1-alpha.6 -m "Release rustfst 0.9.1-alpha.6"
git push --tags
Optionally, if this is a major release, create a GitHub release in the UI.
Projects contained in this repository
This repository contains two main projects:
rustfst
is the Rust re-implementation.rustfst-python
is the python binding ofrustfst
.
License
Licensed under either of
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT) or http://opensource.org/licenses/MIT)
at your option.
Contribution
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Hashes for rustfst_python-1.1.1-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | df74f4377a00f5f7f37b66e83927d37d978159aa9165fa3c124983355cee2450 |
|
MD5 | acb7673f2185eb09225046580540a72e |
|
BLAKE2b-256 | 9efdbda3458570e4e518b2e6dcd50782ded100fde1a0b663f02a7efecf0990a1 |
Hashes for rustfst_python-1.1.1-pp39-pypy39_pp73-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f2e392d6e9b15578d0c0a13615989ea7853299114f7710927a74a725585400fe |
|
MD5 | 4f8e758b0dff151cb6d163fb640586a5 |
|
BLAKE2b-256 | b351dd3b326b45992e02a55aac631e0830f02339adfeaa80b508243c591907c0 |
Hashes for rustfst_python-1.1.1-pp39-pypy39_pp73-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a20d6c8afcddf64770c9de0aebd3c4c893ebe422f91e38d8362bc9e02f10835f |
|
MD5 | d51256ca02ac095882a7eae56900971a |
|
BLAKE2b-256 | be825a1a0ed18035bc2422db6b4f02670e0b5eef3087ec10c1805d3c59cd2f41 |
Hashes for rustfst_python-1.1.1-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a0655276943fe1c43205de1ab2578c9e6396bd943757f000528eb86037de2d08 |
|
MD5 | 0cb2ba016053a700881a64c6afcba1a6 |
|
BLAKE2b-256 | 8a36ab82d555abd52490995ab09050a3642ef237dd2e6305ea82a9617ba502ef |
Hashes for rustfst_python-1.1.1-pp38-pypy38_pp73-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1cb35bd4d96f03edcc70546b67777b6b5b8696094c42262bff506d381cec3ff9 |
|
MD5 | adcc0ac9200682627c962d45534e6801 |
|
BLAKE2b-256 | 8c8f7de715d18c30ca191e454ad64d136039bcb4f0d9e1778e3eb46d3a2dbab6 |
Hashes for rustfst_python-1.1.1-pp38-pypy38_pp73-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9091a34876daa217b26ad38978ca134ef3efeeb6122a9b632ce2ad9b0941c44c |
|
MD5 | 852bf4335b25cc6e02529cafb0d27dc1 |
|
BLAKE2b-256 | 4bb6f8e616c71801492ef144387b381a61796f27a01fa5c58ec185f86a50eaff |
Hashes for rustfst_python-1.1.1-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ef0493cf6714e3e7c9dd262c3106afd4ed14abfb62b27213382d5a21a38914b8 |
|
MD5 | 26618ead2c382ff7f488f737335a1e82 |
|
BLAKE2b-256 | d3a7e12f528ec298b32a88d8eb74dffdd578feb25d841b45f3b789a49f8fd002 |
Hashes for rustfst_python-1.1.1-pp37-pypy37_pp73-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8462f13490bdedbd700b7cdd6f82bc007b8052475451d5a0f13aeab447bf0adf |
|
MD5 | 2595e3540e690adf05c454cbd082cae0 |
|
BLAKE2b-256 | 2fce4bf42fcf5d2306f0243e88a88ab127ee54adb8520738732ca50314ddf834 |
Hashes for rustfst_python-1.1.1-pp37-pypy37_pp73-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5bc6e894ad404374cb13ae51242fe5e4366a6d388f6f34bf5d8fc42cee1aad83 |
|
MD5 | 00fbfa71106e1456bbdf9f3e9da59ca4 |
|
BLAKE2b-256 | a073912510235d740e76d0a80821a30c909e3246ed75b02c762f3779efdfbb2d |
Hashes for rustfst_python-1.1.1-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 73adf07e8b42d88543e4f1a2ca1834485f3fb0ca1b82c09226ae4f2f7873272d |
|
MD5 | 96a0653477948517d15ef5e43ab21985 |
|
BLAKE2b-256 | 1c8013e7c30cbb081df0a03753686aeb330a39132b198ce6aeea6b710e5d6a27 |
Hashes for rustfst_python-1.1.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 72f32b0e75e44b34c15cbb602db91f12f10a5ef182f2d06eaf8f50ac0e59e085 |
|
MD5 | 4bd1d0f5c58d1a09cf7c2c26037b6142 |
|
BLAKE2b-256 | 0c08944a7c774121290d136a4f87596e1710a137c4c354ad5d306dbcdbbdc852 |
Hashes for rustfst_python-1.1.1-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 697750bb4996dcd245484fdc208d3ae5425bf3add2ade29bae8198ca9576d778 |
|
MD5 | 1a4172c49f1862a7c4e310609b5b33b2 |
|
BLAKE2b-256 | ca649ec9880b7b9899649ac28d13c45cb55dbe4ef7bfa3c99c51815f56bcfc71 |
Hashes for rustfst_python-1.1.1-cp311-cp311-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e6b0fa55d3c3742306868d35b816a691654f6e5b5b39ed9b5360abf864e374be |
|
MD5 | ea6025bad26141f519f12da29da57f71 |
|
BLAKE2b-256 | 984aaa421fc15694e4bd8104a88b21a41f1d117435eefd9fdb748e0ff2d9d3cd |
Hashes for rustfst_python-1.1.1-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7548595e5ee096486675964116abb299b59cc0abcd41d8cd963aa29662e717d3 |
|
MD5 | 3ef313ef19ebd1f3248de8246a50e524 |
|
BLAKE2b-256 | 220fabd1db14b6466329fc4ee830177f2cc34f49fd06556978f5c7fd07cb6a01 |
Hashes for rustfst_python-1.1.1-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 84679cd3f7c5f237976a6487e7e09415bc68149d759a2656bebe7590a00fde50 |
|
MD5 | effaeaef6bab6c2db72a6595c1b8bc18 |
|
BLAKE2b-256 | 38767538e148abc7b008a76ae575aa58d6efd9e782d124c862c3a51debbde81f |
Hashes for rustfst_python-1.1.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b4593b3ea4c3982c1c61b5d8e0e6456c23b774fc0926fd3a4dd714e8b8d7221c |
|
MD5 | 3e33ad4a6296a06050441da7589404ed |
|
BLAKE2b-256 | fee25c862afed897209466c0472eeb2ba6d548de472b721cc14d518f46a99828 |
Hashes for rustfst_python-1.1.1-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c3b40376df8806f5a809b7a7a7147ecac704ca9ce613526c60c7aa75d0133a26 |
|
MD5 | 093931c2ed7e66e5bf0cedf36d7e19d2 |
|
BLAKE2b-256 | caad91a549323fdb9df14b7c9ca1a9fbebbfc728e14a3c7f43e2ed99091e4254 |
Hashes for rustfst_python-1.1.1-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e771e5daef024e1643e23091d1f6cadf92e03a81a1d243f52c560deaa610bd02 |
|
MD5 | 9dacd4dee2d9db2e898d27905239aa88 |
|
BLAKE2b-256 | b4b86ff27220e04af4a2ef313eb54b448e309e10cc1e30884170ac48e6962baa |
Hashes for rustfst_python-1.1.1-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8763ae567ad09de7733518eaeef1583f4c023f9dba67cfae2e235b4ad0d10360 |
|
MD5 | 4595464af01d721162a89f05ced1fc2e |
|
BLAKE2b-256 | 0992ef76c0379a708c45af41de6471d3cbb589893814bfc49643ea95dfbaf4ae |
Hashes for rustfst_python-1.1.1-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3b8772fd27f65b1867e162bc9be3315db84b3694d69991a64e0725827aae34d8 |
|
MD5 | 687550a49b7a8db91f34907e0310887c |
|
BLAKE2b-256 | 4c1e0758ea4c385de1dcb2aa49af60672f99e1efc1e28f5d648a96e588b24de3 |
Hashes for rustfst_python-1.1.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7b45020caa7d164ba6d3642ab7f187f14f1c7c3c6d5d89db3f384e15db1677bb |
|
MD5 | 3edbc273ddd71e2ef32f62b921243662 |
|
BLAKE2b-256 | 38e36e8d514655ebb3e9d942c636c5f772f79a64c517dd1f7db1384eb06fe901 |
Hashes for rustfst_python-1.1.1-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 66c2a1d6b4b84735a91434c89b1a0f9a988dca769577b24577df6773ff626580 |
|
MD5 | 8f7ba89ce298210336cfcc1658bb5a5a |
|
BLAKE2b-256 | 8d364fa299cf0d7ee045ab1c819dc8f6c9b38192216a1ca6a0171411cfbd0750 |
Hashes for rustfst_python-1.1.1-cp39-cp39-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c24f0f81c7f0ed23778dd56a100586ddaa13993df3ea695842ff93b7449c345d |
|
MD5 | 16dbc436a9555213881ea43075f594c1 |
|
BLAKE2b-256 | e04a43edfbe77a6d6c8de0b310f1789f2cbe36aeb3136852a11372eefcaf42d5 |
Hashes for rustfst_python-1.1.1-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0b7fa790e71ee41bd3ea0b6996f0357979c5e155ed77fe6e36d5e6cf1f7af3c4 |
|
MD5 | 57079792a1eb2fa94d14fce278a8e52d |
|
BLAKE2b-256 | 482e0df2524307c75c830660336667aca95a948fa193b019f1843fb6d6b52f03 |
Hashes for rustfst_python-1.1.1-cp38-cp38-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a2e2290aeb52fb2a4756ab94876f013f27f3ead5b499c57d3582bcd87f602511 |
|
MD5 | cc352fbc92dc75ea9a59dad340a2dc50 |
|
BLAKE2b-256 | 83626570a6e1ba44792f78e92fa8d0a5b4f5c81b983394f87337ebf7f57b74ad |
Hashes for rustfst_python-1.1.1-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2c4198704b0db98d90d0b745870712f87fb2f2bbebedf65cf298034158a6041d |
|
MD5 | df7c8739518833f99b8dba488a227939 |
|
BLAKE2b-256 | f8904000b6dc8ff695bdabe95b299e2025ce9f378022d7d3cb00ec90a6a69725 |
Hashes for rustfst_python-1.1.1-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 81deeca59dd74f05287b56477cd06f4adf220653381c05ab64e2629794785c13 |
|
MD5 | 320c79ebb37f6d68873e2475854b20d8 |
|
BLAKE2b-256 | c9dfebe05765b8f66a3ed7d9cf205802015f1b4d334f7cc51f106e497e91c1a9 |
Hashes for rustfst_python-1.1.1-cp38-cp38-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ed341b0849e832ad3b46181b37ab6207b3fd618a5d7c5caa345e3c1c1591d52e |
|
MD5 | 1396b8b24993eca2a00f478cc683178a |
|
BLAKE2b-256 | 2d85cc7c5bf02986101ed869e2bdbef0834c8094472a4c0695e025d1b2914c65 |
Hashes for rustfst_python-1.1.1-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 37d07cd26f6e991f41bc7e2e1b2c2d8d68ca91391065ad2adf32963f87ec5c13 |
|
MD5 | 40803996d43eaf414ea8dde17e13055c |
|
BLAKE2b-256 | 7c589cc67d71da82bb3cd766fe8aee74dab1bebc7c3fdf2bdfd994d6d468f552 |
Hashes for rustfst_python-1.1.1-cp37-cp37m-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 127d937ba6bee5b8dabac0f43bd2cdb89343d2ec17f6395da06ca7bc0c62c02a |
|
MD5 | f9cfb413bcdb62a95c826c6ab278761f |
|
BLAKE2b-256 | c2717a0faeaf2067904a00977550a29b4d8c648f2f46d38b5985db1d9ff7991b |
Hashes for rustfst_python-1.1.1-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 367c5ed83e44b046ea658bfa4e5fc8b8de8c56d5d8bb839cc1bfbeaffb0620f6 |
|
MD5 | 3de2452ff4d9da29a196d0c0682d712a |
|
BLAKE2b-256 | 9a5a5236e184743015b86f1a096480eb32a43a248b78dbffaadd307d873face0 |
Hashes for rustfst_python-1.1.1-cp37-cp37m-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e86f3575fc07294e993cfe109f3fe1f3f1143ed25eeb69f3a5b814427addb866 |
|
MD5 | 4cfbd11cde771443294b4fbfd3668184 |
|
BLAKE2b-256 | f04a540e32b86af02dca7487b40bd2e13189e2abc025bd8fec40f26f050d74f4 |
Hashes for rustfst_python-1.1.1-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ff9ac3f5643fe52f378cfce91e3d01545533d6d78fcc6a15aa95da86f05dc7e3 |
|
MD5 | 524e85d7391d58d0251376241836e292 |
|
BLAKE2b-256 | 5cee28648713348c18596d8ca30a49a5fd2c6d016597ac4f9658790da37fc824 |