Skip to content

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.dict
index 53f4670b1..afde04083 100644
--- a/harper-core/dictionary.dict
+++ b/harper-core/dictionary.dict
@@ -15268,6 +15268,7 @@ boar/~NSg
board/~NwgSVGd>Z
boarder/Ng
boarding/~V6Ng
+boarding school/NgS
boardinghouse/NgS
boardroom/NgS
boardwalk/~NgSV
@@ -18266,6 +18267,7 @@ coexistent/JN
coexists/Vh
coextensive/J
coffee/~NwSgJV
+coffee shop/NgS
coffeecake/NwSg
coffeehouse/~NgS
coffeemaker/NSg
@@ -26155,6 +26157,7 @@ frolicsome/J
from/~P
frond/NSg
front/~NSJVdGW
+front door/NgS
front-line/J
front man/N0g♂
front matter/Nmg
@@ -28673,6 +28676,7 @@ high-quality/J
high-ranking/J
high-res/J
high-rise/NgS
+high school/NgS
high-speed/J
high tech/Ng
highball/NSgV
@@ -34407,6 +34411,7 @@ midcentury/J
midday/~Ng
midden/NgS
middle/~NgSJVG
+middle school/NgS
middle-sized/J
middlebrow/JNSg
middleman/NgV
@@ -37246,6 +37251,7 @@ opaqueness/Nmg
opcode/NSg
ope/~JVSN
open/~J^Y>pVGdSNgZzB
+open house/NgS
open source/Ng
open-source/JVSdG
opencast/J
@@ -40766,6 +40772,7 @@ primal/~JNV
primality/J
primarily/~R # focusing/viewpoint/comment adverb
primary/~JNSgVdG
+primary school/NgS
primate/~NgS
prime/~JNgSV
primer/~NgJ
@@ -41277,6 +41284,7 @@ pubic/J
pubis/Ng
public/~JNmgVr
public key/NgS
+public school/NgS
publican/NgSr
publication/~NwSgr
publicise/VGdS!_₹
diff --git a/harper-core/src/expr/mergeable_words.rs b/harper-core/src/expr/mergeable_words.rs
index 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.rs
index 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.",
- )
+ );
}
}