22 #include "config_auto.h" 56 static const char kPermuterTypeNoPerm[] =
"None";
57 static const char kPermuterTypePuncPerm[] =
"Punctuation";
58 static const char kPermuterTypeTopPerm[] =
"Top Choice";
59 static const char kPermuterTypeLowerPerm[] =
"Top Lower Case";
60 static const char kPermuterTypeUpperPerm[] =
"Top Upper Case";
61 static const char kPermuterTypeNgramPerm[] =
"Ngram";
62 static const char kPermuterTypeNumberPerm[] =
"Number";
63 static const char kPermuterTypeUserPatPerm[] =
"User Pattern";
64 static const char kPermuterTypeSysDawgPerm[] =
"System Dictionary";
65 static const char kPermuterTypeDocDawgPerm[] =
"Document Dictionary";
66 static const char kPermuterTypeUserDawgPerm[] =
"User Dictionary";
67 static const char kPermuterTypeFreqDawgPerm[] =
"Frequent Words Dictionary";
68 static const char kPermuterTypeCompoundPerm[] =
"Compound";
70 static const char *
const kPermuterTypeNames[] = {
72 kPermuterTypePuncPerm,
74 kPermuterTypeLowerPerm,
75 kPermuterTypeUpperPerm,
76 kPermuterTypeNgramPerm,
77 kPermuterTypeNumberPerm,
78 kPermuterTypeUserPatPerm,
79 kPermuterTypeSysDawgPerm,
80 kPermuterTypeDocDawgPerm,
81 kPermuterTypeUserDawgPerm,
82 kPermuterTypeFreqDawgPerm,
83 kPermuterTypeCompoundPerm
99 unichar_id_ = src_unichar_id;
100 rating_ = src_rating;
101 certainty_ = src_cert;
104 script_id_ = src_script_id;
123 matrix_cell_ = other.matrix_cell_;
124 min_xheight_ = other.min_xheight_;
125 max_xheight_ = other.max_xheight_;
127 classifier_ = other.classifier_;
128 fonts_ = other.fonts_;
138 tprintf(
"Baseline diff %g for %d v %d\n",
139 baseline_diff, unichar_id_, other.unichar_id_);
149 overlap /= denominator;
151 tprintf(
"PosAndSize for %d v %d: bl diff = %g, ranges %g, %g / %g ->%g\n",
152 unichar_id_, other.unichar_id_, baseline_diff,
153 this_range, other_range, denominator, overlap);
162 BLOB_CHOICE_LIST* bc_list) {
164 BLOB_CHOICE_IT choice_it(bc_list);
165 for (choice_it.mark_cycle_pt(); !choice_it.cycled_list();
166 choice_it.forward()) {
176 return kPermuterTypeNames[permuter];
182 switch (script_pos) {
205 if (unicharset.
encode_string(cleaned.c_str(),
true, &encoding, &lengths,
207 lengths.push_back(
'\0');
208 STRING src_lengths = &lengths[0];
227 const char *src_lengths,
230 uinT8 src_permuter) {
231 int src_string_len = strlen(src_string);
232 if (src_string_len == 0) {
235 this->
init(src_lengths ? strlen(src_lengths): src_string_len);
238 for (
int i = 0; i < length_; ++i) {
239 int unichar_length = src_lengths ? src_lengths[i] : 1;
241 unicharset_->
unichar_to_id(src_string+offset, unichar_length);
243 certainties_[i] = src_certainty;
244 offset += unichar_length;
247 adjust_factor_ = 1.0f;
248 rating_ = src_rating;
249 certainty_ = src_certainty;
250 permuter_ = src_permuter;
251 dangerous_ambig_found_ =
false;
258 delete[] unichar_ids_;
259 delete[] script_pos_;
261 delete[] certainties_;
265 return kPermuterTypeNames[permuter_];
273 BLOB_CHOICE_LIST* result = ratings->
get(coord.
col, coord.
row);
274 if (result == NULL) {
275 result =
new BLOB_CHOICE_LIST;
276 ratings->
put(coord.
col, coord.
row, result);
285 for (
int i = 0; i < index; ++i)
287 int row = col + state_[index] - 1;
295 unichar_ids_[index] = blob_choice->
unichar_id();
297 state_[index] = blob_count;
298 certainties_[index] = blob_choice->
certainty();
308 for (
int i = 0; i < length_; ++i) {
309 if (unichar_ids_[i] == unichar_id) {
326 for (
int i = 0; i < num; ++i) {
328 state_[start - 1] += state_[start + i];
329 else if (start + num < length_)
330 state_[start + num] += state_[start + i];
332 for (
int i = start; i + num < length_; ++i) {
333 unichar_ids_[i] = unichar_ids_[i + num];
334 script_pos_[i] = script_pos_[i + num];
335 state_[i] = state_[i + num];
336 certainties_[i] = certainties_[i + num];
347 for (
int i = 0; i < length_ / 2; ++i) {
349 unichar_ids_[i] = unicharset_->
get_mirror(unichar_ids_[length_-1-i]);
350 unichar_ids_[length_-1-i] = unicharset_->
get_mirror(tmp_id);
352 if (length_ % 2 != 0) {
353 unichar_ids_[length_/2] = unicharset_->
get_mirror(unichar_ids_[length_/2]);
367 while (*start <
length() &&
386 while (start < end &&
398 if (end < start) { end = start; }
400 for (
int i = start; i < end; i++) {
402 unichar_ids_[i], state_[i], 0.0f, certainties_[i]);
414 for (i = 0; i < length_; ++i) {
431 STRING *word_lengths_str)
const {
433 if (word_lengths_str != NULL) *word_lengths_str =
"";
434 for (
int i = 0; i < length_; ++i) {
437 if (word_lengths_str != NULL) {
438 *word_lengths_str += strlen(ch);
452 if (length_ == reserved_) {
468 while (reserved_ < length_ + second.
length()) {
472 for (
int i = 0; i < second.
length(); ++i) {
473 unichar_ids_[length_ + i] = other_unichar_ids[i];
474 state_[length_ + i] = second.state_[i];
475 certainties_[length_ + i] = second.certainties_[i];
478 length_ += second.
length();
479 if (second.adjust_factor_ > adjust_factor_)
480 adjust_factor_ = second.adjust_factor_;
481 rating_ += second.
rating();
484 if (second.dangerous_ambig_found_)
485 dangerous_ambig_found_ =
true;
503 while (reserved_ < source.
length()) {
507 unicharset_ = source.unicharset_;
509 for (
int i = 0; i < source.
length(); ++i) {
510 unichar_ids_[i] = other_unichar_ids[i];
511 state_[i] = source.state_[i];
512 certainties_[i] = source.certainties_[i];
515 length_ = source.
length();
516 adjust_factor_ = source.adjust_factor_;
517 rating_ = source.
rating();
522 dangerous_ambig_found_ = source.dangerous_ambig_found_;
538 for (
int i = 0; i < length_; ++i)
544 int position_counts[4];
545 for (
int i = 0; i < 4; i++) {
546 position_counts[i] = 0;
550 for (
int blob_index = 0; blob_index < length_; ++blob_index, ++chunk_index) {
554 if (state_ != NULL) {
555 for (
int i = 1; i < state_[blob_index]; ++i) {
557 tblob = word->
blobs[chunk_index];
566 position_counts[script_pos_[blob_index]]++;
573 tprintf(
"Most characters of %s are subscript or superscript.\n" 574 "That seems wrong, so I'll assume we got the baseline wrong\n",
577 for (
int i = 0; i < length_; i++) {
580 position_counts[sp]--;
591 for (
int blob_index = 0; blob_index < length_; ++blob_index) {
597 chunk_index += state_ != NULL ? state_[blob_index] : 1;
605 if (positions != script_pos_) {
606 delete [] script_pos_;
608 memcpy(script_pos_, positions,
sizeof(positions[0]) * length);
613 for (
int i = 0; i < length_; ++i)
614 script_pos_[i] = position;
620 const TBOX& blob_box,
623 int top = blob_box.
top();
624 int bottom = blob_box.
bottom();
625 int min_bottom, max_bottom, min_top, max_top;
627 &min_bottom, &max_bottom,
635 }
else if (top < sub_thresh_top && bottom < sub_thresh_bot) {
637 }
else if (bottom > sup_thresh_bot) {
643 tprintf(
"%s Character %s[bot:%d top: %d] " 644 "bot_range[%d,%d] top_range[%d, %d] " 645 "sub_thresh[bot:%d top:%d] sup_thresh_bot %d\n",
648 min_bottom, max_bottom, min_top, max_top,
649 sub_thresh_bot, sub_thresh_top,
658 int *sid =
new int[max_script];
660 for (x = 0; x < max_script; x++) sid[x] = 0;
661 for (x = 0; x < length_; ++x) {
679 for (x = 1; x < max_script; x++)
680 if (sid[x] >= sid[max_sid]) max_sid = x;
681 if (sid[max_sid] < length_ / 2)
689 int total_chunks = 0;
690 for (
int i = 0; i < length_; ++i) {
691 total_chunks += state_[i];
692 if (total_chunks > blob_position) {
701 int total_chunks = 0;
702 for (
int i = 0; i < length_; ++i) {
703 total_chunks += state_[i];
715 for (
int i = 0; i < length_; ++i) {
718 tprintf(
" : R=%g, C=%g, F=%g, Perm=%d, xht=[%g,%g], ambig=%d\n",
719 rating_, certainty_, adjust_factor_, permuter_,
720 min_x_height_, max_x_height_, dangerous_ambig_found_);
722 for (
int i = 0; i < length_; ++i) {
726 for (
int i = 0; i < length_; ++i) {
730 for (
int i = 0; i < length_; ++i) {
734 for (
int i = 0; i < length_; ++i) {
735 tprintf(
"\t%.3f", certainties_[i]);
743 for (
int i = 0; i < length_; ++i)
751 #ifndef GRAPHICS_DISABLED 753 const int kNumColors = 6;
757 bool already_done = prev_drawn_state.
size() == length_;
758 if (!already_done) prev_drawn_state.
init_to_size(length_, 0);
759 for (
int i = 0; i < length_; ++i) {
760 if (prev_drawn_state[i] != state_[i]) {
761 already_done =
false;
763 prev_drawn_state[i] = state_[i];
765 if (already_done || word->
blobs.
empty())
return;
768 if (segm_window == NULL) {
769 segm_window =
new ScrollView(
"Segmentation", 5, 10, 500, 256,
770 2000.0, 256.0,
true);
772 segm_window->
Clear();
777 for (
int c = 0; c < length_; ++c) {
780 for (
int i = 0; i < state_[c]; ++i, ++blob_index) {
783 blob->
plot(segm_window, color, color);
797 if (word2.
unicharset() != uchset)
return false;
802 if (w1end - w1start != w2end - w2start)
return false;
803 for (
int i = 0; i < w1end - w1start; i++) {
823 BLOB_CHOICE_LIST *ratings,
825 if (ratings->length() == 0) {
833 c_it.set_to_list(ratings);
834 for (c_it.mark_cycle_pt(); !c_it.cycled_list(); c_it.forward()) {
835 c_it.data()->print(¤t_unicharset);
836 if (!c_it.at_last())
tprintf(
"\n");
MATRIX_COORD MatrixCoord(int index) const
static const float kBadRating
void double_the_size()
Make more space in unichar_id_ and fragment_lengths_ arrays.
const char * id_to_unichar_ext(UNICHAR_ID id) const
void remove_unichar_ids(int index, int num)
void get_top_bottom(UNICHAR_ID unichar_id, int *min_bottom, int *max_bottom, int *min_top, int *max_top) const
void put(ICOORD pos, const T &thing)
bool encode_string(const char *str, bool give_up_on_failure, GenericVector< UNICHAR_ID > *encoding, GenericVector< char > *lengths, int *encoded_length) const
void print_ratings_list(const char *msg, BLOB_CHOICE_LIST *ratings, const UNICHARSET ¤t_unicharset)
bool get_isdigit(UNICHAR_ID unichar_id) const
int GetTopScriptID() const
float min_xheight() const
BLOB_CHOICE * FindMatchingChoice(UNICHAR_ID char_id, BLOB_CHOICE_LIST *bc_list)
void print_state(const char *msg) const
const UNICHARSET * unicharset() const
void UpdateStateForSplit(int blob_position)
void SetScriptPositions(bool small_caps, TWERD *word)
T ClipToRange(const T &x, const T &lower_bound, const T &upper_bound)
UNICHAR_ID unichar_id(int index) const
static string CleanupString(const char *utf8_str)
void SetAllScriptPositions(tesseract::ScriptPos position)
void append_unichar_id(UNICHAR_ID unichar_id, int blob_count, float rating, float certainty)
inT16 fontinfo_id2() const
int TotalOfStates() const
float min_x_height() const
UNICHAR_ID unichar_id() const
const char * permuter_name() const
WERD_CHOICE(const UNICHARSET *unicharset)
const char * string() const
bool has_rtl_unichar_id() const
const UNICHAR_ID * unichar_ids() const
Direction get_direction(UNICHAR_ID unichar_id) const
const double kMinXHeightMatch
BLOB_CHOICE_LIST * blob_choices(int index, MATRIX *ratings) const
void append_unichar_id_space_allocated(UNICHAR_ID unichar_id, int blob_count, float rating, float certainty)
UNICHAR_ID unichar_to_id(const char *const unichar_repr) const
WERD_CHOICE & operator+=(const WERD_CHOICE &second)
void reverse_and_mirror_unichar_ids()
tesseract::ScriptPos BlobPosition(int index) const
bool contains_unichar_id(UNICHAR_ID unichar_id) const
UNICHAR_ID to_lower(UNICHAR_ID unichar_id) const
char window_wait(ScrollView *win)
inT16 fontinfo_id() const
const int kMaxDropCapBottom
bool PosAndSizeAgree(const BLOB_CHOICE &other, float x_height, bool debug) const
TBOX bounding_box() const
void plot(ScrollView *window, ScrollView::Color color, ScrollView::Color child_color)
void set_blob_choice(int index, int blob_count, const BLOB_CHOICE *blob_choice)
#define ELISTIZE(CLASSNAME)
const int kMinSuperscriptOffset
void string_and_lengths(STRING *word_str, STRING *word_lengths_str) const
const int kBlnBaselineOffset
float max_xheight() const
void punct_stripped(int *start_core, int *end_core) const
void GetNonSuperscriptSpan(int *start, int *end) const
void ZoomToRectangle(int x1, int y1, int x2, int y2)
static tesseract::ScriptPos ScriptPositionOf(bool print_debug, const UNICHARSET &unicharset, const TBOX &blob_box, UNICHAR_ID unichar_id)
void make_bad()
Set the fields in this choice to be default (bad) values.
int get_script_table_size() const
GenericVector< TBLOB * > blobs
bool EqualIgnoringCaseAndTerminalPunct(const WERD_CHOICE &word1, const WERD_CHOICE &word2)
const int kMinSubscriptOffset
void init_to_size(int size, T t)
UNICHAR_ID get_mirror(UNICHAR_ID unichar_id) const
void DisplaySegmentation(TWERD *word)
WERD_CHOICE & operator=(const WERD_CHOICE &source)
WERD_CHOICE shallow_copy(int start, int end) const
float max_x_height() const
const double kMaxBaselineDrift
int get_script(UNICHAR_ID unichar_id) const
const char * id_to_unichar(UNICHAR_ID id) const
const double kMaxOverlapDenominator
const char * ScriptPosToString(enum ScriptPos script_pos)
const STRING & unichar_string() const