25 #if !defined(DISABLED_LEGACY_ENGINE)
73 const char* truth_str,
const TBOX& word_box) {
75 truth_has_char_boxes_ =
false;
79 unicharset.
encode_string(truth_str,
false, &encoding, &lengths,
nullptr);
81 for (
int i = 0; i < encoding.
size(); total_length += lengths[i++]) {
82 STRING uch(truth_str + total_length);
93 const char* char_str,
const TBOX& char_box) {
94 STRING symbol_str(char_str);
96 if (
id != INVALID_UNICHAR_ID) {
98 if (normed_uch.length() > 0) symbol_str = normed_uch;
100 int length = truth_word_.
length();
104 truth_has_char_boxes_ =
true;
105 else if (truth_word_.
BlobBox(length - 1) == char_box)
106 truth_has_char_boxes_ =
false;
113 truth_has_char_boxes_ =
false;
118 if (word_choice ==
nullptr)
return false;
121 for (
int i = 0; i < word_choice->
length(); ++i) {
126 return truth_str == normed_choice_str;
132 (*debug) +=
"Truth ";
133 for (
int i = 0; i < this->truth_text_.
size(); ++i) {
134 (*debug) += this->truth_text_[i];
136 if (!this->truth_has_char_boxes_) (*debug) +=
" (no char boxes)";
137 if (choice !=
nullptr) {
138 (*debug) +=
" Choice ";
141 (*debug) += choice_str;
153 norm_box_tolerance_ = kBlamerBoxTolerance * denorm.
x_scale();
158 for (
int b = 0; b < truth_word_.
length(); ++b) {
160 topleft.
x = box.
left();
161 topleft.
y = box.
top();
166 TBOX norm_box(norm_topleft.
x, norm_botright.
y,
167 norm_botright.
x, norm_topleft.
y);
181 int begin2_truth_index = -1;
183 truth_has_char_boxes_) {
184 debug_str =
"Looking for truth split at";
187 debug_str +=
"\nnorm_truth_word boxes:\n";
188 if (norm_truth_word_.
length() > 1) {
190 for (b = 1; b < norm_truth_word_.
length(); ++b) {
192 if ((abs(word1_right - norm_truth_word_.
BlobBox(b - 1).
right()) <
193 norm_box_tolerance_) &&
194 (abs(word2_left - norm_truth_word_.
BlobBox(b).
left()) <
195 norm_box_tolerance_)) {
196 begin2_truth_index = b;
197 debug_str +=
"Split found";
206 if (begin2_truth_index > 0) {
207 bundle1->truth_has_char_boxes_ =
true;
208 bundle1->norm_box_tolerance_ = norm_box_tolerance_;
209 bundle2->truth_has_char_boxes_ =
true;
210 bundle2->norm_box_tolerance_ = norm_box_tolerance_;
212 for (b = 0; b < norm_truth_word_.
length(); ++b) {
213 if (b == begin2_truth_index) curr_bb = bundle2;
216 curr_bb->truth_text_.
push_back(truth_text_[b]);
222 debug_str +=
"Truth split not found";
223 debug_str += truth_has_char_boxes_ ?
224 "\n" :
" (no truth char boxes)\n";
236 if (bundle1.incorrect_result_reason_ !=
IRR_CORRECT &&
239 debug_str +=
"Blame from part 1: ";
240 debug_str += bundle1.debug_;
241 irr = bundle1.incorrect_result_reason_;
243 if (bundle2.incorrect_result_reason_ !=
IRR_CORRECT &&
246 debug_str +=
"Blame from part 2: ";
247 debug_str += bundle2.debug_;
249 irr = bundle2.incorrect_result_reason_;
250 }
else if (irr != bundle2.incorrect_result_reason_) {
254 incorrect_result_reason_ = irr;
256 SetBlame(irr, debug_str,
nullptr,
debug);
264 const TBOX& blob_box,
265 const BLOB_CHOICE_LIST& choices,
267 if (!truth_has_char_boxes_ ||
271 for (
int b = 0; b < norm_truth_word_.
length(); ++b) {
272 const TBOX &truth_box = norm_truth_word_.
BlobBox(b);
278 bool incorrect_adapted =
false;
279 UNICHAR_ID incorrect_adapted_id = INVALID_UNICHAR_ID;
280 const char *truth_str = truth_text_[b].c_str();
283 BLOB_CHOICE_IT choices_it(const_cast<BLOB_CHOICE_LIST*>(&choices));
284 for (choices_it.mark_cycle_pt(); !choices_it.cycled_list();
285 choices_it.forward()) {
292 incorrect_adapted =
true;
297 STRING debug_str =
"unichar ";
298 debug_str += truth_str;
299 debug_str +=
" not found in classification list";
301 }
else if (incorrect_adapted) {
302 STRING debug_str =
"better rating for adapted ";
304 debug_str +=
" than for correct ";
305 debug_str += truth_str;
317 if (
NoTruth() || !truth_has_char_boxes_ ||
322 bool missing_chop =
false;
326 int16_t truth_x = -1;
327 while (box_index < truth_word_.
length() && blob_index < num_blobs) {
334 truth_x + norm_box_tolerance_) {
341 if (missing_chop || box_index < norm_truth_word_.
length()) {
344 debug_str.
add_str_int(
"Detected missing chop (tolerance=",
345 norm_box_tolerance_);
346 debug_str +=
") at Bounding Box=";
349 debug_str.
add_str_int(
"\nNo chop for truth at x=", truth_x);
352 norm_truth_word_.
length() - box_index);
353 debug_str +=
" truth box(es)";
355 debug_str +=
"\nMaximally chopped word boxes:\n";
356 for (blob_index = 0; blob_index < num_blobs; ++blob_index) {
361 debug_str +=
"Truth bounding boxes:\n";
362 for (box_index = 0; box_index < norm_truth_word_.
length(); ++box_index) {
377 const UNICHARSET& unicharset,
bool valid_permuter,
bool debug) {
378 if (valid_permuter) {
380 best_choice_is_dict_and_top_choice_ =
true;
385 for (blob_choice_it.mark_cycle_pt(); !blob_choice_it.cycled_list();
386 blob_choice_it.forward()) {
387 if (!(unicharset.
get_fragment(blob_choice_it.data()->unichar_id()))) {
388 first_choice = blob_choice_it.data();
394 best_choice_is_dict_and_top_choice_ =
false;
400 if (best_choice_is_dict_and_top_choice_) {
401 debug_str =
"Best choice is: incorrect, top choice, dictionary word";
402 debug_str +=
" with permuter ";
405 debug_str =
"Classifier/Old LM tradeoff is to blame";
414 #ifndef DISABLED_LEGACY_ENGINE
416 #endif // ndef DISABLED_LEGACY_ENGINE
417 if (incorrect_result_reason_ !=
IRR_CORRECT || !truth_has_char_boxes_)
421 debug_str +=
"Blamer computing correct_segmentation_cols\n";
422 int curr_box_col = 0;
423 int next_box_col = 0;
424 int num_blobs =
word->NumBlobs();
425 if (num_blobs == 0)
return;
427 int16_t next_box_x =
word->blobs[blob_index]->bounding_box().right();
428 for (
int truth_idx = 0; blob_index < num_blobs &&
429 truth_idx < norm_truth_word_.
length();
432 int16_t curr_box_x = next_box_x;
433 if (blob_index + 1 < num_blobs)
434 next_box_x =
word->blobs[blob_index + 1]->bounding_box().right();
435 int16_t truth_x = norm_truth_word_.
BlobBox(truth_idx).
right();
436 debug_str.
add_str_int(
"Box x coord vs. truth: ", curr_box_x);
439 if (curr_box_x > (truth_x + norm_box_tolerance_)) {
441 }
else if (curr_box_x >= truth_x - norm_box_tolerance_ &&
442 (blob_index + 1 >= num_blobs ||
443 next_box_x > truth_x + norm_box_tolerance_)) {
444 correct_segmentation_cols_.
push_back(curr_box_col);
445 correct_segmentation_rows_.
push_back(next_box_col-1);
450 curr_box_col = next_box_col;
453 if (blob_index < num_blobs ||
454 correct_segmentation_cols_.
size() != norm_truth_word_.
length()) {
455 debug_str.
add_str_int(
"Blamer failed to find correct segmentation"
456 " (tolerance=", norm_box_tolerance_);
457 if (blob_index >= num_blobs) debug_str +=
" blob == nullptr";
459 debug_str.
add_str_int(
" path length ", correct_segmentation_cols_.
size());
463 correct_segmentation_cols_.
clear();
464 correct_segmentation_rows_.
clear();
471 !segsearch_is_looking_for_blame_ &&
472 truth_has_char_boxes_ &&
476 #if !defined(DISABLED_LEGACY_ENGINE)
480 bool debug,
STRING* debug_str,
482 double max_char_wh_ratio,
484 segsearch_is_looking_for_blame_ =
true;
486 tprintf(
"segsearch starting to look for blame\n");
490 *debug_str +=
"Correct segmentation:\n";
491 for (
int idx = 0; idx < correct_segmentation_cols_.
size(); ++idx) {
492 debug_str->
add_str_int(
"col=", correct_segmentation_cols_[idx]);
493 debug_str->
add_str_int(
" row=", correct_segmentation_rows_[idx]);
495 if (!ratings->
Classified(correct_segmentation_cols_[idx],
496 correct_segmentation_rows_[idx],
499 correct_segmentation_rows_[idx],
501 false, max_char_wh_ratio, word_res)) {
502 segsearch_is_looking_for_blame_ =
false;
503 *debug_str +=
"\nFailed to insert pain point\n";
509 #endif // !defined(DISABLED_LEGACY_ENGINE)
513 return segsearch_is_looking_for_blame_;
518 bool debug,
STRING *debug_str) {
530 if (segsearch_is_looking_for_blame_) {
531 segsearch_is_looking_for_blame_ =
false;
532 if (best_choice_is_dict_and_top_choice_) {
533 *debug_str =
"Best choice is: incorrect, top choice, dictionary word";
534 *debug_str +=
" with permuter ";
537 }
else if (best_correctly_segmented_rating_ <
539 *debug_str +=
"Correct segmentation state was not explored";
542 if (best_correctly_segmented_rating_ >=
544 *debug_str +=
"Correct segmentation paths were pruned by LM\n";
547 best_correctly_segmented_rating_);
570 STRING debug_str =
"Choice is incorrect after recognition";
589 misadaption_debug_ =
"misadapt to word (";
591 misadaption_debug_ +=
"): ";