55 static const char kPermuterTypeNoPerm[] =
"None";
56 static const char kPermuterTypePuncPerm[] =
"Punctuation";
57 static const char kPermuterTypeTopPerm[] =
"Top Choice";
58 static const char kPermuterTypeLowerPerm[] =
"Top Lower Case";
59 static const char kPermuterTypeUpperPerm[] =
"Top Upper Case";
60 static const char kPermuterTypeNgramPerm[] =
"Ngram";
61 static const char kPermuterTypeNumberPerm[] =
"Number";
62 static const char kPermuterTypeUserPatPerm[] =
"User Pattern";
63 static const char kPermuterTypeSysDawgPerm[] =
"System Dictionary";
64 static const char kPermuterTypeDocDawgPerm[] =
"Document Dictionary";
65 static const char kPermuterTypeUserDawgPerm[] =
"User Dictionary";
66 static const char kPermuterTypeFreqDawgPerm[] =
"Frequent Words Dictionary";
67 static const char kPermuterTypeCompoundPerm[] =
"Compound";
69 static const char *
const kPermuterTypeNames[] = {
71 kPermuterTypePuncPerm,
73 kPermuterTypeLowerPerm,
74 kPermuterTypeUpperPerm,
75 kPermuterTypeNgramPerm,
76 kPermuterTypeNumberPerm,
77 kPermuterTypeUserPatPerm,
78 kPermuterTypeSysDawgPerm,
79 kPermuterTypeDocDawgPerm,
80 kPermuterTypeUserDawgPerm,
81 kPermuterTypeFreqDawgPerm,
82 kPermuterTypeCompoundPerm
98 unichar_id_ = src_unichar_id;
100 certainty_ = src_cert;
103 script_id_ = src_script_id;
122 matrix_cell_ = other.matrix_cell_;
123 min_xheight_ = other.min_xheight_;
124 max_xheight_ = other.max_xheight_;
126 classifier_ = other.classifier_;
127 fonts_ = other.fonts_;
137 tprintf(
"Baseline diff %g for %d v %d\n",
138 baseline_diff, unichar_id_, other.unichar_id_);
148 overlap /= denominator;
150 tprintf(
"PosAndSize for %d v %d: bl diff = %g, ranges %g, %g / %g ->%g\n",
151 unichar_id_, other.unichar_id_, baseline_diff,
152 this_range, other_range, denominator, overlap);
161 BLOB_CHOICE_LIST* bc_list) {
163 BLOB_CHOICE_IT choice_it(bc_list);
164 for (choice_it.mark_cycle_pt(); !choice_it.cycled_list();
165 choice_it.forward()) {
175 return kPermuterTypeNames[
permuter];
181 switch (script_pos) {
200 : unicharset_(&unicharset){
205 STRING src_lengths = &lengths[0];
224 const char *src_lengths,
227 uinT8 src_permuter) {
228 int src_string_len = strlen(src_string);
229 if (src_string_len == 0) {
232 this->
init(src_lengths ? strlen(src_lengths): src_string_len);
235 for (
int i = 0; i < length_; ++i) {
236 int unichar_length = src_lengths ? src_lengths[i] : 1;
238 unicharset_->
unichar_to_id(src_string+offset, unichar_length);
240 certainties_[i] = src_certainty;
241 offset += unichar_length;
244 adjust_factor_ = 1.0f;
245 rating_ = src_rating;
246 certainty_ = src_certainty;
247 permuter_ = src_permuter;
248 dangerous_ambig_found_ =
false;
255 delete[] unichar_ids_;
256 delete[] script_pos_;
258 delete[] certainties_;
262 return kPermuterTypeNames[permuter_];
270 BLOB_CHOICE_LIST* result = ratings->
get(coord.
col, coord.
row);
271 if (result == NULL) {
272 result =
new BLOB_CHOICE_LIST;
273 ratings->
put(coord.
col, coord.
row, result);
282 for (
int i = 0; i < index; ++i)
284 int row = col + state_[index] - 1;
292 unichar_ids_[index] = blob_choice->
unichar_id();
294 state_[index] = blob_count;
295 certainties_[index] = blob_choice->
certainty();
305 for (
int i = 0; i < length_; ++i) {
323 for (
int i = 0; i < num; ++i) {
325 state_[start - 1] += state_[start + i];
326 else if (start + num < length_)
327 state_[start + num] += state_[start + i];
329 for (
int i = start; i + num < length_; ++i) {
330 unichar_ids_[i] = unichar_ids_[i + num];
331 script_pos_[i] = script_pos_[i + num];
332 state_[i] = state_[i + num];
333 certainties_[i] = certainties_[i + num];
344 for (
int i = 0; i < length_ / 2; ++i) {
346 unichar_ids_[i] = unicharset_->
get_mirror(unichar_ids_[length_-1-i]);
347 unichar_ids_[length_-1-i] = unicharset_->
get_mirror(tmp_id);
349 if (length_ % 2 != 0) {
350 unichar_ids_[length_/2] = unicharset_->
get_mirror(unichar_ids_[length_/2]);
364 while (*start <
length() &&
383 while (start < end &&
395 if (end < start) { end = start; }
397 for (
int i = start; i < end; i++) {
399 unichar_ids_[i], state_[i], 0.0f, certainties_[i]);
411 for (i = 0; i < length_; ++i) {
428 STRING *word_lengths_str)
const {
430 if (word_lengths_str != NULL) *word_lengths_str =
"";
431 for (
int i = 0; i < length_; ++i) {
434 if (word_lengths_str != NULL) {
435 *word_lengths_str += strlen(ch);
448 float rating,
float certainty) {
449 if (length_ == reserved_) {
465 while (reserved_ < length_ + second.
length()) {
469 for (
int i = 0; i < second.
length(); ++i) {
470 unichar_ids_[length_ + i] = other_unichar_ids[i];
471 state_[length_ + i] = second.state_[i];
472 certainties_[length_ + i] = second.certainties_[i];
475 length_ += second.
length();
476 if (second.adjust_factor_ > adjust_factor_)
477 adjust_factor_ = second.adjust_factor_;
478 rating_ += second.
rating();
481 if (second.dangerous_ambig_found_)
482 dangerous_ambig_found_ =
true;
500 while (reserved_ < source.
length()) {
504 unicharset_ = source.unicharset_;
506 for (
int i = 0; i < source.
length(); ++i) {
507 unichar_ids_[i] = other_unichar_ids[i];
508 state_[i] = source.state_[i];
509 certainties_[i] = source.certainties_[i];
512 length_ = source.
length();
513 adjust_factor_ = source.adjust_factor_;
514 rating_ = source.
rating();
519 dangerous_ambig_found_ = source.dangerous_ambig_found_;
535 for (
int i = 0; i < length_; ++i)
541 int position_counts[4];
542 for (
int i = 0; i < 4; i++) {
543 position_counts[i] = 0;
547 for (
int blob_index = 0; blob_index < length_; ++blob_index, ++chunk_index) {
551 if (state_ != NULL) {
552 for (
int i = 1; i < state_[blob_index]; ++i) {
554 tblob = word->
blobs[chunk_index];
563 position_counts[script_pos_[blob_index]]++;
570 tprintf(
"Most characters of %s are subscript or superscript.\n" 571 "That seems wrong, so I'll assume we got the baseline wrong\n",
574 for (
int i = 0; i < length_; i++) {
577 position_counts[sp]--;
588 for (
int blob_index = 0; blob_index < length_; ++blob_index) {
594 chunk_index += state_ != NULL ? state_[blob_index] : 1;
602 if (positions != script_pos_) {
603 delete [] script_pos_;
605 memcpy(script_pos_, positions,
sizeof(positions[0]) *
length);
610 for (
int i = 0; i < length_; ++i)
611 script_pos_[i] = position;
617 const TBOX& blob_box,
620 int top = blob_box.
top();
621 int bottom = blob_box.
bottom();
622 int min_bottom, max_bottom, min_top, max_top;
624 &min_bottom, &max_bottom,
632 }
else if (top < sub_thresh_top && bottom < sub_thresh_bot) {
634 }
else if (bottom > sup_thresh_bot) {
640 tprintf(
"%s Character %s[bot:%d top: %d] " 641 "bot_range[%d,%d] top_range[%d, %d] " 642 "sub_thresh[bot:%d top:%d] sup_thresh_bot %d\n",
645 min_bottom, max_bottom, min_top, max_top,
646 sub_thresh_bot, sub_thresh_top,
655 int *sid =
new int[max_script];
657 for (x = 0; x < max_script; x++) sid[x] = 0;
658 for (x = 0; x < length_; ++x) {
676 for (x = 1; x < max_script; x++)
677 if (sid[x] >= sid[max_sid]) max_sid = x;
678 if (sid[max_sid] < length_ / 2)
686 int total_chunks = 0;
687 for (
int i = 0; i < length_; ++i) {
688 total_chunks += state_[i];
689 if (total_chunks > blob_position) {
698 int total_chunks = 0;
699 for (
int i = 0; i < length_; ++i) {
700 total_chunks += state_[i];
712 for (
int i = 0; i < length_; ++i) {
715 tprintf(
" : R=%g, C=%g, F=%g, Perm=%d, xht=[%g,%g], ambig=%d\n",
716 rating_, certainty_, adjust_factor_, permuter_,
717 min_x_height_, max_x_height_, dangerous_ambig_found_);
719 for (
int i = 0; i < length_; ++i) {
723 for (
int i = 0; i < length_; ++i) {
727 for (
int i = 0; i < length_; ++i) {
731 for (
int i = 0; i < length_; ++i) {
732 tprintf(
"\t%.3f", certainties_[i]);
740 for (
int i = 0; i < length_; ++i)
748 #ifndef GRAPHICS_DISABLED 750 const int kNumColors = 6;
754 bool already_done = prev_drawn_state.
size() == length_;
755 if (!already_done) prev_drawn_state.
init_to_size(length_, 0);
756 for (
int i = 0; i < length_; ++i) {
757 if (prev_drawn_state[i] != state_[i]) {
758 already_done =
false;
760 prev_drawn_state[i] = state_[i];
762 if (already_done || word->
blobs.
empty())
return;
765 if (segm_window == NULL) {
766 segm_window =
new ScrollView(
"Segmentation", 5, 10, 500, 256,
767 2000.0, 256.0,
true);
769 segm_window->
Clear();
774 for (
int c = 0; c < length_; ++c) {
777 for (
int i = 0; i < state_[c]; ++i, ++blob_index) {
780 blob->
plot(segm_window, color, color);
794 if (word2.
unicharset() != uchset)
return false;
799 if (w1end - w1start != w2end - w2start)
return false;
800 for (
int i = 0; i < w1end - w1start; i++) {
820 BLOB_CHOICE_LIST *ratings,
822 if (ratings->length() == 0) {
830 c_it.set_to_list(ratings);
831 for (c_it.mark_cycle_pt(); !c_it.cycled_list(); c_it.forward()) {
832 c_it.data()->print(¤t_unicharset);
833 if (!c_it.at_last())
tprintf(
"\n");
void string_and_lengths(STRING *word_str, STRING *word_lengths_str) const
const int kBlnBaselineOffset
void UpdateStateForSplit(int blob_position)
const UNICHAR_ID * unichar_ids() const
GenericVector< TBLOB * > blobs
BLOB_CHOICE * FindMatchingChoice(UNICHAR_ID char_id, BLOB_CHOICE_LIST *bc_list)
const STRING & unichar_string() const
const int kMinSubscriptOffset
UNICHAR_ID unichar_id(int index) const
int get_script_table_size() const
bool EqualIgnoringCaseAndTerminalPunct(const WERD_CHOICE &word1, const WERD_CHOICE &word2)
const double kMaxBaselineDrift
void get_top_bottom(UNICHAR_ID unichar_id, int *min_bottom, int *max_bottom, int *min_top, int *max_top) const
const char * permuter_name() const
UNICHAR_ID get_mirror(UNICHAR_ID unichar_id) const
Direction get_direction(UNICHAR_ID unichar_id) const
bool contains_unichar_id(UNICHAR_ID unichar_id) const
const double kMaxOverlapDenominator
const int kMaxDropCapBottom
tesseract::ScriptPos BlobPosition(int index) const
float min_xheight() const
void double_the_size()
Make more space in unichar_id_ and fragment_lengths_ arrays.
static tesseract::ScriptPos ScriptPositionOf(bool print_debug, const UNICHARSET &unicharset, const TBOX &blob_box, UNICHAR_ID unichar_id)
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
void SetAllScriptPositions(tesseract::ScriptPos position)
const char * id_to_unichar_ext(UNICHAR_ID id) const
WERD_CHOICE shallow_copy(int start, int end) const
TBOX bounding_box() const
void print_state(const char *msg) const
const UNICHARSET * unicharset() const
bool PosAndSizeAgree(const BLOB_CHOICE &other, float x_height, bool debug) const
UNICHAR_ID unichar_id() const
bool encode_string(const char *str, bool give_up_on_failure, GenericVector< UNICHAR_ID > *encoding, GenericVector< char > *lengths, int *encoded_length) const
void remove_unichar_ids(int index, int num)
int GetTopScriptID() const
const char * string() const
void make_bad()
Set the fields in this choice to be default (bad) values.
void DisplaySegmentation(TWERD *word)
void put(ICOORD pos, const T &thing)
void plot(ScrollView *window, ScrollView::Color color, ScrollView::Color child_color)
WERD_CHOICE & operator=(const WERD_CHOICE &source)
inT16 fontinfo_id2() const
void print_ratings_list(const char *msg, BLOB_CHOICE_LIST *ratings, const UNICHARSET ¤t_unicharset)
WERD_CHOICE & operator+=(const WERD_CHOICE &second)
MATRIX_COORD MatrixCoord(int index) const
float min_x_height() const
void reverse_and_mirror_unichar_ids()
int TotalOfStates() const
void punct_stripped(int *start_core, int *end_core) const
int get_script(UNICHAR_ID unichar_id) const
UNICHAR_ID TESS_API unichar_to_id(const char *const unichar_repr) const
void SetScriptPositions(bool small_caps, TWERD *word)
void set_blob_choice(int index, int blob_count, const BLOB_CHOICE *blob_choice)
float max_x_height() const
BLOB_CHOICE_LIST * blob_choices(int index, MATRIX *ratings) const
static const float kBadRating
void GetNonSuperscriptSpan(int *start, int *end) const
const double kMinXHeightMatch
inT16 fontinfo_id() const
bool get_isdigit(UNICHAR_ID unichar_id) const
WERD_CHOICE(const UNICHARSET *unicharset)
char window_wait(ScrollView *win)
const char * id_to_unichar(UNICHAR_ID id) const
float max_xheight() const
bool has_rtl_unichar_id() const
const int kMinSuperscriptOffset
void init_to_size(int size, T t)
UNICHAR_ID to_lower(UNICHAR_ID unichar_id) const
void append_unichar_id_space_allocated(UNICHAR_ID unichar_id, int blob_count, float rating, float certainty)
const char * ScriptPosToString(enum ScriptPos script_pos)
void ZoomToRectangle(int x1, int y1, int x2, int y2)
void append_unichar_id(UNICHAR_ID unichar_id, int blob_count, float rating, float certainty)