Struct spellabet::PhoneticConverter
source · pub struct PhoneticConverter { /* private fields */ }
Expand description
A phonetic converter.
Implementations§
source§impl PhoneticConverter
impl PhoneticConverter
sourcepub fn new(alphabet: &SpellingAlphabet) -> Self
pub fn new(alphabet: &SpellingAlphabet) -> Self
Creates and returns a new instance of PhoneticConverter
using the
desired spelling alphabet character mappings.
§Arguments
alphabet
- TheSpellingAlphabet
to use for character conversions.
§Examples
let converter = PhoneticConverter::new(&SpellingAlphabet::default());
sourcepub const fn mappings(&self) -> &HashMap<char, String>
pub const fn mappings(&self) -> &HashMap<char, String>
Get the current character mappings of the PhoneticConverter
instance.
sourcepub const fn nonce_form(self, nonce_form: bool) -> Self
pub const fn nonce_form(self, nonce_form: bool) -> Self
Configures the current PhoneticConverter
instance to either output
code words in “nonce form” or not, based on the given boolean value.
Nonce form means each letter character is expanded into the form “‘A’ as in ALFA”. Digits and symbols are always returned using the normal output format.
§Arguments
nonce_form
- If true, enables nonce form output. Otherwise, the normal output format is used.
§Examples
let converter = PhoneticConverter::new(&SpellingAlphabet::default()).nonce_form(true);
println!("{}", converter.convert("Hello"));
'H' as in HOTEL, 'e' as in echo, 'l' as in lima, 'l' as in lima, 'o' as in oscar
sourcepub fn with_overrides(self, overrides_map: HashMap<char, String>) -> Self
pub fn with_overrides(self, overrides_map: HashMap<char, String>) -> Self
Modifies the conversion map of the current PhoneticConverter
instance
by adding or replacing mappings based on the given overrides map.
§Arguments
overrides_map
- The desired character to code word mappings to override. The capitalization of the keys and values will be automatically normalized.
§Examples
use std::collections::HashMap;
let mut converter = PhoneticConverter::new(&SpellingAlphabet::default());
let mut overrides_map = HashMap::new();
overrides_map.insert('a', "Apple".to_string());
overrides_map.insert('b', "Banana".to_string());
println!("BEFORE: {}", converter.convert("abcd"));
BEFORE: alfa bravo charlie delta
converter = converter.with_overrides(overrides_map);
println!("AFTER: {}", converter.convert("abcd"));
AFTER: apple banana charlie delta
sourcepub fn convert(&self, text: &str) -> String
pub fn convert(&self, text: &str) -> String
Converts the given text into a string of code words using the current
character mappings of the PhoneticConverter
instance.
§Arguments
text
- The text to convert into code words.
§Examples
let converter = PhoneticConverter::new(&SpellingAlphabet::default());
assert_eq!(converter.convert("Hello"), "HOTEL echo lima lima oscar");
sourcepub fn dump_alphabet(&self, writer: impl Write, verbose: bool) -> Result<()>
pub fn dump_alphabet(&self, writer: impl Write, verbose: bool) -> Result<()>
Writes the current character mappings of the PhoneticConverter
instance to the given writer.
§Arguments
writer
- The output destination.verbose
- If true, dumps all characters. Otherwise, dumps only letter characters.
§Errors
This function will return an error if writing to the provided writer fails. The specific conditions under which this may occur depend on the nature of the writer.
§Examples
let converter = PhoneticConverter::new(&SpellingAlphabet::default());
let mut buf = Vec::new();
let verbose = false;
converter.dump_alphabet(&mut buf, verbose)?;
let output = String::from_utf8(buf)?;
println!("{output}");
a -> Alfa
b -> Bravo
c -> Charlie
...