Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
refactor(core): proper noun linters use canonical casing and JSON file
Elijah Potter3154473e3583
diff --git a/harper-core/dictionary.dict b/harper-core/dictionary.dictindex 538614fa2..1ed166762 100644--- a/harper-core/dictionary.dict+++ b/harper-core/dictionary.dict@@ -27614,10 +27614,10 @@ hysterics/1Mi/18USis/4813iOS/2M-iPad/1M-iPhone/1M-iPod/1M-iTunes/M+iPad/1MS+iPhone/1MS+iPod/1MS+iTunes/MSiamb/1MSiambi/1iambic/51SMdiff --git a/harper-core/proper_noun_rules.json b/harper-core/proper_noun_rules.jsonnew file mode 100644index 000000000..a1576b17e--- /dev/null+++ b/harper-core/proper_noun_rules.json@@ -0,0 +1,410 @@+{+ "Americas": {+ "canonical": [+ "South America",+ "North America",+ "Central America"+ ],+ "description": "When referring to North, Central, and South America, make sure to treat them as a proper noun."+ },+ "Australia": {+ "canonical": [+ "Australian Capital Territory",+ "New South Wales",+ "Northern Territory",+ "South Australia",+ "Western Australia",+ "Alice Springs",+ "Gold Coast",+ "Sunshine Coast"+ ],+ "description": "When referring to the states of Australia, make sure to treat them as a proper noun."+ },+ "OceansAndSeas": {+ "canonical": [+ "Atlantic Ocean",+ "Pacific Ocean",+ "Indian Ocean",+ "Southern Ocean",+ "Arctic Ocean",+ "Mediterranean Sea",+ "Caribbean Sea",+ "Baltic Sea",+ "Red Sea",+ "Black Sea",+ "Caspian Sea",+ "Coral Sea",+ "Bering Sea",+ "North Sea",+ "South China Sea"+ ],+ "description": "When referring to the world's oceans and seas, ensure they are treated as proper nouns."+ },+ "Canada": {+ "canonical": [+ "British Columbia",+ "New Brunswick",+ "Northwest Territories",+ "Nova Scotia",+ "Prince Edward Island",+ "Quebec City"+ ],+ "description": "When referring to the provinces of Canada, make sure to treat them as a proper noun."+ },+ "Malaysia": {+ "canonical": [+ "Negeri Sembilan",+ "Alor Setar",+ "George Town",+ "Johor Bahru",+ "Kota Bahru",+ "Kota Kinabalu",+ "Kuala Lumpur",+ "Kuala Terengganu",+ "Shah Alam"+ ],+ "description": "When referring to the states of Malaysia and their capitals, make sure to treat them as a proper noun."+ },+ "Countries": {+ "canonical": [+ "Equatorial Guinea",+ "Papua New Guinea",+ "Cayman Islands",+ "Falkland Islands",+ "Marshall Islands",+ "Solomon Islands",+ "British Virgin Islands",+ "United States Virgin Islands",+ "Northern Mariana Islands",+ "New Caledonia",+ "New Zealand",+ "Northern Cyprus",+ "Northern Ireland",+ "Central African Republic",+ "Czech Republic",+ "Dominican Republic",+ "Saint Helena",+ "Saint Lucia",+ "Saint Martin",+ "South Africa",+ "South Ossetia",+ "South Sudan",+ "American Samoa",+ "Antigua and Barbuda",+ "Bosnia and Herzegovina",+ "Burkina Faso",+ "Cape Verde",+ "Costa Rica",+ "Democratic Republic of the Congo",+ "East Timor",+ "El Salvador",+ "French Polynesia",+ "Guinea-Bissau",+ "Isle of Man",+ "Ivory Coast",+ "North Macedonia",+ "Puerto Rico",+ "São Tomé and Príncipe",+ "Saudi Arabia",+ "Sierra Leone",+ "Sint Maarten",+ "Sri Lanka",+ "Trinidad and Tobago",+ "Western Sahara"+ ],+ "description": "When referring to Countries, make sure to treat it as a proper noun."+ },+ "NationalCapitals": {+ "canonical": [+ "Belize City",+ "Guatemala City",+ "Kuwait City",+ "Mexico City",+ "Panama City",+ "Vatican City",+ "Ho Chi Minh City",+ "Saint Kitts and Nevis",+ "Saint Pierre and Miquelon",+ "Saint Vincent and the Grenadines",+ "San José",+ "San Juan",+ "San Marino",+ "San Salvador",+ "Cape Town",+ "George Town",+ "Abu Dhabi",+ "Addis Ababa",+ "Bandar Seri Begawan",+ "Buenos Aires",+ "Diego Garcia",+ "La Paz",+ "New Delhi",+ "Pago Pago",+ "Phnom Penh",+ "Port Louis",+ "Port Moresby",+ "Port Vila",+ "Port of Spain",+ "Porto-Novo",+ "Santo Domingo",+ "São Tomé",+ "The Bahamas",+ "The Hague"+ ],+ "description": "When referring to national capitals, make sure to treat it as a proper noun."+ },+ "ChineseCommunistParty": {+ "canonical": [+ "Chinese Communist Party"+ ],+ "description": "When referring to the political party, make sure to treat them as a proper noun."+ },+ "UnitedOrganizations": {+ "canonical": [+ "United Nations",+ "United States",+ "United Kingdom",+ "United Airlines",+ "United Arab Emirates"+ ],+ "description": "When referring to national or international organizations, make sure to treat them as a proper noun."+ },+ "Holidays": {+ "canonical": [+ "Absolution Day",+ "Admission Day",+ "Alaska Day",+ "Anzac Day",+ "Arbor Day",+ "Armistice Day",+ "Ascension Day",+ "Australia Day",+ "Bastille Day",+ "Boxing Day",+ "Canada Day",+ "Christmas Day",+ "Columbus Day",+ "Commonwealth Day",+ "Darwin Day",+ "Discovery Day",+ "Dominion Day",+ "Earth Day",+ "Easter Day",+ "Election Day",+ "Emancipation Day",+ "Empire Day",+ "Father's Day",+ "Flag Day",+ "Freedom Day",+ "Galentine's Day",+ "Groundhog Day",+ "Halloween",+ "Independence Day",+ "Jamhuri Day",+ "Jubilee Day",+ "Kamehameha Day",+ "Kenyatta Day",+ "Labor Day",+ "Labour Day",+ "Madaraka Day",+ "Mashujaa Day",+ "May Day",+ "Memorial Day",+ "Merdeka Day",+ "Mother's Day",+ "National Freedom Day",+ "New Year's Day",+ "Patrick's Day",+ "Presidents' Day",+ "Remembrance Day",+ "Republic Day",+ "Rizal Day",+ "Thanksgiving Day",+ "Ulster Day",+ "Valentine's Day",+ "Veterans Day",+ "Victoria Day",+ "Victory Day",+ "Waitangi Day",+ "Wattle Day",+ "Year's Day",+ "Youth Day",+ "Black Friday",+ "Cyber Monday"+ ],+ "description": "When referring to holidays, make sure to treat them as a proper noun."+ },+ "Koreas": {+ "canonical": [+ "South Korea",+ "North Korea"+ ],+ "description": "When referring to the nations, make sure to treat them as a proper noun."+ },+ "AmazonNames": {+ "canonical": [+ "Amazon Shopping",+ "Amazon Web Services",+ "Amazon Lambda",+ "Amazon RDS",+ "Amazon DynamoDB",+ "Amazon SageMaker",+ "Amazon Rekognition",+ "Amazon CloudFront",+ "Amazon ECS",+ "Amazon EKS",+ "Amazon CloudWatch",+ "Amazon IAM",+ "Amazon Prime",+ "Amazon Kindle"+ ],+ "description": "When referring to the various products of Amazon.com, make sure to treat them as a proper noun."+ },+ "GoogleNames": {+ "canonical": [+ "Google Search",+ "Google Cloud",+ "Google Maps",+ "Google Docs",+ "Google Sheets",+ "Google Slides",+ "Google Drive",+ "Google Meet",+ "Google Gmail",+ "Google Calendar",+ "Google Chrome",+ "Google ChromeOS",+ "Google Android",+ "Google Play",+ "Google Bard",+ "Google Gemini",+ "Google YouTube",+ "Google Photos",+ "Google Analytics",+ "Google AdSense",+ "Google Pixel",+ "Google Nest",+ "Google Workspace"+ ],+ "description": "When referring to Google products and services, make sure to treat them as proper nouns."+ },+ "AzureNames": {+ "canonical": [+ "Azure DevOps",+ "Azure Functions",+ "Azure Cosmos DB",+ "Azure SQL Database",+ "Azure Kubernetes Service",+ "Azure Virtual Machines",+ "Azure Monitor",+ "Azure Storage",+ "Azure Active Directory",+ "Azure App Service",+ "Azure Key Vault",+ "Azure Cognitive Services",+ "Azure Service Bus",+ "Azure Event Hub"+ ],+ "description": "When referring to Azure cloud services, make sure to treat them as proper nouns."+ },+ "MicrosoftNames": {+ "canonical": [+ "Microsoft Windows",+ "Microsoft Office",+ "Microsoft Teams",+ "Microsoft Excel",+ "Microsoft PowerPoint",+ "Microsoft Word",+ "Microsoft Outlook",+ "Microsoft OneDrive",+ "Microsoft SharePoint",+ "Microsoft Xbox",+ "Microsoft Surface",+ "Microsoft Edge",+ "Microsoft Bing",+ "Microsoft Dynamics",+ "Microsoft Visual Studio"+ ],+ "description": "When referring to Microsoft products and services, make sure to treat them as proper nouns."+ },+ "AppleNames": {+ "canonical": [+ "Apple iPhone",+ "Apple iPad",+ "Apple iMac",+ "Apple MacBook",+ "Apple Watch",+ "Apple TV",+ "Apple Music",+ "Apple Arcade",+ "Apple iCloud",+ "Apple Safari",+ "Apple HomeKit",+ "Apple CarPlay",+ "Apple MacBook Pro",+ "Apple MacBook Air",+ "Apple Mac Pro",+ "Apple Mac Mini",+ "Apple AirPods",+ "Apple AirPods Pro",+ "Apple AirPods Max",+ "Apple Vision Pro"+ ],+ "description": "When referring to Apple products and services, make sure to treat them as proper nouns."+ },+ "MetaNames": {+ "canonical": [+ "Meta Oculus",+ "Meta Portals",+ "Meta Quest",+ "Meta Gaming",+ "Meta Horizon",+ "Meta Reality Labs"+ ],+ "description": "When referring to Meta products and services, make sure to treat them as proper nouns."+ },+ "JetpackNames": {+ "canonical": [+ "Jetpack VaultPress Backup",+ "Jetpack VaultPress",+ "Jetpack Scan",+ "Jetpack Akismet Anti-spam",+ "Jetpack Stats",+ "Jetpack Social",+ "Jetpack Blaze",+ "Jetpack AI Assistant",+ "Jetpack Site Search",+ "Jetpack Boost",+ "Jetpack VideoPress",+ "Jetpack For Agencies",+ "Jetpack CRM"+ ],+ "description": "Ensure proper capitalization of Jetpack-related terms."+ },+ "TumblrNames": {+ "canonical": [+ "Tumblr Blaze",+ "Tumblr Pro",+ "Tumblr Live",+ "Tumblr Ads",+ "Tumblr Communities",+ "Tumblr Shop",+ "Tumblr Dashboard"+ ],+ "description": "Ensure proper capitalization of Tumblr-related terms."+ },+ "PocketCastsNames": {+ "canonical": [+ "Pocket Casts",+ "Pocket Casts Plus"+ ],+ "description": "Ensure proper capitalization of Pocket Casts and Pocket Casts Plus as brand names."+ },+ "DayOneNames": {+ "canonical": [+ "Day One",+ "Day One Premium"+ ],+ "description": "Ensure proper capitalization of Day One and Day One Premium as brand names."+ }+}diff --git a/harper-core/src/linting/proper_noun_capitalization_linters.rs b/harper-core/src/linting/proper_noun_capitalization_linters.rsindex 004318dfa..352e898d8 100644--- a/harper-core/src/linting/proper_noun_capitalization_linters.rs+++ b/harper-core/src/linting/proper_noun_capitalization_linters.rs@@ -1,47 +1,117 @@+use hashbrown::HashMap;+use serde::{Deserialize, Serialize};+use super::{Lint, LintKind, Suggestion};use super::{LintGroup, PatternLinter};-use crate::patterns::{EitherPattern, IsNotTitleCase, Pattern, SequencePattern, WordSet};-use crate::{Dictionary, make_title_case};+use crate::parsers::PlainEnglish;+use crate::patterns::{AnyCapitalization, Pattern, PatternMap, SequencePattern};+use crate::{Dictionary, Document, TokenKind};use crate::{Token, TokenStringExt};use std::sync::Arc;+/// A linter that corrects the capitalization of multi-word proper nouns.+/// They are corrected to a "canonical capitalization" provided at construction.+///+/// If you would like to add a proper noun to Harper, see `proper_noun_rules.json`.pub struct ProperNounCapitalizationLinter<D: Dictionary + 'static> {- pattern: Box<dyn Pattern>,+ pattern_map: PatternMap<Document>,description: String,dictionary: Arc<D>,}impl<D: Dictionary + 'static> ProperNounCapitalizationLinter<D> {+ /// Wrapper function around [`Self::new`] that allows construction with Strings.+ pub fn new_strs(+ canonical_versions: impl IntoIterator<Item = impl AsRef<str>>,+ description: impl ToString,+ dictionary: D,+ ) -> Self {+ Self::new(+ canonical_versions+ .into_iter()+ .map(|s| s.as_ref().chars().collect::<Vec<_>>()),+ description,+ dictionary,+ )+ }++ /// Create a linter that corrects the capitalization of phrases provided.pub fn new(- search_for: impl Pattern + 'static,+ canonical_versions: impl IntoIterator<Item = impl AsRef<[char]>>,description: impl ToString,dictionary: D,) -> Self {let dictionary = Arc::new(dictionary);+ let mut pattern_map = PatternMap::default();++ for can_vers in canonical_versions {+ let (pattern, document) = Self::create_pattern(can_vers.as_ref().to_vec(), &dictionary);+ pattern_map.insert(pattern, document);+ }+Self {- pattern: Box::new(IsNotTitleCase::new(- Box::new(search_for),- dictionary.clone(),- )),+ pattern_map,dictionary: dictionary.clone(),description: description.to_string(),}}++ /// Given the canonical casing of a phrase, create a [`Pattern`] that matches+ /// _incorrect_ casing of that phrase.+ fn create_pattern(+ canonical_case: Vec<char>,+ dictionary: &impl Dictionary,+ ) -> (SequencePattern, Document) {+ // Split into tokens.+ let doc = Document::new_from_vec(canonical_case.into(), &PlainEnglish, dictionary);++ let mut pattern = SequencePattern::default();++ for token in doc.tokens() {+ match token.kind {+ TokenKind::Word(_) => {+ let word_canon_chars = doc.get_span_content(token.span);++ pattern = pattern.then(AnyCapitalization::new(word_canon_chars.into()));+ }+ TokenKind::Space(_) | TokenKind::Newline(_) => pattern = pattern.then_whitespace(),+ _ => (),+ }+ }++ (pattern, doc)+ }}impl<D: Dictionary + 'static> PatternLinter for ProperNounCapitalizationLinter<D> {fn pattern(&self) -> &dyn Pattern {- self.pattern.as_ref()+ &self.pattern_map}fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Option<Lint> {- let proper = make_title_case(matched_tokens, source, &self.dictionary);+ let canonical_case = self.pattern_map.lookup(matched_tokens, source).unwrap();++ let mut broken = false;++ for (err_token, correct_token) in matched_tokens.iter().zip(canonical_case.fat_tokens()) {+ let err_chars = err_token.span.get_content(source);+ if err_chars != correct_token.content {+ broken = true;+ break;+ }+ }++ if !broken {+ return None;+ }Some(Lint {span: matched_tokens.span()?,lint_kind: LintKind::Capitalization,- suggestions: vec![Suggestion::ReplaceWith(proper)],+ suggestions: vec![Suggestion::ReplaceWith(+ canonical_case.get_source().to_vec(),+ )],message: self.description.to_string(),priority: 31,})@@ -52,1190 +122,39 @@ impl<D: Dictionary + 'static> PatternLinter for ProperNounCapitalizationLinter<D}}-pub fn lint_group(dictionary: Arc<impl Dictionary + 'static>) -> LintGroup {- let mut group = LintGroup::empty();-- group.add(- "Americas",- Box::new(ProperNounCapitalizationLinter::new(- SequencePattern::default()- .then(WordSet::new(&["South", "North", "Central"]))- .then_whitespace()- .t_aco("America"),- "When referring to North, Central, and South America, make sure to treat them as a proper noun.",- dictionary.clone()))-);-- group.add(- "Australia",- Box::new(ProperNounCapitalizationLinter::new(- EitherPattern::new(vec![- // the states and territories- Box::new(- SequencePattern::default()- .t_aco("Australian")- .then_whitespace()- .t_aco("Capital")- .then_whitespace()- .t_aco("Territory"),- ),- Box::new(- SequencePattern::default()- .t_aco("New")- .then_whitespace()- .t_aco("South")- .then_whitespace()- .t_aco("Wales"),- ),- Box::new(- SequencePattern::default()- .t_aco("Northern")- .then_whitespace()- .t_aco("Territory"),- ),- Box::new(- SequencePattern::default()- .t_aco("South")- .then_whitespace()- .t_aco("Australia"),- ),- Box::new(- SequencePattern::default()- .t_aco("Western")- .then_whitespace()- .t_aco("Australia"),- ),- // major cities- Box::new(- SequencePattern::default()- .t_aco("Alice")- .then_whitespace()- .t_aco("Springs"),- ),- Box::new(- SequencePattern::default()- .t_aco("Gold")- .then_whitespace()- .t_aco("Coast"),- ),- Box::new(- SequencePattern::default()- .t_aco("Sunshine")- .then_whitespace()- .t_aco("Coast"),- ),- ]),- "When referring to the states of Australia, make sure to treat them as a proper noun.",- dictionary.clone(),- )),- );-- group.add(- "OceansAndSeas",- Box::new(ProperNounCapitalizationLinter::new(- EitherPattern::new(vec![- Box::new(- SequencePattern::default()- .then(WordSet::new(&[- "Atlantic",- "Pacific",- "Indian",- "Southern",- "Arctic",- ]))- .then_whitespace()- .t_aco("Ocean")- ),- Box::new(- SequencePattern::default()- .then(WordSet::new(&[- "Mediterranean",- "Caribbean",- "Baltic",- "Red",- "Black",- "Caspian",- "Coral",- "Bering",- "North",- ]))- .then_whitespace()- .t_aco("Sea")- ),- Box::new(- SequencePattern::default()- .t_aco("South")- .then_whitespace()- .t_aco("China")- .then_whitespace()- .t_aco("Sea")- ),- ]),- "When referring to the world's oceans and seas, ensure they are treated as proper nouns.",- dictionary.clone()- ))-);-- group.add(- "Canada",- Box::new(ProperNounCapitalizationLinter::new(- EitherPattern::new(vec![- // the provinces and territories- Box::new(- SequencePattern::default()- .t_aco("British")- .then_whitespace()- .t_aco("Columbia"),- ),- Box::new(- SequencePattern::default()- .t_aco("New")- .then_whitespace()- .t_aco("Brunswick"),- ),- Box::new(- SequencePattern::default()- .t_aco("Northwest")- .then_whitespace()- .t_aco("Territories"),- ),- Box::new(- SequencePattern::default()- .t_aco("Nova")- .then_whitespace()- .t_aco("Scotia"),- ),- Box::new(- SequencePattern::default()- .t_aco("Prince")- .then_whitespace()- .t_aco("Edward")- .then_whitespace()- .t_aco("Island"),- ),- // major cities- Box::new(- SequencePattern::default()- .t_aco("Quebec")- .then_whitespace()- .t_aco("City"),- ),- ]),- "When referring to the provinces of Canada, make sure to treat them as a proper noun.",- dictionary.clone(),- )),- );-- group.add(- "Koreas",- Box::new(ProperNounCapitalizationLinter::new(- SequencePattern::default()- .then(WordSet::new(&["South", "North"]))- .then_whitespace()- .t_aco("Korea"),- "When referring to the nations, make sure to treat them as a proper noun.",- dictionary.clone(),- )),- );-- group.add(- "Malaysia",- Box::new(ProperNounCapitalizationLinter::new(- EitherPattern::new(vec![- // multi-word states- Box::new(SequencePattern::default()- .t_aco("Negeri")- .then_whitespace()- .t_aco("Sembilan")- ),- // multi-word state capitals- Box::new(SequencePattern::default()- .t_aco("Alor")- .then_whitespace()- .t_aco("Setar")- ),- Box::new(SequencePattern::default()- .t_aco("George")- .then_whitespace()- .t_aco("Town")- ),- Box::new(SequencePattern::default()- .then(EitherPattern::new(vec![- Box::new(WordSet::new(&[- "Johor",- "Kota"- ])),- ]))- .then_whitespace()- .t_aco("Bahru")- ),- Box::new(SequencePattern::default()- .t_aco("Kota")- .then_whitespace()- .t_aco("Kinabalu")- ),- Box::new(SequencePattern::default()- .t_aco("Kuala")- .then_whitespace()- .then(EitherPattern::new(vec![- Box::new(WordSet::new(&[- "Lumpur",- "Terengganu"- ])),- ]))- ),- Box::new(SequencePattern::default()- .t_aco("Shah")- .then_whitespace()- .t_aco("Alam")- )- ]),- "When referring to the states of Malaysia and their capitals, make sure to treat them as a proper noun.",- dictionary.clone()))-);-- group.add(- "Countries",- Box::new(ProperNounCapitalizationLinter::new(- EitherPattern::new(vec![- // Grouped country names- // ... Guinea- Box::new(- SequencePattern::default()- .then(EitherPattern::new(vec![- Box::new(SequencePattern::aco("Equatorial")),- Box::new(SequencePattern::aco("Papua").then_whitespace().t_aco("New")),- ]))- .then_whitespace()- .t_aco("Guinea"),- ),- // ... Islands- Box::new(- SequencePattern::default()- .then(EitherPattern::new(vec![- Box::new(WordSet::new(&["Cayman", "Falkland", "Marshall", "Solomon"])),- Box::new(- SequencePattern::default()- .then(EitherPattern::new(vec![- Box::new(SequencePattern::aco("British")),- Box::new(- SequencePattern::aco("United")- .then_whitespace()- .t_aco("States"),- ),- ]))- .then_whitespace()- .t_aco("Virgin"),- ),- Box::new(- SequencePattern::aco("Northern")- .then_whitespace()- .t_aco("Mariana"),- ),- ]))- .then_whitespace()- .t_aco("Islands"),- ),- // New ...- Box::new(- SequencePattern::aco("New")- .then_whitespace()- .then(WordSet::new(&["Caledonia", "Zealand"])),- ),- // Northern ...- Box::new(- SequencePattern::aco("Northern")- .then_whitespace()- .then(WordSet::new(&["Cyprus", "Ireland"])),- ),- // ... Republic- Box::new(- SequencePattern::default()- .then(EitherPattern::new(vec![- Box::new(- SequencePattern::aco("Central")- .then_whitespace()- .t_aco("African"),- ),- Box::new(WordSet::new(&["Czech", "Dominican"])),- ]))- .then_whitespace()- .t_aco("Republic"),- ),- // Saint ...- Box::new(- SequencePattern::aco("Saint")- .then_whitespace()- .then(WordSet::new(&["Helena", "Lucia", "Martin"])),- ),- // South ...- Box::new(- SequencePattern::aco("South")- .then_whitespace()- .then(WordSet::new(&["Africa", "Ossetia", "Sudan"])),- ),- // South Korea is under "Koreas"- // One-off country names- Box::new(- SequencePattern::aco("American")- .then_whitespace()- .t_aco("Samoa"),- ),- Box::new(- SequencePattern::aco("Antigua")- .then_whitespace()- .t_aco("and")- .then_whitespace()- .t_aco("Barbuda"),- ),- // United Arab Emirates is under "United Organizations"- Box::new(- SequencePattern::aco("Bosnia")- .then_whitespace()- .t_aco("and")- .then_whitespace()- .t_aco("Herzegovina"),- ),- Box::new(- SequencePattern::aco("Burkina")- .then_whitespace()- .t_aco("Faso"),- ),- Box::new(- SequencePattern::aco("Cape")- .then_whitespace()- .t_aco("Verde"),- ),- Box::new(- SequencePattern::aco("Costa")- .then_whitespace()- .t_aco("Rica"),- ),- Box::new(- SequencePattern::aco("Democratic")- .then_whitespace()- .t_aco("Republic")- .then_whitespace()- .t_aco("of")- .then_whitespace()- .t_aco("the")- .then_whitespace()- .t_aco("Congo"),- ),- Box::new(- SequencePattern::aco("East")- .then_whitespace()- .t_aco("Timor"),- ),- Box::new(- SequencePattern::aco("El")- .then_whitespace()- .t_aco("Salvador"),- ),- Box::new(- SequencePattern::aco("French")- .then_whitespace()- .t_aco("Polynesia"),- ),- Box::new(SequencePattern::aco("Guinea").then_hyphen().t_aco("Bissau")),- Box::new(- SequencePattern::aco("Isle")- .then_whitespace()- .t_aco("of")- .then_whitespace()- .t_aco("Man"),- ),- Box::new(- SequencePattern::aco("Ivory")- .then_whitespace()- .t_aco("Coast"),- ),- Box::new(- SequencePattern::aco("North")- .then_whitespace()- .t_aco("Macedonia"),- ),- Box::new(- SequencePattern::aco("Puerto")- .then_whitespace()- .t_aco("Rico"),- ),- Box::new(- SequencePattern::aco("São")- .then_whitespace()- .t_aco("Tomé")- .then_whitespace()- .t_aco("and")- .then_whitespace()- .t_aco("Príncipe"),- ),- Box::new(- SequencePattern::aco("Saudi")- .then_whitespace()- .t_aco("Arabia"),- ),- Box::new(- SequencePattern::aco("Sierra")- .then_whitespace()- .t_aco("Leone"),- ),- Box::new(- SequencePattern::aco("Sint")- .then_whitespace()- .t_aco("Maarten"),- ),- Box::new(SequencePattern::aco("Sri").then_whitespace().t_aco("Lanka")),- Box::new(- SequencePattern::aco("Trinidad")- .then_whitespace()- .t_aco("and")- .then_whitespace()- .t_aco("Tobago"),- ),- Box::new(- SequencePattern::aco("Western")- .then_whitespace()- .t_aco("Sahara"),- ),- ]),- "When referring to Countries, make sure to treat it as a proper noun.",- dictionary.clone(),- )),- );-- group.add(- "NationalCapitals",- Box::new(ProperNounCapitalizationLinter::new(- EitherPattern::new(vec![- // Grouped capital names- // ... City- Box::new(- SequencePattern::default()- .then(EitherPattern::new(vec![- Box::new(WordSet::new(&[- "Belize",- "Guatemala",- "Kuwait",- "Mexico",- "Panama",- "Vatican",- ])),- Box::new(- SequencePattern::aco("Ho")- .then_whitespace()- .t_aco("Chi")- .then_whitespace()- .t_aco("Minh"),- ),- ]))- .then_whitespace()- .t_aco("City"),- ),- // Saint ...- Box::new(- SequencePattern::aco("Saint")- .then_whitespace()- .then(EitherPattern::new(vec![- Box::new(- SequencePattern::aco("Kitts")- .then_whitespace()- .t_aco("and")- .then_whitespace()- .t_aco("Nevis"),- ),- Box::new(- SequencePattern::aco("Pierre")- .then_whitespace()- .t_aco("and")- .then_whitespace()- .t_aco("Miquelon"),- ),- Box::new(- SequencePattern::aco("Vincent")- .then_whitespace()- .t_aco("and")- .then_whitespace()- .t_aco("the")- .then_whitespace()- .t_aco("Grenadines"),- ),- ])),- ),- // San ...- Box::new(- SequencePattern::aco("San")- .then_whitespace()- .then(WordSet::new(&["José", "Juan", "Marino", "Salvador"])),- ),- // St. ... TODO the period should be optional but this doesn't match even when it's not optional- // Box::new(- // SequencePattern::aco("St")- // .then_period()- // .then_whitespace()- // .then(Box::new(WordSet::new(&["Helier", "John's", "Pierre"])))- // ),- // ... Town- Box::new(- SequencePattern::default()- .then(WordSet::new(&[- "Cape", "George", // Cayman Islands- ]))- .then_whitespace()- .t_aco("Town"),- ),- // One-off capital names- Box::new(SequencePattern::aco("Abu").then_whitespace().t_aco("Dhabi")),- Box::new(- SequencePattern::aco("Addis")- .then_whitespace()- .t_aco("Ababa"),- ),- // Andorra la Vella can't be done here because "la" must not be capitalized- Box::new(- SequencePattern::aco("Bandar")- .then_whitespace()- .t_aco("Seri")- .then_whitespace()- .t_aco("Begawan"),- ),- Box::new(- SequencePattern::aco("Buenos")- .then_whitespace()- .t_aco("Aires"),- ),- // Dar es Salaam can't be done here because "es" must not be capitalized- Box::new(- SequencePattern::aco("Diego")- .then_whitespace()- .t_aco("Garcia"),- ),- // Kuala Lumpur is under "Malaysia"- Box::new(SequencePattern::aco("La").then_whitespace().t_aco("Paz")),- Box::new(SequencePattern::aco("New").then_whitespace().t_aco("Delhi")),- Box::new(SequencePattern::aco("Pago").then_whitespace().t_aco("Pago")),- Box::new(- SequencePattern::aco("Phnom")- .then_whitespace()- .t_aco("Penh"),- ),- // Port-au-Prince can't be done here because "au" must not be capitalized- Box::new(- SequencePattern::aco("Port")- .then_whitespace()- .then(EitherPattern::new(vec![- Box::new(WordSet::new(&["Louis", "Moresby", "Vila"])),- Box::new(SequencePattern::aco("of").then_whitespace().t_aco("Spain")),- ])),- ),- Box::new(SequencePattern::aco("Porto").then_hyphen().t_aco("Novo")),- Box::new(- SequencePattern::aco("Santo")- .then_whitespace()- .t_aco("Domingo"),- ),- Box::new(SequencePattern::aco("São").then_whitespace().t_aco("Tomé")),- Box::new(- SequencePattern::aco("The")- .then_whitespace()- .then(WordSet::new(&["Bahamas", "Hague"])),- ),- ]),- "When referring to national capitals, make sure to treat it as a proper noun.",- dictionary.clone(),- )),- );-- group.add(- "ChineseCommunistParty",- Box::new(ProperNounCapitalizationLinter::new(- SequencePattern::aco("Chinese")- .then_whitespace()- .t_aco("Communist")- .then_whitespace()- .t_aco("Party"),- "When referring to the political party, make sure to treat them as a proper noun.",- dictionary.clone(),- )),- );-- group.add(- "UnitedOrganizations",- Box::new(ProperNounCapitalizationLinter::new(- SequencePattern::default()- .t_aco("United")- .then_whitespace()- .then(EitherPattern::new(vec![- Box::new(SequencePattern::aco("Nations")),- Box::new(SequencePattern::aco("States")),- Box::new(SequencePattern::aco("Kingdom")),- Box::new(SequencePattern::aco("Airlines")),- Box::new(- SequencePattern::default()- .t_aco("Arab")- .then_whitespace()- .t_aco("Emirates")- )- ])),- "When referring to national or international organizations, make sure to treat them as a proper noun.",- dictionary.clone()))-);-- group.add(- "Holidays",- Box::new(ProperNounCapitalizationLinter::new(- EitherPattern::new(vec![- Box::new(- SequencePattern::default()- .then(EitherPattern::new(vec![- Box::new(WordSet::new(&[- "Absolution",- "Admission",- "Alaska",- "Anzac",- "ANZAC",- "Arbor",- "Armistice",- "Ascension",- "Australia",- "Ayurveda",- "Bastille",- "Bonifacio",- "Boxing",- "Canada",- "Career",- "Chewidden",- "Christmas",- "Class",- "Columbus",- "Commonwealth",- "D",- "Darwin",- "Discovery",- "Distaff",- "Dominion",- "Earth",- "Easter",- "Election",- "Emancipation",- "Empire",- "Evolution",- "Family",- "Father's",- "Fathers'",- "Flag",- "Forefathers'",- "Foundation",- "Freedom",- "Galentine's",- "Groundhog",- "Gypsy",- "Halloween",- "Independence",- "Invasion",- "Ivy",- "Jamhuri",- "Jubilee",- "Kamehameha",- "Kenyatta",- "Labor",- "Labour",- "Lady",- "Land",- "Lei",- "Madaraka",- "Mashujaa",- "May",- "Memorial",- "Merdeka",- "Midsummer",- "Midsummer's",- "Mother's",- "Mothers'",- "Nakba",- "Nevada",- "Occupation",- "Parents",- "Patrick's",- "Patriots'",- "Pi",- "Picrous",- "Pioneer",- "Presidents'",- "Remembrance",- "Republic",- "Restoration",- "Rizal",- "Roc",- "Rock",- "Seward's",- "Singles'",- "Statehood",- "Tax",- "Thanksgiving",- "Treason",- "Ulster",- "Valentine's",- "VE",- "VJ",- "VP",- "Veterans",- "Victoria",- "Victory",- "Waffle",- "Waitangi",- "Wattle",- "White",- "Wren",- "Years",- "Year's",- "Youth",- ])),- Box::new(- SequencePattern::default()- .t_aco("National")- .then_whitespace()- .t_aco("Freedom"),- ),- Box::new(- SequencePattern::default()- .t_aco("All")- .then_whitespace()- .t_aco("Saints"),- ),- Box::new(- SequencePattern::default()- .t_aco("All")- .then_whitespace()- .t_aco("Souls"),- ),- ]))- .then_whitespace()- .t_aco("Day"),- ),- Box::new(- SequencePattern::default()- .t_aco("Black")- .then_whitespace()- .t_aco("Friday"),- ),- Box::new(- SequencePattern::default()- .t_aco("Cyber")- .then_whitespace()- .t_aco("Monday"),- ),- ]),- "When referring to holidays, make sure to treat them as a proper noun.",- dictionary.clone(),- )),- );-- group.add(- "AmazonNames",- Box::new(ProperNounCapitalizationLinter::new(- SequencePattern::default()- .t_aco("Amazon")- .then_whitespace()- .then(EitherPattern::new(vec![- Box::new(- SequencePattern::default()- .t_aco("Shopping")- ),- Box::new(- SequencePattern::default()- .t_aco("Web")- .then_whitespace()- .t_aco("Services")- ),- Box::new(- SequencePattern::default()- .t_aco("Lambda")- ),- Box::new(- SequencePattern::default()- .t_aco("RDS")- ),- Box::new(- SequencePattern::default()- .t_aco("DynamoDB")- ),- Box::new(- SequencePattern::default()- .t_aco("SageMaker")- ),- Box::new(- SequencePattern::default()- .t_aco("Rekognition")- ),- Box::new(- SequencePattern::default()- .t_aco("CloudFront")- ),- Box::new(- SequencePattern::default()- .t_aco("ECS")- ),- Box::new(- SequencePattern::default()- .t_aco("EKS")- ),- Box::new(- SequencePattern::default()- .t_aco("CloudWatch")- ),- Box::new(- SequencePattern::default()- .t_aco("IAM")- ),- Box::new(- SequencePattern::default()- .t_aco("Prime")- ),- Box::new(- SequencePattern::default()- .t_aco("Kindle")- )- ])),- "When referring to the various products of Amazon.com, make sure to treat them as a proper noun.",- dictionary.clone()))-);-- group.add(- "GoogleNames",- Box::new(ProperNounCapitalizationLinter::new(- SequencePattern::default()- .t_aco("Google")- .then_whitespace()- .then(WordSet::new(&[- "Search",- "Cloud",- "Maps",- "Docs",- "Sheets",- "Slides",- "Drive",- "Meet",- "Gmail",- "Calendar",- "Chrome",- "ChromeOS",- "Android",- "Play",- "Bard",- "Gemini",- "YouTube",- "Photos",- "Analytics",- "AdSense",- "Pixel",- "Nest",- "Workspace",- ])),- "When referring to Google products and services, make sure to treat them as proper nouns."- ,dictionary.clone()))- );-- group.add(- "AzureNames",- Box::new(ProperNounCapitalizationLinter::new(- SequencePattern::default()- .t_aco("Azure")- .then_whitespace()- .then(EitherPattern::new(vec![- Box::new(SequencePattern::aco("DevOps")),- Box::new(SequencePattern::aco("Functions")),- Box::new(- SequencePattern::default()- .t_aco("Cosmos")- .then_whitespace()- .t_aco("DB"),- ),- Box::new(- SequencePattern::default()- .t_aco("SQL")- .then_whitespace()- .t_aco("Database"),- ),- Box::new(- SequencePattern::default()- .t_aco("Kubernetes")- .then_whitespace()- .t_aco("Service"),- ),- Box::new(- SequencePattern::default()- .t_aco("Virtual")- .then_whitespace()- .t_aco("Machines"),- ),- Box::new(SequencePattern::aco("Monitor")),- Box::new(SequencePattern::aco("Storage")),- Box::new(- SequencePattern::default()- .t_aco("Active")- .then_whitespace()- .t_aco("Directory"),- ),- Box::new(- SequencePattern::default()- .t_aco("App")- .then_whitespace()- .t_aco("Service"),- ),- Box::new(- SequencePattern::default()- .t_aco("Key")- .then_whitespace()- .t_aco("Vault"),- ),- Box::new(- SequencePattern::default()- .t_aco("Cognitive")- .then_whitespace()- .t_aco("Services"),- ),- Box::new(- SequencePattern::default()- .t_aco("Service")- .then_whitespace()- .t_aco("Bus"),- ),- Box::new(- SequencePattern::default()- .t_aco("Event")- .then_whitespace()- .t_aco("Hub"),- ),- ])),- "When referring to Azure cloud services, make sure to treat them as proper nouns.",- dictionary.clone(),- )),- );-- group.add(- "MicrosoftNames",- Box::new(ProperNounCapitalizationLinter::new(- SequencePattern::default()- .t_aco("Microsoft")- .then_whitespace()- .then(EitherPattern::new(vec![- Box::new(WordSet::new(&[- "Windows",- "Office",- "Teams",- "Excel",- "PowerPoint",- "Word",- "Outlook",- "OneDrive",- "SharePoint",- "Xbox",- "Surface",- "Edge",- "Bing",- "Dynamics",- ])),- Box::new(- SequencePattern::default()- .t_aco("Visual")- .then_whitespace()- .t_aco("Studio")- )- ])),- "When referring to Microsoft products and services, make sure to treat them as proper nouns.",- dictionary.clone()))-);-- group.add(- "AppleNames",- Box::new(ProperNounCapitalizationLinter::new(- SequencePattern::default()- .t_aco("Apple")- .then_whitespace()- .then(EitherPattern::new(vec![- Box::new(WordSet::new(&[- "iPhone", "iPad", "iMac", "MacBook", "Watch", "TV", "Music", "Arcade",- "iCloud", "Safari", "HomeKit", "CarPlay",- ])),- Box::new(- SequencePattern::aco("MacBook")- .then_whitespace()- .t_aco("Pro")- ),- Box::new(- SequencePattern::aco("MacBook")- .then_whitespace()- .t_aco("Air")- ),- Box::new(SequencePattern::aco("Mac").then_whitespace().t_aco("Pro")),- Box::new(SequencePattern::aco("Mac").then_whitespace().t_aco("Mini")),- Box::new(SequencePattern::aco("AirPods")),- Box::new(- SequencePattern::aco("AirPods")- .then_whitespace()- .t_aco("Pro")- ),- Box::new(- SequencePattern::aco("AirPods")- .then_whitespace()- .t_aco("Max")- ),- Box::new(- SequencePattern::default()- .t_aco("Vision")- .then_whitespace()- .t_aco("Pro")- )- ])),- "When referring to Apple products and services, make sure to treat them as proper nouns.",- dictionary.clone()))-- );-- group.add(- "MetaNames",- Box::new(ProperNounCapitalizationLinter::new(SequencePattern::aco("Meta")- .then_whitespace()- .then(EitherPattern::new(vec![- Box::new(WordSet::new(&[- "Oculus", "Portals", "Quest", "Gaming", "Horizon",- ])),- Box::new(- SequencePattern::default()- .t_aco("Reality")- .then_whitespace()- .t_aco("Labs")- ),- ])),- "When referring to Meta products and services, make sure to treat them as proper nouns."- , dictionary.clone()- ))- );-- group.add(- "JetpackNames",- Box::new(ProperNounCapitalizationLinter::new(- SequencePattern::default()- .t_aco("Jetpack")- .then_whitespace()- .then(EitherPattern::new(vec![- Box::new(- SequencePattern::default()- .t_aco("VaultPress")- .then_whitespace()- .t_aco("Backup"),- ),- Box::new(SequencePattern::default().t_aco("VaultPress")),- Box::new(SequencePattern::default().t_aco("Scan")),- Box::new(- SequencePattern::default()- .t_aco("Akismet")- .then_whitespace()- .t_aco("Anti-spam"),- ),- Box::new(SequencePattern::default().t_aco("Stats")),- Box::new(SequencePattern::default().t_aco("Social")),- Box::new(SequencePattern::default().t_aco("Blaze")),- Box::new(- SequencePattern::default()- .t_aco("AI")- .then_whitespace()- .t_aco("Assistant"),- ),- Box::new(- SequencePattern::default()- .t_aco("Site")- .then_whitespace()- .t_aco("Search"),- ),- Box::new(SequencePattern::default().t_aco("Boost")),- Box::new(SequencePattern::default().t_aco("VideoPress")),- Box::new(- SequencePattern::default()- .t_aco("For")- .then_whitespace()- .t_aco("Agencies"),- ),- Box::new(SequencePattern::default().t_aco("CRM")),- ])),- "Ensure proper capitalization of Jetpack-related terms.",- dictionary.clone(),- )),- );+#[derive(Serialize, Deserialize)]+struct RuleEntry {+ canonical: Vec<String>,+ description: String,+}- group.add(- "TumblrNames",- Box::new(ProperNounCapitalizationLinter::new(- SequencePattern::default()- .t_aco("Tumblr")- .then_whitespace()- .then(EitherPattern::new(vec![- Box::new(SequencePattern::default().t_aco("Blaze")),- Box::new(SequencePattern::default().t_aco("Pro")),- Box::new(SequencePattern::default().t_aco("Live")),- Box::new(SequencePattern::default().t_aco("Ads")),- Box::new(SequencePattern::default().t_aco("Communities")),- Box::new(SequencePattern::default().t_aco("Shop")),- Box::new(SequencePattern::default().t_aco("Dashboard")),- ])),- "Ensure proper capitalization of Tumblr-related terms.",- dictionary.clone(),- )),- );+/// For the time being, this panics on invalid JSON.+/// Do not use with user provided JSON.+fn lint_group_from_json(json: &str, dictionary: Arc<impl Dictionary + 'static>) -> LintGroup {+ let mut group = LintGroup::empty();- group.add(- "PocketCastsNames",- Box::new(ProperNounCapitalizationLinter::new(- EitherPattern::new(vec![- Box::new(- SequencePattern::default()- .t_aco("Pocket")- .then_whitespace()- .t_aco("Casts"),- ),- Box::new(- SequencePattern::default()- .t_aco("Pocket")- .then_whitespace()- .t_aco("Casts")- .then_whitespace()- .t_aco("Plus"),- ),- ]),- "Ensure proper capitalization of Pocket Casts and Pocket Casts Plus as brand names.",- dictionary.clone(),- )),- );+ let rules: HashMap<String, RuleEntry> = serde_json::from_str(json).unwrap();- group.add(- "DayOneNames",- Box::new(ProperNounCapitalizationLinter::new(- EitherPattern::new(vec![- Box::new(- SequencePattern::default()- .t_aco("Day")- .then_whitespace()- .t_aco("One"),- ),- Box::new(- SequencePattern::default()- .t_aco("Day")- .then_whitespace()- .t_aco("One")- .then_whitespace()- .t_aco("Premium"),- ),- ]),- "Ensure proper capitalization of Day One and Day One Premium as brand names.",- dictionary.clone(),- )),- );+ for (key, rule) in rules.into_iter() {+ group.add(+ key,+ Box::new(ProperNounCapitalizationLinter::new_strs(+ rule.canonical,+ rule.description,+ dictionary.clone(),+ )),+ );+ }group.set_all_rules_to(Some(true));group}+pub fn lint_group(dictionary: Arc<impl Dictionary + 'static>) -> LintGroup {+ lint_group_from_json(include_str!("../../proper_noun_rules.json"), dictionary)+}+#[cfg(test)]mod tests {use crate::{@@ -1364,8 +283,6 @@ mod tests {assert_suggestion_result("arctic ocean", lint_group(dictionary), "Arctic Ocean");}- // Lowercase tests for seas-#[test]fn test_mediterranean_sea_lowercase() {let dictionary = FstDictionary::curated();diff --git a/harper-core/src/patterns/mod.rs b/harper-core/src/patterns/mod.rsindex 738397930..ad7ebc677 100644--- a/harper-core/src/patterns/mod.rs+++ b/harper-core/src/patterns/mod.rs@@ -21,6 +21,7 @@ mod invert;mod is_not_title_case;mod naive_pattern_group;mod noun_phrase;+mod pattern_map;mod repeating_pattern;mod sequence_pattern;mod similar_to_phrase;@@ -45,6 +46,7 @@ pub use invert::Invert;pub use is_not_title_case::IsNotTitleCase;pub use naive_pattern_group::NaivePatternGroup;pub use noun_phrase::NounPhrase;+pub use pattern_map::PatternMap;pub use repeating_pattern::RepeatingPattern;pub use sequence_pattern::SequencePattern;pub use similar_to_phrase::SimilarToPhrase;diff --git a/harper-core/src/patterns/pattern_map.rs b/harper-core/src/patterns/pattern_map.rsnew file mode 100644index 000000000..89d3fd0bf--- /dev/null+++ b/harper-core/src/patterns/pattern_map.rs@@ -0,0 +1,80 @@+use crate::Token;++use super::Pattern;++/// A map from [`Pattern`] to arbitrary data.+///+/// When used as a [`Pattern`] in of itself, it simply iterates through+/// all contained patterns, returning the first match found.+/// You should not assume this search is deterministic.+///+/// If you'd like to use this structure in a [`PatternLinter`](crate::linting::PatternLinter), you may want to provide+/// the map as the search pattern, then use a pattern look-up once more to determine+/// the corresponding key.+pub struct PatternMap<T>+where+ T: Send + Sync,+{+ rows: Vec<Row<T>>,+}++struct Row<T>+where+ T: Send + Sync,+{+ pub key: Box<dyn Pattern>,+ pub element: T,+}++impl<T> Default for PatternMap<T>+where+ T: Send + Sync,+{+ fn default() -> Self {+ Self {+ rows: Default::default(),+ }+ }+}++impl<T> PatternMap<T>+where+ T: Send + Sync,+{+ pub fn insert(&mut self, pattern: impl Pattern + 'static, value: T) {+ self.rows.push(Row {+ key: Box::new(pattern),+ element: value,+ });+ }++ /// Lookup the corresponding value for the given map.+ pub fn lookup(&self, tokens: &[Token], source: &[char]) -> Option<&T> {+ for row in &self.rows {+ let len = row.key.matches(tokens, source);++ if len != 0 {+ return Some(&row.element);+ }+ }++ None+ }+}++impl<T> Pattern for PatternMap<T>+where+ T: Send + Sync,+{+ fn matches(&self, tokens: &[Token], source: &[char]) -> usize {+ for row in &self.rows {+ let len = row.key.matches(tokens, source);++ if len != 0 {+ return len;+ }+ }++ 0+ }+}