From f417d886312f5211e40b52eae2f7962f1b62d9e0 Mon Sep 17 00:00:00 2001 From: VoiceLessQ Date: Sat, 24 Oct 2026 19:19:25 -0100 Subject: [PATCH] Proofing options reach the Russian and Turkish checks too MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit storytold/wordcraft#3966 made Ignore words in UPPERCASE and Ignore words that contain numbers reach six languages' own checks, one copy at a time, and Russian and Turkish were left out. lib.rs still called `ru::is_correct(word)` and `tr::is_correct(word)`, which accept words with digits (both) and short words in capitals (Russian) whatever the options say, so turning either option off changed nothing in those languages. Both now have `is_correct_with_options`, like the others. Russian's rule for a part in capitals (`ИИ-консультант`) follows the option too, as Indonesian's does, and its digits rule keeps counting superscripts (`м²`). Turkish had no test dictionary, so its two dictionary tests returned early wherever none was installed, CI included; it has a synthetic one now, also in the dictionaries-folder test. Tests: proofing_options_reach_the_russian_check and proofing_options_reach_the_turkish_check. Calling the old checks from lib.rs fails each, and so does ungating the Russian part rule or the Turkish hyphen recursion. --- crates/proof/src/lib.rs | 9 ++++--- crates/proof/src/ru/mod.rs | 45 +++++++++++++++++++++++++++----- crates/proof/src/tr/mod.rs | 39 ++++++++++++++++++++++----- crates/proof/src/tr/suggest.rs | 5 +--- crates/proof/src/tr/test_dict.rs | 22 ++++++++++++++++ 5 files changed, 99 insertions(+), 21 deletions(-) create mode 100644 crates/proof/src/tr/test_dict.rs diff --git a/crates/proof/src/lib.rs b/crates/proof/src/lib.rs index 8d0baba5e..47216d72a 100644 --- a/crates/proof/src/lib.rs +++ b/crates/proof/src/lib.rs @@ -778,8 +778,8 @@ pub fn is_correct_in_with(word: &str, lang: ProofLang, opts: &Options) -> bool { ProofLang::Hr => accepted_by_options(word, opts) || hr::is_correct_with_options(word, opts), ProofLang::Pt => accepted_by_options(word, opts) || pt::is_correct_with_options(word, opts), ProofLang::It => accepted_by_options(word, opts) || it::is_correct_with_options(word, opts), - ProofLang::Tr => accepted_by_options(word, opts) || tr::is_correct(word), - ProofLang::Ru => accepted_by_options(word, opts) || ru::is_correct(word), + ProofLang::Tr => accepted_by_options(word, opts) || tr::is_correct_with_options(word, opts), + ProofLang::Ru => accepted_by_options(word, opts) || ru::is_correct_with_options(word, opts), ProofLang::Es | ProofLang::EsMx | ProofLang::Es419 | ProofLang::EsUy => { accepted_by_options(word, opts) || es::is_correct_with_options(word, lang, opts) } @@ -1075,6 +1075,7 @@ mod tests { ("pt_BR", pt::test_dict::AFF, pt::test_dict::DIC), ("it_IT", it::test_dict::AFF, it::test_dict::DIC), ("hr_HR", hr::test_dict::AFF, hr::test_dict::DIC), + ("tr_TR", tr::test_dict::AFF, tr::test_dict::DIC), ("ru_RU", ru::test_dict::AFF, ru::test_dict::DIC), ("cs_CZ", cs::test_dict::AFF, cs::test_dict::DIC), ("id_ID", id::test_dict::AFF, id::test_dict::DIC), @@ -1094,9 +1095,9 @@ mod tests { let before = dictionary_generation(); set_dictionaries_folder(Some(dir.clone())); assert!(dictionary_generation() > before, "cached checks are dropped"); - // Turkish has no test dictionary to put in the folder; Russian, Czech, Indonesian and the Spanishes follow it. + // Turkish follows the four below; Russian, Czech, Indonesian and the Spanishes follow it. let found = installed_dictionaries(); - assert_eq!(found.get(4).map(|(lang, _)| *lang), Some(ProofLang::Tr)); + assert_eq!(found.get(4), Some(&(ProofLang::Tr, Some(dir.join("tr_TR.dic"))))); assert_eq!( found[7..], [ diff --git a/crates/proof/src/ru/mod.rs b/crates/proof/src/ru/mod.rs index c92e27125..d5035e6a3 100644 --- a/crates/proof/src/ru/mod.rs +++ b/crates/proof/src/ru/mod.rs @@ -130,18 +130,30 @@ pub fn is_correct(word: &str) -> bool { is_correct_with(&dictionary(), word) } +/// [`is_correct`] with the File › Options › Proofing choices: with Ignore words in UPPERCASE off, +/// short words in capitals are checked against the dictionary too, and with Ignore words that +/// contain numbers off, so are words with digits or superscripts. +pub fn is_correct_with_options(word: &str, opts: &crate::Options) -> bool { + check(&dictionary(), word, opts) +} + /// [`is_correct`] against a given dictionary (an empty one accepts everything). pub fn is_correct_with(d: &AffixDict, word: &str) -> bool { + check(d, word, &crate::Options::default()) +} + +/// [`is_correct_with_options`] against a given dictionary. +fn check(d: &AffixDict, word: &str, opts: &crate::Options) -> bool { if d.is_empty() { return true; // no Russian dictionary installed: no spelling marks } let w = word.trim_matches(|c: char| !c.is_alphanumeric() && c != '-'); let w = w.trim_matches('-'); let n = w.chars().count(); - if n <= 1 || w.chars().any(char::is_numeric) || !w.chars().any(char::is_alphabetic) { + if n <= 1 || (opts.ignore_numbers && w.chars().any(char::is_numeric)) || !w.chars().any(char::is_alphabetic) { return true; } - if w.chars().all(|c| !c.is_lowercase()) && n <= ABBREVIATION_LETTERS { + if opts.ignore_uppercase && w.chars().all(|c| !c.is_lowercase()) && n <= ABBREVIATION_LETTERS { return true; } // Camel case (`ВКонтакте`, `МирКрасок`): a brand or an identifier, not a dictionary word. @@ -161,7 +173,7 @@ pub fn is_correct_with(d: &AffixDict, word: &str) -> bool { return true; } if w.contains('-') { - return hyphenated(d, w); + return hyphenated(d, w, opts); } solid_compound(d, w) } @@ -340,7 +352,7 @@ fn known(d: &AffixDict, w: &str) -> bool { /// A hyphenated word that isn't in the dictionary whole: its parts, with the particles, /// prefixes and linking vowels Russian joins with a hyphen. -fn hyphenated(d: &AffixDict, w: &str) -> bool { +fn hyphenated(d: &AffixDict, w: &str, opts: &crate::Options) -> bool { let parts: Vec<&str> = w.split('-').collect(); if parts.iter().any(|p| p.is_empty()) || parts.len() > 4 { return false; @@ -353,7 +365,7 @@ fn hyphenated(d: &AffixDict, w: &str) -> bool { && lp.iter().skip(k).all(|p| PARTICLES.contains(p)) { let base = parts.get(..k).map(|b| b.join("-")).unwrap_or_default(); - return known(d, &base) || (base.contains('-') && hyphenated(d, &base)); + return known(d, &base) || (base.contains('-') && hyphenated(d, &base, opts)); } match lp.as_slice() { // `кое-кто`, `кой-какой`. @@ -369,7 +381,7 @@ fn hyphenated(d: &AffixDict, w: &str) -> bool { let last = parts.len() - 1; parts.iter().enumerate().all(|(i, p)| { // An abbreviation part (`ИИ-консультант`, `SMS-рассылка`) or a known word. - if known(d, p) || (p.chars().all(|c| !c.is_lowercase()) && p.chars().count() <= ABBREVIATION_LETTERS) { + if known(d, p) || (opts.ignore_uppercase && p.chars().all(|c| !c.is_lowercase()) && p.chars().count() <= ABBREVIATION_LETTERS) { return true; } // A first part with a linking vowel: the stem (`северо` → `север`) or its adjective @@ -497,4 +509,25 @@ mod tests { crate::add_word("Зкщфрусскоеслово"); assert!(is_correct("зкщфрусскоеслово")); } + + /// With Ignore words in UPPERCASE or Ignore words that contain numbers off, Russian checks + /// those words against the dictionary instead of accepting them. + #[test] + fn proofing_options_reach_the_russian_check() { + test_dict::install(); + let lang = crate::ProofLang::Ru; + let defaults = crate::Options::default(); + let check_caps = crate::Options { ignore_uppercase: false, ..defaults }; + let check_digits = crate::Options { ignore_numbers: false, ..defaults }; + assert!(crate::is_correct_in_with("ПРЕВЕТ", lang, &defaults), "short capitals are abbreviations by default"); + assert!(!crate::is_correct_in_with("ПРЕВЕТ", lang, &check_caps)); + assert!(crate::is_correct_in_with("ПРИВЕТ", lang, &check_caps)); + assert!(crate::is_correct_in_with("ИИ-консультант", lang, &defaults)); + assert!(!crate::is_correct_in_with("ИИ-консультант", lang, &check_caps), "a part in capitals is checked too"); + assert!(crate::is_correct_in_with("превет2", lang, &defaults)); + assert!(!crate::is_correct_in_with("превет2", lang, &check_digits)); + assert!(!crate::is_correct_in_with("м²", lang, &check_digits), "a superscript is a number too"); + assert!(crate::is_correct_in_with("2026", lang, &check_digits), "numbers alone stay correct"); + assert!(is_correct("ПРЕВЕТ"), "direct checks keep the defaults"); + } } diff --git a/crates/proof/src/tr/mod.rs b/crates/proof/src/tr/mod.rs index bcab1546b..ab3fe92d7 100644 --- a/crates/proof/src/tr/mod.rs +++ b/crates/proof/src/tr/mod.rs @@ -15,6 +15,8 @@ pub mod dict; pub mod grammar; pub mod hyphen; pub mod suggest; +#[cfg(test)] +pub(crate) mod test_dict; use std::sync::{Arc, RwLock}; @@ -87,15 +89,26 @@ pub fn is_correct(word: &str) -> bool { is_correct_with(&dictionary(), word) } +/// [`is_correct`] with the File › Options › Proofing choices: with Ignore words that contain +/// numbers off, words with digits are checked against the dictionary too. +pub fn is_correct_with_options(word: &str, opts: &crate::Options) -> bool { + check(&dictionary(), word, opts) +} + /// [`is_correct`] against a given dictionary. pub fn is_correct_with(d: &TrDict, word: &str) -> bool { + check(d, word, &crate::Options::default()) +} + +/// [`is_correct_with_options`] against a given dictionary. +fn check(d: &TrDict, word: &str, opts: &crate::Options) -> bool { if d.is_empty() { return true; // No Turkish dictionary installed: no spelling marks } let w = word.trim_matches(|c: char| !c.is_alphanumeric() && c != '\'' && c != '’'); let w = w.trim_matches(['\'', '’']); let n = w.chars().count(); - if n <= 1 || w.chars().any(|c| c.is_ascii_digit()) || !w.chars().any(char::is_alphabetic) { + if n <= 1 || (opts.ignore_numbers && w.chars().any(|c| c.is_ascii_digit())) || !w.chars().any(char::is_alphabetic) { return true; } // Check user dictionary first @@ -120,7 +133,7 @@ pub fn is_correct_with(d: &TrDict, word: &str) -> bool { } // Compound words separated by hyphens (e.g., `afyon-karahisar`) - if w.contains('-') && w.split('-').all(|part| part.is_empty() || is_correct_with(d, part)) { + if w.contains('-') && w.split('-').all(|part| part.is_empty() || check(d, part, opts)) { return true; } @@ -146,11 +159,8 @@ mod tests { } #[test] - fn installed_dictionary_checks_words_if_present() { - // Machines without a Turkish Hunspell dictionary (CI, most desktops) have nothing to check. - if !has_dictionary() { - return; - } + fn installed_dictionary_checks_words() { + test_dict::install(); assert!(is_correct("kitap")); assert!(is_correct("kitaplar")); assert!(is_correct("evde")); @@ -165,4 +175,19 @@ mod tests { crate::add_word("benimbilmedigimkelime"); assert!(is_correct("benimbilmedigimkelime")); } + + /// With Ignore words that contain numbers off, Turkish checks words with digits against the + /// dictionary instead of accepting them, hyphen parts included. + #[test] + fn proofing_options_reach_the_turkish_check() { + test_dict::install(); + let lang = crate::ProofLang::Tr; + let defaults = crate::Options::default(); + let check_digits = crate::Options { ignore_numbers: false, ..defaults }; + assert!(crate::is_correct_in_with("kitapp2", lang, &defaults)); + assert!(!crate::is_correct_in_with("kitapp2", lang, &check_digits)); + assert!(!crate::is_correct_in_with("ev-kitapp2", lang, &check_digits)); + assert!(crate::is_correct_in_with("2026", lang, &check_digits), "numbers alone stay correct"); + assert!(is_correct("kitapp2"), "direct checks keep the defaults"); + } } diff --git a/crates/proof/src/tr/suggest.rs b/crates/proof/src/tr/suggest.rs index dedc1bb26..f91f8e3c9 100644 --- a/crates/proof/src/tr/suggest.rs +++ b/crates/proof/src/tr/suggest.rs @@ -285,10 +285,7 @@ mod tests { #[test] fn typo_transposition_is_suggested() { - // Suggestions come from the installed Turkish dictionary; without one there are none. - if !super::super::has_dictionary() { - return; - } + crate::tr::test_dict::install(); let sugs = suggest("sorna", 3); assert!(sugs.contains(&"sonra".to_string()), "sorna should suggest sonra, got {sugs:?}"); diff --git a/crates/proof/src/tr/test_dict.rs b/crates/proof/src/tr/test_dict.rs new file mode 100644 index 000000000..de4a13195 --- /dev/null +++ b/crates/proof/src/tr/test_dict.rs @@ -0,0 +1,22 @@ +//! A tiny synthetic Turkish Hunspell dictionary for tests (written for these tests; not taken from +//! any real dictionary). Real dictionaries are installed on the system, never bundled. + +/// No affix rules: the forms the tests look up are listed whole. +pub const AFF: &str = "SET UTF-8\n"; + +/// The words the Turkish tests expect to be known. +pub const DIC: &str = "7 +kitap +kitaplar +ev +evde +olacaklardan +sonra +geldin +"; + +pub fn install() { + if let Err(e) = super::install_dictionary(AFF, DIC) { + panic!("test dictionary: {e}"); + } +}