23 static int LeadingUnicharsToChopped(
WERD_RES *word,
int num_unichars) {
25 for (
int i = 0; i < num_unichars; i++)
30 static int TrailingUnicharsToChopped(
WERD_RES *word,
int num_unichars) {
32 for (
int i = 0; i < num_unichars; i++)
47 int super_y_bottom,
int sub_y_top,
48 ScriptPos *leading_pos,
int *num_leading_outliers,
49 ScriptPos *trailing_pos,
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) {
138 NULL, NULL, &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) {
153 &lpos, &num_remainder_leading, NULL, NULL);
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, NULL);
470 NULL, retry_rebuild_trailing);
472 *is_good = good_prefix && good_suffix;
473 if (!*is_good && !*retry_rebuild_leading && !*retry_rebuild_trailing) {
495 tprintf(
"%s superscript fix: %s\n", *is_good ?
"ACCEPT" :
"REJECT",
522 float certainty_threshold,
524 int *right_ok)
const {
525 int initial_ok_run_count = 0;
526 int ok_run_count = 0;
527 float worst_certainty = 0.0f;
531 for (
int i = 0; i < wc.
length(); i++) {
535 bool bad_certainty = char_certainty < certainty_threshold;
539 if (choice && fontinfo_table.
size() > 0) {
542 bool font1_is_italic = font_id1 >= 0
545 is_italic = font1_is_italic &&
546 (font_id2 < 0 || fontinfo_table.
get(font_id2).
is_italic());
549 float height_fraction = 1.0f;
551 float normal_height = char_height;
553 int min_bot, max_bot, min_top, max_top;
557 float hi_height = max_top - max_bot;
558 float lo_height = min_top - min_bot;
559 normal_height = (hi_height + lo_height) / 2;
563 height_fraction = char_height / normal_height;
570 tprintf(
" Rejecting: superscript is italic.\n");
573 tprintf(
" Rejecting: punctuation present.\n");
577 tprintf(
" Rejecting: don't believe character %s with certainty %.2f " 578 "which is less than threshold %.2f\n", char_str,
579 char_certainty, certainty_threshold);
582 tprintf(
" Rejecting: character %s seems too small @ %.2f versus " 583 "expected %.2f\n", char_str, char_height, normal_height);
586 if (bad_certainty || bad_height || is_punc || is_italic) {
587 if (ok_run_count == i) {
588 initial_ok_run_count = ok_run_count;
594 if (char_certainty < worst_certainty) {
595 worst_certainty = char_certainty;
598 bool all_ok = ok_run_count == wc.
length();
599 if (all_ok && debug) {
600 tprintf(
" Accept: worst revised certainty is %.2f\n", worst_certainty);
603 if (left_ok) *left_ok = initial_ok_run_count;
604 if (right_ok) *right_ok = ok_run_count;
void GetSubAndSuperscriptCandidates(const WERD_RES *word, int *num_rebuilt_leading, ScriptPos *leading_pos, float *leading_certainty, int *num_rebuilt_trailing, ScriptPos *trailing_pos, float *trailing_certainty, float *avg_certainty, float *unlikely_threshold)
void join_words(WERD_RES *word, WERD_RES *word2, BlamerBundle *orig_bb) const
const int kBlnBaselineOffset
void YOutlierPieces(WERD_RES *word, int rebuilt_blob_index, int super_y_bottom, int sub_y_top, ScriptPos *leading_pos, int *num_leading_outliers, ScriptPos *trailing_pos, int *num_trailing_outliers)
GenericVector< TBLOB * > blobs
void recog_word_recursive(WERD_RES *word)
bool top_bottom_useful() const
const STRING & unichar_string() const
UNICHAR_ID unichar_id(int index) const
double superscript_bettered_certainty
void get_top_bottom(UNICHAR_ID unichar_id, int *min_bottom, int *max_bottom, int *min_top, int *max_top) const
void ConsumeWordResults(WERD_RES *word)
void split_word(WERD_RES *word, int split_pt, WERD_RES **right_piece, BlamerBundle **orig_blamer_bundle) const
bool SubAndSuperscriptFix(WERD_RES *word_res)
WERD_CHOICE * best_choice
TBOX bounding_box() const
double superscript_scaledown_ratio
const UNICHARSET * unicharset() const
bool get_ispunctuation(UNICHAR_ID unichar_id) const
int size() const
Return the size used.
double superscript_worse_certainty
const char * string() const
const FontInfo * fontinfo
bool BelievableSuperscript(bool debug, const WERD_RES &word, float certainty_threshold, int *left_ok, int *right_ok) const
BOOL8 flag(WERD_FLAGS mask) const
double superscript_min_y_bottom
inT16 fontinfo_id2() const
void SetAllScriptPositions(tesseract::ScriptPos position)
BLOB_CHOICE * GetBlobChoice(int index) const
WERD_RES * TrySuperscriptSplits(int num_chopped_leading, float leading_certainty, ScriptPos leading_pos, int num_chopped_trailing, float trailing_certainty, ScriptPos trailing_pos, WERD_RES *word, bool *is_good, int *retry_leading, int *retry_trailing)
int classify_integer_matcher_multiplier
int classify_class_pruner_multiplier
double subscript_max_y_top
const T & get(int id) const
Return the object from an id.
inT16 fontinfo_id() const
UnicityTable< FontInfo > & get_fontinfo_table()
GenericVector< int > best_state
const char * id_to_unichar(UNICHAR_ID id) const
const char * ScriptPosToString(enum ScriptPos script_pos)