22 static int LeadingUnicharsToChopped(
WERD_RES *word,
int num_unichars) {
24 for (
int i = 0; i < num_unichars; i++)
29 static int TrailingUnicharsToChopped(
WERD_RES *word,
int num_unichars) {
31 for (
int i = 0; i < num_unichars; i++)
45 static void YOutlierPieces(
WERD_RES *word,
int rebuilt_blob_index,
46 int super_y_bottom,
int sub_y_top,
47 ScriptPos *leading_pos,
int *num_leading_outliers,
49 int *num_trailing_outliers) {
52 if (!leading_pos) leading_pos = &sp_unused1;
53 if (!num_leading_outliers) num_leading_outliers = &unused1;
54 if (!trailing_pos) trailing_pos = &sp_unused2;
55 if (!num_trailing_outliers) num_trailing_outliers = &unused2;
57 *num_leading_outliers = *num_trailing_outliers = 0;
60 int chopped_start = LeadingUnicharsToChopped(word, rebuilt_blob_index);
61 int num_chopped_pieces = word->
best_state[rebuilt_blob_index];
63 int trailing_outliers = 0;
64 for (
int i = 0; i < num_chopped_pieces; i++) {
67 if (box.
bottom() >= super_y_bottom) {
69 }
else if (box.
top() <= sub_y_top) {
73 if (trailing_outliers == i) {
74 *num_leading_outliers = trailing_outliers;
75 *leading_pos = last_pos;
77 trailing_outliers = 0;
79 if (pos == last_pos) {
82 trailing_outliers = 1;
87 *num_trailing_outliers = trailing_outliers;
88 *trailing_pos = last_pos;
106 int num_leading, num_trailing;
108 float leading_certainty, trailing_certainty;
109 float avg_certainty, unlikely_threshold;
113 word, &num_leading, &sp_leading, &leading_certainty,
114 &num_trailing, &sp_trailing, &trailing_certainty,
115 &avg_certainty, &unlikely_threshold);
117 const char *leading_pos = sp_leading ==
SP_SUBSCRIPT ?
"sub" :
"super";
118 const char *trailing_pos = sp_trailing ==
SP_SUBSCRIPT ?
"sub" :
"super";
126 int num_remainder_leading = 0, num_remainder_trailing = 0;
127 if (num_leading + num_trailing < num_blobs && unlikely_threshold < 0.0) {
132 int last_word_char = num_blobs - 1 - num_trailing;
135 last_char_certainty <= unlikely_threshold) {
137 YOutlierPieces(word, last_word_char, super_y_bottom, sub_y_top,
138 nullptr,
nullptr, &rpos, &num_remainder_trailing);
139 if (num_trailing > 0 && rpos != sp_trailing) num_remainder_trailing = 0;
140 if (num_remainder_trailing > 0 &&
141 last_char_certainty < trailing_certainty) {
142 trailing_certainty = last_char_certainty;
145 bool another_blob_available = (num_remainder_trailing == 0) ||
146 num_leading + num_trailing + 1 < num_blobs;
148 if (another_blob_available &&
150 first_char_certainty <= unlikely_threshold) {
152 YOutlierPieces(word, num_leading, super_y_bottom, sub_y_top,
153 &lpos, &num_remainder_leading,
nullptr,
nullptr);
154 if (num_leading > 0 && lpos != sp_leading) num_remainder_leading = 0;
155 if (num_remainder_leading > 0 &&
156 first_char_certainty < leading_certainty) {
157 leading_certainty = first_char_certainty;
163 if (num_leading + num_trailing +
164 num_remainder_leading + num_remainder_trailing == 0) {
169 tprintf(
"Candidate for superscript detection: %s (",
171 if (num_leading || num_remainder_leading) {
172 tprintf(
"%d.%d %s-leading ", num_leading, num_remainder_leading,
175 if (num_trailing || num_remainder_trailing) {
176 tprintf(
"%d.%d %s-trailing ", num_trailing, num_remainder_trailing,
185 tprintf(
" Certainties -- Average: %.2f Unlikely thresh: %.2f ",
186 avg_certainty, unlikely_threshold);
188 tprintf(
"Orig. leading (min): %.2f ", leading_certainty);
190 tprintf(
"Orig. trailing (min): %.2f ", trailing_certainty);
197 int num_chopped_leading =
198 LeadingUnicharsToChopped(word, num_leading) + num_remainder_leading;
199 int num_chopped_trailing =
200 TrailingUnicharsToChopped(word, num_trailing) + num_remainder_trailing;
202 int retry_leading = 0;
203 int retry_trailing = 0;
204 bool is_good =
false;
206 num_chopped_leading, leading_certainty, sp_leading,
207 num_chopped_trailing, trailing_certainty, sp_trailing,
208 word, &is_good, &retry_leading, &retry_trailing);
211 }
else if (retry_leading || retry_trailing) {
212 int retry_chopped_leading =
213 LeadingUnicharsToChopped(revised, retry_leading);
214 int retry_chopped_trailing =
215 TrailingUnicharsToChopped(revised, retry_trailing);
217 retry_chopped_leading, leading_certainty, sp_leading,
218 retry_chopped_trailing, trailing_certainty, sp_trailing,
219 revised, &is_good, &retry_leading, &retry_trailing);
254 int *num_rebuilt_leading,
256 float *leading_certainty,
257 int *num_rebuilt_trailing,
259 float *trailing_certainty,
260 float *avg_certainty,
261 float *unlikely_threshold) {
262 *avg_certainty = *unlikely_threshold = 0.0f;
263 *num_rebuilt_leading = *num_rebuilt_trailing = 0;
264 *leading_certainty = *trailing_certainty = 0.0f;
274 *leading_pos = *trailing_pos =
SP_NORMAL;
275 int leading_outliers = 0;
276 int trailing_outliers = 0;
278 float normal_certainty_total = 0.0f;
279 float worst_normal_certainty = 0.0f;
282 for (
int b = 0; b < num_blobs; ++b) {
285 if (box.
bottom() >= super_y_bottom) {
287 }
else if (box.
top() <= sub_y_top) {
293 if (char_certainty < worst_normal_certainty) {
294 worst_normal_certainty = char_certainty;
297 normal_certainty_total += char_certainty;
299 if (trailing_outliers == b) {
300 leading_outliers = trailing_outliers;
301 *leading_pos = last_pos;
303 trailing_outliers = 0;
305 if (last_pos == pos) {
308 trailing_outliers = 1;
313 *trailing_pos = last_pos;
314 if (num_normal >= 3) {
316 normal_certainty_total -= worst_normal_certainty;
318 if (num_normal > 0) {
319 *avg_certainty = normal_certainty_total / num_normal;
322 if (num_normal == 0 ||
323 (leading_outliers == 0 && trailing_outliers == 0)) {
330 for (*leading_certainty = 0.0f, *num_rebuilt_leading = 0;
331 *num_rebuilt_leading < leading_outliers;
332 (*num_rebuilt_leading)++) {
334 if (char_certainty > *unlikely_threshold) {
337 if (char_certainty < *leading_certainty) {
338 *leading_certainty = char_certainty;
343 for (*trailing_certainty = 0.0f, *num_rebuilt_trailing = 0;
344 *num_rebuilt_trailing < trailing_outliers;
345 (*num_rebuilt_trailing)++) {
346 int blob_idx = num_blobs - 1 - *num_rebuilt_trailing;
348 if (char_certainty > *unlikely_threshold) {
351 if (char_certainty < *trailing_certainty) {
352 *trailing_certainty = char_certainty;
383 int num_chopped_leading,
float leading_certainty,
ScriptPos leading_pos,
384 int num_chopped_trailing,
float trailing_certainty,
388 int *retry_rebuild_leading,
int *retry_rebuild_trailing) {
391 *retry_rebuild_leading = *retry_rebuild_trailing = 0;
400 if (num_chopped_leading > 0) {
402 split_word(prefix, num_chopped_leading, &core, &bb0);
407 if (num_chopped_trailing > 0) {
408 int split_pt = num_chopped - num_chopped_trailing - num_chopped_leading;
422 tprintf(
" recognizing first %d chopped blobs\n", num_chopped_leading);
426 tprintf(
" The leading bits look like %s %s\n",
437 tprintf(
" recognizing middle %d chopped blobs\n",
438 num_chopped - num_chopped_leading - num_chopped_trailing);
447 tprintf(
" recognizing last %d chopped blobs\n", num_chopped_trailing);
451 tprintf(
" The trailing bits look like %s %s\n",
466 retry_rebuild_leading,
nullptr);
470 nullptr, retry_rebuild_trailing);
472 *is_good = good_prefix && good_suffix;
473 if (!*is_good && !*retry_rebuild_leading && !*retry_rebuild_trailing) {
496 tprintf(
"%s superscript fix: %s\n", *is_good ?
"ACCEPT" :
"REJECT",
523 float certainty_threshold,
525 int *right_ok)
const {
526 int initial_ok_run_count = 0;
527 int ok_run_count = 0;
528 float worst_certainty = 0.0f;
532 for (
int i = 0; i < wc.
length(); i++) {
536 bool bad_certainty = char_certainty < certainty_threshold;
540 if (choice && fontinfo_table.
size() > 0) {
543 bool font1_is_italic = font_id1 >= 0
546 is_italic = font1_is_italic &&
547 (font_id2 < 0 || fontinfo_table.
get(font_id2).
is_italic());
550 float height_fraction = 1.0f;
552 float normal_height = char_height;
554 int min_bot, max_bot, min_top, max_top;
558 float hi_height = max_top - max_bot;
559 float lo_height = min_top - min_bot;
560 normal_height = (hi_height + lo_height) / 2;
564 height_fraction = char_height / normal_height;
571 tprintf(
" Rejecting: superscript is italic.\n");
574 tprintf(
" Rejecting: punctuation present.\n");
578 tprintf(
" Rejecting: don't believe character %s with certainty %.2f "
579 "which is less than threshold %.2f\n", char_str,
580 char_certainty, certainty_threshold);
583 tprintf(
" Rejecting: character %s seems too small @ %.2f versus "
584 "expected %.2f\n", char_str, char_height, normal_height);
587 if (bad_certainty || bad_height || is_punc || is_italic) {
588 if (ok_run_count == i) {
589 initial_ok_run_count = ok_run_count;
595 if (char_certainty < worst_certainty) {
596 worst_certainty = char_certainty;
599 bool all_ok = ok_run_count == wc.
length();
600 if (all_ok && debug) {
601 tprintf(
" Accept: worst revised certainty is %.2f\n", worst_certainty);
604 if (left_ok) *left_ok = initial_ok_run_count;
605 if (right_ok) *right_ok = ok_run_count;