tesseract  4.00.00dev
lm_state.h
Go to the documentation of this file.
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"
26 #include "elst.h"
27 #include "dawg.h"
28 #include "lm_consistency.h"
29 #include "matrix.h"
30 #include "ratngs.h"
31 #include "stopper.h"
32 #include "strngs.h"
33 
34 namespace tesseract {
35 
37 typedef unsigned char LanguageModelFlagsType;
38 
57 
63  : active_dawgs(*a), permuter(pt) {}
66 };
67 
71  LanguageModelNgramInfo(const char *c, int l, bool p, float nc, float ncc)
72  : context(c), context_unichar_step_len(l), pruned(p), ngram_cost(nc),
73  ngram_and_classifier_cost(ncc) {}
74  STRING context; //< context string
82  bool pruned;
84  float ngram_cost;
87 };
88 
91 struct ViterbiStateEntry : public ELIST_LINK {
93  BLOB_CHOICE *b, float c, float ol,
94  const LMConsistencyInfo &ci,
95  const AssociateStats &as,
96  LanguageModelFlagsType tcf,
99  const char *debug_uch)
100  : cost(c), curr_b(b), parent_vse(pe), competing_vse(NULL),
101  ratings_sum(b->rating()),
102  min_certainty(b->certainty()), adapted(b->IsAdapted()), length(1),
103  outline_length(ol), consistency_info(ci), associate_stats(as),
104  top_choice_flags(tcf), dawg_info(d), ngram_info(n),
105  updated(true) {
106  debug_str = (debug_uch == NULL) ? NULL : new STRING();
107  if (pe != NULL) {
108  ratings_sum += pe->ratings_sum;
109  if (pe->min_certainty < min_certainty) {
110  min_certainty = pe->min_certainty;
111  }
112  adapted += pe->adapted;
113  length += pe->length;
114  outline_length += pe->outline_length;
115  if (debug_uch != NULL) *debug_str += *(pe->debug_str);
116  }
117  if (debug_str != NULL && debug_uch != NULL) *debug_str += debug_uch;
118  }
120  delete dawg_info;
121  delete ngram_info;
122  delete debug_str;
123  }
126  static int Compare(const void *e1, const void *e2) {
127  const ViterbiStateEntry *ve1 =
128  *static_cast<const ViterbiStateEntry * const *>(e1);
129  const ViterbiStateEntry *ve2 =
130  *static_cast<const ViterbiStateEntry * const *>(e2);
131  return (ve1->cost < ve2->cost) ? -1 : 1;
132  }
133  inline bool Consistent() const {
134  if (dawg_info != NULL && consistency_info.NumInconsistentCase() == 0) {
135  return true;
136  }
137  return consistency_info.Consistent();
138  }
141  bool HasAlnumChoice(const UNICHARSET& unicharset) {
142  if (curr_b == NULL) return false;
143  UNICHAR_ID unichar_id = curr_b->unichar_id();
144  if (unicharset.get_isalpha(unichar_id) ||
145  unicharset.get_isdigit(unichar_id))
146  return true;
147  return false;
148  }
149  void Print(const char *msg) const;
150 
153  float cost;
154 
161 
164  float ratings_sum; //< sum of ratings of character on the path
165  float min_certainty; //< minimum certainty on the path
166  int adapted; //< number of BLOB_CHOICES from adapted templates
167  int length; //< number of characters on the path
168  float outline_length; //< length of the outline so far
169  LMConsistencyInfo consistency_info; //< path consistency info
170  AssociateStats associate_stats; //< character widths/gaps/seams
171 
174  LanguageModelFlagsType top_choice_flags;
175 
179 
183 
184  bool updated; //< set to true if the entry has just been created/updated
188 };
189 
191 
195  viterbi_state_entries_prunable_length(0),
196  viterbi_state_entries_prunable_max_cost(MAX_FLOAT32),
197  viterbi_state_entries_length(0) {}
199 
201  void Clear();
202 
203  void Print(const char *msg);
204 
206  ViterbiStateEntry_LIST viterbi_state_entries;
212 };
213 
216  explicit BestChoiceBundle(int matrix_dimension)
217  : updated(false), best_vse(NULL) {
218  beam.reserve(matrix_dimension);
219  for (int i = 0; i < matrix_dimension; ++i)
220  beam.push_back(new LanguageModelState);
221  }
223 
225  bool updated;
234 };
235 
236 } // namespace tesseract
237 
238 #endif // TESSERACT_WORDREC_LANGUAGE_MODEL_DEFS_H_
LMConsistencyInfo consistency_info
Definition: lm_state.h:169
int UNICHAR_ID
Definition: unichar.h:33
ViterbiStateEntry * best_vse
Best ViterbiStateEntry and BLOB_CHOICE.
Definition: lm_state.h:233
LanguageModelDawgInfo(const DawgPositionVector *a, PermuterType pt)
Definition: lm_state.h:62
ViterbiStateEntry * competing_vse
Definition: lm_state.h:160
LanguageModelNgramInfo(const char *c, int l, bool p, float nc, float ncc)
Definition: lm_state.h:71
BestChoiceBundle(int matrix_dimension)
Definition: lm_state.h:216
float viterbi_state_entries_prunable_max_cost
Definition: lm_state.h:209
PointerVector< LanguageModelState > beam
Definition: lm_state.h:231
ParamsEditor * pe
Definition: pgedit.cpp:108
unsigned char LanguageModelFlagsType
Used for expressing various language model flags.
Definition: lm_state.h:37
static int Compare(const void *e1, const void *e2)
Definition: lm_state.h:126
bool get_isalpha(UNICHAR_ID unichar_id) const
Definition: unicharset.h:451
float ngram_and_classifier_cost
-[ ln(P_classifier(path)) + scale_factor * ln(P_ngram_model(path)) ]
Definition: lm_state.h:86
bool get_isdigit(UNICHAR_ID unichar_id) const
Definition: unicharset.h:472
bool updated
Flag to indicate whether anything was changed.
Definition: lm_state.h:225
Definition: strngs.h:45
Bundle together all the things pertaining to the best choice/state.
Definition: lm_state.h:215
bool HasAlnumChoice(const UNICHARSET &unicharset)
Definition: lm_state.h:141
#define MAX_FLOAT32
Definition: host.h:66
DANGERR fixpt
Places to try to fix the word suggested by ambiguity checking.
Definition: lm_state.h:227
Struct to store information maintained by various language model components.
Definition: lm_state.h:193
LanguageModelNgramInfo * ngram_info
Definition: lm_state.h:182
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:92
ELISTIZEH(AmbigSpec)
DawgPositionVector active_dawgs
Definition: lm_state.h:64
ViterbiStateEntry_LIST viterbi_state_entries
Storage for the Viterbi state.
Definition: lm_state.h:206
LanguageModelFlagsType top_choice_flags
Definition: lm_state.h:174
LanguageModelDawgInfo * dawg_info
Definition: lm_state.h:178
int viterbi_state_entries_prunable_length
Number and max cost of prunable paths in viterbi_state_entries.
Definition: lm_state.h:208
ViterbiStateEntry * parent_vse
Definition: lm_state.h:157
int viterbi_state_entries_length
Total number of entries in viterbi_state_entries.
Definition: lm_state.h:211
AssociateStats associate_stats
Definition: lm_state.h:170
PermuterType
Definition: ratngs.h:240
BLOB_CHOICE * curr_b
Pointers to BLOB_CHOICE and parent ViterbiStateEntry (not owned by this).
Definition: lm_state.h:156
float ngram_cost
-ln(P_ngram_model(path))
Definition: lm_state.h:84