25 #pragma warning(disable:4244) // Conversion warnings 35 probability_in_context_(&
tesseract::
Dict::def_probability_in_context),
36 params_model_classify_(NULL),
38 STRING_MEMBER(user_words_file,
"",
"A filename of user-provided words.",
39 getCCUtil()->params()),
41 "A suffix of user-provided words located in tessdata.",
42 getCCUtil()->params()),
44 "A filename of user-provided patterns.",
45 getCCUtil()->params()),
47 "A suffix of user-provided patterns located in " 49 getCCUtil()->params()),
51 getCCUtil()->params()),
53 getCCUtil()->params()),
55 getCCUtil()->params()),
57 "Load dawg with punctuation" 59 getCCUtil()->params()),
61 "Load dawg with number" 63 getCCUtil()->params()),
65 "Load dawg with special word " 67 getCCUtil()->params()),
69 "Score penalty (0.1 = 10%) added if there are subscripts " 70 "or superscripts in a word, but it is otherwise OK.",
71 getCCUtil()->params()),
73 "Score penalty (0.1 = 10%) added if an xheight is " 75 getCCUtil()->params()),
77 "Score multiplier for word matches which have good case and" 78 " are frequent in the given language (lower is better).",
79 getCCUtil()->params()),
81 "Score multiplier for word matches that have good case " 83 getCCUtil()->params()),
85 "Default score multiplier for word matches, which may have " 86 "case issues (lower is better).",
87 getCCUtil()->params()),
89 "Score multiplier for glyph fragment segmentations which " 90 "do not match a dictionary word (lower is better).",
91 getCCUtil()->params()),
93 "Score multiplier for poorly cased strings that are not in" 94 " the dictionary and generally look like garbage (lower is" 96 getCCUtil()->params()),
98 "Output file for ambiguities found in the dictionary",
99 getCCUtil()->params()),
101 "Set to 1 for general debug info" 102 ", to 2 for more details, to 3 to see all the debug messages",
103 getCCUtil()->params()),
104 INT_MEMBER(hyphen_debug_level, 0,
"Debug level for hyphenated words.",
105 getCCUtil()->params()),
106 INT_MEMBER(max_viterbi_list_size, 10,
"Maximum size of viterbi list.",
107 getCCUtil()->params()),
109 "Use only the first UTF8 step of the given string" 110 " when computing log probabilities.",
111 getCCUtil()->params()),
112 double_MEMBER(certainty_scale, 20.0,
"Certainty scaling factor",
113 getCCUtil()->params()),
115 "Certainty threshold for non-dict words",
116 getCCUtil()->params()),
117 double_MEMBER(stopper_phase2_certainty_rejection_offset, 1.0,
118 "Reject certainty offset", getCCUtil()->params()),
120 "Size of dict word to be treated as non-dict word",
121 getCCUtil()->params()),
124 " for each dict char above small word size.",
125 getCCUtil()->params()),
127 "Max certaintly variation allowed in a word (in sigma)",
128 getCCUtil()->params()),
129 INT_MEMBER(stopper_debug_level, 0,
"Stopper debug level",
130 getCCUtil()->params()),
132 "Make AcceptableChoice() always return false. Useful" 133 " when there is a need to explore all segmentations",
134 getCCUtil()->params()),
135 INT_MEMBER(tessedit_truncate_wordchoice_log, 10,
136 "Max words to keep in list", getCCUtil()->params()),
138 "Word for which stopper debug" 139 " information should be printed to stdout",
140 getCCUtil()->params()),
142 "Lengths of unichars in word_to_debug",
143 getCCUtil()->params()),
144 INT_MEMBER(fragments_debug, 0,
"Debug character fragments",
145 getCCUtil()->params()),
147 "Don't use any alphabetic-specific tricks." 148 " Set to true in the traineddata config file for" 149 " scripts that are cursive or inherently fixed-pitch",
150 getCCUtil()->params()),
151 BOOL_MEMBER(save_doc_words, 0,
"Save Document Words",
152 getCCUtil()->params()),
154 "Worst certainty for using pending dictionary",
155 getCCUtil()->params()),
157 "Worst certainty for words that can be inserted into the" 158 " document dictionary",
159 getCCUtil()->params()),
161 "Maximum number of different" 162 " character choices to consider during permutation." 163 " This limit is especially useful when user patterns" 164 " are specified, since overly generic patterns can result in" 165 " dawg search exploring an overly large number of options.",
166 getCCUtil()->params()) {
167 dang_ambigs_table_ = NULL;
168 replace_ambigs_table_ = NULL;
169 reject_offset_ = 0.0;
172 last_word_on_line_ =
false;
173 hyphen_unichar_id_ = INVALID_UNICHAR_ID;
174 document_words_ = NULL;
176 dawg_cache_is_ours_ =
false;
177 pending_words_ = NULL;
181 unambig_dawg_ = NULL;
182 wordseg_rating_adjust_factor_ = -1.0f;
183 output_ambig_words_file_ = NULL;
189 if (output_ambig_words_file_ != NULL) fclose(output_ambig_words_file_);
208 if (dawg_cache != NULL) {
209 dawg_cache_ = dawg_cache;
210 dawg_cache_is_ours_ =
false;
213 dawg_cache_is_ours_ =
true;
223 if (punc_dawg_) dawgs_ += punc_dawg_;
228 if (system_dawg) dawgs_ += system_dawg;
233 if (number_dawg) dawgs_ += number_dawg;
244 if (freq_dawg_) dawgs_ += freq_dawg_;
249 if (unambig_dawg_) dawgs_ += unambig_dawg_;
257 if (((
STRING &)user_words_file).length() > 0) {
293 dawgs_ += document_words_;
306 if (punc_dawg_) dawgs_ += punc_dawg_;
311 if (system_dawg) dawgs_ += system_dawg;
316 if (number_dawg) dawgs_ += number_dawg;
323 if (dawgs_.
empty())
return false;
328 for (
int i = 0; i < dawgs_.
length(); ++i) {
329 const Dawg *dawg = dawgs_[i];
331 for (
int j = 0; j < dawgs_.
length(); ++j) {
332 const Dawg *other = dawgs_[j];
333 if (dawg != NULL && other != NULL &&
335 kDawgSuccessors[dawg->
type()][other->
type()]) *lst += j;
345 for (
int i = 0; i < dawgs_.
size(); i++) {
346 if (!dawg_cache_->
FreeDawg(dawgs_[i])) {
350 dawg_cache_->
FreeDawg(bigram_dawg_);
351 if (dawg_cache_is_ours_) {
358 document_words_ = NULL;
359 delete pending_words_;
360 pending_words_ = NULL;
368 bool word_end)
const {
372 tprintf(
"def_letter_is_okay: current unichar=%s word_end=%d" 373 " num active dawgs=%d\n",
382 unichar_id == INVALID_UNICHAR_ID) {
400 if (!dawg && !punc_dawg) {
402 tprintf(
"Received DawgPosition with no dawg or punc_dawg. wth?\n");
410 if (punc_transition_edge != NO_EDGE) {
413 for (
int s = 0; s < slist.
length(); ++s) {
414 int sdawg_index = slist[s];
415 const Dawg *sdawg = dawgs_[sdawg_index];
418 if (dawg_edge != NO_EDGE) {
420 tprintf(
"Letter found in dawg %d\n", sdawg_index);
426 "Append transition from punc dawg to current dawgs: ");
436 if (punc_edge != NO_EDGE) {
438 tprintf(
"Letter found in punctuation dawg\n");
443 "Extend punctuation dawg: ");
454 EDGE_REF punc_edge = punc_node == NO_EDGE ? NO_EDGE
455 : punc_dawg->
edge_char_of(punc_node, unichar_id, word_end);
456 if (punc_edge != NO_EDGE) {
461 "Return to punctuation dawg: ");
482 EDGE_REF edge = (node == NO_EDGE) ? NO_EDGE
490 if (edge != NO_EDGE) {
496 tprintf(
"Punctuation constraint not satisfied at end of word.\n");
508 "Append current dawg to updated active dawgs: ");
520 tprintf(
"Returning %d for permuter code for this character.\n",
534 unichar_id_patterns.
push_back(unichar_id);
536 &unichar_id_patterns);
537 for (
int i = 0; i < unichar_id_patterns.
size(); ++i) {
540 for (
int k = 0; k < 2; ++k) {
542 ? dawg->
edge_char_of(node, unichar_id_patterns[i], word_end)
544 if (edge == NO_EDGE)
continue;
556 "Append current dawg to updated active dawgs: ");
565 bool ambigs_mode)
const {
568 *active_dawgs = hyphen_active_dawgs_;
570 for (i = 0; i < hyphen_active_dawgs_.
size(); ++i) {
572 hyphen_active_dawgs_[i].dawg_index,
573 hyphen_active_dawgs_[i].dawg_ref);
582 bool suppress_patterns)
const {
583 bool punc_dawg_available =
584 (punc_dawg_ != NULL) &&
587 for (
int i = 0; i < dawgs_.
length(); i++) {
588 if (dawgs_[i] != NULL &&
590 int dawg_ty = dawgs_[i]->type();
593 *dawg_pos_vec +=
DawgPosition(-1, NO_EDGE, i, NO_EDGE,
false);
598 }
else if (!punc_dawg_available || !subsumed_by_punc) {
599 *dawg_pos_vec +=
DawgPosition(i, NO_EDGE, -1, NO_EDGE,
false);
615 if (hyphen_word_)
return;
619 int stringlen = best_choice.
length();
625 if (best_choice.
length() >= kDocDictMaxRepChars) {
626 int num_rep_chars = 1;
628 for (
int i = 1; i < best_choice.
length(); ++i) {
634 if (num_rep_chars == kDocDictMaxRepChars)
return;
656 strcpy(filename,
getCCUtil()->imagefile.string());
657 strcat(filename,
".doc");
658 doc_word_file =
open_file (filename,
"a");
659 fprintf(doc_word_file,
"%s\n",
661 fclose(doc_word_file);
669 float additional_adjust,
677 float adjust_factor = additional_adjust;
678 float new_rating = word->
rating();
679 new_rating += kRatingPad;
680 const char *xheight_triggered =
"";
683 switch (xheight_consistency) {
686 xheight_triggered =
", xhtBAD";
690 xheight_triggered =
", xhtSUB";
700 tprintf(
"Consistency could not be calculated.\n");
704 tprintf(
"%sWord: %s %4.2f%s", nonword ?
"Non-" :
"",
710 if (case_is_ok && punc_is_ok) {
712 new_rating *= adjust_factor;
716 new_rating *= adjust_factor;
718 if (!case_is_ok)
tprintf(
", C");
719 if (!punc_is_ok)
tprintf(
", P");
724 if (!is_han && freq_dawg_ != NULL && freq_dawg_->
word_in_dawg(*word)) {
727 new_rating *= adjust_factor;
731 new_rating *= adjust_factor;
736 new_rating *= adjust_factor;
740 new_rating -= kRatingPad;
741 if (modify_rating) word->
set_rating(new_rating);
742 if (debug)
tprintf(
" %4.2f --> %4.2f\n", adjust_factor, new_rating);
752 word_ptr = &temp_word;
760 int last_index = word_ptr->
length() - 1;
764 i == last_index)))
break;
774 delete[] active_dawgs;
781 if (bigram_dawg_ == NULL)
return false;
785 int w1start, w1end, w2start, w2end;
791 if (w1start >= w1end)
return word1.
length() < 3;
792 if (w2start >= w2end)
return word2.
length() < 3;
796 bigram_string.
reserve(w1end + w2end + 1);
797 for (
int i = w1start; i < w1end; i++) {
801 bigram_string.
push_back(question_unichar_id_);
803 bigram_string += normed_ids;
806 for (
int i = w2start; i < w2end; i++) {
810 bigram_string.
push_back(question_unichar_id_);
812 bigram_string += normed_ids;
815 for (
int i = 0; i < bigram_string.
size(); ++i) {
826 int last_index = word.
length() - 1;
828 for (i = 0; i <= last_index; ++i) {
831 new_word.append_unichar_id(unichar_id, 1, 0.0, 0.0);
835 }
else if ((new_len = new_word.length()) == 0 ||
840 for (i = 0; i < dawgs_.
size(); ++i) {
841 if (dawgs_[i] != NULL &&
843 dawgs_[i]->word_in_dawg(new_word))
return true;
851 if (u_set.
han_sid() > 0)
return false;
853 if (u_set.
thai_sid() > 0)
return false;
PermuterType permuter() const
void initialize_patterns(UNICHARSET *unicharset)
const STRING debug_string() const
#define BOOL_MEMBER(name, val, comment, vec)
const CCUtil * getCCUtil() const
double segment_penalty_dict_nonword
virtual void unichar_id_to_patterns(UNICHAR_ID unichar_id, const UNICHARSET &unicharset, GenericVector< UNICHAR_ID > *vec) const
void delete_data_pointers()
virtual bool end_of_word(EDGE_REF edge_ref) const =0
const STRING & lang() const
static bool valid_word_permuter(uint8_t perm, bool numbers_ok)
Check all the DAWGs to see if this word is in any of them.
bool FreeDawg(Dawg *dawg)
double xheight_penalty_inconsistent
void Load(const STRING &lang, TessdataManager *data_file)
GenericVector< int > SuccessorList
double segment_penalty_garbage
void copy_hyphen_info(WERD_CHOICE *word) const
bool valid_punctuation(const WERD_CHOICE &word)
UNICHAR_ID unichar_id(int index) const
int(Dict::* letter_is_okay_)(void *void_dawg_args, UNICHAR_ID unichar_id, bool word_end) const
static NODE_REF GetStartingNode(const Dawg *dawg, EDGE_REF edge_ref)
Returns the appropriate next node given the EDGE_REF.
int case_ok(const WERD_CHOICE &word, const UNICHARSET &unicharset) const
Check a string to see if it matches a set of lexical rules.
#define INT_MEMBER(name, val, comment, vec)
double doc_dict_certainty_threshold
DawgPositionVector * active_dawgs
bool read_pattern_list(const char *filename, const UNICHARSET &unicharset)
char * user_patterns_file
const UNICHARSET * unicharset() const
DawgPositionVector * updated_dawgs
STRING language_data_path_prefix
FILE * open_file(const char *filename, const char *mode)
const STRING & unichar_string() const
void set_permuter(uint8_t perm)
void set_rating(float new_val)
void SetupForLoad(DawgCache *dawg_cache)
UNICHAR_ID unichar_to_id(const char *const unichar_repr) const
void add_document_word(const WERD_CHOICE &best_choice)
Adds a word found on this document to the document specific dictionary.
void init_active_dawgs(DawgPositionVector *active_dawgs, bool ambigs_mode) const
static DawgCache * GlobalDawgCache()
bool get_isupper(UNICHAR_ID unichar_id) const
double xheight_penalty_subscripts
#define STRING_INIT_MEMBER(name, val, comment, vec)
static const UNICHAR_ID kPatternUnicharID
bool add_word_to_dawg(const WERD_CHOICE &word, const GenericVector< bool > *repetitions)
bool valid_bigram(const WERD_CHOICE &word1, const WERD_CHOICE &word2) const
void(Dict::* go_deeper_fxn_)(const char *debug, const BLOB_CHOICE_LIST_VECTOR &char_choices, int char_choice_index, const CHAR_FRAGMENT_INFO *prev_char_frag_info, bool word_ending, WERD_CHOICE *word, float certainties[], float *limit, WERD_CHOICE *best_choice, int *attempts_left, void *void_more_args)
Pointer to go_deeper function.
const char * string() const
const UNICHARSET & getUnicharset() const
void ProcessPatternEdges(const Dawg *dawg, const DawgPosition &info, UNICHAR_ID unichar_id, bool word_end, DawgArgs *dawg_args, PermuterType *current_permuter) const
bool add_unique(const DawgPosition &new_pos, bool debug, const char *debug_msg)
bool word_in_dawg(const WERD_CHOICE &word) const
Returns true if the given word is in the Dawg.
#define double_MEMBER(name, val, comment, vec)
const GenericVector< UNICHAR_ID > & normed_ids(UNICHAR_ID unichar_id) const
virtual EDGE_REF edge_char_of(NODE_REF node, UNICHAR_ID unichar_id, bool word_end) const =0
Returns the edge that corresponds to the letter out of this node.
bool IsSpaceDelimitedLang() const
Returns true if the language is space-delimited (not CJ, or T).
bool read_and_add_word_list(const char *filename, const UNICHARSET &unicharset, Trie::RTLReversePolicy reverse)
void adjust_word(WERD_CHOICE *word, bool nonword, XHeightConsistencyEnum xheight_consistency, float additional_adjust, bool modify_rating, bool debug)
Adjusts the rating of the given word.
virtual EDGE_REF pattern_loop_edge(EDGE_REF edge_ref, UNICHAR_ID unichar_id, bool word_end) const
void LoadLSTM(const STRING &lang, TessdataManager *data_file)
int hyphen_base_size() const
Size of the base word (the part on the line before) of a hyphenated word.
char * user_patterns_suffix
bool get_ispunctuation(UNICHAR_ID unichar_id) const
void set_adjust_factor(float factor)
int valid_word(const WERD_CHOICE &word, bool numbers_ok) const
bool get_isdigit(UNICHAR_ID unichar_id) const
#define STRING_MEMBER(name, val, comment, vec)
UNICHAR_ID char_for_dawg(UNICHAR_ID ch, const Dawg *dawg) const
Dawg * GetSquishedDawg(const STRING &lang, TessdataType tessdata_dawg_type, int debug_level, TessdataManager *data_file)
#define BOOL_INIT_MEMBER(name, val, comment, vec)
bool hyphenated() const
Returns true if we've recorded the beginning of a hyphenated word.
void punct_stripped(int *start_core, int *end_core) const
void append_unichar_id_space_allocated(UNICHAR_ID unichar_id, int blob_count, float rating, float certainty)
double segment_penalty_dict_case_bad
double segment_penalty_dict_case_ok
double segment_penalty_dict_frequent_word
int GetTopScriptID() const
double doc_dict_pending_threshold
void default_dawgs(DawgPositionVector *anylength_dawgs, bool suppress_patterns) const
int def_letter_is_okay(void *void_dawg_args, UNICHAR_ID unichar_id, bool word_end) const