| Crates.io | sfst |
| lib.rs | sfst |
| version | 0.1.0 |
| created_at | 2025-08-09 10:23:41.845547+00 |
| updated_at | 2025-08-09 10:23:41.845547+00 |
| description | SFST (Stuttgart Finite State Transducer) |
| homepage | |
| repository | |
| max_upload_size | |
| id | 1787812 |
| size | 1,576,402 |
This is a Rust binding for the SFST (Stuttgart Finite State Transducer Tools) library. It provides a safe, idiomatic Rust interface to the C++ SFST library for finite state transducer operations.
../src/ relative to this directory)Clone or place this rust/ directory alongside the SFST source code:
project/
├── src/ # SFST C++ source files
├── python/ # Python bindings (optional)
└── rust/ # This Rust binding
Build the library:
cd rust
cargo build
Run tests:
cargo test
Run the example/test binary:
cargo run --bin main
use sfst;
// Initialize transducer from file
sfst::init("path/to/transducer.a")?;
// Analyze a surface form
let analysis = sfst::analyse("easier")?;
println!("Analysis: {:?}", analysis); // ["easy<ADJ><comp>"]
// Generate a surface form
let generation = sfst::generate("easy<ADJ><comp>")?;
println!("Generation: {:?}", generation); // ["easier"]
// Manual cleanup
sfst::cleanup();
use sfst::Sfst;
// Initialize with automatic cleanup
let sfst = Sfst::new("path/to/transducer.a")?;
// Analyze and generate
let analysis = sfst.analyse("easier")?;
let generation = sfst.generate("easy<ADJ><comp>")?;
// Automatic cleanup when `sfst` goes out of scope
use sfst::{Sfst, SfstError};
match Sfst::new("transducer.a") {
Ok(sfst) => {
match sfst.analyse("word") {
Ok(results) => println!("Results: {:?}", results),
Err(SfstError::TransducerNotInitialized) => {
eprintln!("Transducer not properly initialized");
}
Err(e) => eprintln!("Error: {}", e),
}
}
Err(SfstError::FileError(msg)) => {
eprintln!("Could not load transducer: {}", msg);
}
Err(e) => eprintln!("Error: {}", e),
}
init(filename: &str) -> Result<(), SfstError> - Initialize transducer from fileanalyse(input: &str) -> Result<Vec<String>, SfstError> - Analyze a stringgenerate(input: &str) -> Result<Vec<String>, SfstError> - Generate a stringcleanup() - Manual cleanup (not needed with RAII API)Sfst::new(filename: &str) -> Result<Sfst, SfstError> - Create with automatic cleanupsfst.analyse(input: &str) -> Result<Vec<String>, SfstError> - Analyze a stringsfst.generate(input: &str) -> Result<Vec<String>, SfstError> - Generate a stringpub enum SfstError {
InvalidInput(String), // Invalid input parameters
FileError(String), // File loading/reading errors
TransducerNotInitialized, // Transducer not initialized
AllocationError, // Memory allocation failure
}
rust/
├── Cargo.toml # Package configuration
├── README.md # This file
├── build.rs # Build script for C++ compilation
└── src/
├── lib.rs # Main library code
├── main.rs # Example/test binary
├── sfst_wrapper.h # C header for FFI
└── sfst_wrapper.cpp # C++ wrapper implementation
The crate includes comprehensive tests that mirror the Python test suite:
# Run library tests
cargo test
# Run the example binary (requires test files)
cargo run --bin main
The tests expect the test file python/tests/easy.a to exist. Make sure you have the complete SFST project structure.
This binding follows the same license as the original SFST library.