use core::fmt;
use english_core::EnglishCore;
pub use english_core::grammar::*;
mod noun_array;
use noun_array::*;
mod verb_array;
use verb_array::*;
mod adj_array;
use adj_array::*;
mod noun;
pub use noun::*;
mod verb;
pub use verb::*;
fn strip_trailing_number(word: &str) -> String {
if let Some(last_char) = word.chars().last() {
if last_char.is_ascii_digit() {
return word[..word.len() - 1].to_string();
}
}
word.to_string()
}
pub struct English {}
impl English {
pub fn noun<T: Into<Noun>>(word: T, number: &Number) -> String {
let noun: Noun = word.into();
let base_word = strip_trailing_number(&noun.head);
let head_inflected = match number {
Number::Singular => base_word,
Number::Plural => {
if let Some(x) = get_plural(&noun.head) {
x.to_string()
} else {
EnglishCore::noun(&base_word, number)
}
}
};
format!(
"{}{}{}",
noun.specifier
.as_ref()
.map(|s| format!("{} ", s))
.unwrap_or_default(),
head_inflected,
noun.complement
.as_ref()
.map(|c| format!(" {}", c))
.unwrap_or_default()
)
}
pub fn adj(word: &str, degree: &Degree) -> String {
let base_word = strip_trailing_number(word);
match degree {
Degree::Positive => base_word.to_string(),
Degree::Comparative => {
if let Some((comp, _)) = get_adjective_forms(word) {
comp.to_string()
} else {
EnglishCore::comparative(&base_word)
}
}
Degree::Superlative => {
if let Some((_, sup)) = get_adjective_forms(word) {
sup.to_string()
} else {
EnglishCore::superlative(&base_word)
}
}
}
}
pub fn verb<T: Into<Verb>>(
wordish: T,
person: &Person,
number: &Number,
tense: &Tense,
form: &Form,
) -> String {
let verb: Verb = wordish.into();
let base_word = strip_trailing_number(&verb.head);
let conjugated_head = match get_verb_forms(&verb.head) {
Some(wordik) => match (person, number, tense, form) {
(_, _, _, Form::Infinitive) => base_word.clone(),
(Person::Third, Number::Singular, Tense::Present, Form::Finite) => {
wordik.0.to_string()
}
(_, _, Tense::Present, Form::Finite) => base_word.clone(),
(_, _, Tense::Present, Form::Participle) => wordik.2.to_string(),
(_, _, Tense::Past, Form::Participle) => wordik.3.to_string(),
(_, _, Tense::Past, Form::Finite) => wordik.1.to_string(),
},
None => EnglishCore::verb(&base_word, person, number, tense, form),
};
if let Some(particle) = verb.particle {
format!("{} {}", conjugated_head, particle)
} else {
conjugated_head
}
}
pub fn pronoun(person: &Person, number: &Number, gender: &Gender, case: &Case) -> &'static str {
EnglishCore::pronoun(person, number, gender, case)
}
pub fn add_possessive(word: &str) -> String {
EnglishCore::add_possessive(word)
}
pub fn count<T: Into<Noun>>(word: T, count: u32) -> String {
if count == 1 {
English::noun(word, &Number::Singular)
} else {
English::noun(word, &Number::Plural)
}
}
pub fn count_with_number<T: Into<Noun>>(word: T, count: u32) -> String {
format!("{} {}", count, English::count(word, count))
}
}