pub struct Chooser { /* private fields */ }Expand description
A fake words generator using Markov chains.
Chooser generates pronounceable pseudo-words by modeling character transition
probabilities. It’s initialized with pre-trained weights and can generate unlimited
words efficiently.
§Thread Safety
Chooser is both Send and Sync, making it safe to share across threads.
The internal HashMap is read-only after initialization.
§Examples
use flabild::Chooser;
let chooser = Chooser::new();
let word = chooser.word().expect("Failed to generate word");
println!("Generated: {}", word);Implementations§
Source§impl Chooser
impl Chooser
Sourcepub fn new() -> Chooser
pub fn new() -> Chooser
Creates a Chooser from embedded weights.
This is the standard way to create a Chooser. It loads pre-computed probability
weights from data embedded in the binary at compile time.
§Panics
Panics if the embedded weights data is corrupted or cannot be deserialized. This should only happen if the binary has been corrupted or if there’s a version mismatch between the weights data format and the deserialization code.
If you need to handle initialization errors gracefully, use try_new instead.
§Examples
use flabild::Chooser;
let chooser = Chooser::new();
// Ready to generate wordsSourcepub fn try_new() -> Result<Self>
pub fn try_new() -> Result<Self>
Attempts to create a Chooser from embedded weights.
This is the fallible version of new. Use this if you want to handle
initialization errors instead of panicking.
§Errors
Returns an error if:
- The embedded weights data is corrupted
- The data format doesn’t match what
serde_cborexpects - There’s a deserialization failure for any other reason
§Examples
use flabild::Chooser;
match Chooser::try_new() {
Ok(chooser) => {
println!("Chooser initialized successfully");
}
Err(e) => {
eprintln!("Failed to initialize: {}", e);
}
}Sourcepub fn word(&self) -> Result<String>
pub fn word(&self) -> Result<String>
Generates a random fake word.
Uses the Markov chain model to generate a pronounceable pseudo-word. The word starts from an initial state and continues adding characters based on probability weights until a terminator is reached.
§Errors
Returns an error if:
- Weights are missing for a character pair encountered during generation (indicates corrupted or incomplete training data)
- Generation exceeds the maximum word length of 80 characters (should be extremely rare with properly trained weights)
§Examples
§Basic generation
use flabild::Chooser;
let chooser = Chooser::new();
let word = chooser.word()?;
println!("Generated: {}", word);§Handling errors
use flabild::Chooser;
let chooser = Chooser::new();
match chooser.word() {
Ok(word) => println!("Success: {}", word),
Err(e) => eprintln!("Generation failed: {}", e),
}§Batch generation
use flabild::Chooser;
let chooser = Chooser::new();
let words: Result<Vec<String>, _> = (0..10)
.map(|_| chooser.word())
.collect();
match words {
Ok(words) => println!("Generated {} words", words.len()),
Err(e) => eprintln!("Error: {}", e),
}