26 #if !defined(DISABLED_LEGACY_ENGINE) 75 const char* truth_str,
const TBOX& word_box) {
77 truth_has_char_boxes_ =
false;
81 unicharset.
encode_string(truth_str,
false, &encoding, &lengths,
nullptr);
83 for (
int i = 0; i < encoding.
size(); total_length += lengths[i++]) {
84 STRING uch(truth_str + total_length);
95 const char* char_str,
const TBOX& char_box) {
96 STRING symbol_str(char_str);
98 if (
id != INVALID_UNICHAR_ID) {
100 if (normed_uch.length() > 0) symbol_str = normed_uch;
102 int length = truth_word_.
length();
106 truth_has_char_boxes_ =
true;
107 else if (truth_word_.
BlobBox(length - 1) == char_box)
108 truth_has_char_boxes_ =
false;
115 truth_has_char_boxes_ =
false;
120 if (word_choice ==
nullptr)
return false;
123 for (
int i = 0; i < word_choice->
length(); ++i) {
128 return truth_str == normed_choice_str;
134 (*debug) +=
"Truth ";
135 for (
int i = 0; i < this->truth_text_.
length(); ++i) {
136 (*debug) += this->truth_text_[i];
138 if (!this->truth_has_char_boxes_) (*debug) +=
" (no char boxes)";
139 if (choice !=
nullptr) {
140 (*debug) +=
" Choice ";
143 (*debug) += choice_str;
155 norm_box_tolerance_ = kBlamerBoxTolerance * denorm.
x_scale();
160 for (
int b = 0; b < truth_word_.
length(); ++b) {
162 topleft.
x = box.
left();
163 topleft.
y = box.
top();
168 TBOX norm_box(norm_topleft.
x, norm_botright.
y,
169 norm_botright.
x, norm_topleft.
y);
183 int begin2_truth_index = -1;
185 truth_has_char_boxes_) {
186 debug_str =
"Looking for truth split at";
189 debug_str +=
"\nnorm_truth_word boxes:\n";
190 if (norm_truth_word_.
length() > 1) {
192 for (b = 1; b < norm_truth_word_.
length(); ++b) {
194 if ((abs(word1_right - norm_truth_word_.
BlobBox(b - 1).
right()) <
195 norm_box_tolerance_) &&
196 (abs(word2_left - norm_truth_word_.
BlobBox(b).
left()) <
197 norm_box_tolerance_)) {
198 begin2_truth_index = b;
199 debug_str +=
"Split found";
208 if (begin2_truth_index > 0) {
209 bundle1->truth_has_char_boxes_ =
true;
210 bundle1->norm_box_tolerance_ = norm_box_tolerance_;
211 bundle2->truth_has_char_boxes_ =
true;
212 bundle2->norm_box_tolerance_ = norm_box_tolerance_;
214 for (b = 0; b < norm_truth_word_.
length(); ++b) {
215 if (b == begin2_truth_index) curr_bb = bundle2;
218 curr_bb->truth_text_.
push_back(truth_text_[b]);
224 debug_str +=
"Truth split not found";
225 debug_str += truth_has_char_boxes_ ?
226 "\n" :
" (no truth char boxes)\n";
238 if (bundle1.incorrect_result_reason_ !=
IRR_CORRECT &&
241 debug_str +=
"Blame from part 1: ";
242 debug_str += bundle1.debug_;
243 irr = bundle1.incorrect_result_reason_;
245 if (bundle2.incorrect_result_reason_ !=
IRR_CORRECT &&
248 debug_str +=
"Blame from part 2: ";
249 debug_str += bundle2.debug_;
251 irr = bundle2.incorrect_result_reason_;
252 }
else if (irr != bundle2.incorrect_result_reason_) {
256 incorrect_result_reason_ = irr;
258 SetBlame(irr, debug_str,
nullptr,
debug);
266 const TBOX& blob_box,
267 const BLOB_CHOICE_LIST& choices,
269 if (!truth_has_char_boxes_ ||
273 for (
int b = 0; b < norm_truth_word_.
length(); ++b) {
274 const TBOX &truth_box = norm_truth_word_.
BlobBox(b);
280 bool incorrect_adapted =
false;
281 UNICHAR_ID incorrect_adapted_id = INVALID_UNICHAR_ID;
282 const char *truth_str = truth_text_[b].string();
285 BLOB_CHOICE_IT choices_it(const_cast<BLOB_CHOICE_LIST*>(&choices));
286 for (choices_it.mark_cycle_pt(); !choices_it.cycled_list();
287 choices_it.forward()) {
294 incorrect_adapted =
true;
299 STRING debug_str =
"unichar ";
300 debug_str += truth_str;
301 debug_str +=
" not found in classification list";
303 }
else if (incorrect_adapted) {
304 STRING debug_str =
"better rating for adapted ";
306 debug_str +=
" than for correct ";
307 debug_str += truth_str;
319 if (
NoTruth() || !truth_has_char_boxes_ ||
324 bool missing_chop =
false;
328 int16_t truth_x = -1;
329 while (box_index < truth_word_.
length() && blob_index < num_blobs) {
336 truth_x + norm_box_tolerance_) {
343 if (missing_chop || box_index < norm_truth_word_.
length()) {
346 debug_str.
add_str_int(
"Detected missing chop (tolerance=",
347 norm_box_tolerance_);
348 debug_str +=
") at Bounding Box=";
351 debug_str.
add_str_int(
"\nNo chop for truth at x=", truth_x);
354 norm_truth_word_.
length() - box_index);
355 debug_str +=
" truth box(es)";
357 debug_str +=
"\nMaximally chopped word boxes:\n";
358 for (blob_index = 0; blob_index < num_blobs; ++blob_index) {
363 debug_str +=
"Truth bounding boxes:\n";
364 for (box_index = 0; box_index < norm_truth_word_.
length(); ++box_index) {
379 const UNICHARSET& unicharset,
bool valid_permuter,
bool debug) {
380 if (valid_permuter) {
382 best_choice_is_dict_and_top_choice_ =
true;
387 for (blob_choice_it.mark_cycle_pt(); !blob_choice_it.cycled_list();
388 blob_choice_it.forward()) {
389 if (!(unicharset.
get_fragment(blob_choice_it.data()->unichar_id()))) {
390 first_choice = blob_choice_it.data();
396 best_choice_is_dict_and_top_choice_ =
false;
402 if (best_choice_is_dict_and_top_choice_) {
403 debug_str =
"Best choice is: incorrect, top choice, dictionary word";
404 debug_str +=
" with permuter ";
407 debug_str =
"Classifier/Old LM tradeoff is to blame";
416 #ifndef DISABLED_LEGACY_ENGINE 418 #endif // ndef DISABLED_LEGACY_ENGINE 419 if (incorrect_result_reason_ !=
IRR_CORRECT || !truth_has_char_boxes_)
423 debug_str +=
"Blamer computing correct_segmentation_cols\n";
424 int curr_box_col = 0;
425 int next_box_col = 0;
426 int num_blobs =
word->NumBlobs();
427 if (num_blobs == 0)
return;
429 int16_t next_box_x =
word->blobs[blob_index]->bounding_box().right();
430 for (
int truth_idx = 0; blob_index < num_blobs &&
431 truth_idx < norm_truth_word_.
length();
434 int16_t curr_box_x = next_box_x;
435 if (blob_index + 1 < num_blobs)
436 next_box_x =
word->blobs[blob_index + 1]->bounding_box().right();
437 int16_t truth_x = norm_truth_word_.
BlobBox(truth_idx).
right();
438 debug_str.
add_str_int(
"Box x coord vs. truth: ", curr_box_x);
441 if (curr_box_x > (truth_x + norm_box_tolerance_)) {
443 }
else if (curr_box_x >= truth_x - norm_box_tolerance_ &&
444 (blob_index + 1 >= num_blobs ||
445 next_box_x > truth_x + norm_box_tolerance_)) {
446 correct_segmentation_cols_.
push_back(curr_box_col);
447 correct_segmentation_rows_.
push_back(next_box_col-1);
452 curr_box_col = next_box_col;
455 if (blob_index < num_blobs ||
456 correct_segmentation_cols_.
length() != norm_truth_word_.
length()) {
457 debug_str.
add_str_int(
"Blamer failed to find correct segmentation" 458 " (tolerance=", norm_box_tolerance_);
459 if (blob_index >= num_blobs) debug_str +=
" blob == nullptr";
465 correct_segmentation_cols_.
clear();
466 correct_segmentation_rows_.
clear();
473 !segsearch_is_looking_for_blame_ &&
474 truth_has_char_boxes_ &&
478 #if !defined(DISABLED_LEGACY_ENGINE) 486 bool debug,
STRING *debug_str,
488 segsearch_is_looking_for_blame_ =
true;
490 tprintf(
"segsearch starting to look for blame\n");
494 *debug_str +=
"Correct segmentation:\n";
495 for (
int idx = 0; idx < correct_segmentation_cols_.
length(); ++idx) {
496 debug_str->
add_str_int(
"col=", correct_segmentation_cols_[idx]);
497 debug_str->
add_str_int(
" row=", correct_segmentation_rows_[idx]);
499 if (!ratings->
Classified(correct_segmentation_cols_[idx],
500 correct_segmentation_rows_[idx],
502 !cb->
Run(correct_segmentation_cols_[idx],
503 correct_segmentation_rows_[idx])) {
504 segsearch_is_looking_for_blame_ =
false;
505 *debug_str +=
"\nFailed to insert pain point\n";
511 #endif // !defined(DISABLED_LEGACY_ENGINE) 515 return segsearch_is_looking_for_blame_;
520 bool debug,
STRING *debug_str) {
532 if (segsearch_is_looking_for_blame_) {
533 segsearch_is_looking_for_blame_ =
false;
534 if (best_choice_is_dict_and_top_choice_) {
535 *debug_str =
"Best choice is: incorrect, top choice, dictionary word";
536 *debug_str +=
" with permuter ";
539 }
else if (best_correctly_segmented_rating_ <
541 *debug_str +=
"Correct segmentation state was not explored";
544 if (best_correctly_segmented_rating_ >=
546 *debug_str +=
"Correct segmentation paths were pruned by LM\n";
549 best_correctly_segmented_rating_);
572 STRING debug_str =
"Choice is incorrect after recognition";
591 misadaption_debug_ =
"misadapt to word (";
593 misadaption_debug_ +=
"): ";
const char kBlameSegsearchHeur[]
DLLSYM void tprintf(const char *format,...)
void InsertBox(int index, const TBOX &box)
static const char * permuter_name(uint8_t permuter)
const char *const kIncorrectResultReasonNames[]
static const char * IncorrectReasonName(IncorrectResultReason irr)
GenericVector< TBLOB * > blobs
const char kBlameClassifier[]
bool x_almost_equal(const TBOX &box, int tolerance) const
bool GuidedSegsearchStillGoing() const
BlamerBundle * blamer_bundle
BLOB_CHOICE_LIST * GetBlobChoices(int index) const
static void LastChanceBlame(bool debug, WERD_RES *word)
const char * IncorrectReason() const
void FinishSegSearch(const WERD_CHOICE *best_choice, bool debug, STRING *debug_str)
const char * string() const
const UNICHARSET * unicharset() const
const char kBlameChopper[]
UNICHAR_ID unichar_id() const
bool GuidedSegsearchNeeded(const WERD_CHOICE *best_choice) const
void InitForSegSearch(const WERD_CHOICE *best_choice, MATRIX *ratings, UNICHAR_ID wildcard_id, bool debug, STRING *debug_str, TessResultCallback2< bool, int, int > *pp_cb)
void StartHypothesisList()
void NormTransform(const DENORM *first_norm, const TPOINT &pt, TPOINT *transformed) const
void SetWordTruth(const UNICHARSET &unicharset, const char *truth_str, const TBOX &word_box)
void string_and_lengths(STRING *word_str, STRING *word_lengths_str) const
UNICHAR_ID unichar_to_id(const char *const unichar_repr) const
bool ChoiceIsCorrect(const WERD_CHOICE *word_choice) const
const CHAR_FRAGMENT * get_fragment(UNICHAR_ID unichar_id) const
void print_to_str(STRING *str) const
void JoinBlames(const BlamerBundle &bundle1, const BlamerBundle &bundle2, bool debug)
void truncate_at(int32_t index)
const char kBlameUnknown[]
const char kBlameNoTruth[]
void add_str_int(const char *str, int number)
UNICHAR_ID unichar_id(int index) const
const char kBlameAdaption[]
void BlameClassifier(const UNICHARSET &unicharset, const TBOX &blob_box, const BLOB_CHOICE_LIST &choices, bool debug)
const char kBlameNoTruthSplit[]
const char kBlameClassOldLMTradeoff[]
const char * id_to_unichar(UNICHAR_ID id) const
void FillDebugString(const STRING &msg, const WERD_CHOICE *choice, STRING *debug)
void SplitBundle(int word1_right, int word2_left, bool debug, BlamerBundle *bundle1, BlamerBundle *bundle2) const
static const float kBadRating
const TBOX & BlobBox(int index) const
const char kBlameCorrect[]
const char kBlamePageLayout[]
void SetupCorrectSegmentation(const TWERD *word, bool debug)
void SetChopperBlame(const WERD_RES *word, bool debug)
void SetMisAdaptionDebug(const WERD_CHOICE *best_choice, bool debug)
TBOX bounding_box() const
WERD_CHOICE * best_choice
bool encode_string(const char *str, bool give_up_on_failure, GenericVector< UNICHAR_ID > *encoding, GenericVector< char > *lengths, int *encoded_length) const
void SetupNormTruthWord(const DENORM &denorm)
const char kBlameSegsearchPP[]
STRING TruthString() const
void BlameClassifierOrLangModel(const WERD_RES *word, const UNICHARSET &unicharset, bool valid_permuter, bool debug)
void add_str_double(const char *str, double number)
void SetSymbolTruth(const UNICHARSET &unicharset, const char *char_str, const TBOX &char_box)
const char kBlameClassLMTradeoff[]
const char * get_normed_unichar(UNICHAR_ID unichar_id) const
const STRING & debug() const
bool Classified(int col, int row, int wildcard_id) const