From 76e4c5bd5016347dca083b6932e6adf03f398cad Mon Sep 17 00:00:00 2001 From: Miquel Sabaté Solà Date: Mon, 11 Aug 2025 09:28:57 +0200 Subject: Provide initial implementation for word inflection MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Word inflection for nouns, adjectives has been added so it can be initially be used in commands like 'words show'. Word categories which have no inflections (e.g. adverbs, conjunctions, et al) are skipped. Signed-off-by: Miquel Sabaté Solà --- crates/cli/src/run.rs | 37 +++---------------------------------- 1 file changed, 3 insertions(+), 34 deletions(-) (limited to 'crates/cli/src/run.rs') diff --git a/crates/cli/src/run.rs b/crates/cli/src/run.rs index 0046d47..3e1b866 100644 --- a/crates/cli/src/run.rs +++ b/crates/cli/src/run.rs @@ -6,6 +6,8 @@ use std::io::Write; use std::process::Command; use tempfile::NamedTempFile; +use crate::locale::{current_locale, Locale}; + // Maximum number of times a word has to be run in order to increase the number // of successful runs. const MAX_STEPS: usize = 5; @@ -26,34 +28,6 @@ fn help(msg: Option<&str>) { println!(" -k, --kind \t\tOnly ask for exercises for the given ."); } -// Locale represents the locales accepted for delivering answers on this -// tool. That is, it's not about i18n on the strings for this application. but -// rather the different translations accepted in places like -// `Word.translations`. -enum Locale { - English, - Catalan, -} - -impl Locale { - // Returns the string representation for the locale's code. - fn to_code(&self) -> &str { - match self { - Self::English => "en", - Self::Catalan => "ca", - } - } -} - -impl std::fmt::Display for Locale { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - match self { - Self::English => write!(f, "english"), - Self::Catalan => write!(f, "català"), - } - } -} - // Run the quiz for all the given `words` while expecting answers to be // delivered in the given `locale`. fn run_words(words: Vec, locale: &Locale) -> i32 { @@ -323,12 +297,7 @@ pub fn run(args: Vec) { } } - let raw_locale = std::env::var("LC_ALL").unwrap_or("en".to_string()); - let locale = if raw_locale.starts_with("ca") { - Locale::Catalan - } else { - Locale::English - }; + let locale = current_locale(); let words = match category { Some(cat) => select_relevant_words(cat, &flags, 15), -- cgit v1.2.3 From a6a46c403c780144e55a337a88af49fa9aa55acd Mon Sep 17 00:00:00 2001 From: Miquel Sabaté Solà Date: Mon, 27 Oct 2025 09:31:48 +0100 Subject: Add basic validation for verbs and pronouns MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Miquel Sabaté Solà --- crates/cli/src/inflection.rs | 4 ++-- crates/cli/src/run.rs | 2 +- crates/cli/src/words.rs | 32 +++++++++++++++++++++++--------- lib/mihi/src/lib.rs | 16 ++++++++++++++-- 4 files changed, 40 insertions(+), 14 deletions(-) (limited to 'crates/cli/src/run.rs') diff --git a/crates/cli/src/inflection.rs b/crates/cli/src/inflection.rs index 1b9d932..7d2f195 100644 --- a/crates/cli/src/inflection.rs +++ b/crates/cli/src/inflection.rs @@ -125,8 +125,8 @@ pub fn print_full_inflection_for(word: Word) -> Result<(), String> { match word.category { Category::Noun => print_noun_inflection(&word)?, Category::Adjective => print_adjective_inflection(&word)?, - Category::Verb => todo!(), - Category::Pronoun => todo!(), + Category::Verb => {} // TODO + Category::Pronoun => {} // TODO Category::Adverb | Category::Preposition | Category::Conjunction diff --git a/crates/cli/src/run.rs b/crates/cli/src/run.rs index 3e1b866..10e0721 100644 --- a/crates/cli/src/run.rs +++ b/crates/cli/src/run.rs @@ -23,7 +23,7 @@ fn help(msg: Option<&str>) { println!("Options:"); println!(" -c, --category \tOnly ask for words on the given ."); println!(" -e, --exercises\t\tOnly practice with exercises."); - println!(" -f, --flag\t\tFilter words by a boolean flag. Multiple flags can be provided."); + println!(" -f, --flag\t\t\tFilter words by a boolean flag. Multiple flags can be provided."); println!(" -h, --help\t\t\tPrint this message."); println!(" -k, --kind \t\tOnly ask for exercises for the given ."); } diff --git a/crates/cli/src/words.rs b/crates/cli/src/words.rs index ba86041..23a3946 100644 --- a/crates/cli/src/words.rs +++ b/crates/cli/src/words.rs @@ -248,7 +248,7 @@ fn ask_for_word_based_on(enunciated: String, word: Word) -> Result }; let inflection_id = match category { - Category::Noun | Category::Adjective => { + Category::Noun | Category::Adjective | Category::Verb => { let Ok(inflection) = Text::new("Inflection:") .with_initial_value(word.inflection_id().unwrap_or(0).to_string().as_str()) .prompt() @@ -262,7 +262,6 @@ fn ask_for_word_based_on(enunciated: String, word: Word) -> Result } _ => None, }; - let kind = kind.trim().to_string(); // TODO: refine guess once the inflection is known: select from possible values. let kind = match category { @@ -276,14 +275,28 @@ fn ask_for_word_based_on(enunciated: String, word: Word) -> Result _ => String::from("-"), }; - let Ok(regular) = Confirm::new("Regular:").with_default(word.regular).prompt() else { - return Err("abort!".to_string()); + let regular = if matches!( + category, + Category::Noun | Category::Adjective | Category::Verb + ) { + let Ok(regular) = Confirm::new("Regular:").with_default(word.regular).prompt() else { + return Err("abort!".to_string()); + }; + regular + } else { + true }; - let Ok(locative) = Confirm::new("Locative:") - .with_default(word.locative) - .prompt() - else { - return Err("abort!".to_string()); + + let locative = if matches!(category, Category::Noun) { + let Ok(locative) = Confirm::new("Locative:") + .with_default(word.locative) + .prompt() + else { + return Err("abort!".to_string()); + }; + locative + } else { + false }; let Ok(raw_weight) = Text::new("Weight:") @@ -597,6 +610,7 @@ fn show_info(word: Word) -> Result<(), String> { } // Conjugation, declension + kind. + // TODO: to_human match word.conjugation_id { Some(id) => println!("Conjugation: {}", id), None => match word.declension_id { diff --git a/lib/mihi/src/lib.rs b/lib/mihi/src/lib.rs index 36da6d2..78400f2 100644 --- a/lib/mihi/src/lib.rs +++ b/lib/mihi/src/lib.rs @@ -375,6 +375,19 @@ pub fn create_word(word: Word) -> Result<(), String> { )) } }, + Category::Verb => match word.conjugation_id { + Some(1..6) => { + if word.kind.as_str() != "verb" { + return Err(format!("bad kind for verb")); + } + } + Some(val) => return Err(format!("the conjugation ID '{val}' is not valid")), + None => { + return Err(String::from( + "you have to provide the conjugation ID for this verb", + )) + } + }, Category::Adverb | Category::Preposition | Category::Conjunction @@ -384,8 +397,7 @@ pub fn create_word(word: Word) -> Result<(), String> { return Err(format!("no inflection allowed for '{}'", word.category)); } } - // TODO - _ => { + Category::Unknown | Category::Pronoun => { return Err(format!( "you cannot create a word from the '{}' category", word.category -- cgit v1.2.3