From 8a5a402f4b0f9b45ebcccaef3ff1605e84376d86 Mon Sep 17 00:00:00 2001 From: ruchirguitar Date: Fri, 27 Nov 2026 22:48:04 +0530 Subject: [PATCH] proof: keep zero-width joiners inside words MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit words() ended a word at a zero-width joiner or non-joiner, so a Hindi or Gujarati word written with one in a conjunct (क्‍ष, પુસ્‍તક) was split and each half was spell-checked and repeat-checked alone. A joiner between two letters, digits or marks is now inside the word (UAX #29); one at a word's edge is not. The Gujarati/Hindi lookup tries the word as typed, then without its joiners, as a dictionary with Hunspell's IGNORE would. Co-Authored-By: Claude Opus 5.5 --- crates/proof/src/gu/mod.rs | 23 +++++++++++++++++++++++ crates/proof/src/hi/mod.rs | 16 ++++++++++++++++ crates/proof/src/lib.rs | 24 +++++++++++++++++++++--- crates/proof/src/script_checks.rs | 9 ++++++--- 4 files changed, 66 insertions(+), 6 deletions(-) diff --git a/crates/proof/src/gu/mod.rs b/crates/proof/src/gu/mod.rs index d511c4ecd3..27857ad08d 100644 --- a/crates/proof/src/gu/mod.rs +++ b/crates/proof/src/gu/mod.rs @@ -130,6 +130,29 @@ mod tests { } } + /// A zero-width joiner or non-joiner in a conjunct (`પુસ્` U+200D `તક`) keeps the word whole, + /// and the word is found under the dictionary's spelling, which has no joiner. + #[test] + fn words_with_joiners_are_one_word_and_spelled() { + test_dict::install(); + for word in ["પુસ્\u{200D}તક", "પુસ્\u{200C}તકોમાં"] { + let text = format!("ઘર {word} ગામ"); + let at = text.find(word).unwrap(); + assert!(crate::words(&text).contains(&(at, at + word.len())), "{word:?}"); + assert!(is_correct(word), "{word:?}"); + let issues = crate::check_text(&text, &[(0..text.len(), crate::ProofLang::Gu)]); + assert!(issues.is_empty(), "{word:?}: {issues:?}"); + } + // Still a misspelling with a joiner, and a joiner at a word's edge is not in the word. + assert!(!is_correct("પુસ્\u{200D}તકક")); + let text = "ઘર\u{200D} ગામ"; + assert!(crate::words(text).contains(&(0, "ઘર".len()))); + // A word with a joiner written twice is a repeated word. + let text = "પુસ્\u{200D}તક પુસ્\u{200D}તક"; + let issues = crate::check_grammar_in(text, crate::ProofLang::Gu); + assert!(issues.iter().any(|i| i.start == 0 && i.end == text.len()), "{issues:?}"); + } + #[test] fn forbidden_words_are_rejected() { test_dict::install(); diff --git a/crates/proof/src/hi/mod.rs b/crates/proof/src/hi/mod.rs index 047f78607a..3adb055c26 100644 --- a/crates/proof/src/hi/mod.rs +++ b/crates/proof/src/hi/mod.rs @@ -143,6 +143,22 @@ mod tests { test_dict::install(); } + /// A zero-width joiner or non-joiner after a virama (`क्` U+200D `ष`) keeps the word whole, and + /// the word is found under the dictionary's spelling, which has no joiner. + #[test] + fn words_with_joiners_are_one_word_and_spelled() { + test_dict::install(); + for word in ["परीक्\u{200D}षण", "परीक्\u{200C}षण", "मित्\u{200D}रों"] { + let text = format!("घर {word} पानी।"); + let at = text.find(word).unwrap(); + assert!(crate::words(&text).contains(&(at, at + word.len())), "{word:?}"); + assert!(is_correct(word), "{word:?}"); + let issues = crate::check_text(&text, &[(0..text.len(), crate::ProofLang::Hi)]); + assert!(issues.is_empty(), "{word:?}: {issues:?}"); + } + assert!(!is_correct("परीक्\u{200D}षणण")); + } + #[test] fn user_dictionary_applies() { test_dict::install(); diff --git a/crates/proof/src/lib.rs b/crates/proof/src/lib.rs index abc9c8e542..acec84798c 100644 --- a/crates/proof/src/lib.rs +++ b/crates/proof/src/lib.rs @@ -489,13 +489,25 @@ fn in_dialect(s: &str, word: &str, lang: ProofLang) -> String { } /// Words in `text`: (byte start, byte end). Combining marks belong to the word they follow, so a -/// word typed decomposed (`tambie\u{301}n`, NFD) is one word, as its precomposed form is. +/// word typed decomposed (`tambie\u{301}n`, NFD) is one word, as its precomposed form is. A +/// zero-width joiner or non-joiner between two letters, digits or marks is inside the word, as in +/// Unicode's word boundaries (UAX #29): Indic text uses one to choose how a conjunct is drawn +/// (`क्` U+200D `ष`, `પુસ્` U+200D `તક`). A joiner at a word's edge is not part of it. pub fn words(text: &str) -> Vec<(usize, usize)> { let mut out = Vec::new(); let mut start: Option = None; - let is_w = |c: char| c.is_alphanumeric() || c == '\'' || c == '’' || c == '-' || unicode_normalization::char::is_combining_mark(c); + let is_core = |c: char| c.is_alphanumeric() || unicode_normalization::char::is_combining_mark(c); + let is_w = |c: char| is_core(c) || c == '\'' || c == '’' || c == '-'; + // The char before was a letter, digit or mark, or a joiner kept after one. + let mut after_core = false; for (i, c) in text.char_indices() { - match (start, is_w(c)) { + let w = if is_joiner(c) { + after_core && text.get(i + c.len_utf8()..).and_then(|rest| rest.chars().find(|&n| !is_joiner(n))).is_some_and(is_core) + } else { + is_w(c) + }; + after_core = if is_joiner(c) { w } else { is_core(c) }; + match (start, w) { (None, true) => start = Some(i), (Some(s), false) => { out.push((s, i)); @@ -518,6 +530,12 @@ pub fn words(text: &str) -> Vec<(usize, usize)> { .collect() } +/// Is `c` a zero-width joiner or non-joiner (U+200D, U+200C)? It changes how letters are drawn, +/// not which letters a word has. +pub(crate) fn is_joiner(c: char) -> bool { + matches!(c, '\u{200C}' | '\u{200D}') +} + /// Spelling issues in a paragraph of English text (no suggestions; ask [`suggest`] on demand). pub fn check_spelling(text: &str) -> Vec { check_spelling_with(text, &Options::default()) diff --git a/crates/proof/src/script_checks.rs b/crates/proof/src/script_checks.rs index 49f1bdd5c9..0dde8b8a20 100644 --- a/crates/proof/src/script_checks.rs +++ b/crates/proof/src/script_checks.rs @@ -46,10 +46,13 @@ pub(crate) fn is_correct(script: &Script, d: &AffixDict, word: &str, opts: &crat } /// Is one word (no hyphens) in the dictionary or the user dictionary, spelled as it is or as the -/// script's [`Script::variant`]? +/// script's [`Script::variant`]? A word with a zero-width joiner or non-joiner (`પુસ્` U+200D +/// `તક`) is tried as typed, then without its joiners, as a dictionary that ignores them +/// (Hunspell's `IGNORE`) would: they choose how a conjunct is drawn, not how the word is spelled. fn known(script: &Script, d: &AffixDict, w: &str) -> bool { let listed = |w: &str| d.check(w) || crate::in_user_dictionary(&w.to_lowercase()); - listed(w) || script.variant.and_then(|v| v(w)).is_some_and(|v| listed(&v)) + let spelled = |w: &str| listed(w) || script.variant.and_then(|v| v(w)).is_some_and(|v| listed(&v)); + spelled(w) || (w.contains(crate::is_joiner) && spelled(&w.replace(crate::is_joiner, ""))) } /// A language's script, messages and choices for [`check`]. @@ -91,7 +94,7 @@ fn repeated_words(text: &str, ws: &[(usize, usize)], rules: &Rules, v: &mut Vec< 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() || (rules.script.is_letter)(c)) + && w0.chars().all(|c| c.is_alphabetic() || (rules.script.is_letter)(c) || crate::is_joiner(c)) && w0 == w1 && (rules.flag_repeat)(w0) {