Skip to main content

Engine

Struct Engine 

Source
pub struct Engine {
    pub classical: Classical,
    /* private fields */
}
Expand description

The inflection engine.

Holds the settings that affect inflection: classical modes, a persistent count, the gender used for singular pronouns, and any user-defined inflections.

Fields§

§classical: Classical

Implementations§

Source§

impl Engine

Source

pub fn new() -> Self

Examples found in repository?
examples/repl.rs (line 6)
5fn main() {
6    let mut engine = Engine::new();
7    let args: Vec<String> = std::env::args().skip(1).collect();
8    if !args.is_empty() {
9        for arg in args {
10            evaluate(&mut engine, &arg);
11        }
12        return;
13    }
14
15    let interactive = io::stdin().is_terminal();
16    if interactive {
17        println!("Type text containing calls like plural('cat') or a('hour'). Ctrl-D to exit.");
18    }
19    loop {
20        if interactive {
21            print!("> ");
22            io::stdout().flush().ok();
23        }
24        let mut line = String::new();
25        if io::stdin().lock().read_line(&mut line).unwrap_or(0) == 0 {
26            break;
27        }
28        evaluate(&mut engine, line.trim_end_matches('\n'));
29    }
30}
Source

pub fn defnoun(&mut self, singular: &str, plural: &str) -> Result<(), Error>

Sets the noun plural of words matching singular (a regex) to plural.

$1, $2, … in plural are replaced by groups captured by singular.

Source

pub fn defverb( &mut self, s1: &str, p1: &str, s2: &str, p2: &str, s3: &str, p3: &str, ) -> Result<(), Error>

Sets the plurals of the 1st, 2nd and 3rd person singular forms of a verb.

Source

pub fn defadj(&mut self, singular: &str, plural: &str) -> Result<(), Error>

Source

pub fn defa(&mut self, pattern: &str) -> Result<(), Error>

Uses “a” as the indefinite article for words matching pattern.

Source

pub fn defan(&mut self, pattern: &str) -> Result<(), Error>

Uses “an” as the indefinite article for words matching pattern.

Source

pub fn num(&mut self, count: Option<i64>) -> String

Sets the count used by later calls that are not given one, returning it as a string. None clears it and returns an empty string.

Source

pub fn persistent_count(&self) -> Option<i64>

Source

pub fn set_gender(&mut self, gender: Gender)

Source

pub fn gender(&self) -> Gender

Source

pub fn plural(&self, text: &str) -> String

Returns the plural of text, which may be a noun, verb or adjective.

Leading and trailing whitespace is preserved.

Source

pub fn plural_with_count(&self, text: &str, count: impl Into<Count>) -> String

Returns text unchanged if count is singular (1, “a”, “one”, “each”, …), otherwise its plural.

Source

pub fn plural_noun(&self, text: &str) -> String

Source

pub fn plural_noun_with_count( &self, text: &str, count: impl Into<Count>, ) -> String

Source

pub fn plural_verb(&self, text: &str) -> String

Source

pub fn plural_verb_with_count( &self, text: &str, count: impl Into<Count>, ) -> String

Source

pub fn plural_adj(&self, text: &str) -> String

Source

pub fn plural_adj_with_count( &self, text: &str, count: impl Into<Count>, ) -> String

Source

pub fn singular_noun(&self, text: &str) -> Option<String>

Returns the singular of text, a plural noun, or None if text does not look like a plural.

Source

pub fn singular_noun_with_count( &self, text: &str, count: impl Into<Count>, ) -> Option<String>

Source

pub fn singular_noun_with_gender( &self, text: &str, gender: Gender, ) -> Option<String>

Like singular_noun, using gender for pronouns instead of the engine’s gender.

Source

pub fn compare(&self, word1: &str, word2: &str) -> Option<Comparison>

Compares two words for equality regardless of plurality, trying them as nouns, then verbs, then adjectives.

Source

pub fn compare_nouns(&self, word1: &str, word2: &str) -> Option<Comparison>

Source

pub fn compare_verbs(&self, word1: &str, word2: &str) -> Option<Comparison>

Source

pub fn compare_adjs(&self, word1: &str, word2: &str) -> Option<Comparison>

Source

pub fn a(&self, text: &str) -> String

Prefixes text with “a” or “an” as appropriate.

Leading and trailing whitespace is preserved, and an existing “a” or “an” is replaced.

Source

pub fn an(&self, text: &str) -> String

Source

pub fn a_with_count(&self, text: &str, count: impl Into<Count>) -> String

Like a, but if count is not singular, prefixes text with the count instead.

Source

pub fn no(&self, text: &str) -> String

Returns “no” followed by the plural of text when the count is zero, otherwise the count followed by the appropriately inflected text.

Uses the persistent count if one is set, otherwise zero.

Source

pub fn no_with_count(&self, text: &str, count: impl Into<Count>) -> String

Source

pub fn present_participle(&self, word: &str) -> String

Returns the present participle of word, a verb in the 3rd person singular (“runs” -> “running”).

Source

pub fn join(&self, words: &[&str]) -> String

Joins words into a list: “ant, bee, and fly”.

Source

pub fn join_with(&self, words: &[&str], options: &JoinOptions) -> String

Source§

impl Engine

Source

pub fn inflect(&mut self, text: &str) -> Result<String, Error>

Performs the inflections written as function calls inside text.

"The plural of cat is plural('cat')" becomes "The plural of cat is cats". The available functions are plural, plural_noun, plural_verb, plural_adj, singular_noun, a, an, no, ordinal, number_to_words, present_participle and num, taking the same arguments as the Python library.

Examples found in repository?
examples/repl.rs (line 33)
32fn evaluate(engine: &mut Engine, text: &str) {
33    match engine.inflect(text) {
34        Ok(output) => println!("{output}"),
35        Err(err) => eprintln!("error: {err}"),
36    }
37}
Source§

impl Engine

Source

pub fn ordinal(&self, num: impl Display) -> String

Returns the ordinal of a number (“1” -> “1st”) or of a number word (“one” -> “first”).

Source

pub fn number_to_words(&self, num: impl Display) -> Result<String, Error>

Returns a number in words: 1234 -> “one thousand, two hundred and thirty-four”.

Source

pub fn number_to_words_with( &self, num: impl Display, options: &NumberToWordsOptions, ) -> Result<String, Error>

Source

pub fn number_to_words_list( &self, num: impl Display, options: &NumberToWordsOptions, ) -> Result<Vec<String>, Error>

Like number_to_words_with, returning the separate chunks (“one thousand”, “two hundred and thirty-four”).

Trait Implementations§

Source§

impl Clone for Engine

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Engine

Source§

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

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

impl Default for Engine

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 = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.