tesseract  v4.0.0-17-g361f3264
Open Source OCR Engine
lm_state.h
1 // File: lm_state.h
3 // Description: Structures and functionality for capturing the state of
4 // segmentation search guided by the language model.
5 //
6 // Author: Rika Antonova
7 // Created: Mon Jun 20 11:26:43 PST 2012
8 //
9 // (C) Copyright 2012, Google Inc.
10 // Licensed under the Apache License, Version 2.0 (the "License");
11 // you may not use this file except in compliance with the License.
12 // You may obtain a copy of the License at
13 // http://www.apache.org/licenses/LICENSE-2.0
14 // Unless required by applicable law or agreed to in writing, software
15 // distributed under the License is distributed on an "AS IS" BASIS,
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 // See the License for the specific language governing permissions and
18 // limitations under the License.
19 //
21 
22 #ifndef TESSERACT_WORDREC_LANGUAGE_MODEL_DEFS_H_
23 #define TESSERACT_WORDREC_LANGUAGE_MODEL_DEFS_H_
24 
25 #include "associate.h" // for AssociateStats
26 #include "dawg.h" // for DawgPositionVector
27 #include "elst.h" // for ELIST_ITERATOR, ELISTIZEH, ELIST_LINK
28 #include "genericvector.h" // for PointerVector
29 #include "lm_consistency.h" // for LMConsistencyInfo
30 #include "ratngs.h" // for BLOB_CHOICE, PermuterType
31 #include "stopper.h" // for DANGERR
32 #include "strngs.h" // for STRING
33 #include "unichar.h" // for UNICHAR_ID
34 #include "unicharset.h" // for UNICHARSET
35 
36 namespace tesseract {
37 
39 using LanguageModelFlagsType = unsigned char;
40 
59 
64  LanguageModelDawgInfo(const DawgPositionVector *a, PermuterType pt)
65  : active_dawgs(*a), permuter(pt) {}
67  PermuterType permuter;
68 };
69 
73  LanguageModelNgramInfo(const char *c, int l, bool p, float nc, float ncc)
74  : context(c), context_unichar_step_len(l), pruned(p), ngram_cost(nc),
75  ngram_and_classifier_cost(ncc) {}
76  STRING context; //< context string
84  bool pruned;
86  float ngram_cost;
89 };
90 
93 struct ViterbiStateEntry : public ELIST_LINK {
95  BLOB_CHOICE *b, float c, float ol,
96  const LMConsistencyInfo &ci,
97  const AssociateStats &as,
101  const char *debug_uch)
102  : cost(c), curr_b(b), parent_vse(pe), competing_vse(nullptr),
103  ratings_sum(b->rating()),
104  min_certainty(b->certainty()), adapted(b->IsAdapted()), length(1),
105  outline_length(ol), consistency_info(ci), associate_stats(as),
106  top_choice_flags(tcf), dawg_info(d), ngram_info(n),
107  updated(true) {
108  debug_str = (debug_uch == nullptr) ? nullptr : new STRING();
109  if (pe != nullptr) {
110  ratings_sum += pe->ratings_sum;
111  if (pe->min_certainty < min_certainty) {
112  min_certainty = pe->min_certainty;
113  }
114  adapted += pe->adapted;
115  length += pe->length;
116  outline_length += pe->outline_length;
117  if (debug_uch != nullptr) *debug_str += *(pe->debug_str);
118  }
119  if (debug_str != nullptr && debug_uch != nullptr) *debug_str += debug_uch;
120  }
122  delete dawg_info;
123  delete ngram_info;
124  delete debug_str;
125  }
128  static int Compare(const void *e1, const void *e2) {
129  const ViterbiStateEntry *ve1 =
130  *static_cast<const ViterbiStateEntry *const *>(e1);
131  const ViterbiStateEntry *ve2 =
132  *static_cast<const ViterbiStateEntry *const *>(e2);
133  return (ve1->cost < ve2->cost) ? -1 : 1;
134  }
135  inline bool Consistent() const {
136  if (dawg_info != nullptr && consistency_info.NumInconsistentCase() == 0) {
137  return true;
138  }
139  return consistency_info.Consistent();
140  }
143  bool HasAlnumChoice(const UNICHARSET& unicharset) {
144  if (curr_b == nullptr) return false;
145  UNICHAR_ID unichar_id = curr_b->unichar_id();
146  if (unicharset.get_isalpha(unichar_id) ||
147  unicharset.get_isdigit(unichar_id))
148  return true;
149  return false;
150  }
151  void Print(const char *msg) const;
152 
155  float cost;
156 
163 
166  float ratings_sum; //< sum of ratings of character on the path
167  float min_certainty; //< minimum certainty on the path
168  int adapted; //< number of BLOB_CHOICES from adapted templates
169  int length; //< number of characters on the path
170  float outline_length; //< length of the outline so far
171  LMConsistencyInfo consistency_info; //< path consistency info
172  AssociateStats associate_stats; //< character widths/gaps/seams
173 
177 
181 
185 
186  bool updated; //< set to true if the entry has just been created/updated
190 };
191 
192 ELISTIZEH(ViterbiStateEntry)
193 
194 struct LanguageModelState {
197  viterbi_state_entries_prunable_length(0),
198  viterbi_state_entries_prunable_max_cost(FLT_MAX),
199  viterbi_state_entries_length(0) {}
201 
203  void Clear();
204 
205  void Print(const char *msg);
206 
208  ViterbiStateEntry_LIST viterbi_state_entries;
214 };
215 
218  explicit BestChoiceBundle(int matrix_dimension)
219  : updated(false), best_vse(nullptr) {
220  beam.reserve(matrix_dimension);
221  for (int i = 0; i < matrix_dimension; ++i)
222  beam.push_back(new LanguageModelState);
223  }
225 
227  bool updated;
236 };
237 
238 } // namespace tesseract
239 
240 #endif // TESSERACT_WORDREC_LANGUAGE_MODEL_DEFS_H_
Definition: lm_state.h:63
LanguageModelNgramInfo * ngram_info
Definition: lm_state.h:184
PermuterType permuter
Definition: lm_state.h:67
LMConsistencyInfo consistency_info
Definition: lm_state.h:171
~ViterbiStateEntry()
Definition: lm_state.h:121
LanguageModelNgramInfo(const char *c, int l, bool p, float nc, float ncc)
Definition: lm_state.h:73
~LanguageModelState()
Definition: lm_state.h:200
float min_certainty
Definition: lm_state.h:167
Definition: lm_state.h:93
STRING context
Definition: lm_state.h:76
int viterbi_state_entries_length
Total number of entries in viterbi_state_entries.
Definition: lm_state.h:213
Struct to store information maintained by various language model components.
Definition: lm_state.h:195
float ngram_cost
-ln(P_ngram_model(path))
Definition: lm_state.h:86
Definition: unicharset.h:146
bool Consistent() const
Definition: lm_state.h:135
LanguageModelDawgInfo(const DawgPositionVector *a, PermuterType pt)
Definition: lm_state.h:64
float outline_length
Definition: lm_state.h:170
Definition: lm_consistency.h:39
int context_unichar_step_len
Definition: lm_state.h:79
BestChoiceBundle(int matrix_dimension)
Definition: lm_state.h:218
Definition: baseapi.cpp:94
int length
Definition: lm_state.h:169
int viterbi_state_entries_prunable_length
Number and max cost of prunable paths in viterbi_state_entries.
Definition: lm_state.h:210
int adapted
Definition: lm_state.h:168
LanguageModelFlagsType top_choice_flags
Definition: lm_state.h:176
BLOB_CHOICE * curr_b
Pointers to BLOB_CHOICE and parent ViterbiStateEntry (not owned by this).
Definition: lm_state.h:158
bool get_isalpha(UNICHAR_ID unichar_id) const
Definition: unicharset.h:486
ViterbiStateEntry * best_vse
Best ViterbiStateEntry and BLOB_CHOICE.
Definition: lm_state.h:235
Bundle together all the things pertaining to the best choice/state.
Definition: lm_state.h:217
bool updated
Definition: lm_state.h:186
bool updated
Flag to indicate whether anything was changed.
Definition: lm_state.h:227
ViterbiStateEntry(ViterbiStateEntry *pe, BLOB_CHOICE *b, float c, float ol, const LMConsistencyInfo &ci, const AssociateStats &as, LanguageModelFlagsType tcf, LanguageModelDawgInfo *d, LanguageModelNgramInfo *n, const char *debug_uch)
Definition: lm_state.h:94
ViterbiStateEntry * competing_vse
Definition: lm_state.h:162
static int Compare(const void *e1, const void *e2)
Definition: lm_state.h:128
ViterbiStateEntry_LIST viterbi_state_entries
Storage for the Viterbi state.
Definition: lm_state.h:208
DANGERR fixpt
Places to try to fix the word suggested by ambiguity checking.
Definition: lm_state.h:229
Definition: dawg.h:381
bool pruned
Definition: lm_state.h:84
~BestChoiceBundle()
Definition: lm_state.h:224
bool get_isdigit(UNICHAR_ID unichar_id) const
Definition: unicharset.h:507
AssociateStats associate_stats
Definition: lm_state.h:172
DawgPositionVector active_dawgs
Definition: lm_state.h:66
Definition: strngs.h:45
STRING * debug_str
Definition: lm_state.h:189
PointerVector< LanguageModelState > beam
Definition: lm_state.h:233
ViterbiStateEntry * parent_vse
Definition: lm_state.h:159
float ngram_and_classifier_cost
-[ ln(P_classifier(path)) + scale_factor * ln(P_ngram_model(path)) ]
Definition: lm_state.h:88
Definition: genericvector.h:457
LanguageModelState()
Definition: lm_state.h:196
unsigned char LanguageModelFlagsType
Used for expressing various language model flags.
Definition: lm_state.h:39
LanguageModelDawgInfo * dawg_info
Definition: lm_state.h:180
bool HasAlnumChoice(const UNICHARSET &unicharset)
Definition: lm_state.h:143
float viterbi_state_entries_prunable_max_cost
Definition: lm_state.h:211
float ratings_sum
Definition: lm_state.h:166
float cost
Definition: lm_state.h:155
Definition: ratngs.h:49
Definition: lm_state.h:72
Definition: associate.h:36