Skip to content

Commit

Permalink
feat(core): make proper nouns important to the SpellCheck rule
Browse files Browse the repository at this point in the history
  • Loading branch information
elijah-potter committed Jan 6, 2025
1 parent 250e650 commit c5b570e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
3 changes: 2 additions & 1 deletion harper-core/dictionary.dict
Original file line number Diff line number Diff line change
Expand Up @@ -31305,7 +31305,7 @@ marital/5Y
maritime/5
marjoram/1M
mark/14AMDSG
markdown/12SM
Markdown/12SM
marked/54U
markedly/
marker/14MS
Expand Down Expand Up @@ -49768,3 +49768,4 @@ uncheck/SM
upsample/SMG
organoid/SM
centric/SM
Harper/SM
28 changes: 28 additions & 0 deletions harper-core/src/linting/spell_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,31 @@ impl<T: Dictionary> Linter for SpellCheck<T> {
"Looks and provides corrections for misspelled words."
}
}

#[cfg(test)]
mod tests {
use crate::{
linting::tests::{assert_lint_count, assert_suggestion_result},
FstDictionary,
};

use super::SpellCheck;

#[test]
fn markdown_capitalized() {
assert_suggestion_result(
"The word markdown should be capitalized.",
SpellCheck::new(FstDictionary::curated()),
"The word Markdown should be capitalized.",
);
}

#[test]
fn harper_automattic_capitalized() {
assert_lint_count(
"So should harper and automattic.",
SpellCheck::new(FstDictionary::curated()),
2,
);
}
}
2 changes: 1 addition & 1 deletion harper-core/src/spell/fst_dictionary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ lazy_static! {
thread_local! {
// Builders are computationally expensive and do not depend on the word, so we store a
// collection of builders and the associated edit distance here.
// Currently, the edit distance we use is 3, but a value that does not exist in this
// Currently, the edit distance we use is three, but a value that does not exist in this
// collection will create a new builder of that distance and push it to the collection.
static AUTOMATON_BUILDERS: RefCell<Vec<(u8, LevenshteinAutomatonBuilder)>> = RefCell::new(vec![(
EXPECTED_DISTANCE,
Expand Down
11 changes: 10 additions & 1 deletion harper-core/src/spell/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::borrow::Cow;
use std::ops::Index;

use itertools::{Itertools, MinMaxResult};

Expand All @@ -15,7 +16,7 @@ mod full_dictionary;
mod hunspell;
mod merged_dictionary;

#[derive(PartialEq)]
#[derive(PartialEq, Debug)]
pub struct FuzzyMatchResult<'a> {
word: &'a [char],
edit_distance: u8,
Expand Down Expand Up @@ -59,6 +60,14 @@ fn order_suggestions(matches: Vec<FuzzyMatchResult>) -> Vec<&[char]> {
found.swap(0, 2);
}

if let Some(noun_index) = found
.iter()
.skip(3)
.position(|i| i.metadata.is_proper_noun())
{
found.swap(2, noun_index + 3);
}

// Make commonality relevant
found.sort_by_key(|fmr| if fmr.metadata.common { 0 } else { 1 });

Expand Down

0 comments on commit c5b570e

Please sign in to comment.