36 static const
double kStopperAmbiguityThresholdGain = 8.0;
39 static const
double kStopperAmbiguityThresholdOffset = 1.5;
53 static
double StopperAmbigThreshold(
double f1,
double f2) {
54 return (f2 - f1) * kStopperAmbiguityThresholdGain -
55 kStopperAmbiguityThresholdOffset;
64 bool merge_similar_words,
65 BLOCK_LIST *the_block_list,
68 BLOCK_IT block_it(the_block_list);
70 for (block_it.mark_cycle_pt();
71 !block_it.cycled_list(); block_it.forward()) {
72 block_res_it.add_to_end(
new BLOCK_RES(merge_similar_words,
85 ROW_IT row_it (the_block->
row_list ());
99 for (row_it.mark_cycle_pt(); !row_it.cycled_list(); row_it.forward()) {
100 row_res_it.add_to_end(
new ROW_RES(merge_similar_words, row_it.data()));
121 bool add_next_word =
false;
125 for (word_it.mark_cycle_pt(); !word_it.cycled_list(); word_it.forward()) {
133 }
else if (merge_similar_words) {
137 word_res->
odd_size = !add_next_word;
139 WERD* next_word = word_it.data_relative(1);
140 if (merge_similar_words) {
148 int prev_right = union_box.
right();
149 union_box += next_box;
153 add_next_word =
false;
162 copy_word =
new WERD;
163 *copy_word = *(word_it.data());
167 word_res_it.add_to_end(combo);
173 word_res_it.add_to_end(word_res);
206 WERD_CHOICE_IT wc_it(const_cast<WERD_CHOICE_LIST*>(&source.
best_choices));
208 for (wc_it.mark_cycle_pt(); !wc_it.cycled_list(); wc_it.forward()) {
210 wc_dest_it.add_after_then_move(
new WERD_CHOICE(*choice));
212 if (!wc_dest_it.empty()) {
213 wc_dest_it.move_to_first();
297 const TBOX* norm_box,
300 bool allow_detailed_fx,
317 float word_xheight = use_body_size && row !=
NULL && row->
body_size() > 0.0f
321 norm_mode_hint, norm_box, &
denorm);
351 if (blob_count > 0) {
357 for (b_it.mark_cycle_pt(); !b_it.cycled_list(); b_it.forward()) {
358 TBOX box = b_it.data()->bounding_box();
363 delete [] fake_choices;
395 for (
int b = 0; b < num_blobs; ++b) {
399 if (b + 1 < num_blobs) {
420 for (wc_it.mark_cycle_pt(); !wc_it.cycled_list(); wc_it.forward()) {
432 WERD_CHOICE_IT wc_it(const_cast<WERD_CHOICE_LIST*>(&
best_choices));
433 for (wc_it.forward(); !wc_it.at_first(); wc_it.forward()) {
452 tprintf(
"raw_choice has total of states = %d vs ratings dim of %d\n",
458 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward(), ++index) {
461 tprintf(
"Cooked #%d has total of states = %d vs ratings dim of %d\n",
480 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward(), ++index) {
491 tprintf(
"Best choice: accepted=%d, adaptable=%d, done=%d : ",
508 if (debug_level >= 2)
512 for (it.forward(); !it.at_first(); it.forward(), ++index) {
521 int i = 0, j = 0, chunk = 0;
527 while (i < choice->length() && j < best_choice->length()) {
530 if (debug_level >= 2) {
532 label.
add_str_int(
"\nDiscarding bad choice #", index);
534 tprintf(
"i %d j %d Chunk %d Choice->Blob[i].Certainty %.4g"
535 " BestChoice->ChunkCertainty[Chunk] %g Threshold %g\n",
544 while (choice_chunk < chunk && ++i < choice->length())
545 choice_chunk += choice->
state(i);
547 while (best_chunk < chunk && ++j < best_choice->length())
563 float avg_rating = 0.0f;
564 int num_error_chunks = 0;
567 while (chunk < end_chunk) {
568 if (chunk >= end_raw_chunk) {
580 if (num_error_chunks > 0) {
581 avg_rating /= num_error_chunks;
582 *thresholds = (avg_rating / -certainty_scale) * (1.0 - rating_margin);
584 *thresholds = max_rating;
587 if (*thresholds > max_rating)
588 *thresholds = max_rating;
589 if (*thresholds < min_rating)
590 *thresholds = min_rating;
620 float max_certainty_delta =
623 if (max_certainty_delta > -kStopperAmbiguityThresholdOffset)
624 max_certainty_delta = -kStopperAmbiguityThresholdOffset;
626 max_certainty_delta) {
630 tprintf(
"Discarding choice \"%s\" with an overly low certainty"
631 " %.3f vs best choice certainty %.3f (Threshold: %.3f)\n",
645 bool inserted =
false;
650 if (choice->
rating() > word_choice->
rating() && !inserted) {
652 it.add_before_stay_put(word_choice);
654 if (num_choices == 0)
665 tprintf(
"Discarding duplicate choice \"%s\", rating %g vs %g\n",
673 if (num_choices > max_num_choices)
677 }
while (!it.at_first());
679 if (!inserted && num_choices < max_num_choices) {
680 it.add_to_end(word_choice);
682 if (num_choices == 0)
690 word_choice->
print(
" Word Choice");
702 template<
class T>
static void MovePointerData(T** dest, T**src) {
711 WERD_CHOICE_IT it(const_cast<WERD_CHOICE_LIST*>(&
best_choices));
712 for (it.mark_cycle_pt(); !it.cycled_list(); it.forward()) {
713 if (!it.at_first()) alternates_str +=
"\", \"";
714 alternates_str += it.data()->unichar_string();
716 tprintf(
"Alternates for \"%s\": {\"%s\"}\n",
724 for (
int b = start_blob; b <= last_blob; ++b) {
836 for (
int i = 0; i < word_len; ++i) {
863 for (wc_it.mark_cycle_pt(); !wc_it.cycled_list(); wc_it.forward())
864 wc_it.data()->SetAllScriptPositions(position);
879 for (
int c = 0; c < blob_count; ++c) {
880 BLOB_CHOICE_LIST* choice_list =
new BLOB_CHOICE_LIST;
881 BLOB_CHOICE_IT choice_it(choice_list);
882 choice_it.add_after_then_move(choices[c]);
896 for (
int b = 0; b < num_blobs; ++b) {
900 BLOB_CHOICE_LIST* choices =
ratings->
get(b, b);
901 if (choices !=
NULL && !choices->empty()) {
902 BLOB_CHOICE_IT bc_it(choices);
905 rating = choice->
rating();
936 bool modified =
false;
940 if (new_id != INVALID_UNICHAR_ID &&
956 BLOB_CHOICE_IT bc_it(blob_choices);
957 bc_it.add_before_then_move(blob_choice);
986 static int is_simple_quote(
const char* signed_str,
int length) {
987 const unsigned char* str =
988 reinterpret_cast<const unsigned char*
>(signed_str);
990 return (length == 1 && (*str ==
'\'' || *str ==
'`')) ||
992 (length == 3 && ((*str == 0xe2 &&
993 *(str + 1) == 0x80 &&
994 *(str + 2) == 0x98) ||
996 *(str + 1) == 0x80 &&
997 *(str + 2) == 0x99)));
1005 if (is_simple_quote(ch, strlen(ch)) &&
1006 is_simple_quote(next_ch, strlen(next_ch)))
1008 return INVALID_UNICHAR_ID;
1027 if (strlen(ch) == 1 && strlen(next_ch) == 1 &&
1028 (*ch ==
'-' || *ch ==
'~') && (*next_ch ==
'-' || *next_ch ==
'~'))
1030 return INVALID_UNICHAR_ID;
1057 return INVALID_UNICHAR_ID;
1074 for (
int index = start; index < start + count - 1; ++index) {
1195 return word_res == other.word_res &&
1196 row_res == other.row_res &&
1197 block_res == other.block_res;
1202 if (other.block_res ==
NULL) {
1204 if (block_res ==
NULL)
1208 if (block_res ==
NULL) {
1211 if (block_res == other.block_res) {
1212 if (other.row_res ==
NULL || row_res ==
NULL) {
1216 if (row_res == other.row_res) {
1219 if (word_res == other.word_res) {
1225 for (word_res_it.mark_cycle_pt(); !word_res_it.cycled_list();
1226 word_res_it.forward()) {
1227 if (word_res_it.data() == word_res) {
1229 }
else if (word_res_it.data() == other.word_res) {
1238 for (row_res_it.mark_cycle_pt(); !row_res_it.cycled_list();
1239 row_res_it.forward()) {
1240 if (row_res_it.data() == row_res) {
1242 }
else if (row_res_it.data() == other.row_res) {
1251 for (block_res_it.mark_cycle_pt();
1252 !block_res_it.cycled_list(); block_res_it.forward()) {
1253 if (block_res_it.data() == block_res) {
1255 }
else if (block_res_it.data() == other.block_res) {
1275 WERD_RES_IT wr_it(&
row()->word_res_list);
1276 for (wr_it.mark_cycle_pt(); !wr_it.cycled_list(); wr_it.forward()) {
1278 if (word == word_res)
1282 wr_it.add_before_then_move(new_res);
1283 if (wr_it.at_first()) {
1294 static void ComputeBlobEnds(
const WERD_RES& word, C_BLOB_LIST* next_word_blobs,
1300 TBOX blob_box = blob_it.data()->bounding_box();
1302 for (
int b = 1; b < length; ++b) {
1303 blob_box += blob_it.data()->bounding_box();
1309 if (!blob_it.at_first() || next_word_blobs !=
NULL) {
1310 if (blob_it.at_first())
1311 blob_it.set_to_list(next_word_blobs);
1312 blob_end = (blob_box.
right() + blob_it.data()->bounding_box().left()) / 2;
1323 if (words->
empty()) {
1330 (*words)[0]->word->set_flag(
W_BOL,
true);
1332 (*words)[0]->word->set_blanks(1);
1342 WERD_IT w_it(
row()->
row->word_list());
1344 for (w_it.mark_cycle_pt(); !w_it.cycled_list(); w_it.forward()) {
1345 WERD* word = w_it.data();
1346 if (word == input_word->
word)
1353 WERD_RES_IT wr_it(&
row()->word_res_list);
1354 for (wr_it.mark_cycle_pt(); !wr_it.cycled_list(); wr_it.forward()) {
1356 if (word == input_word)
1366 for (
int w = 0; w < words->
size(); ++w) {
1370 C_BLOB_LIST* next_word_blobs =
1371 w + 1 < words->
size() ? (*words)[w + 1]->word->cblob_list() :
NULL;
1372 ComputeBlobEnds(*word_w, next_word_blobs, &blob_ends);
1378 for (
int i = 0; i < blob_ends.
size(); ++i) {
1379 int end_x = blob_ends[i];
1382 while (!src_b_it.empty() &&
1383 src_b_it.data()->bounding_box().x_middle() < end_x) {
1384 blob_box += src_b_it.data()->bounding_box();
1385 dest_it.add_after_then_move(src_b_it.extract());
1388 while (!rej_b_it.empty() &&
1389 rej_b_it.data()->bounding_box().x_middle() < end_x) {
1390 blob_box += rej_b_it.data()->bounding_box();
1391 dest_it.add_after_then_move(rej_b_it.extract());
1397 if (i > 0 && blob_box.
left() < blob_ends[i - 1])
1398 blob_box.
set_left(blob_ends[i - 1]);
1399 if (blob_box.
right() > end_x)
1401 box_word->InsertBox(i, blob_box);
1406 for (
int i = 0; i < box_word->length(); ++i) {
1407 TBOX box = box_word->BlobBox(i);
1411 for (dest_it.mark_cycle_pt(); !dest_it.cycled_list();
1412 dest_it.forward()) {
1413 TBOX blob_box = dest_it.data()->bounding_box();
1414 if (blob_box.
left() < blob_ends[i] &&
1415 (i == 0 || blob_box.
right() >= blob_ends[i - 1])) {
1416 if (i > 0 && blob_box.
left() < blob_ends[i - 1])
1417 blob_box.
set_left(blob_ends[i - 1]);
1418 if (blob_box.
right() > blob_ends[i])
1420 box_word->ChangeBox(i, blob_box);
1431 w_it.add_before_stay_put(word_w->
word);
1435 wr_it.add_before_stay_put(word_w);
1443 delete w_it.extract();
1444 delete wr_it.extract();
1457 WERD_IT w_it(
row()->
row->word_list());
1458 for (w_it.mark_cycle_pt(); !w_it.cycled_list(); w_it.forward()) {
1459 if (w_it.data() == word_res->
word) {
1464 delete w_it.extract();
1468 WERD_RES_IT wr_it(&
row()->word_res_list);
1469 for (wr_it.mark_cycle_pt(); !wr_it.cycled_list(); wr_it.forward()) {
1470 if (wr_it.data() == word_res) {
1476 delete wr_it.extract();
1489 WERD_RES_IT wr_it(&
row()->word_res_list);
1490 for (wr_it.mark_cycle_pt();
1491 !wr_it.cycled_list() && wr_it.data() != word_res; wr_it.forward()) {
1495 real_word = wr_it.data()->word;
1511 block_res_it.mark_cycle_pt();
1512 prev_block_res =
NULL;
1513 prev_row_res =
NULL;
1514 prev_word_res =
NULL;
1518 next_block_res =
NULL;
1519 next_row_res =
NULL;
1520 next_word_res =
NULL;
1521 internal_forward(
true, empty_ok);
1522 return internal_forward(
false, empty_ok);
1533 if (row_res == next_row_res) {
1536 word_res_it.move_to_first();
1537 for (word_res_it.mark_cycle_pt();
1538 !word_res_it.cycled_list() && word_res_it.data() != next_word_res;
1539 word_res_it.forward()) {
1540 if (!word_res_it.data()->part_of_combo) {
1541 if (prev_row_res == row_res) prev_word_res = word_res;
1542 word_res = word_res_it.data();
1546 word_res_it.forward();
1550 for (wr_it.mark_cycle_pt(); !wr_it.cycled_list(); wr_it.forward()) {
1551 if (!wr_it.data()->part_of_combo) {
1552 if (prev_row_res == row_res) prev_word_res = word_res;
1553 word_res = wr_it.data();
1574 WERD_RES *PAGE_RES_IT::internal_forward(
bool new_block,
bool empty_ok) {
1575 bool new_row =
false;
1577 prev_block_res = block_res;
1578 prev_row_res = row_res;
1579 prev_word_res = word_res;
1580 block_res = next_block_res;
1581 row_res = next_row_res;
1582 word_res = next_word_res;
1583 next_block_res =
NULL;
1584 next_row_res =
NULL;
1585 next_word_res =
NULL;
1587 while (!block_res_it.cycled_list()) {
1590 row_res_it.set_to_list(&block_res_it.data()->row_res_list);
1591 row_res_it.mark_cycle_pt();
1592 if (row_res_it.empty() && empty_ok) {
1593 next_block_res = block_res_it.data();
1598 while (!row_res_it.cycled_list()) {
1601 word_res_it.set_to_list(&row_res_it.data()->word_res_list);
1602 word_res_it.mark_cycle_pt();
1605 while (!word_res_it.cycled_list() && word_res_it.data()->part_of_combo)
1606 word_res_it.forward();
1607 if (!word_res_it.cycled_list()) {
1608 next_block_res = block_res_it.data();
1609 next_row_res = row_res_it.data();
1610 next_word_res = word_res_it.data();
1611 word_res_it.forward();
1615 row_res_it.forward();
1619 block_res_it.forward();
1638 if (!row)
return NULL;
1652 while (block_res == next_block_res &&
1653 (next_row_res !=
NULL && next_row_res->
row !=
NULL &&
1655 internal_forward(
false,
true);
1657 return internal_forward(
false,
true);
1667 while (block_res == next_block_res) {
1668 internal_forward(
false,
true);
1670 return internal_forward(
false,
true);
1674 inT16 chars_in_word;
1675 inT16 rejects_in_word = 0;
1685 block_res->
rej_count += rejects_in_word;
1687 if (chars_in_word == rejects_in_word)
void SetupFake(const UNICHARSET &uch)
void SetScriptPositions()
void ConsumeWordResults(WERD_RES *word)
void DebugWordChoices(bool debug, const char *word_to_debug)
void set_unichar_id(UNICHAR_ID unichar_id, int index)
BLOB_CHOICE_LIST * blob_choices(int index, MATRIX *ratings) const
bool LogNewRawChoice(WERD_CHOICE *word_choice)
WERD_RES_LIST word_res_list
WERD_CHOICE_LIST best_choices
tesseract::BoxWord * box_word
void remove_unichar_id(int index)
BLOCK_RES_LIST block_res_list
const UNICHAR_ID unichar_to_id(const char *const unichar_repr) const
void SetScriptPositions(bool small_caps, TWERD *word)
bool HasAnySplits() const
void ReplaceBestChoice(WERD_CHOICE *choice)
WERD_RES * start_page(bool empty_ok)
void ComputeAdaptionThresholds(float certainty_scale, float min_rating, float max_rating, float rating_margin, float *thresholds)
WERD_CHOICE * best_choice
T get(int column, int row) const
#define ELISTIZE(CLASSNAME)
void ReplaceCurrentWord(tesseract::PointerVector< WERD_RES > *words)
bool LogNewCookedChoice(int max_num_choices, bool debug, WERD_CHOICE *word_choice)
void append_unichar_id_space_allocated(UNICHAR_ID unichar_id, int blob_count, float rating, float certainty)
CLISTIZE(BLOCK_RES) ELISTIZE(ROW_RES) ELISTIZE(WERD_RES) static const double kStopperAmbiguityThresholdGain
inT32 whole_word_rej_count
const TBOX & BlobBox(int index) const
WERD_RES & operator=(const WERD_RES &source)
void MergeBoxes(int start, int end)
void set_permuter(uinT8 perm)
void MergeBlobs(int start, int end)
const double kMaxWordSizeRatio
void SetupBasicsFromChoppedWord(const UNICHARSET &unicharset_in)
void SetupWordScript(const UNICHARSET &unicharset_in)
TBOX bounding_box() const
void InitForRetryRecognition(const WERD_RES &source)
GenericVector< STRING > correct_text
void operator=(const ELIST_LINK &)
void UpdateStateForSplit(int blob_position)
const FontInfo * fontinfo
static int SortByXMiddle(const void *v1, const void *v2)
void ComputeBoundingBoxes()
bool PrepareToInsertSeam(const GenericVector< SEAM * > &seams, const GenericVector< TBLOB * > &blobs, int insert_index, bool modify)
BLOB_CHOICE * FindMatchingChoice(UNICHAR_ID char_id, BLOB_CHOICE_LIST *bc_list)
void put(int column, int row, const T &thing)
void start_seam_list(TWERD *word, GenericVector< SEAM * > *seam_array)
bool dangerous_ambig_found() const
static TWERD * PolygonalCopy(bool allow_detailed_fx, WERD *src)
UNICHAR_ID BothHyphens(UNICHAR_ID id1, UNICHAR_ID id2)
bool HyphenBoxesOverlap(const TBOX &box1, const TBOX &box2)
void ClipToOriginalWord(const BLOCK *block, WERD *original_word)
const STRING & unichar_string() const
void delete_matrix_pointers()
void DebugTopChoice(const char *msg) const
int GetBlobsGap(int blob_index)
MATRIX_COORD MatrixCoord(int index) const
void MakeCurrentWordFuzzy()
WERD_RES * restart_page()
int state(int index) const
void insert(T t, int index)
void FakeClassifyWord(int blob_count, BLOB_CHOICE **choices)
void make_bad()
Set the fields in this choice to be default (bad) values.
void CloneChoppedToRebuild()
bool script_has_xheight() const
const UNICHAR_ID unichar_id(int index) const
void delete_data_pointers()
const char *const id_to_unichar(UNICHAR_ID id) const
const double kMaxLineSizeRatio
const UNICHARSET * uch_set
UNICHAR_ID BothSpaces(UNICHAR_ID id1, UNICHAR_ID id2)
void FilterWordChoices(int debug_level)
GenericVector< int > blob_gaps
WERD_RES * forward_block()
bool Valid(const MATRIX &m) const
void SetupBlobWidthsAndGaps()
tesseract::Tesseract * tesseract
bool PiecesAllNatural(int start, int count) const
void PrintBestChoices() const
_ConstTessMemberResultCallback_0_0< false, R, T1 >::base * NewPermanentTessCallback(const T1 *obj, R(T2::*member)() const)
GenericVector< SEAM * > seam_array
int cmp(const PAGE_RES_IT &other) const
void remove_pos(inT16 pos)
const FontInfo * fontinfo2
void set_script_id(int id)
void InsertSeam(int blob_number, SEAM *seam)
static void JoinPieces(const GenericVector< SEAM * > &seams, const GenericVector< TBLOB * > &blobs, int first, int last)
void string_and_lengths(STRING *word_str, STRING *word_lengths_str) const
int GetBlobsWidth(int start_blob, int last_blob)
ROW_RES_LIST row_res_list
void add_str_int(const char *str, int number)
WERD_RES * InsertSimpleCloneWord(const WERD_RES &clone_res, WERD *new_word)
void CopyTruth(const BlamerBundle &other)
GenericVector< TBLOB * > blobs
bool AlternativeChoiceAdjustmentsWorseThan(float threshold) const
tesseract::BoxWord * bln_boxes
bool ConditionalBlobMerge(TessResultCallback2< UNICHAR_ID, UNICHAR_ID, UNICHAR_ID > *class_cb, TessResultCallback2< bool, const TBOX &, const TBOX & > *box_cb)
bool get_enabled(UNICHAR_ID unichar_id) const
float adjust_factor() const
MATRIX * ConsumeAndMakeBigger(int ind)
bool SetupForRecognition(const UNICHARSET &unicharset_in, tesseract::Tesseract *tesseract, Pix *pix, int norm_mode, const TBOX *norm_box, bool numeric_mode, bool use_body_size, bool allow_detailed_fx, ROW *row, const BLOCK *block)
const int kWordrecMaxNumJoinChunks
void CopyResults(const BlamerBundle &other)
void SetAllScriptPositions(tesseract::ScriptPos position)
void InsertBox(int index, const TBOX &box)
void MergeAdjacentBlobs(int index)
BOOL8 flag(WERD_FLAGS mask) const
static BoxWord * CopyFromNormalized(TWERD *tessword)
bool contains_unichar(const char *const unichar_repr) const
void initialise(inT16 length)
void move(GenericVector< T > *from)
const BLOCK * block() const
GenericVector< int > best_state
CRUNCH_MODE unlv_crunch_mode
GenericVector< int > blob_widths
void SetupNormTruthWord(const DENORM &denorm)
UNICHAR_ID BothQuotes(UNICHAR_ID id1, UNICHAR_ID id2)
bool operator==(const PAGE_RES_IT &other) const
BLOB_CHOICE * GetBlobChoice(int index) const
WERD_CHOICE ** prev_word_best_choice
void SetAllScriptPositions(tesseract::ScriptPos position)
TBOX bounding_box() const
const double kMaxWordGapRatio
ROW_LIST * row_list()
get rows
void copy_on(WERD_RES *word_res)
static void BreakPieces(const GenericVector< SEAM * > &seams, const GenericVector< TBLOB * > &blobs, int first, int last)
const char * string() const
WERD_RES * forward_paragraph()
POLY_BLOCK * poly_block() const
int TotalOfStates() const
void set_flag(WERD_FLAGS mask, BOOL8 value)
void FakeWordFromRatings()
void BestChoiceToCorrectText()
UNICHAR_ID unichar_id() const
BLOB_CHOICE_LIST * GetBlobChoices(int index) const
BlamerBundle * blamer_bundle
void set_unichar_id(UNICHAR_ID newunichar_id)
void BLNormalize(const BLOCK *block, const ROW *row, Pix *pix, bool inverse, float x_height, float baseline_shift, bool numeric_mode, tesseract::OcrEngineMode hint, const TBOX *norm_box, DENORM *word_denorm)
C_BLOB_LIST * rej_cblob_list()
C_BLOB_LIST * cblob_list()
void IncreaseBandSize(int bandwidth)
void CopySimpleFields(const WERD_RES &source)