Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

fix(core): make title case algorithm more error-tolerant

Elijah Potter203c606a1972
diff --git a/harper-core/src/lib.rs b/harper-core/src/lib.rs
index 226522a55..aa5c16b1a 100644
--- a/harper-core/src/lib.rs
+++ b/harper-core/src/lib.rs
@@ -29,6 +29,7 @@ pub use punctuation::{Punctuation, Quote};
pub use span::Span;
pub use spell::{Dictionary, FstDictionary, FullDictionary, MergedDictionary};
pub use sync::Lrc;
+pub use title_case::{make_title_case, make_title_case_str};
pub use token::{FatToken, Token, TokenKind, TokenStringExt};
pub use vec_ext::VecExt;
pub use word_metadata::{AdverbData, ConjunctionData, NounData, Tense, VerbData, WordMetadata};
diff --git a/harper-core/src/title_case.rs b/harper-core/src/title_case.rs
index 36b36c144..aee11ace1 100644
--- a/harper-core/src/title_case.rs
+++ b/harper-core/src/title_case.rs
@@ -34,12 +34,18 @@ pub fn make_title_case(
}
while let Some((index, word)) = words.next() {
- let metadata = word.kind.as_word().unwrap();
+ let chars = document.get_span_content(word.span);
+ let chars_lower = chars.to_lower();
+
+ let metadata = word
+ .kind
+ .as_word()
+ .unwrap()
+ .or(&dict.get_word_metadata(&chars_lower));
let should_capitalize = !metadata.preposition
&& !metadata.article
- && !SPECIAL_CONJUNCTIONS
- .contains(document.get_span_content(word.span).to_lower().as_slice())
+ && !SPECIAL_CONJUNCTIONS.contains(chars_lower.as_slice())
|| index == 0
|| words.peek().is_none();
@@ -94,11 +100,11 @@ mod tests {
fn start_as_uppercase() {
assert_eq!(
make_title_case_str(
- "THE FIRST AND LAST WORDS SHOULD BE CAPITALIZED, EVEN IF IT IS \"THE\"",
+ "THIS IS A TEST",
&mut PlainEnglish,
FstDictionary::curated()
),
- "The First and Last Words Should Be Capitalized, Even If It Is \"The\""
+ "This Is a Test"
)
}
}