From e79e653839324b9a507595387e9b2956813d3c56 Mon Sep 17 00:00:00 2001 From: Hamza Date: Mon, 16 Nov 2026 16:11:08 +0500 Subject: [PATCH 1/7] Spelling: compound parts take their flags from affixes, and ONLYINCOMPOUND affixes stay in compounds MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit German frami gives most compound beginnings their flag through a zero suffix's continuation flags (Vor/hij with SFX j 0 0/xoc), not on the root, so Vorschlag, Werkzeuge, Maßnahme and most other compounds were reported as misspelt. A compound part may now be a root with one affix whose continuation flags (or root) carry the compound flag: a suffix on the last part, a prefix on the first, and elsewhere only with COMPOUNDPERMITFLAG, which is now read. An affix marked ONLYINCOMPOUND no longer makes a word on its own (Vor, Deckungs). Fixes #2145. --- crates/engine/src/spell.rs | 101 ++++++++++++++++++++++++++++++------- 1 file changed, 84 insertions(+), 17 deletions(-) diff --git a/crates/engine/src/spell.rs b/crates/engine/src/spell.rs index 8bc88e646..3bfbf7652 100644 --- a/crates/engine/src/spell.rs +++ b/crates/engine/src/spell.rs @@ -22,6 +22,14 @@ enum FlagMode { Num, } +/// Where a part sits in a compound. +#[derive(Clone, Copy, Debug, PartialEq)] +enum Pos { + First, + Middle, + Last, +} + #[derive(Clone, Debug)] enum Cond { Any, @@ -108,6 +116,8 @@ pub struct Dictionary { compound_begin: Option, compound_middle: Option, compound_end: Option, + /// `COMPOUNDPERMITFLAG`: an affix carrying it may sit inside a compound, not only at its ends. + compound_permit: Option, compound_min: usize, compound_max: usize, } @@ -167,6 +177,7 @@ impl Dictionary { ["COMPOUNDBEGIN", f, ..] => d.compound_begin = one_flag(Some(f), mode), ["COMPOUNDMIDDLE", f, ..] => d.compound_middle = one_flag(Some(f), mode), ["COMPOUNDEND", f, ..] | ["COMPOUNDLAST", f, ..] => d.compound_end = one_flag(Some(f), mode), + ["COMPOUNDPERMITFLAG", f, ..] => d.compound_permit = one_flag(Some(f), mode), ["COMPOUNDMIN", n, ..] => d.compound_min = n.parse::().unwrap_or(3).max(1), ["COMPOUNDWORDMAX", n, ..] => d.compound_max = n.parse().unwrap_or(0), ["REP", n] => { @@ -250,11 +261,18 @@ impl Dictionary { self.words.get(word).is_some_and(|hs| hs.iter().any(|f| self.has(f, self.forbidden))) } + /// An affix that may only be used inside a compound (`ONLYINCOMPOUND` among its continuation + /// flags): frami's zero suffixes that turn `Vor` into a compound's first part don't make `Vor` + /// a word of its own (#2145). + fn only_in_compound_affix(&self, a: &Affix) -> bool { + self.only_in_compound.is_some_and(|o| a.cont.contains(&o)) + } + /// Strip suffixes from `w` (one, or two through continuation flags); `extra` flags must also be /// on the root, `cross` means the suffix must allow a prefix. fn suffixed(&self, w: &[char], extra: &[Flag], cross: bool) -> bool { for s in &self.sfx { - if cross && !s.cross { + if (cross && !s.cross) || self.only_in_compound_affix(s) { continue; } let Some(stem) = strip_end(w, &s.add, &s.strip) else { continue }; @@ -269,7 +287,7 @@ impl Dictionary { } // A second suffix whose continuation allows this one (`walk/A` + `ing/B`…). for s2 in &self.sfx { - if !s.cont.is_empty() || !s2.cont.contains(&s.flag) { + if !s.cont.is_empty() || !s2.cont.contains(&s.flag) || self.only_in_compound_affix(s2) { continue; } let Some(stem2) = strip_end(&stem, &s2.add, &s2.strip) else { continue }; @@ -285,24 +303,36 @@ impl Dictionary { false } - /// A root (or, for the last part, a suffixed root) carrying `flag`, as a compound part. - fn compound_part(&self, part: &[char], flag: Flag, last: bool) -> bool { + /// A root carrying `flag`, or a root with one affix, as a compound part at `pos`. + fn compound_part(&self, part: &[char], flag: Flag, pos: Pos) -> bool { let word: String = part.iter().collect(); let ok = self.words.get(&word).is_some_and(|hs| hs.iter().any(|f| !self.has(f, self.forbidden) && f.contains(&flag))); - ok || (last && self.suffixed_compound_end(part, flag)) + ok || self.affixed_compound_part(part, flag, pos) } - /// The last part of a compound with a suffix: the root must carry the compound `flag`. - fn suffixed_compound_end(&self, w: &[char], flag: Flag) -> bool { - self.sfx.iter().any(|s| { - strip_end(w, &s.add, &s.strip).is_some_and(|stem| { - s.cond_ok(&stem, false) - && self - .words - .get(&stem.iter().collect::()) - .is_some_and(|hs| hs.iter().any(|f| !self.has(f, self.forbidden) && f.contains(&flag) && f.contains(&s.flag))) + /// A compound part made of a root and one affix, as Hunspell reads them: a suffix ends the last + /// part and a prefix starts the first; anywhere else the affix needs `COMPOUNDPERMITFLAG`. The + /// compound `flag` may be on the root or among the affix's continuation flags. German frami + /// gives many first parts their flag that way: `Vor/hij` with `SFX j 0 0/xoc` begins + /// `Vorschlag` (#2145). + fn affixed_compound_part(&self, w: &[char], flag: Flag, pos: Pos) -> bool { + let permit = |a: &Affix| self.compound_permit.is_some_and(|p| a.cont.contains(&p)); + let fits = |a: &Affix, stem: &[char]| { + self.words.get(&stem.iter().collect::()).is_some_and(|hs| { + hs.iter().any(|f| !self.has(f, self.forbidden) && f.contains(&a.flag) && (f.contains(&flag) || a.cont.contains(&flag))) }) - }) + }; + let suffixed = self + .sfx + .iter() + .filter(|s| pos == Pos::Last || permit(s)) + .any(|s| strip_end(w, &s.add, &s.strip).is_some_and(|stem| s.cond_ok(&stem, false) && fits(s, &stem))); + suffixed + || self + .pfx + .iter() + .filter(|p| pos == Pos::First || permit(p)) + .any(|p| strip_start(w, &p.add, &p.strip).is_some_and(|stem| p.cond_ok(&stem, true) && fits(p, &stem))) } /// `w` made of dictionary parts allowed in compounds (`COMPOUNDFLAG`, or the begin, middle and @@ -323,11 +353,12 @@ impl Dictionary { let role = if index == 0 { self.compound_begin } else { self.compound_middle }; for i in min..=w.len() - min { let (head, rest) = w.split_at(i); - let head_ok = [self.compound, role].into_iter().flatten().any(|f| self.compound_part(head, f, false)); + let pos = if index == 0 { Pos::First } else { Pos::Middle }; + let head_ok = [self.compound, role].into_iter().flatten().any(|f| self.compound_part(head, f, pos)); if !head_ok { continue; } - if [self.compound, self.compound_end].into_iter().flatten().any(|f| self.compound_part(rest, f, true)) { + if [self.compound, self.compound_end].into_iter().flatten().any(|f| self.compound_part(rest, f, Pos::Last)) { return true; } if self.compounded(rest, index + 1, failed) { @@ -350,6 +381,9 @@ impl Dictionary { return true; } for p in &self.pfx { + if self.only_in_compound_affix(p) { + continue; + } let Some(stem) = strip_start(&w, &p.add, &p.strip) else { continue }; if !p.cond_ok(&stem, true) { continue; @@ -972,6 +1006,39 @@ SFX D Y 1\nSFX D 0 ed/S .\n"; } } + #[test] + fn affixes_give_compound_parts_their_flags() { + // #2145, the way German frami builds compounds: the first and middle parts get their flags + // from zero affixes' continuation classes, not from the root, and those roots need an affix. + let aff = "COMPOUNDBEGIN B\nCOMPOUNDMIDDLE M\nCOMPOUNDEND E\nCOMPOUNDPERMITFLAG P\nONLYINCOMPOUND O\nNEEDAFFIX N\n\ + COMPOUNDMIN 2\nSFX j Y 1\nSFX j 0 0/BOP .\nSFX m Y 1\nSFX m 0 0/MOP .\nPFX q Y 1\nPFX q 0 0/BO .\n\ + SFX s Y 1\nSFX s 0 e .\nSFX u Y 1\nSFX u 0 s .\nPFX v Y 1\nPFX v 0 un .\n"; + let dic = "6\nvor/Nj\nschlag/OEs\nwerk/Njm\nzeug/OEs\nding/Nq\nhaus/E\n"; + let d = Dictionary::parse(aff.as_bytes(), dic.as_bytes()); + // First part through SFX j, middle part through SFX m (it carries the permit flag), last + // part from its root, with or without a suffix there. + for w in ["vorschlag", "vorschlage", "werkzeug", "werkzeuge", "vorwerkzeug", "werkhaus", "dinghaus"] { + assert!(d.check(w), "{w}"); + } + for w in [ + // A NEEDAFFIX or ONLYINCOMPOUND root alone, or a part with no compound flag at all. + "vor", + "werk", + "schlag", + "hausvor", + "schlagzeug", + // A suffix on the first part without the permit flag (s), and a prefix on a later part + // without it (v). + "vorschlageschlag", + "werkunhaus", + // Not words. + "vorschlagx", + "xvorschlag", + ] { + assert!(!d.check(w), "{w}"); + } + } + #[test] fn overlapping_compound_roots_with_a_failing_ending_stay_cheap() { // Roots a..aaaaaaaa overlap, so many splits of "aaa…ab" reach the same (tail, part) state; unmemoized that is millions of searches. From 77cc922b7fc5c9fd0fbccdc85febb87149fa75e6 Mon Sep 17 00:00:00 2001 From: Hamza Date: Mon, 16 Nov 2026 16:36:04 +0500 Subject: [PATCH 2/7] Spelling: an affix that needs another doesn't finish a part, and compound-only suffixes don't end a compound As Hunspell 1.7.6 reads the same dictionaries: an affix carrying NEEDAFFIX among its continuation flags finishes neither a word nor a compound part (cat/S with SFX S 0 s/XN: cats and suncats are wrong), and at the end of a compound a suffix that may only be used inside compounds may be empty but may not add letters (SFX S 0 s/XO: suncats is wrong, SFX S 0 0/XO: suncat is right). --- crates/engine/src/spell.rs | 50 +++++++++++++++++++++++++++++++++----- 1 file changed, 44 insertions(+), 6 deletions(-) diff --git a/crates/engine/src/spell.rs b/crates/engine/src/spell.rs index 3bfbf7652..e0c4fbc46 100644 --- a/crates/engine/src/spell.rs +++ b/crates/engine/src/spell.rs @@ -268,11 +268,17 @@ impl Dictionary { self.only_in_compound.is_some_and(|o| a.cont.contains(&o)) } + /// An affix that needs a further affix (`NEEDAFFIX` among its continuation flags): it can't be + /// the last affix of a word or of a compound part. + fn needs_another_affix(&self, a: &Affix) -> bool { + self.need_affix.is_some_and(|n| a.cont.contains(&n)) + } + /// Strip suffixes from `w` (one, or two through continuation flags); `extra` flags must also be /// on the root, `cross` means the suffix must allow a prefix. fn suffixed(&self, w: &[char], extra: &[Flag], cross: bool) -> bool { for s in &self.sfx { - if (cross && !s.cross) || self.only_in_compound_affix(s) { + if (cross && !s.cross) || self.only_in_compound_affix(s) || self.needs_another_affix(s) { continue; } let Some(stem) = strip_end(w, &s.add, &s.strip) else { continue }; @@ -317,15 +323,20 @@ impl Dictionary { /// `Vorschlag` (#2145). fn affixed_compound_part(&self, w: &[char], flag: Flag, pos: Pos) -> bool { let permit = |a: &Affix| self.compound_permit.is_some_and(|p| a.cont.contains(&p)); + // An affix that itself needs another (`NEEDAFFIX` among its continuation flags) doesn't + // finish a part on its own. let fits = |a: &Affix, stem: &[char]| { - self.words.get(&stem.iter().collect::()).is_some_and(|hs| { - hs.iter().any(|f| !self.has(f, self.forbidden) && f.contains(&a.flag) && (f.contains(&flag) || a.cont.contains(&flag))) - }) + !self.needs_another_affix(a) + && self.words.get(&stem.iter().collect::()).is_some_and(|hs| { + hs.iter().any(|f| !self.has(f, self.forbidden) && f.contains(&a.flag) && (f.contains(&flag) || a.cont.contains(&flag))) + }) }; let suffixed = self .sfx .iter() - .filter(|s| pos == Pos::Last || permit(s)) + // At the end of a compound, Hunspell refuses a suffix that adds letters and may only be + // used inside compounds; a zero-length one is accepted. + .filter(|s| if pos == Pos::Last { s.add.is_empty() || !self.only_in_compound_affix(s) } else { permit(s) }) .any(|s| strip_end(w, &s.add, &s.strip).is_some_and(|stem| s.cond_ok(&stem, false) && fits(s, &stem))); suffixed || self @@ -388,7 +399,7 @@ impl Dictionary { if !p.cond_ok(&stem, true) { continue; } - if self.root(&stem.iter().collect::(), &[p.flag], true) { + if !self.needs_another_affix(p) && self.root(&stem.iter().collect::(), &[p.flag], true) { return true; } if p.cross && self.suffixed(&stem, &[p.flag], true) { @@ -1039,6 +1050,33 @@ SFX D Y 1\nSFX D 0 ed/S .\n"; } } + #[test] + fn affixes_that_need_another_or_only_live_in_compounds_follow_hunspell() { + // Each row as Hunspell 1.7.6 reads the same dictionary: `sun/X` and `cat/S`, `SFX S` adding + // the compound flag X and NEEDAFFIX (N) or ONLYINCOMPOUND (O). + let dict = |sfx: &str| { + let aff = format!("COMPOUNDFLAG X\nCOMPOUNDMIN 3\nNEEDAFFIX N\nONLYINCOMPOUND O\nSFX S Y 1\nSFX S {sfx} .\n"); + Dictionary::parse(aff.as_bytes(), b"2\nsun/X\ncat/S\n".as_slice()) + }; + let rows: [(&str, &[&str], &[&str]); 4] = [ + // An affix that needs another finishes neither a word nor a compound part. + ("0 s/XN", &["sun", "cat"], &["cats", "suncats", "catssun"]), + ("0 0/XN", &["sun", "cat"], &["suncat"]), + // At the end of a compound a compound-only suffix may be empty, not add letters. + ("0 s/XO", &["sun", "cat"], &["cats", "suncats"]), + ("0 0/XO", &["sun", "cat", "suncat"], &["catsun", "suncatsun"]), + ]; + for (sfx, good, bad) in rows { + let d = dict(sfx); + for w in good { + assert!(d.check(w), "{sfx}: {w}"); + } + for w in bad { + assert!(!d.check(w), "{sfx}: {w}"); + } + } + } + #[test] fn overlapping_compound_roots_with_a_failing_ending_stay_cheap() { // Roots a..aaaaaaaa overlap, so many splits of "aaa…ab" reach the same (tail, part) state; unmemoized that is millions of searches. From 74f6e2c9b43b6b2aa7a1a86b7cc9b42c66ca209e Mon Sep 17 00:00:00 2001 From: "autocraft-bot[bot]" <354215646+autocraft-bot[bot]@users.noreply.github.com> Date: Mon, 16 Nov 2026 11:40:03 +0000 Subject: [PATCH 3/7] Apply review fixes - crates/engine/src/spell.rs --- crates/engine/src/spell.rs | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/crates/engine/src/spell.rs b/crates/engine/src/spell.rs index 3bfbf7652..678e8ecda 100644 --- a/crates/engine/src/spell.rs +++ b/crates/engine/src/spell.rs @@ -311,16 +311,18 @@ impl Dictionary { } /// A compound part made of a root and one affix, as Hunspell reads them: a suffix ends the last - /// part and a prefix starts the first; anywhere else the affix needs `COMPOUNDPERMITFLAG`. The + /// part and a prefix starts the first; anywhere else the affix needs `COMPOUNDPERMITFLAG`. + /// An affix that carries NEEDAFFIX needs a second one and doesn't count. The /// compound `flag` may be on the root or among the affix's continuation flags. German frami /// gives many first parts their flag that way: `Vor/hij` with `SFX j 0 0/xoc` begins /// `Vorschlag` (#2145). fn affixed_compound_part(&self, w: &[char], flag: Flag, pos: Pos) -> bool { let permit = |a: &Affix| self.compound_permit.is_some_and(|p| a.cont.contains(&p)); let fits = |a: &Affix, stem: &[char]| { - self.words.get(&stem.iter().collect::()).is_some_and(|hs| { - hs.iter().any(|f| !self.has(f, self.forbidden) && f.contains(&a.flag) && (f.contains(&flag) || a.cont.contains(&flag))) - }) + !self.need_affix.is_some_and(|n| a.cont.contains(&n)) + && self.words.get(&stem.iter().collect::()).is_some_and(|hs| { + hs.iter().any(|f| !self.has(f, self.forbidden) && f.contains(&a.flag) && (f.contains(&flag) || a.cont.contains(&flag))) + }) }; let suffixed = self .sfx @@ -1012,8 +1014,8 @@ SFX D Y 1\nSFX D 0 ed/S .\n"; // from zero affixes' continuation classes, not from the root, and those roots need an affix. let aff = "COMPOUNDBEGIN B\nCOMPOUNDMIDDLE M\nCOMPOUNDEND E\nCOMPOUNDPERMITFLAG P\nONLYINCOMPOUND O\nNEEDAFFIX N\n\ COMPOUNDMIN 2\nSFX j Y 1\nSFX j 0 0/BOP .\nSFX m Y 1\nSFX m 0 0/MOP .\nPFX q Y 1\nPFX q 0 0/BO .\n\ - SFX s Y 1\nSFX s 0 e .\nSFX u Y 1\nSFX u 0 s .\nPFX v Y 1\nPFX v 0 un .\n"; - let dic = "6\nvor/Nj\nschlag/OEs\nwerk/Njm\nzeug/OEs\nding/Nq\nhaus/E\n"; + SFX s Y 1\nSFX s 0 e .\nSFX u Y 1\nSFX u 0 s .\nPFX v Y 1\nPFX v 0 un .\nSFX t Y 1\nSFX t 0 0/BNP .\n"; + let dic = "8\nvor/Nj\nschlag/OEs\nwerk/Njm\nzeug/OEs\nding/Nq\nhaus/Ev\ntag/Bs\ntor/t\n"; let d = Dictionary::parse(aff.as_bytes(), dic.as_bytes()); // First part through SFX j, middle part through SFX m (it carries the permit flag), last // part from its root, with or without a suffix there. @@ -1031,6 +1033,9 @@ SFX D Y 1\nSFX D 0 ed/S .\n"; // without it (v). "vorschlageschlag", "werkunhaus", + // tag carries B but its suffix s has no permit flag; SFX t gives B but carries NEEDAFFIX. + "tageschlag", + "torhaus", // Not words. "vorschlagx", "xvorschlag", From 4bc331c46dc761c47b1fe61a547bff030beff7fb Mon Sep 17 00:00:00 2001 From: Hamza Date: Mon, 16 Nov 2026 17:07:40 +0500 Subject: [PATCH 4/7] Spelling: a prefix completes a suffix that needs another affix work/US with SFX S 0 s/N: unworks is a word (the prefix completes the suffix), works isn't. The last change refused the suffix in the prefix's cross product too; a prefix that needs nothing more now completes it, as Hunspell 1.7.6 reads it. When both need another affix, neither completes the other. --- crates/engine/src/spell.rs | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/crates/engine/src/spell.rs b/crates/engine/src/spell.rs index af25f270d..6d0bde8d1 100644 --- a/crates/engine/src/spell.rs +++ b/crates/engine/src/spell.rs @@ -275,10 +275,12 @@ impl Dictionary { } /// Strip suffixes from `w` (one, or two through continuation flags); `extra` flags must also be - /// on the root, `cross` means the suffix must allow a prefix. - fn suffixed(&self, w: &[char], extra: &[Flag], cross: bool) -> bool { + /// on the root, `cross` means the suffix must allow a prefix. `completed`: a prefix that needs + /// nothing more is already on the word, so a suffix that needs another affix has it + /// (`work/US` with `SFX S 0 s/N` makes `unworks`, not `works`). + fn suffixed(&self, w: &[char], extra: &[Flag], cross: bool, completed: bool) -> bool { for s in &self.sfx { - if (cross && !s.cross) || self.only_in_compound_affix(s) || self.needs_another_affix(s) { + if (cross && !s.cross) || self.only_in_compound_affix(s) || (self.needs_another_affix(s) && !completed) { continue; } let Some(stem) = strip_end(w, &s.add, &s.strip) else { continue }; @@ -389,7 +391,7 @@ impl Dictionary { return true; } let w: Vec = word.chars().collect(); - if self.suffixed(&w, &[], false) { + if self.suffixed(&w, &[], false, false) { return true; } for p in &self.pfx { @@ -403,7 +405,7 @@ impl Dictionary { if !self.needs_another_affix(p) && self.root(&stem.iter().collect::(), &[p.flag], true) { return true; } - if p.cross && self.suffixed(&stem, &[p.flag], true) { + if p.cross && self.suffixed(&stem, &[p.flag], true, !self.needs_another_affix(p)) { return true; } } @@ -1081,6 +1083,25 @@ SFX D Y 1\nSFX D 0 ed/S .\n"; } } + #[test] + fn a_prefix_completes_a_suffix_that_needs_another_affix() { + // As Hunspell 1.7.6 reads it: `work/US`, a suffix needing another affix (N). The prefix + // completes it (`unworks`); alone it doesn't (`works`); when both need another, neither + // completes the other. + for (pfx, good, bad) in + [("0 un", &["work", "unwork", "unworks"][..], &["works"][..]), ("0 un/N", &["work"][..], &["works", "unwork", "unworks"][..])] + { + let aff = format!("NEEDAFFIX N\nPFX U Y 1\nPFX U {pfx} .\nSFX S Y 1\nSFX S 0 s/N .\n"); + let d = Dictionary::parse(aff.as_bytes(), b"1\nwork/US\n".as_slice()); + for w in good { + assert!(d.check(w), "{pfx}: {w}"); + } + for w in bad { + assert!(!d.check(w), "{pfx}: {w}"); + } + } + } + #[test] fn overlapping_compound_roots_with_a_failing_ending_stay_cheap() { // Roots a..aaaaaaaa overlap, so many splits of "aaa…ab" reach the same (tail, part) state; unmemoized that is millions of searches. From d163b84e530a606f7231d0b49e9103ab27cdeae5 Mon Sep 17 00:00:00 2001 From: "autocraft-bot[bot]" <354215646+autocraft-bot[bot]@users.noreply.github.com> Date: Mon, 16 Nov 2026 14:12:15 +0000 Subject: [PATCH 5/7] Apply review fixes - crates/engine/src/spell.rs --- crates/engine/src/spell.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/crates/engine/src/spell.rs b/crates/engine/src/spell.rs index 6d0bde8d1..21e2f3e69 100644 --- a/crates/engine/src/spell.rs +++ b/crates/engine/src/spell.rs @@ -320,7 +320,7 @@ impl Dictionary { /// A compound part made of a root and one affix, as Hunspell reads them: a suffix ends the last /// part and a prefix starts the first; anywhere else the affix needs `COMPOUNDPERMITFLAG`. - /// An affix that carries `NEEDAFFIX` needs a second one and doesn't count. The compound `flag` + /// An affix that carries `NEEDAFFIX` needs a second one and doesn't count. One whose continuation flags carry `COMPOUNDEND` may only be on the last part. The compound `flag` /// may be on the root or among the affix's continuation flags. German frami /// gives many first parts their flag that way: `Vor/hij` with `SFX j 0 0/xoc` begins /// `Vorschlag` (#2145). @@ -330,6 +330,8 @@ impl Dictionary { // finish a part on its own. let fits = |a: &Affix, stem: &[char]| { !self.needs_another_affix(a) + // Only the last part may take an affix whose continuation flags carry the compound end flag. + && (pos == Pos::Last || !self.compound_end.is_some_and(|e| a.cont.contains(&e))) && self.words.get(&stem.iter().collect::()).is_some_and(|hs| { hs.iter().any(|f| !self.has(f, self.forbidden) && f.contains(&a.flag) && (f.contains(&flag) || a.cont.contains(&flag))) }) @@ -1102,6 +1104,15 @@ SFX D Y 1\nSFX D 0 ed/S .\n"; } } + #[test] + fn an_affix_carrying_the_compound_end_flag_only_ends_a_compound() { + let aff = "COMPOUNDFLAG X\nCOMPOUNDEND E\nCOMPOUNDPERMITFLAG P\nCOMPOUNDMIN 3\nSFX S Y 1\nSFX S 0 s/XEP .\n"; + let d = Dictionary::parse(aff.as_bytes(), b"2\nsun/X\ncat/S\n".as_slice()); + assert!(d.check("suncats")); + assert!(d.check("cats")); + assert!(!d.check("catssun")); + } + #[test] fn overlapping_compound_roots_with_a_failing_ending_stay_cheap() { // Roots a..aaaaaaaa overlap, so many splits of "aaa…ab" reach the same (tail, part) state; unmemoized that is millions of searches. From 6921db0f835557ed77a3dddd766e831dacb0ee2d Mon Sep 17 00:00:00 2001 From: Hamza Date: Tue, 17 Nov 2026 13:54:46 +0500 Subject: [PATCH 6/7] Spelling: wrap affixed_compound_part's doc comment --- crates/engine/src/spell.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/engine/src/spell.rs b/crates/engine/src/spell.rs index 21e2f3e69..61c5117ae 100644 --- a/crates/engine/src/spell.rs +++ b/crates/engine/src/spell.rs @@ -320,10 +320,10 @@ impl Dictionary { /// A compound part made of a root and one affix, as Hunspell reads them: a suffix ends the last /// part and a prefix starts the first; anywhere else the affix needs `COMPOUNDPERMITFLAG`. - /// An affix that carries `NEEDAFFIX` needs a second one and doesn't count. One whose continuation flags carry `COMPOUNDEND` may only be on the last part. The compound `flag` - /// may be on the root or among the affix's continuation flags. German frami - /// gives many first parts their flag that way: `Vor/hij` with `SFX j 0 0/xoc` begins - /// `Vorschlag` (#2145). + /// An affix that carries `NEEDAFFIX` needs a second one and doesn't count. One whose + /// continuation flags carry `COMPOUNDEND` may only be on the last part. The compound `flag` + /// may be on the root or among the affix's continuation flags. German frami gives many first + /// parts their flag that way: `Vor/hij` with `SFX j 0 0/xoc` begins `Vorschlag` (#2145). fn affixed_compound_part(&self, w: &[char], flag: Flag, pos: Pos) -> bool { let permit = |a: &Affix| self.compound_permit.is_some_and(|p| a.cont.contains(&p)); // An affix that itself needs another (`NEEDAFFIX` among its continuation flags) doesn't From 62e00839c72553485ec79e435e2e0f0aed7fdb67 Mon Sep 17 00:00:00 2001 From: Hamza Date: Tue, 17 Nov 2026 14:15:49 +0500 Subject: [PATCH 7/7] Spelling: the compound-end exclusion applies to the plain compound flag only A suffix whose continuation flags carry COMPOUNDEND was refused in any part but the last, whichever flag found it. Hunspell refuses it only on the plain COMPOUNDFLAG lookup: with COMPOUNDBEGIN B, COMPOUNDEND E and SFX S 0 s/BEP, Hunspell 1.7.6 accepts `catssun`, which this rejected (CodeRabbit's review). --- crates/engine/src/spell.rs | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/crates/engine/src/spell.rs b/crates/engine/src/spell.rs index 61c5117ae..a5654ccea 100644 --- a/crates/engine/src/spell.rs +++ b/crates/engine/src/spell.rs @@ -320,18 +320,21 @@ impl Dictionary { /// A compound part made of a root and one affix, as Hunspell reads them: a suffix ends the last /// part and a prefix starts the first; anywhere else the affix needs `COMPOUNDPERMITFLAG`. - /// An affix that carries `NEEDAFFIX` needs a second one and doesn't count. One whose - /// continuation flags carry `COMPOUNDEND` may only be on the last part. The compound `flag` + /// An affix that carries `NEEDAFFIX` needs a second one and doesn't count. A suffix found + /// through the plain compound flag whose continuation flags carry `COMPOUNDEND` may only be on + /// the last part. The compound `flag` /// may be on the root or among the affix's continuation flags. German frami gives many first /// parts their flag that way: `Vor/hij` with `SFX j 0 0/xoc` begins `Vorschlag` (#2145). fn affixed_compound_part(&self, w: &[char], flag: Flag, pos: Pos) -> bool { let permit = |a: &Affix| self.compound_permit.is_some_and(|p| a.cont.contains(&p)); // An affix that itself needs another (`NEEDAFFIX` among its continuation flags) doesn't // finish a part on its own. - let fits = |a: &Affix, stem: &[char]| { + let fits = |a: &Affix, stem: &[char], suffix: bool| { !self.needs_another_affix(a) - // Only the last part may take an affix whose continuation flags carry the compound end flag. - && (pos == Pos::Last || !self.compound_end.is_some_and(|e| a.cont.contains(&e))) + // Only the last part may take a suffix found through the plain compound flag whose + // continuation flags carry the compound end flag; the begin, middle and prefix + // lookups don't check it, as in Hunspell. + && (pos == Pos::Last || !suffix || Some(flag) != self.compound || !self.compound_end.is_some_and(|e| a.cont.contains(&e))) && self.words.get(&stem.iter().collect::()).is_some_and(|hs| { hs.iter().any(|f| !self.has(f, self.forbidden) && f.contains(&a.flag) && (f.contains(&flag) || a.cont.contains(&flag))) }) @@ -342,13 +345,13 @@ impl Dictionary { // At the end of a compound, Hunspell refuses a suffix that adds letters and may only be // used inside compounds; a zero-length one is accepted. .filter(|s| if pos == Pos::Last { s.add.is_empty() || !self.only_in_compound_affix(s) } else { permit(s) }) - .any(|s| strip_end(w, &s.add, &s.strip).is_some_and(|stem| s.cond_ok(&stem, false) && fits(s, &stem))); + .any(|s| strip_end(w, &s.add, &s.strip).is_some_and(|stem| s.cond_ok(&stem, false) && fits(s, &stem, true))); suffixed || self .pfx .iter() .filter(|p| pos == Pos::First || permit(p)) - .any(|p| strip_start(w, &p.add, &p.strip).is_some_and(|stem| p.cond_ok(&stem, true) && fits(p, &stem))) + .any(|p| strip_start(w, &p.add, &p.strip).is_some_and(|stem| p.cond_ok(&stem, true) && fits(p, &stem, false))) } /// `w` made of dictionary parts allowed in compounds (`COMPOUNDFLAG`, or the begin, middle and @@ -1111,6 +1114,15 @@ SFX D Y 1\nSFX D 0 ed/S .\n"; assert!(d.check("suncats")); assert!(d.check("cats")); assert!(!d.check("catssun")); + // Through the begin flag, the end flag on the suffix doesn't matter: Hunspell 1.7.6 + // accepts `catssun` here (and `cats`), and rejects `catsun`, `suncats` and `suncat`. + let aff = "COMPOUNDBEGIN B\nCOMPOUNDEND E\nCOMPOUNDPERMITFLAG P\nCOMPOUNDMIN 3\nSFX S Y 1\nSFX S 0 s/BEP .\n"; + let d = Dictionary::parse(aff.as_bytes(), b"2\nsun/E\ncat/S\n".as_slice()); + assert!(d.check("catssun")); + assert!(d.check("cats")); + for w in ["catsun", "suncats", "suncat"] { + assert!(!d.check(w), "{w}"); + } } #[test]