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.2-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 77801b729311f35f6d6bc60e41bb3d4f7182627079de990d6baa1e9a9663b928 |
|
MD5 | 66732b112ada24dae01195c9dcbd5365 |
|
BLAKE2b-256 | 235568e2ca4e66c5be71840a291fa3d0936ad614d1ba82b498b50d61272b6158 |
Hashes for rustfst_python-1.1.2-pp39-pypy39_pp73-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ec2a8bf1225e4884423c0ccbb5e1db31cf2f1b838926410846959370709f0847 |
|
MD5 | 2051ea8b1c163986bc6c0df40f9aa8ae |
|
BLAKE2b-256 | cef6b249068d53fae18252fa18318f3b7fa6b8b7d516b48dd34ec6390460a09d |
Hashes for rustfst_python-1.1.2-pp39-pypy39_pp73-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8eed05f44c9fdb9d1f7834e92763f1fe50910e4d8d110e7990036729704b8fe8 |
|
MD5 | 606e8af2403288e0084dae6d5e7c52d6 |
|
BLAKE2b-256 | 9596002a1d4e2bccf60024568e58cd37b429be0f99b9438bcce3e34f76146e67 |
Hashes for rustfst_python-1.1.2-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 536c402fcf923e1191086b163a41f62c4888fdcf05ac5062fea85b8642c721d4 |
|
MD5 | 54ca08cbb955e32a2a42399d56e9d37f |
|
BLAKE2b-256 | 2c8414027e2d09c46c35c9014314cc7bce4224b03d90f5510edaa35ff3f9ccd4 |
Hashes for rustfst_python-1.1.2-pp38-pypy38_pp73-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1ca69e68aa0592dacfca935b697c3b1d63ee8a66cedaf2a01940641daf83b031 |
|
MD5 | 4f3960556f3166076b243eeb522d6dd4 |
|
BLAKE2b-256 | 3f069f01a28a7d376e65ed0e5c60d01f3280105e82fdbb2e9f06ebb4c055c46b |
Hashes for rustfst_python-1.1.2-pp38-pypy38_pp73-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 26ce9502405c96f2e561c5546f778f14fcc93195da6a72dc7f710b1d2208929e |
|
MD5 | 6053e3b8b7fd08df149a9eec85a827f8 |
|
BLAKE2b-256 | 97d1e6bebd4a708b415372ee27c9b2a32844be81d280d7a90156be62c7e9721a |
Hashes for rustfst_python-1.1.2-pp37-pypy37_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 92a4bab7b90d469d304cbf9b39733878c47665b0095c20ddefd1dea88a4a4fa7 |
|
MD5 | f18266a11b87a953f713c777e53b74f4 |
|
BLAKE2b-256 | 31375997d658a60b19fa5cb63cbca8befafec5d5d9bfd323f7f3928328edb05e |
Hashes for rustfst_python-1.1.2-pp37-pypy37_pp73-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7df5beecb324ff7d4adb85bc62aec86911223b0089bb7c17c3bbe88aff492eee |
|
MD5 | 9db9da5c571b381f19cacb2aabbcab51 |
|
BLAKE2b-256 | 118e1ac566e63674996f9db7f721b70ca18be890ffc1f4d5bc6468bebd66256f |
Hashes for rustfst_python-1.1.2-pp37-pypy37_pp73-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2fab44fdc104d89927bf03bdc450edad952f77ff1bd43ea5dc1a849a616f3831 |
|
MD5 | 856e3e519c7a66e8d626056a174432b2 |
|
BLAKE2b-256 | 4c4825fb867f74780d26b2076b8927a75b867ec98fd24bdffe204cf67efa1bc6 |
Hashes for rustfst_python-1.1.2-cp311-cp311-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c9c998241bc0e474834d473d78f882ddf5225d59f6cf2be7c71e2c806f0d5fbe |
|
MD5 | 3f3e25eb1962be4a57f4dbe0a1c2d6fe |
|
BLAKE2b-256 | 9e3df9ecd5e3477cca1281f70ebefb9839eb245c1f2e0566f771d89e16988af1 |
Hashes for rustfst_python-1.1.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b35cc535c1d3be75074752cd240b19b0d80e075796d85d01e86e4c8df6576dc1 |
|
MD5 | b2198bf52df55bb74d2fbcc6fda87ef0 |
|
BLAKE2b-256 | 4a978cc77ca73bcdb5d8dc3bd22eb3f8974cf1872326f554b6ecda51aaf45b61 |
Hashes for rustfst_python-1.1.2-cp311-cp311-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 456391b82e0cfe1d2b9672b473f1ed686cc247fb92fc097233c2e0db0638c826 |
|
MD5 | 3f5f4f7769256dc3e38e536a13d45a43 |
|
BLAKE2b-256 | 5d3cabc3e71fcc2d4dabb05981886b68cd57b99d8bc410af3f95717cef670295 |
Hashes for rustfst_python-1.1.2-cp311-cp311-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 146ea1d378a1c3810f0d91c83af3c2e3a055bf6fca7df5006d5c39ddf0d1ebd7 |
|
MD5 | 689d750349fbcf9c6529b69639df469b |
|
BLAKE2b-256 | d01098fd7824d1b272dc2970a45a64d4bfae94a558aec44456f05e8432a04227 |
Hashes for rustfst_python-1.1.2-cp311-cp311-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 50a9f916356308d935e56b550fcb64f47989a7d9611ad09544ca0603499f4a80 |
|
MD5 | 1fa7c2c28775fb7e09c304baf3eb8431 |
|
BLAKE2b-256 | ee7c74dcd3ab8263c57c1723ec32a12d7f49a80affc147ba9883a367b8a6ffa2 |
Hashes for rustfst_python-1.1.2-cp310-cp310-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5d2ea418a9c331751802f9aa874d400d36cd9daefa25ee89fd0980c27d9c786d |
|
MD5 | ace2e4c3b76fe892445c1e2556632170 |
|
BLAKE2b-256 | 6910650e016098323c196e1c608494374bf05bcf4187744d8b45987fdf5bcf70 |
Hashes for rustfst_python-1.1.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6d3481f494d87104241ca91f475919425ee1d6fb9e310c13879a415c67305304 |
|
MD5 | 6715be1f37ba734d780a5063b61eaefc |
|
BLAKE2b-256 | 7b1e58e84d0a8f99f39cf5b642a637bd8fd4df1486d7b842dae69e92f82a57b6 |
Hashes for rustfst_python-1.1.2-cp310-cp310-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 64e6c2f7eced095753a36e95ba914b4a679d100ba4cd413939f6480224bfdeb6 |
|
MD5 | c33f5d50e3c21e52d1b77d785ae7bfdd |
|
BLAKE2b-256 | f1438f9afba7725a4176540cf61abbd7e1cbbff58ad3764b36dc4fa362021625 |
Hashes for rustfst_python-1.1.2-cp310-cp310-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b692e751d72a690e799df22bb06054450afde36dfe3a5be18aec156dae8773c2 |
|
MD5 | de11601e4e3b00b8ab898c49c5c27d62 |
|
BLAKE2b-256 | c93b32686493a74ebe706ea65c20c7a9bc228cb4ce55b0754e03220d83268b39 |
Hashes for rustfst_python-1.1.2-cp310-cp310-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 751000afdcee046d3707f31ef6601d7bc4912267bd619071603b87035e6a8813 |
|
MD5 | 81224deb6411b3a1d37677854989631d |
|
BLAKE2b-256 | ca9d62ca9646c2cbec7749ea668c43d6c5759da2388752baa16f97849d6d5173 |
Hashes for rustfst_python-1.1.2-cp39-cp39-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 323664b87f73cd740bd15aa76eec9ff241a34d73f62ff29e892e875583db2763 |
|
MD5 | 7bca3be0da2321f4273ce0d378dc142e |
|
BLAKE2b-256 | 701aa0e20ab3b65530ec38581af7eeb7b7ed067ab2c75edcb1f362fcc64154b4 |
Hashes for rustfst_python-1.1.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6fdd1fc9fac6fdc8cc02f6a334dfe404bd2fdaf368066e19688c2c88f4ef592b |
|
MD5 | 1d951a615392025b3be9527166fac963 |
|
BLAKE2b-256 | 377fe0c30952f2ed7f07c655a5bba8fb7bd0b7c8ed7a35c7f6ed57d15e5a7175 |
Hashes for rustfst_python-1.1.2-cp39-cp39-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 08a570eb357b7451ed08f6e0ec365857fa406805a2f62b8da3391530e47baef1 |
|
MD5 | 291e8bf54301259eebcdf38c8b6c4ff1 |
|
BLAKE2b-256 | f42b968d8136c2a1eb4d589787c6e589f37fcd9a453a231dff8e899b5aeb1e0c |
Hashes for rustfst_python-1.1.2-cp39-cp39-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fdb6d119c1a65a0c5d0af256051866fcf7be535fd36b4a2f506d252378821457 |
|
MD5 | 267057b944c193aa98faaf43d86739ac |
|
BLAKE2b-256 | f79fe8b35ffc6881f0cc29b8e0f3b8a3604c0fb14721a1ce7bafdadfe3e428f7 |
Hashes for rustfst_python-1.1.2-cp39-cp39-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6a40394dca7b3b71342199713ffc3a938357df90f7544d8c668581e2e63a182b |
|
MD5 | 68781f41cb7f3b0cdcfb1473f81180f9 |
|
BLAKE2b-256 | 9280ad0351a491f0d9d9129eafc9e2cb7b30b13ec861a54cc8412c1933095cff |
Hashes for rustfst_python-1.1.2-cp38-cp38-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | eb4c4146d12752df9069e0bfa00ff5cf241edd419ccbadc8f1897cb861d58349 |
|
MD5 | 647b82a6cb18c56488d14907452a8025 |
|
BLAKE2b-256 | c9f6938c89668e14ba4a5f00f82386a0cdd845edcba9e4919a23179744d39cdf |
Hashes for rustfst_python-1.1.2-cp38-cp38-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 17695a7e0ae1115869bed802c4ae5b35b7557322c3cbcf1a5e21a8596b80c7df |
|
MD5 | 5520c01a8e72e7ae3389c5d90e42e345 |
|
BLAKE2b-256 | 65e451913fa6e2ea6583a4f5c11a4a79e8e2ff24e0011b796750b6a691ed9c79 |
Hashes for rustfst_python-1.1.2-cp38-cp38-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 12bd56aa83597bb584767e632cdd644d7841764cc1d29987dbcb8842df551298 |
|
MD5 | 1ce73e44329b685c7387a3fc7c012c47 |
|
BLAKE2b-256 | cb951d3585221e128e8cdf2720f4dac8ed16af532016932885df90e674975b59 |
Hashes for rustfst_python-1.1.2-cp38-cp38-macosx_11_0_arm64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | da267c74917762f64c267979f39caa0bcb055ad0ca67ef0dca48026784bdf95b |
|
MD5 | f39d22a6af5caed30854458c52c58685 |
|
BLAKE2b-256 | b5bfdd934c08dd50c08da800169a5eb7a6c5284b197af49f83d1b162c635e940 |
Hashes for rustfst_python-1.1.2-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dabda9da739088cec24dd824dd2f35a3715f66bd023edca2486088842a97cd7f |
|
MD5 | 7507596075bd9acb3588f1c7a6d57551 |
|
BLAKE2b-256 | a42c32310d88350e8fb99a59755c67bf7d3dcb7bc764392b3234e7687971e450 |
Hashes for rustfst_python-1.1.2-cp37-cp37m-musllinux_1_1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8484fe2ea5a460c8869e3ab2d6cee33351a02edfe0f1fa99307ad18b649a8667 |
|
MD5 | 96ef2ea9ed922f28f5097d9f569d9e34 |
|
BLAKE2b-256 | a7fe134e649e8d9e8099ae2987af1028b28f9a9ff921fb33e000194771a6e2d2 |
Hashes for rustfst_python-1.1.2-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6b37e4f3b0165376e1bac75a62d207f4f558351e8d0f32acd742efb9ac9bd5d7 |
|
MD5 | 3dda4762e0f717791c9b86f174035fd9 |
|
BLAKE2b-256 | 4ca9ae9aa9fac8b0e5c43d721433f2f50f77af34783ab2528569a8a2899cd961 |
Hashes for rustfst_python-1.1.2-cp37-cp37m-manylinux_2_5_i686.manylinux1_i686.manylinux_2_17_i686.manylinux2014_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c2ad8e7b55319f57f52283b4c05c5037cf9e333732823432f1883c0c75691eb5 |
|
MD5 | 0219301427c496058b69efb9e2926cd0 |
|
BLAKE2b-256 | 1fa161d0babe0295ee4a4be04259310ac1d12ab8498f73be3b08f5d624bb8a53 |
Hashes for rustfst_python-1.1.2-cp37-cp37m-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0328afc22ee60ad1da4b63acad5a2abbc734da1bb9dfbb17ced790fd9e6c92e9 |
|
MD5 | 694fff25f404c41384f06d865ffbfe68 |
|
BLAKE2b-256 | 5d4ad8f924425a29e4658a4038f073ec16792f04adabf35a2a4cd272eb62b975 |