Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
fix(core): don't merge compound nouns across an open compound (#4149)
Mauro Pereira1830de0442c4
diff --git a/harper-core/dictionary.dict b/harper-core/dictionary.dictindex 53f4670b1..afde04083 100644--- a/harper-core/dictionary.dict+++ b/harper-core/dictionary.dict@@ -15268,6 +15268,7 @@ boar/~NSgboard/~NwgSVGd>Zboarder/Ngboarding/~V6Ng+boarding school/NgSboardinghouse/NgSboardroom/NgSboardwalk/~NgSV@@ -18266,6 +18267,7 @@ coexistent/JNcoexists/Vhcoextensive/Jcoffee/~NwSgJV+coffee shop/NgScoffeecake/NwSgcoffeehouse/~NgScoffeemaker/NSg@@ -26155,6 +26157,7 @@ frolicsome/Jfrom/~Pfrond/NSgfront/~NSJVdGW+front door/NgSfront-line/Jfront man/N0g♂front matter/Nmg@@ -28673,6 +28676,7 @@ high-quality/Jhigh-ranking/Jhigh-res/Jhigh-rise/NgS+high school/NgShigh-speed/Jhigh tech/Nghighball/NSgV@@ -34407,6 +34411,7 @@ midcentury/Jmidday/~Ngmidden/NgSmiddle/~NgSJVG+middle school/NgSmiddle-sized/Jmiddlebrow/JNSgmiddleman/NgV@@ -37246,6 +37251,7 @@ opaqueness/Nmgopcode/NSgope/~JVSNopen/~J^Y>pVGdSNgZzB+open house/NgSopen source/Ngopen-source/JVSdGopencast/J@@ -40766,6 +40772,7 @@ primal/~JNVprimality/Jprimarily/~R # focusing/viewpoint/comment adverbprimary/~JNSgVdG+primary school/NgSprimate/~NgSprime/~JNgSVprimer/~NgJ@@ -41277,6 +41284,7 @@ pubic/Jpubis/Ngpublic/~JNmgVrpublic key/NgS+public school/NgSpublican/NgSrpublication/~NwSgrpublicise/VGdS!_₹diff --git a/harper-core/src/expr/mergeable_words.rs b/harper-core/src/expr/mergeable_words.rsindex 4af163ecc..f46db9c54 100644--- a/harper-core/src/expr/mergeable_words.rs+++ b/harper-core/src/expr/mergeable_words.rs@@ -31,6 +31,18 @@ impl MergeableWords {}}+ /// Whether the two adjacent words are themselves an open compound in the dictionary.+ ///+ /// Used to tell "high school | teacher" from "high | school teacher": if the left pair is+ /// already a compound, the boundary falls between them and the right pair must not merge.+ fn is_open_compound(&self, word_a: &Token, word_b: &Token, source: &[char]) -> bool {+ let mut compound: CharString = word_a.get_ch(source).into();+ compound.push(' ');+ compound.extend_from_slice(word_b.get_ch(source));++ self.dict.get_word_metadata(&compound).is_some()+ }+/// Get the merged word from the dictionary if these words can be merged./// Returns None if the words should remain separate (according to the predicate).pub fn get_merged_word(@@ -68,6 +80,17 @@ impl Expr for MergeableWords {return None;}+ // "high school teacher": when the word before the pair forms an open compound with the+ // first word of the pair, the compound boundary falls to the left and the pair spans two+ // compounds rather than being one.+ if cursor >= 2+ && tokens[cursor - 1].kind.is_whitespace()+ && tokens[cursor - 2].kind.is_word()+ && self.is_open_compound(&tokens[cursor - 2], &tokens[cursor], source)+ {+ return None;+ }+if self.get_merged_word(&tokens[cursor], &tokens[cursor + 2], source).is_some()diff --git a/harper-core/src/linting/compound_nouns/mod.rs b/harper-core/src/linting/compound_nouns/mod.rsindex d8c7bf4ec..070c19a70 100644--- a/harper-core/src/linting/compound_nouns/mod.rs+++ b/harper-core/src/linting/compound_nouns/mod.rs@@ -388,12 +388,34 @@ mod tests {);}+ #[test]+ fn allow_issue_3950() {+ assert_no_lints("She is a high school teacher.", test_linter());+ }++ #[test]+ fn allow_public_key_server() {+ assert_no_lints(+ "Alternatively, you can import them from a public key server.",+ test_linter(),+ );+ }++ #[test]+ fn still_flags_after_unrelated_adjective() {+ assert_suggestion_result(+ "The device features a responsive touch screen.",+ test_linter(),+ "The device features a responsive touchscreen.",+ );+ }+#[test]fn fix_big_break_through() {assert_suggestion_result("I think a big break through is needed for AGI so I haven’t been worried about it.",test_linter(),"I think a big breakthrough is needed for AGI so I haven’t been worried about it.",- )+ );}}