Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
fix(core): don't flag spelling inside known proper nouns (#4838)
Gary Jones83487a375256
diff --git a/harper-core/proper_noun_rules.json b/harper-core/proper_noun_rules.jsonindex 7c955bf62..6b0f9d2a7 100644--- a/harper-core/proper_noun_rules.json+++ b/harper-core/proper_noun_rules.json@@ -528,35 +528,60 @@},"NotablePlaces": {"canonical": [+ "Ann Arbor","Baton Rouge","Big Sur","Bretton Woods",+ "Centre Court","Coney Island","Darien Gap","Des Moines","El Paso","Hong Kong",+ "Johnson Space Center",+ "Kennedy Space Center","Las Palmas","Las Vegas",+ "Lincoln Center","Los Angeles","New York","New York City","Niagara Falls","Novi Sad",+ "One World Trade Center","Panama Canal",+ "Pearl Harbor","Rio de Janeiro",- "San Francisco"+ "Rockefeller Center",+ "San Francisco",+ "World Trade Center"],"description": "Ensure proper capitalization of notable places that are significant regional centers, travel destinations, or have international importance."},"ProperNouns": {"canonical": ["Association for Computing Machinery",+ "Australian Labor Party","Bhagavad Gita",+ "Centers for Disease Control and Prevention",+ "Department of Defense",+ "Department of Labor",+ "Food and Agriculture Organization","Gilded Age",+ "International Civil Aviation Organization",+ "International Labour Organization","Jim Crow","Khmer Rouge",- "Pax Americana"+ "Labour Party",+ "Ministry of Defence",+ "National Hurricane Center",+ "North Atlantic Treaty Organization",+ "Organization of American States",+ "Pan American Health Organization",+ "Pax Americana",+ "World Health Organization",+ "World Intellectual Property Organization",+ "World Trade Organization"],"description": "Ensure proper capitalization of proper nouns."},diff --git a/harper-core/src/linting/proper_noun_capitalization_linters.rs b/harper-core/src/linting/proper_noun_capitalization_linters.rsindex 2a42113f6..d9b4b223e 100644--- a/harper-core/src/linting/proper_noun_capitalization_linters.rs+++ b/harper-core/src/linting/proper_noun_capitalization_linters.rs@@ -1,4 +1,4 @@-use crate::expr::{Expr, ExprMap, FixedPhrase};+use crate::expr::{Expr, ExprMap, FixedPhrase, LongestMatchOf};use hashbrown::HashMap;use serde::{Deserialize, Serialize};@@ -113,6 +113,19 @@ pub fn lint_group() -> LintGroup {lint_group_from_json(include_str!("../../proper_noun_rules.json"))}+/// Matches every proper noun in `proper_noun_rules.json`, in any capitalization.+pub(super) fn proper_nouns_expr() -> LongestMatchOf {+ let rules: HashMap<String, RuleEntry> =+ serde_json::from_str(include_str!("../../proper_noun_rules.json")).unwrap();++ LongestMatchOf::new(+ rules+ .into_values()+ .flat_map(|rule| rule.canonical)+ .map(|name| Box::new(FixedPhrase::from_phrase(&name)) as Box<dyn Expr>),+ )+}+#[cfg(test)]mod tests {use super::lint_group;@@ -122,6 +135,25 @@ mod tests {create_test_pool!(LintGroup, LintGroup, lint_group());+ #[test]+ fn name_with_american_spelling_lowercase() {+ assert_suggestion_result("pearl harbor", test_linter(), "Pearl Harbor");+ }++ #[test]+ fn world_health_organization_lowercase() {+ assert_suggestion_result(+ "world health organization",+ test_linter(),+ "World Health Organization",+ );+ }++ #[test]+ fn name_with_british_spelling_lowercase() {+ assert_suggestion_result("ministry of defence", test_linter(), "Ministry of Defence");+ }+#[test]fn americas_lowercase() {assert_suggestion_result("south america", test_linter(), "South America");diff --git a/harper-core/src/linting/spell_check.rs b/harper-core/src/linting/spell_check.rsindex 3d9da5b97..44d566c46 100644--- a/harper-core/src/linting/spell_check.rs+++ b/harper-core/src/linting/spell_check.rs@@ -4,10 +4,11 @@ use itertools::Itertools;use lru::LruCache;use smallvec::ToSmallVec;+use super::proper_noun_capitalization_linters::proper_nouns_expr;use super::{Lint, LintKind, Linter};use super::{Suggestion, informal_laughter::is_informal_laughter};use crate::document::Document;-use crate::expr::{Filter, SequenceExpr};+use crate::expr::{Filter, LongestMatchOf, SequenceExpr};use crate::spell::{Dictionary, suggest_correct_spelling};use crate::{CharString, CharStringExt, Dialect, TokenStringExt, remove_lints_overlapping_expr};@@ -18,6 +19,7 @@ wheredictionary: T,suggestion_cache: LruCache<CharString, Vec<CharString>>,dialect: Dialect,+ proper_nouns: LongestMatchOf,}impl<T: Dictionary> SpellCheck<T> {@@ -26,6 +28,7 @@ impl<T: Dictionary> SpellCheck<T> {dictionary,suggestion_cache: LruCache::new(NonZero::new(10000).unwrap()),dialect,+ proper_nouns: proper_nouns_expr(),}}@@ -159,6 +162,9 @@ impl<T: Dictionary> Linter for SpellCheck<T> {}remove_lints_overlapping_expr(&parenthetical_plural_s_expr(), document, &mut lints);+ // Names keep their official spelling in every dialect, such as `Pearl Harbor` in British+ // English. Their capitalization is handled by `ProperNounCapitalizationLinter`.+ remove_lints_overlapping_expr(&self.proper_nouns, document, &mut lints);lints}@@ -218,6 +224,33 @@ mod tests {);}+ #[test]+ fn american_spelling_in_name_in_british_dialect() {+ assert_lint_count(+ "The attack on Pearl Harbor came before Labor Day.",+ SpellCheck::new(FstDictionary::curated(), Dialect::British),+ 0,+ );+ }++ #[test]+ fn british_spelling_in_name_in_american_dialect() {+ assert_lint_count(+ "The Ministry of Defence met the Labour Party.",+ SpellCheck::new(FstDictionary::curated(), Dialect::American),+ 0,+ );+ }++ #[test]+ fn american_spelling_outside_name_in_british_dialect() {+ assert_lint_count(+ "The ship left the harbor near Pearl Harbor.",+ SpellCheck::new(FstDictionary::curated(), Dialect::British),+ 1,+ );+ }+#[test]fn canadian_words_in_australian_dialect() {assert_lint_count(diff --git a/harper-core/tests/text/linters/The Great Gatsby.snap.yml b/harper-core/tests/text/linters/The Great Gatsby.snap.ymlindex f198ecac8..79e4b3d11 100644--- a/harper-core/tests/text/linters/The Great Gatsby.snap.yml+++ b/harper-core/tests/text/linters/The Great Gatsby.snap.yml@@ -3661,17 +3661,6 @@ Suggest:-Lint: Spelling (63 priority)-Message: |- 2492 | some of the rooms. Let’s go to Coney Island, old sport. In my car.”- | ^~~~~ Did you mean to spell `Coney` this way?-Suggest:- - Replace with: “Convey”- - Replace with: “Conley”- - Replace with: “Corey”---Lint: Spelling (63 priority)Message: |2526 | “There’s another little thing,” he said uncertainly, and hesitated.@@ -3709,18 +3698,6 @@ Suggest:-Lint: Spelling (63 priority)-Message: |- 2561 | sleep as I entered my front door. So I don’t know whether or not Gatsby went to- 2562 | Coney Island, or for how many hours he “glanced into rooms” while his house- | ^~~~~ Did you mean to spell `Coney` this way?-Suggest:- - Replace with: “Convey”- - Replace with: “Conley”- - Replace with: “Corey”---Lint: Enhancement (31 priority)Message: |2594 | “Looks very good,” he remarked vaguely. “One of the papers said they thought the