mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-29 17:55:45 +01:00
proof: keep zero-width joiners inside words
words() ended a word at a zero-width joiner or non-joiner, so a Hindi or Gujarati word written with one in a conjunct (क्ष, પુસ્તક) was split and each half was spell-checked and repeat-checked alone. A joiner between two letters, digits or marks is now inside the word (UAX #29); one at a word's edge is not. The Gujarati/Hindi lookup tries the word as typed, then without its joiners, as a dictionary with Hunspell's IGNORE would. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9c34eaa615
commit
8a5a402f4b
4 files changed
+66
-6
No files matched your search
@@ -130,6 +130,29 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
/// A zero-width joiner or non-joiner in a conjunct (`પુસ્` U+200D `તક`) keeps the word whole,
|
||||
/// and the word is found under the dictionary's spelling, which has no joiner.
|
||||
#[test]
|
||||
fn words_with_joiners_are_one_word_and_spelled() {
|
||||
test_dict::install();
|
||||
for word in ["પુસ્\u{200D}તક", "પુસ્\u{200C}તકોમાં"] {
|
||||
let text = format!("ઘર {word} ગામ");
|
||||
let at = text.find(word).unwrap();
|
||||
assert!(crate::words(&text).contains(&(at, at + word.len())), "{word:?}");
|
||||
assert!(is_correct(word), "{word:?}");
|
||||
let issues = crate::check_text(&text, &[(0..text.len(), crate::ProofLang::Gu)]);
|
||||
assert!(issues.is_empty(), "{word:?}: {issues:?}");
|
||||
}
|
||||
// Still a misspelling with a joiner, and a joiner at a word's edge is not in the word.
|
||||
assert!(!is_correct("પુસ્\u{200D}તકક"));
|
||||
let text = "ઘર\u{200D} ગામ";
|
||||
assert!(crate::words(text).contains(&(0, "ઘર".len())));
|
||||
// A word with a joiner written twice is a repeated word.
|
||||
let text = "પુસ્\u{200D}તક પુસ્\u{200D}તક";
|
||||
let issues = crate::check_grammar_in(text, crate::ProofLang::Gu);
|
||||
assert!(issues.iter().any(|i| i.start == 0 && i.end == text.len()), "{issues:?}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn forbidden_words_are_rejected() {
|
||||
test_dict::install();
|
||||
|
||||
@@ -143,6 +143,22 @@ mod tests {
|
||||
test_dict::install();
|
||||
}
|
||||
|
||||
/// A zero-width joiner or non-joiner after a virama (`क्` U+200D `ष`) keeps the word whole, and
|
||||
/// the word is found under the dictionary's spelling, which has no joiner.
|
||||
#[test]
|
||||
fn words_with_joiners_are_one_word_and_spelled() {
|
||||
test_dict::install();
|
||||
for word in ["परीक्\u{200D}षण", "परीक्\u{200C}षण", "मित्\u{200D}रों"] {
|
||||
let text = format!("घर {word} पानी।");
|
||||
let at = text.find(word).unwrap();
|
||||
assert!(crate::words(&text).contains(&(at, at + word.len())), "{word:?}");
|
||||
assert!(is_correct(word), "{word:?}");
|
||||
let issues = crate::check_text(&text, &[(0..text.len(), crate::ProofLang::Hi)]);
|
||||
assert!(issues.is_empty(), "{word:?}: {issues:?}");
|
||||
}
|
||||
assert!(!is_correct("परीक्\u{200D}षणण"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn user_dictionary_applies() {
|
||||
test_dict::install();
|
||||
|
||||
+21
-3
@@ -489,13 +489,25 @@ fn in_dialect(s: &str, word: &str, lang: ProofLang) -> String {
|
||||
}
|
||||
|
||||
/// Words in `text`: (byte start, byte end). Combining marks belong to the word they follow, so a
|
||||
/// word typed decomposed (`tambie\u{301}n`, NFD) is one word, as its precomposed form is.
|
||||
/// word typed decomposed (`tambie\u{301}n`, NFD) is one word, as its precomposed form is. A
|
||||
/// zero-width joiner or non-joiner between two letters, digits or marks is inside the word, as in
|
||||
/// Unicode's word boundaries (UAX #29): Indic text uses one to choose how a conjunct is drawn
|
||||
/// (`क्` U+200D `ष`, `પુસ્` U+200D `તક`). A joiner at a word's edge is not part of it.
|
||||
pub fn words(text: &str) -> Vec<(usize, usize)> {
|
||||
let mut out = Vec::new();
|
||||
let mut start: Option<usize> = None;
|
||||
let is_w = |c: char| c.is_alphanumeric() || c == '\'' || c == '’' || c == '-' || unicode_normalization::char::is_combining_mark(c);
|
||||
let is_core = |c: char| c.is_alphanumeric() || unicode_normalization::char::is_combining_mark(c);
|
||||
let is_w = |c: char| is_core(c) || c == '\'' || c == '’' || c == '-';
|
||||
// The char before was a letter, digit or mark, or a joiner kept after one.
|
||||
let mut after_core = false;
|
||||
for (i, c) in text.char_indices() {
|
||||
match (start, is_w(c)) {
|
||||
let w = if is_joiner(c) {
|
||||
after_core && text.get(i + c.len_utf8()..).and_then(|rest| rest.chars().find(|&n| !is_joiner(n))).is_some_and(is_core)
|
||||
} else {
|
||||
is_w(c)
|
||||
};
|
||||
after_core = if is_joiner(c) { w } else { is_core(c) };
|
||||
match (start, w) {
|
||||
(None, true) => start = Some(i),
|
||||
(Some(s), false) => {
|
||||
out.push((s, i));
|
||||
@@ -518,6 +530,12 @@ pub fn words(text: &str) -> Vec<(usize, usize)> {
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Is `c` a zero-width joiner or non-joiner (U+200D, U+200C)? It changes how letters are drawn,
|
||||
/// not which letters a word has.
|
||||
pub(crate) fn is_joiner(c: char) -> bool {
|
||||
matches!(c, '\u{200C}' | '\u{200D}')
|
||||
}
|
||||
|
||||
/// Spelling issues in a paragraph of English text (no suggestions; ask [`suggest`] on demand).
|
||||
pub fn check_spelling(text: &str) -> Vec<Issue> {
|
||||
check_spelling_with(text, &Options::default())
|
||||
|
||||
@@ -46,10 +46,13 @@ pub(crate) fn is_correct(script: &Script, d: &AffixDict, word: &str, opts: &crat
|
||||
}
|
||||
|
||||
/// Is one word (no hyphens) in the dictionary or the user dictionary, spelled as it is or as the
|
||||
/// script's [`Script::variant`]?
|
||||
/// script's [`Script::variant`]? A word with a zero-width joiner or non-joiner (`પુસ્` U+200D
|
||||
/// `તક`) is tried as typed, then without its joiners, as a dictionary that ignores them
|
||||
/// (Hunspell's `IGNORE`) would: they choose how a conjunct is drawn, not how the word is spelled.
|
||||
fn known(script: &Script, d: &AffixDict, w: &str) -> bool {
|
||||
let listed = |w: &str| d.check(w) || crate::in_user_dictionary(&w.to_lowercase());
|
||||
listed(w) || script.variant.and_then(|v| v(w)).is_some_and(|v| listed(&v))
|
||||
let spelled = |w: &str| listed(w) || script.variant.and_then(|v| v(w)).is_some_and(|v| listed(&v));
|
||||
spelled(w) || (w.contains(crate::is_joiner) && spelled(&w.replace(crate::is_joiner, "")))
|
||||
}
|
||||
|
||||
/// A language's script, messages and choices for [`check`].
|
||||
@@ -91,7 +94,7 @@ fn repeated_words(text: &str, ws: &[(usize, usize)], rules: &Rules, v: &mut Vec<
|
||||
let (Some(w0), Some(w1), Some(gap)) = (text.get(*a0..*b0), text.get(*a1..*b1), text.get(*b0..*a1)) else { continue };
|
||||
if !gap.is_empty()
|
||||
&& gap.chars().all(char::is_whitespace)
|
||||
&& w0.chars().all(|c| c.is_alphabetic() || (rules.script.is_letter)(c))
|
||||
&& w0.chars().all(|c| c.is_alphabetic() || (rules.script.is_letter)(c) || crate::is_joiner(c))
|
||||
&& w0 == w1
|
||||
&& (rules.flag_repeat)(w0)
|
||||
{
|
||||
|
||||
Reference in new issue
Block a user