use crate::phonetic::llev::RuleSetChar;
use std::sync::OnceLock;
pub fn base() -> &'static RuleSetChar {
static RULESET: OnceLock<RuleSetChar> = OnceLock::new();
RULESET.get_or_init(|| {
let content = include_str!("../../../data/rules/english/base.llev");
let file = crate::phonetic::llev::parse_str(content)
.expect("Invalid embedded base.llev - this is a bug in liblevenshtein");
RuleSetChar::from_llev(&file)
.expect("Failed to compile base rules - this is a bug in liblevenshtein")
})
}
pub fn homophones() -> &'static RuleSetChar {
static RULESET: OnceLock<RuleSetChar> = OnceLock::new();
RULESET.get_or_init(|| {
let content = include_str!("../../../data/rules/english/homophones.llev");
let file = crate::phonetic::llev::parse_str(content)
.expect("Invalid embedded homophones.llev - this is a bug in liblevenshtein");
RuleSetChar::from_llev(&file)
.expect("Failed to compile homophones rules - this is a bug in liblevenshtein")
})
}
pub fn text_speak() -> &'static RuleSetChar {
static RULESET: OnceLock<RuleSetChar> = OnceLock::new();
RULESET.get_or_init(|| {
let content = include_str!("../../../data/rules/english/text_speak.llev");
let file = crate::phonetic::llev::parse_str(content)
.expect("Invalid embedded text_speak.llev - this is a bug in liblevenshtein");
RuleSetChar::from_llev(&file)
.expect("Failed to compile text_speak rules - this is a bug in liblevenshtein")
})
}
pub fn american() -> &'static RuleSetChar {
static RULESET: OnceLock<RuleSetChar> = OnceLock::new();
RULESET.get_or_init(|| {
let content = include_str!("../../../data/rules/english/american.llev");
let file = crate::phonetic::llev::parse_str(content)
.expect("Invalid embedded american.llev - this is a bug in liblevenshtein");
RuleSetChar::from_llev(&file)
.expect("Failed to compile american rules - this is a bug in liblevenshtein")
})
}
pub fn british() -> &'static RuleSetChar {
static RULESET: OnceLock<RuleSetChar> = OnceLock::new();
RULESET.get_or_init(|| {
let content = include_str!("../../../data/rules/english/british.llev");
let file = crate::phonetic::llev::parse_str(content)
.expect("Invalid embedded british.llev - this is a bug in liblevenshtein");
RuleSetChar::from_llev(&file)
.expect("Failed to compile british rules - this is a bug in liblevenshtein")
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_base_loads() {
let rules = base();
assert!(!rules.is_empty(), "base rules should not be empty");
assert!(
rules.len() > 50,
"expected >50 base rules, got {}",
rules.len()
);
}
#[test]
fn test_homophones_loads() {
let rules = homophones();
assert!(!rules.is_empty(), "homophones rules should not be empty");
}
#[test]
fn test_text_speak_loads() {
let rules = text_speak();
assert!(!rules.is_empty(), "text_speak rules should not be empty");
}
#[test]
fn test_base_applies() {
let rules = base();
let result = rules.apply("phone");
assert!(
result.contains("f") && (result.contains("oʊn") || result.contains("on")),
"expected 'f' and 'oʊn' or 'on' in result, got: {}",
result
);
}
#[test]
fn test_homophones_applies() {
let rules = homophones();
let result = rules.apply("too");
assert_eq!(result, "to", "expected 'to', got: {}", result);
}
#[test]
fn test_text_speak_applies() {
let rules = text_speak();
let result = rules.apply("thx");
assert_eq!(result, "thanks", "expected 'thanks', got: {}", result);
}
}