Struct spellabet::PhoneticConverter

source ·
pub struct PhoneticConverter { /* private fields */ }
Expand description

A phonetic converter.

Implementations§

source§

impl PhoneticConverter

source

pub fn new(alphabet: &SpellingAlphabet) -> Self

Creates and returns a new instance of PhoneticConverter using the desired spelling alphabet character mappings.

§Arguments
§Examples
let converter = PhoneticConverter::new(&SpellingAlphabet::default());
source

pub const fn mappings(&self) -> &HashMap<char, String>

Get the current character mappings of the PhoneticConverter instance.

source

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
source

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
source

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");
source

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
...

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

§

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

§

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.