Skip to main content

Chooser

Struct Chooser 

Source
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

Source

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 words
Source

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_cbor expects
  • 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);
    }
}
Source

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),
}

Trait Implementations§

Source§

impl Debug for Chooser

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Chooser

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V