pub struct ErrorModel {
pub p_deletion: f64,
pub p_insertion: f64,
pub p_substitution: f64,
pub p_transposition: f64,
/* private fields */
}Expand description
Error model for the noisy channel model
Fields§
§p_deletion: f64Probability of deletion errors
p_insertion: f64Probability of insertion errors
p_substitution: f64Probability of substitution errors
p_transposition: f64Probability of transposition errors
Implementations§
Source§impl ErrorModel
impl ErrorModel
Sourcepub fn new(
p_deletion: f64,
p_insertion: f64,
p_substitution: f64,
p_transposition: f64,
) -> Self
pub fn new( p_deletion: f64, p_insertion: f64, p_substitution: f64, p_transposition: f64, ) -> Self
Create a new error model with custom error probabilities
Examples found in repository?
examples/statistical_spelling_demo.rs (line 393)
387fn noise_model_demo() -> Result<(), Box<dyn std::error::Error>> {
388 println!("\n=== Error Model Demo ===\n");
389
390 // Create different error models with varying error type probabilities
391 let models = [
392 ("Default", ErrorModel::default()),
393 ("Deletion-heavy", ErrorModel::new(0.7, 0.1, 0.1, 0.1)),
394 ("Insertion-heavy", ErrorModel::new(0.1, 0.7, 0.1, 0.1)),
395 ("Substitution-heavy", ErrorModel::new(0.1, 0.1, 0.7, 0.1)),
396 ("Transposition-heavy", ErrorModel::new(0.1, 0.1, 0.1, 0.7)),
397 ];
398
399 // Test cases for different error types
400 let test_pairs = [
401 ("recieve", "receive"), // Transposition (i and e)
402 ("acheive", "achieve"), // Transposition (i and e)
403 ("languge", "language"), // Deletion (missing 'a')
404 ("programing", "programming"), // Deletion (missing 'm')
405 ("probblem", "problem"), // Insertion (extra 'b')
406 ("committe", "committee"), // Insertion (missing 'e')
407 ("definately", "definitely"), // Substitution ('a' instead of 'i')
408 ("seperate", "separate"), // Substitution ('e' instead of 'a')
409 ];
410
411 // Test each error model
412 println!(
413 "{:<20} {:<12} {:<12} {:<12} {:<12} {:<12}",
414 "Model", "Delete Prob", "Insert Prob", "Subst Prob", "Transp Prob", "Example"
415 );
416 println!("{:-<80}", "");
417
418 for (name, model) in &models {
419 // Pick one example to show
420 let (typo, correct) = test_pairs[0];
421 let probability = model.error_probability(typo, correct);
422
423 println!(
424 "{:<20} {:<12.2} {:<12.2} {:<12.2} {:<12.2} {:<12.4}",
425 name,
426 model.p_deletion,
427 model.p_insertion,
428 model.p_substitution,
429 model.p_transposition,
430 probability
431 );
432 }
433
434 println!("\nError probabilities for different error types (using default model):");
435
436 let default_model = ErrorModel::default();
437
438 for (typo, correct) in &test_pairs {
439 let prob = default_model.error_probability(typo, correct);
440 println!("{typo:<12} -> {correct:<12}: {prob:.6}");
441 }
442
443 println!("\nImpact on correction with custom error model:");
444
445 // Create a statistical corrector with a custom error model
446 let custom_config = StatisticalCorrectorConfig {
447 language_model_weight: 0.3,
448 edit_distance_weight: 0.7,
449 ..Default::default()
450 };
451
452 let mut custom_corrector = StatisticalCorrector::new(custom_config);
453 train_language_model(&mut custom_corrector);
454 add_example_words(&mut custom_corrector);
455
456 // Create a transposition-heavy error model (good for common spelling errors)
457 let transposition_model = ErrorModel::new(0.1, 0.1, 0.1, 0.7);
458 custom_corrector.set_error_model(transposition_model);
459
460 // Test some examples
461 println!("\nCorrecting text with transposition-heavy error model:");
462 let testtext = "I recieved a mesage about thier acheivements.";
463 let corrected = custom_corrector.correcttext(testtext)?;
464
465 println!("Before: {testtext}");
466 println!("After: {corrected}");
467
468 Ok(())
469}Sourcepub fn with_max_distance(self, maxdistance: usize) -> Self
pub fn with_max_distance(self, maxdistance: usize) -> Self
Set the maximum edit distance to consider
Sourcepub fn error_probability(&self, typo: &str, correct: &str) -> f64
pub fn error_probability(&self, typo: &str, correct: &str) -> f64
Calculate the error probability P(typo | correct)
Examples found in repository?
examples/statistical_spelling_demo.rs (line 421)
387fn noise_model_demo() -> Result<(), Box<dyn std::error::Error>> {
388 println!("\n=== Error Model Demo ===\n");
389
390 // Create different error models with varying error type probabilities
391 let models = [
392 ("Default", ErrorModel::default()),
393 ("Deletion-heavy", ErrorModel::new(0.7, 0.1, 0.1, 0.1)),
394 ("Insertion-heavy", ErrorModel::new(0.1, 0.7, 0.1, 0.1)),
395 ("Substitution-heavy", ErrorModel::new(0.1, 0.1, 0.7, 0.1)),
396 ("Transposition-heavy", ErrorModel::new(0.1, 0.1, 0.1, 0.7)),
397 ];
398
399 // Test cases for different error types
400 let test_pairs = [
401 ("recieve", "receive"), // Transposition (i and e)
402 ("acheive", "achieve"), // Transposition (i and e)
403 ("languge", "language"), // Deletion (missing 'a')
404 ("programing", "programming"), // Deletion (missing 'm')
405 ("probblem", "problem"), // Insertion (extra 'b')
406 ("committe", "committee"), // Insertion (missing 'e')
407 ("definately", "definitely"), // Substitution ('a' instead of 'i')
408 ("seperate", "separate"), // Substitution ('e' instead of 'a')
409 ];
410
411 // Test each error model
412 println!(
413 "{:<20} {:<12} {:<12} {:<12} {:<12} {:<12}",
414 "Model", "Delete Prob", "Insert Prob", "Subst Prob", "Transp Prob", "Example"
415 );
416 println!("{:-<80}", "");
417
418 for (name, model) in &models {
419 // Pick one example to show
420 let (typo, correct) = test_pairs[0];
421 let probability = model.error_probability(typo, correct);
422
423 println!(
424 "{:<20} {:<12.2} {:<12.2} {:<12.2} {:<12.2} {:<12.4}",
425 name,
426 model.p_deletion,
427 model.p_insertion,
428 model.p_substitution,
429 model.p_transposition,
430 probability
431 );
432 }
433
434 println!("\nError probabilities for different error types (using default model):");
435
436 let default_model = ErrorModel::default();
437
438 for (typo, correct) in &test_pairs {
439 let prob = default_model.error_probability(typo, correct);
440 println!("{typo:<12} -> {correct:<12}: {prob:.6}");
441 }
442
443 println!("\nImpact on correction with custom error model:");
444
445 // Create a statistical corrector with a custom error model
446 let custom_config = StatisticalCorrectorConfig {
447 language_model_weight: 0.3,
448 edit_distance_weight: 0.7,
449 ..Default::default()
450 };
451
452 let mut custom_corrector = StatisticalCorrector::new(custom_config);
453 train_language_model(&mut custom_corrector);
454 add_example_words(&mut custom_corrector);
455
456 // Create a transposition-heavy error model (good for common spelling errors)
457 let transposition_model = ErrorModel::new(0.1, 0.1, 0.1, 0.7);
458 custom_corrector.set_error_model(transposition_model);
459
460 // Test some examples
461 println!("\nCorrecting text with transposition-heavy error model:");
462 let testtext = "I recieved a mesage about thier acheivements.";
463 let corrected = custom_corrector.correcttext(testtext)?;
464
465 println!("Before: {testtext}");
466 println!("After: {corrected}");
467
468 Ok(())
469}Trait Implementations§
Source§impl Clone for ErrorModel
impl Clone for ErrorModel
Source§fn clone(&self) -> ErrorModel
fn clone(&self) -> ErrorModel
Returns a duplicate of the value. Read more
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for ErrorModel
impl Debug for ErrorModel
Auto Trait Implementations§
impl Freeze for ErrorModel
impl RefUnwindSafe for ErrorModel
impl Send for ErrorModel
impl Sync for ErrorModel
impl Unpin for ErrorModel
impl UnwindSafe for ErrorModel
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.