19 #ifndef TESSERACT_DICT_DICT_H_ 20 #define TESSERACT_DICT_DICT_H_ 35 #define MAX_WERD_LENGTH (inT64) 128 54 static const int kRatingPad = 4;
55 static const char kDictWildcard[] =
"\u2606";
56 static const int kDictMaxWildcards = 2;
59 static const char kHyphenSymbol[] =
"-";
60 static const char kSlashSymbol[] =
"/";
61 static const char kQuestionSymbol[] =
"?";
62 static const char kApostropheSymbol[] =
"'";
63 static const float kSimCertaintyScale = -10.0;
64 static const float kSimCertaintyOffset = -10.0;
65 static const float kSimilarityFloor = 100.0;
66 static const int kDocDictMaxRepChars = 4;
78 : active_dawgs(d), updated_dawgs(up), permuter(p), valid_end(false) {}
98 return getCCUtil()->unicharset;
101 return getCCUtil()->unicharset;
104 return getCCUtil()->unichar_ambigs;
110 getUnicharset().normed_ids(unichar_id);
111 return normed_ids.
size() == 1 &&
112 (normed_ids[0] == hyphen_unichar_id_ ||
113 normed_ids[0] == slash_unichar_id_);
119 getUnicharset().normed_ids(unichar_id);
120 return normed_ids.
size() == 1 && normed_ids[0] == apostrophe_unichar_id_;
127 !last_word_on_line_ && hyphen_word_;
131 return this->hyphenated() ? hyphen_word_->length() : 0;
137 if (this->hyphenated()) {
138 *word = *hyphen_word_;
139 if (hyphen_debug_level) word->
print(
"copy_hyphen_info: ");
144 if (!last_word_on_line_ || first_pos)
147 getUnicharset().normed_ids(unichar_id);
148 return normed_ids.
size() == 1 && normed_ids[0] == hyphen_unichar_id_;
152 int word_index = word.
length() - 1;
153 return has_hyphen_end(word.
unichar_id(word_index), word_index == 0);
158 void reset_hyphen_vars(
bool last_word_on_line);
180 bool ambigs_mode)
const;
183 bool suppress_patterns)
const;
196 void go_deeper_dawg_fxn(
199 bool word_ending,
WERD_CHOICE *word,
float certainties[],
200 float *limit,
WERD_CHOICE *best_choice,
int *attempts_left,
201 void *void_more_args);
204 void (
Dict::*go_deeper_fxn_)(
const char *debug,
206 int char_choice_index,
209 float certainties[],
float *limit,
211 void *void_more_args);
215 void permute_choices(
218 int char_choice_index,
231 int char_choice_index,
240 bool fragment_state_okay(
UNICHAR_ID curr_unichar_id,
241 float curr_rating,
float curr_certainty,
243 const char *debug,
int word_ending,
249 bool fix_replaceable,
258 void ReplaceAmbig(
int wrong_ngram_begin_index,
int wrong_ngram_size,
263 int LengthOfShortestAlphaRun(
const WERD_CHOICE &WordChoice);
273 bool AcceptableChoice(
const WERD_CHOICE& best_choice,
278 bool AcceptableResult(
WERD_RES* word);
279 void EndDangerousAmbigs();
281 void DebugWordChoices();
283 void SettupStopperPass1();
285 void SettupStopperPass2();
299 void SetupForLoad(
DawgCache *dawg_cache);
301 void Load(
const char *data_file_name,
const STRING &
lang);
309 if (pending_words_ != NULL)
310 pending_words_->clear();
311 if (document_words_ != NULL)
312 document_words_->clear();
351 int def_letter_is_okay(
void* void_dawg_args,
354 int (
Dict::*letter_is_okay_)(
void* void_dawg_args,
359 return (this->*letter_is_okay_)(void_dawg_args,
unichar_id, word_end);
364 double (
Dict::*probability_in_context_)(
const char*
lang,
368 int character_bytes);
372 const char* character,
373 int character_bytes) {
374 return (this->*probability_in_context_)(
375 getCCUtil()->lang.string(),
376 context, context_bytes,
382 const char* lang,
const char* context,
int context_bytes,
383 const char* character,
int character_bytes) {
388 (void)character_bytes;
391 double ngram_probability_in_context(
const char* lang,
394 const char* character,
395 int character_bytes);
398 float (
Dict::*params_model_classify_)(
const char *
lang,
void *path);
399 float ParamsModelClassify(
const char *lang,
void *path);
403 return (this->*params_model_classify_)(
404 getCCUtil()->lang.string(), path);
410 inline int NumDawgs()
const {
return dawgs_.size(); }
412 inline const Dawg *
GetDawg(
int index)
const {
return dawgs_[index]; }
419 if (edge_ref == NO_EDGE)
return 0;
421 if (node == 0) node = NO_EDGE;
429 if (!dawg)
return ch;
430 switch (dawg->
type()) {
432 return getUnicharset().get_isdigit(ch) ? Dawg::kPatternUnicharID : ch;
459 int valid_word(
const WERD_CHOICE &word,
bool numbers_ok)
const;
461 return valid_word(word,
false);
464 return valid_word(word,
true);
469 return valid_word(word);
481 void add_document_word(
const WERD_CHOICE &best_choice);
485 float additional_adjust,
490 wordseg_rating_adjust_factor_ = f;
493 bool IsSpaceDelimitedLang()
const;
518 bool last_word_on_line_;
526 bool dawg_cache_is_ours_;
530 Trie *pending_words_;
544 Trie *document_words_;
547 float wordseg_rating_adjust_factor_;
549 FILE *output_ambig_words_file_;
555 STRING_VAR_H(user_words_file,
"",
"A filename of user-provided words.");
557 "A suffix of user-provided words located in tessdata.");
559 "A filename of user-provided patterns.");
561 "A suffix of user-provided patterns located in tessdata.");
562 BOOL_VAR_H(load_system_dawg,
true,
"Load system word dawg.");
563 BOOL_VAR_H(load_freq_dawg,
true,
"Load frequent word dawg.");
564 BOOL_VAR_H(load_unambig_dawg,
true,
"Load unambiguous word dawg.");
566 "Load dawg with punctuation patterns.");
567 BOOL_VAR_H(load_number_dawg,
true,
"Load dawg with number patterns.");
569 "Load dawg with special word bigrams.");
571 "Score penalty (0.1 = 10%) added if there are subscripts " 572 "or superscripts in a word, but it is otherwise OK.");
574 "Score penalty (0.1 = 10%) added if an xheight is " 577 "Score multiplier for word matches which have good case and" 578 "are frequent in the given language (lower is better).");
581 "Score multiplier for word matches that have good case " 582 "(lower is better).");
585 "Default score multiplier for word matches, which may have " 586 "case issues (lower is better).");
590 "Multipler to for the best choice from the ngram model.");
593 "Score multiplier for glyph fragment segmentations which " 594 "do not match a dictionary word (lower is better).");
597 "Score multiplier for poorly cased strings that are not in" 598 " the dictionary and generally look like garbage (lower is" 601 "Output file for ambiguities found in the dictionary");
602 INT_VAR_H(dawg_debug_level, 0,
"Set to 1 for general debug info" 603 ", to 2 for more details, to 3 to see all the debug messages");
604 INT_VAR_H(hyphen_debug_level, 0,
"Debug level for hyphenated words.");
605 INT_VAR_H(max_viterbi_list_size, 10,
"Maximum size of viterbi list.");
607 "Use only the first UTF8 step of the given string" 608 " when computing log probabilities.");
611 "Certainty threshold for non-dict words");
612 double_VAR_H(stopper_phase2_certainty_rejection_offset, 1.0,
613 "Reject certainty offset");
615 "Size of dict word to be treated as non-dict word");
617 "Certainty to add for each dict char above small word size.");
619 "Max certaintly variation allowed in a word (in sigma)");
620 INT_VAR_H(stopper_debug_level, 0,
"Stopper debug level");
621 BOOL_VAR_H(stopper_no_acceptable_choices,
false,
622 "Make AcceptableChoice() always return false. Useful" 623 " when there is a need to explore all segmentations");
625 "Deprecated- backward compatibility only");
626 INT_VAR_H(tessedit_truncate_wordchoice_log, 10,
"Max words to keep in list");
627 STRING_VAR_H(word_to_debug,
"",
"Word for which stopper debug information" 628 " should be printed to stdout");
630 "Lengths of unichars in word_to_debug");
631 INT_VAR_H(fragments_debug, 0,
"Debug character fragments");
632 BOOL_VAR_H(segment_nonalphabetic_script,
false,
633 "Don't use any alphabetic-specific tricks." 634 "Set to true in the traineddata config file for" 635 " scripts that are cursive or inherently fixed-pitch");
638 "Worst certainty for using pending dictionary");
639 double_VAR_H(doc_dict_certainty_threshold, -2.25,
"Worst certainty" 640 " for words that can be inserted into the document dictionary");
641 INT_VAR_H(max_permuter_attempts, 10000,
"Maximum number of different" 642 " character choices to consider during permutation." 643 " This limit is especially useful when user patterns" 644 " are specified, since overly generic patterns can result in" 645 " dawg search exploring an overly large number of options.");
649 #endif // THIRD_PARTY_TESSERACT_DICT_DICT_H_
DawgArgs(DawgPositionVector *d, DawgPositionVector *up, PermuterType p)
void ResetDocumentDictionary()
int valid_word_or_number(const WERD_CHOICE &word) const
#define STRING_VAR_H(name, val, comment)
DawgPositionVector * updated_dawgs
bool is_apostrophe(UNICHAR_ID unichar_id)
bool has_hyphen_end(const WERD_CHOICE &word) const
Same as above, but check the unichar at the end of the word.
void SetWildcardID(UNICHAR_ID id)
UNICHAR_ID unichar_id(int index) const
#define INT_VAR_H(name, val, comment)
void SetWordsegRatingAdjustFactor(float f)
Set wordseg_rating_adjust_factor_ to the given value.
const Dawg * GetDawg(int index) const
Return i-th dawg pointer recorded in the dawgs_ vector.
const UNICHARSET & getUnicharset() const
void update_best_choice(const WERD_CHOICE &word, WERD_CHOICE *best_choice)
UNICHARSET & getUnicharset()
static NODE_REF GetStartingNode(const Dawg *dawg, EDGE_REF edge_ref)
Returns the appropriate next node given the EDGE_REF.
int valid_word(const char *string) const
This function is used by api/tesseract_cube_combiner.cpp.
const CHAR_FRAGMENT * fragment
int NumDawgs() const
Return the number of dawgs in the dawgs_ vector.
#define BOOL_VAR_H(name, val, comment)
int valid_word(const WERD_CHOICE &word) const
double ProbabilityInContext(const char *context, int context_bytes, const char *character, int character_bytes)
Calls probability_in_context_ member function.
void copy_hyphen_info(WERD_CHOICE *word) const
float CallParamsModelClassify(void *path)
const UnicharAmbigs & getUnicharAmbigs() const
UNICHAR_ID WildcardID() const
int LetterIsOkay(void *void_dawg_args, UNICHAR_ID unichar_id, bool word_end) const
Calls letter_is_okay_ member function.
virtual NODE_REF next_node(EDGE_REF edge_ref) const =0
static bool valid_word_permuter(uinT8 perm, bool numbers_ok)
Check all the DAWGs to see if this word is in any of them.
GenericVector< Dawg * > DawgVector
const CCUtil * getCCUtil() const
DawgPositionVector * active_dawgs
const Dawg * GetUnambigDawg() const
Return the points to the unambiguous words dawg.
bool hyphenated() const
Returns true if we've recorded the beginning of a hyphenated word.
double def_probability_in_context(const char *lang, const char *context, int context_bytes, const char *character, int character_bytes)
Default (no-op) implementation of probability in context function.
bool has_hyphen_end(UNICHAR_ID unichar_id, bool first_pos) const
Check whether the word has a hyphen at the end.
#define double_VAR_H(name, val, comment)
int hyphen_base_size() const
Size of the base word (the part on the line before) of a hyphenated word.
const Dawg * GetPuncDawg() const
Return the points to the punctuation dawg.
bool compound_marker(UNICHAR_ID unichar_id)
UNICHAR_ID char_for_dawg(UNICHAR_ID ch, const Dawg *dawg) const