Commit f16443b8 for tesseract
commit f16443b8a572c1692e4078587023d20a93cbc184
Author: Stefan Weil <sw@weilnetz.de>
Date: Thu Nov 19 12:37:39 2026 +0100
Fix IsSpaceDelimited for characters with the null script
UNICHARSET::IsSpaceDelimited() compares the script of a character with
han_sid_, thai_sid_, hangul_sid_, hiragana_sid_ and katakana_sid_. A
script which is missing in the unicharset has the id null_sid_ (0), so
in every model which lacks at least one of those scripts the space,
which has the null script, was reported as not space delimited.
RecodeBeamSearch::ContinueUnichar() uses the result to decide whether
a character can start a dictionary word. For a space it requires a
certainty above worst_dict_cert, but that test was bypassed, so the
beam search could put a very uncertain space between two parts of a
word if the parts are dictionary words ("Vorstandssitzung" was read
as "Vorstands sitzung").
With an LSTM model with word, punctuation and number dawgs the
character error rate on 24,899 lines went from 0.693 % to 0.676 %,
and on 15,667 other lines from 0.446 % to 0.437 %; the number of
spaces added compared to the same model without dawgs went from 614
to 391 and from 187 to 97. Without dawgs the output is unchanged.
Add a unit test which fails without the fix.
Assisted-by: Claude Fable 5.2 (Anthropic)
Signed-off-by: Stefan Weil <sw@weilnetz.de>
diff --git a/src/ccutil/unicharset.h b/src/ccutil/unicharset.h
index 69b085ff..9cffaa7b 100644
--- a/src/ccutil/unicharset.h
+++ b/src/ccutil/unicharset.h
@@ -670,6 +670,12 @@ public:
return true;
}
int script_id = get_script(unichar_id);
+ // The id of a script which is missing in the unicharset is null_sid_,
+ // so a character with the null script (like the space) must not be
+ // compared with those ids.
+ if (script_id == null_sid_) {
+ return true;
+ }
return script_id != han_sid_ && script_id != thai_sid_ &&
script_id != hangul_sid_ && script_id != hiragana_sid_ &&
script_id != katakana_sid_;
diff --git a/unittest/unicharset_test.cc b/unittest/unicharset_test.cc
index 320059e2..d8cd4e0a 100644
--- a/unittest/unicharset_test.cc
+++ b/unittest/unicharset_test.cc
@@ -154,4 +154,26 @@ TEST(UnicharsetTest, OldStyle) {
EXPECT_EQ(u.size(), 111);
}
+TEST(UnicharsetTest, SpaceDelimited) {
+ // The id of a script which is missing in the unicharset is the id of the
+ // null script, so characters with the null script (like the space) must
+ // not be reported as not space delimited.
+ UNICHARSET u;
+ u.unichar_insert(" ");
+ u.unichar_insert("a");
+ EXPECT_EQ(u.unichar_to_id(" "), UNICHAR_SPACE);
+ u.set_script(u.unichar_to_id("a"), "Latin");
+ u.post_load_setup();
+ EXPECT_TRUE(u.IsSpaceDelimited(UNICHAR_SPACE));
+ EXPECT_TRUE(u.IsSpaceDelimited(u.unichar_to_id("a")));
+ EXPECT_TRUE(u.IsSpaceDelimited(INVALID_UNICHAR_ID));
+ // With a script which is not space delimited.
+ u.unichar_insert("\u4e2d");
+ u.set_script(u.unichar_to_id("\u4e2d"), "Han");
+ u.post_load_setup();
+ EXPECT_TRUE(u.IsSpaceDelimited(UNICHAR_SPACE));
+ EXPECT_TRUE(u.IsSpaceDelimited(u.unichar_to_id("a")));
+ EXPECT_FALSE(u.IsSpaceDelimited(u.unichar_to_id("\u4e2d")));
+}
+
} // namespace tesseract