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: ClassicalImplementations§
Source§impl Engine
impl Engine
Sourcepub fn new() -> Self
pub fn new() -> Self
Examples found in repository?
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}Sourcepub fn defnoun(&mut self, singular: &str, plural: &str) -> Result<(), Error>
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.
Sourcepub fn defverb(
&mut self,
s1: &str,
p1: &str,
s2: &str,
p2: &str,
s3: &str,
p3: &str,
) -> Result<(), Error>
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.
pub fn defadj(&mut self, singular: &str, plural: &str) -> Result<(), Error>
Sourcepub fn defa(&mut self, pattern: &str) -> Result<(), Error>
pub fn defa(&mut self, pattern: &str) -> Result<(), Error>
Uses “a” as the indefinite article for words matching pattern.
Sourcepub fn defan(&mut self, pattern: &str) -> Result<(), Error>
pub fn defan(&mut self, pattern: &str) -> Result<(), Error>
Uses “an” as the indefinite article for words matching pattern.
Sourcepub fn num(&mut self, count: Option<i64>) -> String
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.
pub fn persistent_count(&self) -> Option<i64>
pub fn set_gender(&mut self, gender: Gender)
pub fn gender(&self) -> Gender
Sourcepub fn plural(&self, text: &str) -> String
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.
Sourcepub fn plural_with_count(&self, text: &str, count: impl Into<Count>) -> String
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.
pub fn plural_noun(&self, text: &str) -> String
pub fn plural_noun_with_count( &self, text: &str, count: impl Into<Count>, ) -> String
pub fn plural_verb(&self, text: &str) -> String
pub fn plural_verb_with_count( &self, text: &str, count: impl Into<Count>, ) -> String
pub fn plural_adj(&self, text: &str) -> String
pub fn plural_adj_with_count( &self, text: &str, count: impl Into<Count>, ) -> String
Sourcepub fn singular_noun(&self, text: &str) -> Option<String>
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.
pub fn singular_noun_with_count( &self, text: &str, count: impl Into<Count>, ) -> Option<String>
Sourcepub fn singular_noun_with_gender(
&self,
text: &str,
gender: Gender,
) -> Option<String>
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.
Sourcepub fn compare(&self, word1: &str, word2: &str) -> Option<Comparison>
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.
pub fn compare_nouns(&self, word1: &str, word2: &str) -> Option<Comparison>
pub fn compare_verbs(&self, word1: &str, word2: &str) -> Option<Comparison>
pub fn compare_adjs(&self, word1: &str, word2: &str) -> Option<Comparison>
Sourcepub fn a(&self, text: &str) -> String
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.
pub fn an(&self, text: &str) -> String
Sourcepub fn a_with_count(&self, text: &str, count: impl Into<Count>) -> String
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.
Sourcepub fn no(&self, text: &str) -> String
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.
pub fn no_with_count(&self, text: &str, count: impl Into<Count>) -> String
Sourcepub fn present_participle(&self, word: &str) -> String
pub fn present_participle(&self, word: &str) -> String
Returns the present participle of word, a verb in the 3rd person
singular (“runs” -> “running”).
pub fn join_with(&self, words: &[&str], options: &JoinOptions) -> String
Source§impl Engine
impl Engine
Sourcepub fn inflect(&mut self, text: &str) -> Result<String, Error>
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.
Source§impl Engine
impl Engine
Sourcepub fn ordinal(&self, num: impl Display) -> String
pub fn ordinal(&self, num: impl Display) -> String
Returns the ordinal of a number (“1” -> “1st”) or of a number word (“one” -> “first”).
Sourcepub fn number_to_words(&self, num: impl Display) -> Result<String, Error>
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”.
pub fn number_to_words_with( &self, num: impl Display, options: &NumberToWordsOptions, ) -> Result<String, Error>
Sourcepub fn number_to_words_list(
&self,
num: impl Display,
options: &NumberToWordsOptions,
) -> Result<Vec<String>, Error>
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”).