From 867f44a3d8e412145e48221ac10e60e0a5310a19 Mon Sep 17 00:00:00 2001 From: Martin Ball Date: Sun, 1 Nov 2026 18:27:14 +0000 Subject: [PATCH] =?UTF-8?q?CP4.2:=20review=20=E2=80=94=20pack=20hyphenatio?= =?UTF-8?q?n=20patterns=20only=20take=20words=20in=20the=20language's=20sc?= =?UTF-8?q?ript;=20others=20fall=20through=20as=20before?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01FypgCX8LsYYE8fsj4byBm2 --- crates/proof/README.md | 5 +++-- crates/proof/src/hyphen.rs | 21 +++++++++++++++++++-- crates/proof/src/lib.rs | 22 ++++++++++++++++++++++ 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/crates/proof/README.md b/crates/proof/README.md index 1230cd285..d62facba6 100644 --- a/crates/proof/README.md +++ b/crates/proof/README.md @@ -126,8 +126,9 @@ locale (`nl`, `pt-br`, `sr-latn`): **Lookup order.** Spelling: the dictionaries folder, then an installed pack, then the system's Hunspell dictionaries, then (English only) the built-in core list. A slot's Hunspell names map to pack locales (`pt_BR` → `pt-br`, then `pt`, then any other `pt-*` pack). Hyphenation, every -language but English: an installed pack's patterns, then the embedded ones (a Language build), -then the language's rules or the English fallback as before. +language but English: an installed pack's patterns for a word in the language's script (Russian, +Bulgarian, Turkish: their script; others: letters the patterns know), then the embedded ones (a +Language build), then the language's rules or the English fallback as before. **Hostile packs.** Every file is read only if it is a regular file (never through a symbolic link), size-capped (`pack.toml` 1 MB, licences 4 MB, dictionaries and patterns 64 MB), and a pack diff --git a/crates/proof/src/hyphen.rs b/crates/proof/src/hyphen.rs index 11e066d71..22271ca01 100644 --- a/crates/proof/src/hyphen.rs +++ b/crates/proof/src/hyphen.rs @@ -128,8 +128,12 @@ fn core_breaks(core: &[char], lang: ProofLang) -> (Vec, Source) { if n < 2 { return (vec![], Source::None); } - // An installed Language pack's patterns come before the embedded ones and the rules. - if let Some(p) = crate::packs::hyphenation(lang) { + // An installed Language pack's patterns come before the embedded ones and the rules, for a + // word in the language's script; any other word goes on as before (a Latin word in a Russian + // run is hyphenated as English). + if let Some(p) = crate::packs::hyphenation(lang) + && pack_patterns_fit(lang, &p, core) + { return if lang == ProofLang::Pl { core_breaks_pl_with(&p, core) } else { core_breaks_patterns(&p, core) }; } if lang == ProofLang::Pl { @@ -180,6 +184,19 @@ fn core_breaks(core: &[char], lang: ProofLang) -> (Vec, Source) { (heuristic_points(core), Source::Heuristic) } +/// Does a pack's `pats` hyphenate `core` in `lang`? Russian, Bulgarian and Turkish: a word in +/// their script (the guard their rules use). 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 { + match lang { + ProofLang::Ru => core.iter().all(|&c| crate::ru::is_cyrillic(c)), + ProofLang::Bg => core.iter().all(|&c| crate::bg::is_letter(c)), + ProofLang::Tr => core.iter().all(|&c| crate::tr::hyphen::is_letter(c)), + ProofLang::Pl | ProofLang::Pt | ProofLang::It | ProofLang::De => true, + _ => pats.values(core).is_some(), + } +} + /// Polish: exception words, then the patterns, then the Polish syllable heuristic. fn core_breaks_pl(core: &[char]) -> (Vec, Source) { core_breaks_pl_with(Patterns::pl(), core) diff --git a/crates/proof/src/lib.rs b/crates/proof/src/lib.rs index 5f5bcebd2..60e5a4919 100644 --- a/crates/proof/src/lib.rs +++ b/crates/proof/src/lib.rs @@ -1563,6 +1563,28 @@ mod tests { let _ = std::fs::remove_dir_all(&root); } + /// Review of #485: a pack's patterns hyphenate only words in the language's script: a Latin + /// word in a Russian or Dutch run is hyphenated as it was without the pack. + #[test] + fn pack_patterns_only_hyphenate_their_script() { + let _guard = DICTIONARY_TESTS.lock().unwrap_or_else(std::sync::PoisonError::into_inner); + set_dictionaries_folder(Some(shared_test_folder())); + let root = std::env::temp_dir().join(format!("wc-proof-script-hyphen-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&root); + let lim = hyphen::Limits::default(); + let (ru, el) = (ProofLang::Ru, ProofLang::from_tag(Some("el"))); + let before = [hyphen::hyphenate_word_in("hyphenation", &lim, ru), hyphen::hyphenate_word_in("hyphenation", &lim, el)]; + write_pack(&root, "ru", &[("hyphenation.pat", "1на 1ка\n".as_bytes())]); + write_pack(&root, "el", &[("hyphenation.pat", "1να 1κα\n".as_bytes())]); + packs::set_packs_dir(Some(root.clone())); + assert_eq!([hyphen::hyphenate_word_in("hyphenation", &lim, ru), hyphen::hyphenate_word_in("hyphenation", &lim, el)], before); + assert_eq!(hyphen::hyphenate_word_in("канака", &lim, ru), "ка-на-ка", "a Russian word uses the pack"); + assert_eq!(hyphen::hyphenate_word_in("κανακα", &lim, el), "κα-να-κα"); + packs::set_packs_dir(None); + set_dictionaries_folder(Some(shared_test_folder())); + let _ = std::fs::remove_dir_all(&root); + } + /// Pack languages for the cache tests: `qa`, `qb`… each knowing `word`. fn many_packs(root: &std::path::Path, n: usize) -> Vec { (0..n)