From 42958efaa40d5d62d30b52a4490e34f1250ad879 Mon Sep 17 00:00:00 2001 From: ruchirguitar Date: Sat, 21 Nov 2026 21:54:24 +0530 Subject: [PATCH 1/4] Proofing: add Gujarati (gu_IN) spelling, suggestion dispatch and grammar checks --- crates/proof/src/gu/grammar.rs | 111 +++++++++++++++++++ crates/proof/src/gu/mod.rs | 182 +++++++++++++++++++++++++++++++ crates/proof/src/gu/suggest.rs | 61 +++++++++++ crates/proof/src/gu/test_dict.rs | 51 +++++++++ crates/proof/src/hyphen.rs | 4 +- crates/proof/src/lang.rs | 13 ++- crates/proof/src/lib.rs | 31 +++++- crates/proof/src/no_data.rs | 4 +- crates/proof/src/packs.rs | 1 + 9 files changed, 451 insertions(+), 7 deletions(-) create mode 100644 crates/proof/src/gu/grammar.rs create mode 100644 crates/proof/src/gu/mod.rs create mode 100644 crates/proof/src/gu/suggest.rs create mode 100644 crates/proof/src/gu/test_dict.rs diff --git a/crates/proof/src/gu/grammar.rs b/crates/proof/src/gu/grammar.rs new file mode 100644 index 0000000000..ac6e3b3bd6 --- /dev/null +++ b/crates/proof/src/gu/grammar.rs @@ -0,0 +1,111 @@ +//! Generic repeated-word and spacing checks with Gujarati messages. +//! No Gujarati-specific grammar or case rules are inferred. + +use crate::{Issue, IssueKind, ProofLang, words}; + +fn issue(start: usize, end: usize, message: String, suggestions: Vec) -> Issue { + Issue { start, end, kind: IssueKind::Grammar, message, suggestions, lang: ProofLang::Gu } +} + +pub fn check(text: &str) -> Vec { + let mut issues = Vec::new(); + repeated_words(text, &words(text), &mut issues); + spaces(text, &mut issues); + issues.sort_by_key(|i| (i.start, i.end)); + issues +} + +/// Words Gujarati repeats on purpose (`હા હા`, `ના ના`, `ધીમે ધીમે`, `સાચે સાચે`). +const ALLOWED_REPEATS: &[&str] = &["હા", "ના", "ધીમે", "જલ્દી", "વારંવાર", "સાચે", "રોજે", "ખરેખર", "જરા", "થોડું", "સાથે", "પાસે"]; + +fn repeated_words(text: &str, ws: &[(usize, usize)], v: &mut Vec) { + for pair in ws.windows(2) { + let [(a0, b0), (a1, b1)] = pair else { continue }; + let (Some(w0), Some(w1), Some(gap)) = (text.get(*a0..*b0), text.get(*a1..*b1), text.get(*b0..*a1)) else { continue }; + if !gap.is_empty() + && gap.chars().all(char::is_whitespace) + && w0.chars().all(|c| c.is_alphabetic() || ('\u{0A80}'..='\u{0AFF}').contains(&c)) + && w0 == w1 + && !ALLOWED_REPEATS.contains(&w0) + { + v.push(issue(*a0, *b1, format!("પુનરાવર્તિત શબ્દ: “{w1}”"), vec![w0.to_string()])); + } + } +} + +fn spaces(text: &str, v: &mut Vec) { + for (i, c) in text.char_indices() { + if c != ' ' { + continue; + } + let rest = text.get(i + 1..).unwrap_or(""); + let mut it = rest.chars(); + let Some(p) = it.next() else { continue }; + let after = it.next(); + // A ratio/score keeps the spaces in digit-space-colon-space-digit. + let ratio = p == ':' + && text.get(..i).and_then(|before| before.chars().next_back()).is_some_and(|c| c.is_ascii_digit()) + && after == Some(' ') + && it.next().is_some_and(|c| c.is_ascii_digit()); + let flag = !ratio + && match p { + ',' => true, + ';' => !matches!(after, Some(')' | '(' | '-' | 'P' | 'D')), + '.' => !matches!(after, Some('.')) && !after.is_some_and(|a| a.is_ascii_digit()), + ':' | '!' | '?' => after.is_none_or(|a| a.is_whitespace() || matches!(a, '"' | '”' | '’' | '!' | '?')), + _ => false, + }; + if flag { + v.push(issue(i, i + 1 + p.len_utf8(), "વિરામચિહ્ન પહેલાં જગ્યા".into(), vec![p.to_string()])); + } + } + for run in crate::space_runs(text) { + v.push(issue(run.start, run.end, "બેવડી જગ્યા".into(), vec![" ".into()])); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn intentional_repeats_are_fine() { + for text in ["હા હા, હું આવીશ.", "ના ના, આ નથી.", "ધીમે ધીમે ચાલ.", "સાચે સાચે બોલ."] + { + assert!(check(text).is_empty(), "{text}: {:?}", check(text)); + } + assert_eq!(check("પુસ્તક પુસ્તક છે.").len(), 1); + } + + #[test] + fn repeated_words_and_spaces_have_gujarati_messages() { + let text = "ઘર ઘર , શાળા."; + let issues = check(text); + assert_eq!(issues.len(), 3, "{issues:?}"); + assert_eq!(issues[0].message, "પુનરાવર્તિત શબ્દ: “ઘર”"); + assert_eq!(issues[0].suggestions, ["ઘર"]); + assert!(issues.iter().any(|i| i.message == "વિરામચિહ્ન પહેલાં જગ્યા" && &text[i.start..i.end] == " ,")); + assert!(issues.iter().any(|i| i.message == "બેવડી જગ્યા")); + assert!(issues.iter().all(|i| i.lang == ProofLang::Gu && i.kind == IssueKind::Grammar)); + } + + #[test] + fn ratios_and_scores_keep_their_spaces() { + for text in ["3 : 1", "1 : 2", "સ્કોર 12 : 10."] { + assert!(check(text).is_empty(), "{text}: {:?}", check(text)); + } + for text in ["શબ્દ : બીજો", "3 : શબ્દ", "3 :"] { + assert!(check(text).iter().any(|i| i.message == "વિરામચિહ્ન પહેલાં જગ્યા"), "{text}"); + } + } + + proptest::proptest! { + #[test] + fn never_panics_and_ranges_are_valid(s in "\\PC{0,80}") { + for issue in check(&s) { + proptest::prop_assert!(issue.start <= issue.end && issue.end <= s.len()); + proptest::prop_assert!(s.is_char_boundary(issue.start) && s.is_char_boundary(issue.end)); + } + } + } +} diff --git a/crates/proof/src/gu/mod.rs b/crates/proof/src/gu/mod.rs new file mode 100644 index 0000000000..f17a47ec87 --- /dev/null +++ b/crates/proof/src/gu/mod.rs @@ -0,0 +1,182 @@ +//! Gujarati proofing using the installed Hunspell `gu_IN` dictionary (never bundled). +//! Suggestions use its runtime MAP/REP/TRY/KEY tables. Generic grammar checks have Gujarati +//! messages; hyphenation keeps the existing fallback. + +pub mod grammar; +pub(crate) mod suggest; +#[cfg(test)] +pub(crate) mod test_dict; + +use std::sync::Arc; + +use crate::affix::AffixDict; +use crate::slot::DictSlot; + +pub use suggest::suggest; + +/// File names of a Gujarati Hunspell dictionary. +const NAMES: &[&str] = &["gu_IN", "gu-IN", "gu"]; + +/// The Gujarati dictionary, found on first use (see [`DictSlot`]). +pub(crate) static DICT: DictSlot = DictSlot::new(NAMES, "Gujarati"); + +/// The Gujarati dictionary: the installed Hunspell `gu_IN` (read on first use), one given with +/// [`install_dictionary`], or an empty one when none is available (Gujarati spelling is off +/// then; see [`has_dictionary`]). +pub fn dictionary() -> Arc { + DICT.get() +} + +/// Use this Gujarati dictionary from now on (Hunspell `.aff` and `.dic` text, decoded to +/// UTF-8), for example one the user picked. On error the current dictionary stays. +pub fn install_dictionary(aff: &str, dic: &str) -> Result<(), String> { + DICT.install(aff, dic) +} + +/// Is a Gujarati dictionary available (spelling is checked)? +pub fn has_dictionary() -> bool { + DICT.has_dictionary() +} + +/// Returns `true` when `c` is in the Gujarati Unicode block (U+0A80..=U+0AFF). +pub fn is_gujarati_char(c: char) -> bool { + ('\u{0A80}'..='\u{0AFF}').contains(&c) +} + +/// Is `word` spelled correctly in Gujarati? Numbers, single letters, short words with digits and words in other scripts are accepted, and so +/// is every word when no Gujarati dictionary is installed. +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. +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; + } + let w = word.trim_matches(|c: char| !c.is_alphanumeric() && !is_gujarati_char(c) && !matches!(c, '\'' | '’' | '-')); + let w = w.trim_matches(['\'', '’', '-']); + let letter_count = w.chars().filter(|&c| is_gujarati_char(c) || c.is_alphabetic()).count(); + if letter_count <= 1 || (opts.ignore_numbers && w.chars().any(char::is_numeric)) { + return true; + } + // A word in another script (or with no Gujarati characters) is not marked by this dictionary. + if !w.chars().any(is_gujarati_char) { + return true; + } + if w.contains('-') { + // The whole word first, as Hunspell does: a hyphenated entry or user word. + return known(d, w) || w.split('-').all(|p| p.chars().count() <= 1 || !p.chars().any(is_gujarati_char) || known(d, p)); + } + known(d, w) +} + +/// Is one word (no hyphens) in the dictionary or the user dictionary? +fn known(d: &AffixDict, w: &str) -> bool { + d.check(w) || crate::in_user_dictionary(&w.to_lowercase()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn dictionary_lookup_accepts_gujarati_file_names() { + let dir = std::env::temp_dir().join(format!("wordcraft-gu-aliases-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + for name in ["gu_IN", "gu-IN", "gu"] { + std::fs::write(dir.join(format!("{name}.aff")), test_dict::AFF).unwrap(); + std::fs::write(dir.join(format!("{name}.dic")), test_dict::DIC).unwrap(); + let (aff, dic) = crate::hunspell::find_in(&dir, NAMES).unwrap(); + let d = crate::hunspell::load(&aff, &dic).unwrap(); + assert!(is_correct_with(&d, "ઘરો")); + std::fs::remove_file(aff).unwrap(); + std::fs::remove_file(dic).unwrap(); + } + std::fs::remove_dir(dir).unwrap(); + } + + #[test] + fn gujarati_words_affixes_and_case_markers() { + test_dict::install(); + let d = dictionary(); + assert!(has_dictionary()); + assert_eq!(d.len(), 15); + for w in [ + "ઘર", + "ઘરો", + "ઘરોને", + "ઘરોમાં", + "પુસ્તક", + "પુસ્તકો", + "પુસ્તકોમાં", + "પાણી", + "શાળા", + "ભાષા", + "પરીક્ષણ", + "પ્રેમ", + "શહેર", + "ગામ", + "મિત્ર", + "અઘર", + "શાળા-શહેર", + ] { + assert!(is_correct(w), "{w}"); + } + for w in ["ઘરર", "પુસ્તકક", "પાણીઈ", "શાળળા", "zqપ્રતિબંધિત"] { + assert!(!is_correct(w), "{w}"); + } + } + + #[test] + fn forbidden_words_are_rejected() { + test_dict::install(); + assert!(!is_correct("zqપ્રતિબંધિત")); + } + + #[test] + fn missing_dictionary_and_ignored_inputs_are_quiet() { + let empty = AffixDict::default(); + assert!(is_correct_with(&empty, "શાળા")); + assert!(install_dictionary("FLAG bogus\n", "1\nઘર\n").is_err()); + let d = AffixDict::from_hunspell(test_dict::AFF, test_dict::DIC, &[]).unwrap(); + for w in ["", "2026", "અ", "English", "日本語"] { + assert!(is_correct_with(&d, w), "{w}"); + } + } + + #[test] + fn user_dictionary_applies() { + test_dict::install(); + assert!(!is_correct("નવોશબ્દગુજરાતી")); + crate::add_word("નવોશબ્દગુજરાતી"); + assert!(is_correct("નવોશબ્દગુજરાતી")); + } + + #[test] + fn gujarati_dispatch_messages_options_and_mixed_spans() { + crate::hold_dictionary_tests(); + test_dict::install(); + assert!(crate::has_dictionary(Some("gu-IN"))); + assert!(crate::is_correct_for("ઘરો", Some("guj"))); + let text = "ઘર પુસ્તકક"; + let issues = crate::check_text(text, &[(0..text.len(), crate::ProofLang::Gu)]); + assert_eq!(issues.len(), 1, "{issues:?}"); + assert_eq!(&text[issues[0].start..issues[0].end], "પુસ્તકક"); + assert_eq!(issues[0].message, "શક્ય જોડણી ભૂલ"); + assert_eq!(issues[0].lang, crate::ProofLang::Gu); + let text = "ઘર this is is wrong"; + let issues = crate::check_text(text, &[(0..3, crate::ProofLang::Gu), (4..text.len(), crate::ProofLang::En)]); + assert!(issues.iter().any(|i| i.lang == crate::ProofLang::En && i.message.starts_with("Repeated word"))); + } +} diff --git a/crates/proof/src/gu/suggest.rs b/crates/proof/src/gu/suggest.rs new file mode 100644 index 0000000000..9aaaaa6526 --- /dev/null +++ b/crates/proof/src/gu/suggest.rs @@ -0,0 +1,61 @@ +//! Gujarati suggestions: the shared ranking ([`crate::affix_suggest`]) with the installed +//! dictionary's own MAP/REP/TRY/KEY tables and nothing Gujarati hard-coded. + +use crate::affix::AffixDict; +use crate::affix_suggest::Tuning; + +/// Spelling suggestions for a Gujarati word, best first, at most `max`. +pub fn suggest(word: &str, max: usize) -> Vec { + suggest_in(&super::dictionary(), word, max) +} + +/// [`suggest`] against a given dictionary. +pub fn suggest_in(d: &AffixDict, word: &str, max: usize) -> Vec { + crate::affix_suggest::suggest_in(d, word, max, &Tuning::DEFAULT) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn suggestions_are_fast_enough() { + crate::gu::test_dict::install(); + let _ = suggest("ઘર", 1); + let t = std::time::Instant::now(); + for word in ["zqખૂબલાંબોશબ્દગુજરાતી", "પુસતક", "પરીક્ષણણ"] { + let _ = suggest(word, 6); + } + let ms = t.elapsed().as_secs_f64() * 1000.0; + eprintln!("3 Gujarati suggestion calls: {ms:.0} ms"); + assert!(ms < 20_000.0, "{ms} ms"); + } + + #[test] + fn nosuggest_stems_and_forms_are_never_offered() { + crate::affix::tests::assert_nosuggest_is_honoured(suggest_in); + } + + #[test] + fn gujarati_suggestions_use_runtime_tables() { + crate::gu::test_dict::install(); + for (word, want) in [("શાલા", "શાળા"), ("પાની", "પાણી"), ("પ્રેમમ", "પ્રેમ"), ("પરીકશણ", "પરીક્ષણ")] + { + let s = suggest(word, 8); + assert!(s.contains(&want.into()), "{word}: {s:?}"); + } + } + + #[test] + fn suggestions_stay_bounded_and_skip_forbidden_words() { + crate::gu::test_dict::install(); + for w in ["", "બે શબ્દો", &"શ".repeat(41)] { + assert!(suggest(w, 4).is_empty()); + } + assert!(suggest("ઘર", 0).is_empty()); + assert!(suggest_in(&AffixDict::default(), "ઘર", 4).is_empty()); + assert!(suggest("ઘર", 4).len() <= 4); + assert!(!suggest("ઘર", 4).contains(&"ઘર".into())); + assert!(!suggest("zqપ્રતિબંધિત", 8).contains(&"zqપ્રતિબંધિત".into())); + } +} diff --git a/crates/proof/src/gu/test_dict.rs b/crates/proof/src/gu/test_dict.rs new file mode 100644 index 0000000000..5c5bec46e7 --- /dev/null +++ b/crates/proof/src/gu/test_dict.rs @@ -0,0 +1,51 @@ +//! Hand-written synthetic Gujarati test data. No words, rules or suggestion tables were copied +//! from a system dictionary. This is a small fixture, not a redistributable Gujarati dictionary. + +pub const AFF: &str = "SET UTF-8 +FLAG UTF-8 +FORBIDDENWORD F +TRY અઆઇઈઉઊઋએઐઓઔકખગઘઙચછજઝઞટઠડઢણતથદધનપફબભમયરલવશષસહળક્ષજ્ઞાિીુૂૃૄેૈોૌ્ંઃઁ +KEY ષસ|તર +MAP 4 +MAP ઇઈિી +MAP ઉઊુૂ +MAP સશષ +MAP ણન +REP 1 +REP કશ ક્ષ +SFX N Y 4 +SFX N 0 ો/C . +SFX N 0 ો . +SFX N 0 ઓ/C . +SFX N 0 ઓ . +SFX C Y 4 +SFX C 0 ને . +SFX C 0 માં . +SFX C 0 થી . +SFX C 0 નો . +PFX X Y 1 +PFX X 0 અ . +"; + +pub const DIC: &str = "15 +ઘર/NX +પુસ્તક/NX +પાણી +શાળા +ભાષા +પરીક્ષણ +પ્રેમ +શહેર +ગામ/NX +મિત્ર/NX +ઝાડ +ફળ +કમળ +સૂર્ય +zqપ્રતિબંધિત/F +"; + +pub fn install() { + crate::hold_dictionary_tests(); + super::install_dictionary(AFF, DIC).unwrap(); +} diff --git a/crates/proof/src/hyphen.rs b/crates/proof/src/hyphen.rs index cc9d4fbdfb..c046267123 100644 --- a/crates/proof/src/hyphen.rs +++ b/crates/proof/src/hyphen.rs @@ -1008,7 +1008,9 @@ mod tests { #[test] fn languages_without_patterns_keep_the_english_ones() { for w in ["hyphenation", "typography", "internationalization"] { - for lang in [ProofLang::Other, ProofLang::Cs, ProofLang::Id, ProofLang::Es, ProofLang::EsMx, ProofLang::Es419, ProofLang::EsUy] { + for lang in + [ProofLang::Other, ProofLang::Cs, ProofLang::Id, ProofLang::Gu, ProofLang::Es, ProofLang::EsMx, ProofLang::Es419, ProofLang::EsUy] + { assert_eq!(hyphen_points_in(w, &Limits::default(), lang), hyphen_points(w, &Limits::default()), "{w}"); } } diff --git a/crates/proof/src/lang.rs b/crates/proof/src/lang.rs index e5fcf3bbb9..5e087da170 100644 --- a/crates/proof/src/lang.rs +++ b/crates/proof/src/lang.rs @@ -70,6 +70,10 @@ pub enum ProofLang { /// dictionary (else the neighbouring Rioplatense `es_AR`, a Latin American or a generic one), /// and the `vos` forms of verbs (`tenés`, `vení`, `decime`) accepted ([`crate::es::voseo`]). EsUy, + /// Gujarati (`gu`, `gu-IN`, `guj`): the installed Hunspell dictionary (`gu_IN`, when there is + /// one), dictionary-driven suggestions and generic checks with Gujarati messages. Hyphenation + /// keeps the fallback patterns. + Gu, /// German (`de`, `de-DE`, `de-AT`…; `de-CH` and `de-LI` are [`ProofLang::Pack`] `de-ch`, with /// their own dictionary and these patterns): hyphenation by the German patterns (reformed /// spelling), and spelling when a German Hunspell dictionary or `de` pack is found. No grammar @@ -90,7 +94,7 @@ pub enum ProofLang { impl ProofLang { /// Every language with a spelling dictionary, in display order. - pub const PROOFED: [ProofLang; 16] = [ + pub const PROOFED: [ProofLang; 17] = [ ProofLang::En, ProofLang::EnGb, ProofLang::EnAu, @@ -107,6 +111,7 @@ impl ProofLang { ProofLang::EsMx, ProofLang::Es419, ProofLang::EsUy, + ProofLang::Gu, ]; /// The proofing language of a BCP 47 tag (`pl-PL`, `en_GB`, `PL`…). The primary subtag @@ -142,6 +147,8 @@ impl ProofLang { ProofLang::Cs } else if primary.eq_ignore_ascii_case("id") || primary.eq_ignore_ascii_case("ind") || primary.eq_ignore_ascii_case("in") { ProofLang::Id + } else if primary.eq_ignore_ascii_case("gu") || primary.eq_ignore_ascii_case("guj") { + ProofLang::Gu } else if primary.eq_ignore_ascii_case("es") || primary.eq_ignore_ascii_case("spa") { ProofLang::spanish(tag) } else if ["de", "deu", "ger"].iter().any(|c| primary.eq_ignore_ascii_case(c)) { @@ -225,6 +232,7 @@ impl ProofLang { ProofLang::Ru => "ru", ProofLang::Cs => "cs", ProofLang::Id => "id", + ProofLang::Gu => "gu", ProofLang::Es | ProofLang::EsMx | ProofLang::Es419 | ProofLang::EsUy => "es", ProofLang::De => "de", ProofLang::Other => "", @@ -479,10 +487,13 @@ mod tests { assert_tags(&["it", "it-IT", "IT_it", "it-CH", "it-CH.UTF-8", " it-IT ", "ita"], ProofLang::It); assert_tags(&["cs", "cs-CZ", "CS-cz", "cs_CZ", " ces ", "CES", "cze", "CZE"], ProofLang::Cs); assert_tags(&["id", "id-ID", "ID-id", "id_ID", "id_ID.UTF-8", " id-ID ", "ind", "in", "in_ID"], ProofLang::Id); + assert_tags(&["gu", "gu-IN", "GU-in", "gu_IN", " gu-IN ", "guj", "GUJ"], ProofLang::Gu); assert_eq!(ProofLang::Cs.code(), "cs"); assert!(ProofLang::Cs.is_proofed() && ProofLang::PROOFED.contains(&ProofLang::Cs)); assert_eq!(ProofLang::Id.code(), "id"); assert!(ProofLang::Id.is_proofed() && ProofLang::PROOFED.contains(&ProofLang::Id)); + assert_eq!(ProofLang::Gu.code(), "gu"); + assert!(ProofLang::Gu.is_proofed() && ProofLang::PROOFED.contains(&ProofLang::Gu)); } #[test] diff --git a/crates/proof/src/lib.rs b/crates/proof/src/lib.rs index f0f33d0071..b7342feaa5 100644 --- a/crates/proof/src/lib.rs +++ b/crates/proof/src/lib.rs @@ -35,6 +35,9 @@ //! - **Indonesian** ([`ProofLang::Id`], module [`id`]): the installed Hunspell `id_ID` //! dictionary (not bundled), dictionary-driven suggestions and generic checks with Indonesian //! messages. Hyphenation uses the existing English fallback. +//! - **Gujarati** ([`ProofLang::Gu`], module [`gu`]): the installed Hunspell `gu_IN` +//! dictionary (not bundled), dictionary-driven suggestions and generic checks with Gujarati +//! messages. Hyphenation uses the existing English fallback. //! - **Spanish** ([`ProofLang::Es`], [`ProofLang::EsMx`], [`ProofLang::Es419`], [`ProofLang::EsUy`], //! module [`es`]): each variety's installed Hunspell dictionary (`es_ES`, `es_MX`, `es_419`, //! `es_UY`, with fallbacks; not bundled), dictionary-driven suggestions, Uruguayan `vos` forms, @@ -77,6 +80,7 @@ pub mod en_variants; mod english; pub mod es; pub mod fr; +pub mod gu; pub mod hr; pub mod hunspell; pub mod hyphen; @@ -223,7 +227,7 @@ struct Language { /// Every language proofed against a Hunspell dictionary, in the order File › Options › Proofing /// lists them. English has a built-in word list and is handled apart. -static LANGUAGES: [Language; 12] = [ +static LANGUAGES: [Language; 13] = [ Language { lang: ProofLang::Pl, slot: &pl::DICT, @@ -290,6 +294,14 @@ static LANGUAGES: [Language; 12] = [ suggest: |w, n, _| id::suggest(w, n), grammar: |t, _, _| id::grammar::check(t), }, + Language { + lang: ProofLang::Gu, + slot: &gu::DICT, + message: "શક્ય જોડણી ભૂલ", + is_correct: |w, _, o| gu::is_correct_with_options(w, o), + suggest: |w, n, _| gu::suggest(w, n), + grammar: |t, _, _| gu::grammar::check(t), + }, Language { lang: ProofLang::Es, slot: &es::SPAIN_DICT, @@ -1210,6 +1222,7 @@ mod tests { (ProofLang::Ru, "ru_RU"), (ProofLang::Cs, "cs_CZ"), (ProofLang::Id, "id_ID"), + (ProofLang::Gu, "gu_IN"), // #3360: each Spanish variety names its own. (ProofLang::Es, "es_ES"), (ProofLang::EsMx, "es_MX"), @@ -1267,6 +1280,7 @@ mod tests { ("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), + ("gu_IN", gu::test_dict::AFF, gu::test_dict::DIC), ("es_ES", es::test_dict::AFF, es::test_dict::DIC), ("es_MX", es::test_dict::AFF, es::test_dict::DIC_AMERICAS), ("es_419", es::test_dict::AFF, es::test_dict::DIC_AMERICAS), @@ -1507,7 +1521,7 @@ mod tests { // the languages' own tests install theirs concurrently, so an exact path would race them. // The folder is searched first whatever is in use. let found = installed_dictionaries(); - let names = ["pl_PL", "pt_BR", "it_IT", "hr_HR", "tr_TR", "ru_RU", "cs_CZ", "id_ID", "es_ES", "es_MX", "es_419", "es_UY"]; + let names = ["pl_PL", "pt_BR", "it_IT", "hr_HR", "tr_TR", "ru_RU", "cs_CZ", "id_ID", "gu_IN", "es_ES", "es_MX", "es_419", "es_UY"]; assert_eq!( found.iter().map(|(lang, _)| *lang).collect::>(), [ @@ -1519,6 +1533,7 @@ mod tests { ProofLang::Ru, ProofLang::Cs, ProofLang::Id, + ProofLang::Gu, ProofLang::Es, ProofLang::EsMx, ProofLang::Es419, @@ -2631,6 +2646,7 @@ mod tests { ProofLang::Cs, ProofLang::Hr, ProofLang::Id, + ProofLang::Gu, ProofLang::Pl, ProofLang::Ru, ProofLang::Tr, @@ -3231,7 +3247,7 @@ mod tests { let _ = has_dictionary(Some(&s)); let _ = is_correct_for(&s, Some(&s)); let _ = hyphen::hyphen_points(&s, &hyphen::Limits::default()); - for lang in [ProofLang::En, ProofLang::EnGb, ProofLang::EnCa, ProofLang::Pl, ProofLang::Pt, ProofLang::It, ProofLang::Hr, ProofLang::Tr, ProofLang::Ru, ProofLang::Cs, ProofLang::Id, ProofLang::De, ProofLang::Es, ProofLang::EsMx, ProofLang::Es419, ProofLang::EsUy, ProofLang::Other] { + for lang in [ProofLang::En, ProofLang::EnGb, ProofLang::EnCa, ProofLang::Pl, ProofLang::Pt, ProofLang::It, ProofLang::Hr, ProofLang::Tr, ProofLang::Ru, ProofLang::Cs, ProofLang::Id, ProofLang::Gu, ProofLang::De, ProofLang::Es, ProofLang::EsMx, ProofLang::Es419, ProofLang::EsUy, ProofLang::Other] { let _ = check_spelling_in(&s, lang); let _ = check_grammar_in(&s, lang); let _ = is_correct_in(&s, lang); @@ -3274,6 +3290,13 @@ mod tests { let _ = is_correct_in(&s, ProofLang::Id); } + #[test] + fn gujarati_suggestions_never_panic(s in "[\\u{0A80}-\\u{0AFF}'’ -]{0,14}") { + gu::test_dict::install(); + let _ = suggest_in(&s, 4, ProofLang::Gu); + let _ = is_correct_in(&s, ProofLang::Gu); + } + #[test] fn spanish_suggestions_never_panic(s in "[a-zA-ZáéíóúüñÁÉÍÓÚÜÑ¿¡'’ -]{0,14}") { es::test_dict::install(); @@ -3285,7 +3308,7 @@ mod tests { #[test] fn mixed_spans_never_panic(s in "\\PC{0,60}", cuts in proptest::collection::vec((0usize..80, 0usize..80, 0u8..15), 0..6)) { - let langs = [ProofLang::En, ProofLang::EnGb, ProofLang::EnCa, ProofLang::Pl, ProofLang::Pt, ProofLang::It, ProofLang::Tr, ProofLang::Ru, ProofLang::Cs, ProofLang::Id, ProofLang::Es, ProofLang::EsMx, ProofLang::Es419, ProofLang::EsUy, ProofLang::Other]; + let langs = [ProofLang::En, ProofLang::EnGb, ProofLang::EnCa, ProofLang::Pl, ProofLang::Pt, ProofLang::It, ProofLang::Tr, ProofLang::Ru, ProofLang::Cs, ProofLang::Id, ProofLang::Gu, ProofLang::Es, ProofLang::EsMx, ProofLang::Es419, ProofLang::EsUy, ProofLang::Other]; let spans: Vec<(Range, ProofLang)> = cuts.iter().map(|&(a, b, l)| (a.min(b)..a.max(b), langs[l as usize % langs.len()])).collect(); for i in check_text(&s, &spans) { proptest::prop_assert!(i.start <= i.end && i.end <= s.len()); diff --git a/crates/proof/src/no_data.rs b/crates/proof/src/no_data.rs index cdd9025d41..c8912b3d6e 100644 --- a/crates/proof/src/no_data.rs +++ b/crates/proof/src/no_data.rs @@ -7,7 +7,7 @@ use crate::hyphen::{self, Limits, Source}; use crate::{EnglishSource, Options, ProofLang, dict, en_variants, patterns}; -const LANGS: [ProofLang; 19] = [ +const LANGS: [ProofLang; 20] = [ ProofLang::En, ProofLang::EnGb, ProofLang::EnAu, @@ -21,6 +21,7 @@ const LANGS: [ProofLang; 19] = [ ProofLang::Ru, ProofLang::Cs, ProofLang::Id, + ProofLang::Gu, ProofLang::Es, ProofLang::EsMx, ProofLang::Es419, @@ -117,6 +118,7 @@ fn hyphenation_is_off_where_it_needs_data() { for (w, l) in [ ("hyphenation", ProofLang::En), ("hyphenation", ProofLang::Cs), + ("hyphenation", ProofLang::Gu), ("przedsiębiorstwo", ProofLang::Pl), ("computador", ProofLang::Pt), ("calcolatore", ProofLang::It), diff --git a/crates/proof/src/packs.rs b/crates/proof/src/packs.rs index eebd406ca6..e0bd38bfc8 100644 --- a/crates/proof/src/packs.rs +++ b/crates/proof/src/packs.rs @@ -545,6 +545,7 @@ fn hyphenation_locales(lang: ProofLang) -> Vec { ProofLang::Ru => &["ru"], ProofLang::Cs => &["cs"], ProofLang::Id => &["id"], + ProofLang::Gu => &["gu-in", "gu"], ProofLang::Es => &["es-es", "es"], ProofLang::EsMx => &["es-mx", "es-419", "es"], ProofLang::Es419 => &["es-419", "es"], From c29d4b7485206b5f0b4efc456a6e5efaf278e264 Mon Sep 17 00:00:00 2001 From: ruchirguitar Date: Sat, 21 Nov 2026 22:09:34 +0530 Subject: [PATCH 2/4] Proofing: handle Gujarati digits in grammar/spelling and check pack patterns fit --- crates/proof/src/gu/grammar.rs | 18 ++++++++++++------ crates/proof/src/gu/mod.rs | 7 +++++-- crates/proof/src/hyphen.rs | 16 +++++++++++++--- 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/crates/proof/src/gu/grammar.rs b/crates/proof/src/gu/grammar.rs index ac6e3b3bd6..e071161a0f 100644 --- a/crates/proof/src/gu/grammar.rs +++ b/crates/proof/src/gu/grammar.rs @@ -18,13 +18,17 @@ pub fn check(text: &str) -> Vec { /// Words Gujarati repeats on purpose (`હા હા`, `ના ના`, `ધીમે ધીમે`, `સાચે સાચે`). const ALLOWED_REPEATS: &[&str] = &["હા", "ના", "ધીમે", "જલ્દી", "વારંવાર", "સાચે", "રોજે", "ખરેખર", "જરા", "થોડું", "સાથે", "પાસે"]; +fn is_digit(c: char) -> bool { + c.is_ascii_digit() || ('\u{0AE6}'..='\u{0AEF}').contains(&c) +} + fn repeated_words(text: &str, ws: &[(usize, usize)], v: &mut Vec) { for pair in ws.windows(2) { let [(a0, b0), (a1, b1)] = pair else { continue }; let (Some(w0), Some(w1), Some(gap)) = (text.get(*a0..*b0), text.get(*a1..*b1), text.get(*b0..*a1)) else { continue }; if !gap.is_empty() && gap.chars().all(char::is_whitespace) - && w0.chars().all(|c| c.is_alphabetic() || ('\u{0A80}'..='\u{0AFF}').contains(&c)) + && w0.chars().all(|c| c.is_alphabetic() || (super::is_gujarati_char(c) && !c.is_numeric())) && w0 == w1 && !ALLOWED_REPEATS.contains(&w0) { @@ -42,11 +46,11 @@ fn spaces(text: &str, v: &mut Vec) { let mut it = rest.chars(); let Some(p) = it.next() else { continue }; let after = it.next(); - // A ratio/score keeps the spaces in digit-space-colon-space-digit. + // A ratio/score keeps the spaces in digit-space-colon-space-digit (ASCII or Gujarati digits). let ratio = p == ':' - && text.get(..i).and_then(|before| before.chars().next_back()).is_some_and(|c| c.is_ascii_digit()) + && text.get(..i).and_then(|before| before.chars().next_back()).is_some_and(is_digit) && after == Some(' ') - && it.next().is_some_and(|c| c.is_ascii_digit()); + && it.next().is_some_and(is_digit); let flag = !ratio && match p { ',' => true, @@ -75,6 +79,8 @@ mod tests { assert!(check(text).is_empty(), "{text}: {:?}", check(text)); } assert_eq!(check("પુસ્તક પુસ્તક છે.").len(), 1); + assert!(check("1 1").is_empty()); + assert!(check("૧ ૧").is_empty()); } #[test] @@ -91,10 +97,10 @@ mod tests { #[test] fn ratios_and_scores_keep_their_spaces() { - for text in ["3 : 1", "1 : 2", "સ્કોર 12 : 10."] { + for text in ["3 : 1", "1 : 2", "સ્કોર 12 : 10.", "૩ : ૧", "૧ : ૨", "સ્કોર ૧૨ : ૧૦."] { assert!(check(text).is_empty(), "{text}: {:?}", check(text)); } - for text in ["શબ્દ : બીજો", "3 : શબ્દ", "3 :"] { + for text in ["શબ્દ : બીજો", "3 : શબ્દ", "3 :", "૩ : શબ્દ", "૩ :"] { assert!(check(text).iter().any(|i| i.message == "વિરામચિહ્ન પહેલાં જગ્યા"), "{text}"); } } diff --git a/crates/proof/src/gu/mod.rs b/crates/proof/src/gu/mod.rs index f17a47ec87..e72089a4ca 100644 --- a/crates/proof/src/gu/mod.rs +++ b/crates/proof/src/gu/mod.rs @@ -66,7 +66,7 @@ fn check(d: &AffixDict, word: &str, opts: &crate::Options) -> bool { } let w = word.trim_matches(|c: char| !c.is_alphanumeric() && !is_gujarati_char(c) && !matches!(c, '\'' | '’' | '-')); let w = w.trim_matches(['\'', '’', '-']); - let letter_count = w.chars().filter(|&c| is_gujarati_char(c) || c.is_alphabetic()).count(); + let letter_count = w.chars().filter(|&c| (is_gujarati_char(c) && !c.is_numeric()) || c.is_alphabetic()).count(); if letter_count <= 1 || (opts.ignore_numbers && w.chars().any(char::is_numeric)) { return true; } @@ -150,9 +150,12 @@ mod tests { assert!(is_correct_with(&empty, "શાળા")); assert!(install_dictionary("FLAG bogus\n", "1\nઘર\n").is_err()); let d = AffixDict::from_hunspell(test_dict::AFF, test_dict::DIC, &[]).unwrap(); - for w in ["", "2026", "અ", "English", "日本語"] { + for w in ["", "2026", "૧૨", "૨૦૨૬", "અ", "English", "日本語"] { assert!(is_correct_with(&d, w), "{w}"); } + let no_ignore = crate::Options { ignore_numbers: false, ..Default::default() }; + assert!(check(&d, "૧૨", &no_ignore)); + assert!(check(&d, "2026", &no_ignore)); } #[test] diff --git a/crates/proof/src/hyphen.rs b/crates/proof/src/hyphen.rs index c046267123..961546502b 100644 --- a/crates/proof/src/hyphen.rs +++ b/crates/proof/src/hyphen.rs @@ -216,9 +216,9 @@ fn composed_chars(core: &[char]) -> (Vec, Vec>) { (nfc, starts) } -/// Does a pack's `pats` hyphenate `core` in `lang`? Russian, Bulgarian and Turkish: a word in -/// their script (the guard their rules use) whose letters the patterns know, so a word with a -/// letter the pack never mentions (`ё`) goes on to the built-in rules. Polish, Portuguese, Italian +/// Does a pack's `pats` hyphenate `core` in `lang`? Russian, Bulgarian, Turkish and Gujarati: a +/// word in their script (the guard their rules use) whose letters the patterns know, so a word with a +/// letter the pack never mentions goes on to the fallback rules. Polish, Portuguese, Italian /// and German: every word, as their embedded patterns do. Any other language: a word whose letters /// the patterns know. fn pack_patterns_fit(lang: ProofLang, pats: &Patterns, core: &[char]) -> bool { @@ -227,6 +227,7 @@ fn pack_patterns_fit(lang: ProofLang, pats: &Patterns, core: &[char]) -> bool { ProofLang::Bg => core.iter().all(|&c| crate::bg::is_letter(c)) && pack_knows(pats, core), ProofLang::Tr => core.iter().all(|&c| crate::tr::hyphen::is_letter(c)) && pack_knows(pats, core), ProofLang::Pl | ProofLang::Pt | ProofLang::It | ProofLang::De => true, + ProofLang::Gu => core.iter().all(|&c| crate::gu::is_gujarati_char(c)) && pack_knows(pats, core), _ => pats.values(core).is_some(), } } @@ -1053,4 +1054,13 @@ mod tests { assert_eq!(hyphen_points_in(w, &Limits::default(), ProofLang::Bg), hyphen_points(w, &Limits::default()), "{w}"); } } + + #[test] + fn gujarati_pack_patterns_fit_checks_script_and_exceptions() { + let pats = Patterns::parse("1ક 1ત પુ-સ્ત-ક"); + let gu_word: Vec = "પુસ્તક".chars().collect(); + assert!(pack_patterns_fit(ProofLang::Gu, &pats, &gu_word)); + let en_word: Vec = "hyphenation".chars().collect(); + assert!(!pack_patterns_fit(ProofLang::Gu, &pats, &en_word)); + } } From eb40a69e3d89da748b2484ca341f931900d28eed Mon Sep 17 00:00:00 2001 From: ruchirguitar Date: Mon, 23 Nov 2026 14:16:46 +0530 Subject: [PATCH 3/4] test(proof): replace wall-clock timing assertion with deterministic bounded suggestions check --- crates/proof/src/gu/suggest.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/crates/proof/src/gu/suggest.rs b/crates/proof/src/gu/suggest.rs index 9aaaaa6526..3e73964390 100644 --- a/crates/proof/src/gu/suggest.rs +++ b/crates/proof/src/gu/suggest.rs @@ -19,16 +19,17 @@ mod tests { use super::*; #[test] - fn suggestions_are_fast_enough() { + fn suggestion_results_stay_bounded_on_complex_inputs() { crate::gu::test_dict::install(); - let _ = suggest("ઘર", 1); - let t = std::time::Instant::now(); for word in ["zqખૂબલાંબોશબ્દગુજરાતી", "પુસતક", "પરીક્ષણણ"] { - let _ = suggest(word, 6); + let res = suggest(word, 6); + assert!(res.len() <= 6, "{word}: got {} suggestions", res.len()); + assert!(!res.contains(&"zqપ્રતિબંધિત".to_string())); + for s in &res { + assert!(!s.is_empty()); + assert!(super::super::is_correct(s), "suggested word {s} should be valid"); + } } - let ms = t.elapsed().as_secs_f64() * 1000.0; - eprintln!("3 Gujarati suggestion calls: {ms:.0} ms"); - assert!(ms < 20_000.0, "{ms} ms"); } #[test] From 7fa53a4c23b4cfb88305c88bc6de7f5c89b77d94 Mon Sep 17 00:00:00 2001 From: ruchirguitar Date: Fri, 27 Nov 2026 06:06:18 +0530 Subject: [PATCH 4/4] test(proof): restore Gujarati NOSUGGEST collector coverage The merge from main dropped nosuggest_stems_and_forms_are_never_offered because the shared assert_nosuggest_is_honoured helper was removed in 25597ecfc. Rebuild the fixture inline with AffixDict::from_hunspell, as main's tests now do, covering prefixed/suffixed forms, REP replacements and mixed-case input through the Gujarati suggest_in, plus a Gujarati-script NOSUGGEST stem. No production code changed. Co-Authored-By: Claude Opus 5.5 --- crates/proof/src/gu/suggest.rs | 43 ++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/crates/proof/src/gu/suggest.rs b/crates/proof/src/gu/suggest.rs index a51b34aa65..5686f46d6e 100644 --- a/crates/proof/src/gu/suggest.rs +++ b/crates/proof/src/gu/suggest.rs @@ -32,6 +32,49 @@ mod tests { } } + /// A NOSUGGEST stem stays correct spelling, but the Gujarati collector never offers it or any + /// prefixed, suffixed, REP-reached or recased form of it; ordinary stems are still offered. + #[test] + fn nosuggest_stems_and_forms_are_never_offered() { + let aff = "SET UTF-8\nNOSUGGEST N\nFORBIDDENWORD F\nTRY abeklmnostxy\nMAP 1\nMAP ae\nKEY ak\nREP 2\nREP xy toka\nREP zz normal\nPFX X Y 1\nPFX X 0 pre .\nSFX A Y 1\nSFX A 0 ble/B .\nSFX B Y 1\nSFX B 0 s .\n"; + let d = AffixDict::from_hunspell(aff, "4\ntoka/NXA\nnormal/XA\nTkname/N\nbanned/F\n", &[]).unwrap(); + for word in d.forms("toka").into_iter().chain(["TOKA".into(), "Toka".into(), "Tkname".into(), "TKNAME".into()]) { + assert!(d.check(&word), "NOSUGGEST is still correct: {word}"); + assert!(!d.can_suggest(&word), "NOSUGGEST form: {word}"); + } + for input in ["tokax", "tokablesx", "pretokax", "pretokablesx", "toka", "TOKA", "tkname", "teka", "xy", "normaltoka", "toak", "taak"] { + let got = suggest_in(&d, input, 32); + assert!(got.iter().all(|w| w.split(' ').all(|part| d.can_suggest(part))), "{input}: {got:?}"); + assert!(got.iter().all(|w| !w.to_lowercase().contains("toka") && w.to_lowercase() != "tkname"), "{input}: {got:?}"); + } + assert!(suggest_in(&d, "norrmal", 8).contains(&"normal".into())); + assert!(suggest_in(&d, "zz", 8).contains(&"normal".into())); + // Filtering happens before display casing: an ordinary mixed-case stem keeps its all-caps + // suggestion, with and without a NOSUGGEST declaration. + for aff in ["", "NOSUGGEST N\n"] { + let ordinary = AffixDict::from_hunspell(aff, "2\nmixCase\nhidden/N\n", &[]).unwrap(); + assert!(suggest_in(&ordinary, "MIXCASX", 8).contains(&"MIXCASE".into())); + } + + // The same contract in Gujarati script, with Gujarati affixes and REP pairs. + let aff = "SET UTF-8\nNOSUGGEST N\nTRY ગુપ્તસામાન્યઅોં\nREP 2\nREP ગુપત ગુપ્ત\nREP સામાનય સામાન્ય\nPFX X Y 1\nPFX X 0 અ .\nSFX A Y 1\nSFX A 0 ો/B .\nSFX B Y 1\nSFX B 0 માં .\n"; + let d = AffixDict::from_hunspell(aff, "2\nગુપ્ત/NXA\nસામાન્ય/XA\n", &[]).unwrap(); + let hidden = d.forms("ગુપ્ત"); + assert!(hidden.contains(&"અગુપ્તો".into()) && hidden.contains(&"ગુપ્તોમાં".into()), "{hidden:?}"); + for word in hidden.iter().chain(&["અગુપ્તોમાં".to_string()]) { + assert!(d.check(word), "NOSUGGEST is still correct: {word}"); + assert!(!d.can_suggest(word), "NOSUGGEST form: {word}"); + } + for input in ["ગુપત", "અગુપત", "ગુપતો", "અગુપતોમાં", "ગુપ્તત", "અગુપ્તોમા"] + { + let got = suggest_in(&d, input, 32); + assert!(got.iter().all(|w| w.split(' ').all(|part| d.can_suggest(part))), "{input}: {got:?}"); + assert!(got.iter().all(|w| !w.contains("ગુપ્ત")), "{input}: {got:?}"); + } + assert!(suggest_in(&d, "સામાનય", 8).contains(&"સામાન્ય".into())); + assert!(suggest_in(&d, "અસામાનયો", 8).contains(&"અસામાન્યો".into())); + } + #[test] fn gujarati_suggestions_use_runtime_tables() { crate::gu::test_dict::install();