Skip to main content

PhoneticConverter

Struct 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. For Unicode keys, normalization only lowercases when the result is a single Unicode scalar; otherwise the original key is preserved.
§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
...
Source

pub fn sorted_mappings(&self) -> Vec<(char, String)>

Returns the current character mappings sorted by letters, then digits, then symbols, with each group sorted by natural character order.

Trait Implementations§

Source§

impl Clone for PhoneticConverter

Source§

fn clone(&self) -> PhoneticConverter

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PhoneticConverter

Source§

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

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

impl Default for PhoneticConverter

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.