tesseract v5.3.3.20231005
lm_state.h
Go to the documentation of this file.
1
2// File: lm_state.h
3// Description: Structures and functionality for capturing the state of
4// segmentation search guided by the language model.
5// Author: Rika Antonova
6//
7// (C) Copyright 2012, Google Inc.
8// Licensed under the Apache License, Version 2.0 (the "License");
9// you may not use this file except in compliance with the License.
10// You may obtain a copy of the License at
11// http://www.apache.org/licenses/LICENSE-2.0
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17//
19
20#ifndef TESSERACT_WORDREC_LANGUAGE_MODEL_DEFS_H_
21#define TESSERACT_WORDREC_LANGUAGE_MODEL_DEFS_H_
22
23#include <tesseract/unichar.h> // for UNICHAR_ID
24#include "associate.h" // for AssociateStats
25#include "dawg.h" // for DawgPositionVector
26#include "elst.h" // for ELIST_ITERATOR, ELISTIZEH, ELIST_LINK
27#include "lm_consistency.h" // for LMConsistencyInfo
28#include "ratngs.h" // for BLOB_CHOICE, PermuterType
29#include "stopper.h" // for DANGERR
30#include "unicharset.h" // for UNICHARSET
31
32namespace tesseract {
33
35using LanguageModelFlagsType = unsigned char;
36
55
61 : active_dawgs(*a), permuter(pt) {}
64};
65
69 LanguageModelNgramInfo(const char *c, int l, bool p, float nc, float ncc)
70 : context(c)
72 , pruned(p)
73 , ngram_cost(nc)
75 std::string context;
83 bool pruned;
88};
89
94 const LMConsistencyInfo &ci, const AssociateStats &as,
96 const char *debug_uch)
97 : curr_b(b)
98 , parent_vse(pe)
99 , competing_vse(nullptr)
100 , dawg_info(d)
101 , ngram_info(n)
102 , cost(c)
103 , ratings_sum(b->rating())
104 , min_certainty(b->certainty())
105 , adapted(b->IsAdapted())
106 , length(1)
107 , outline_length(ol)
108 , consistency_info(ci)
109 , associate_stats(as)
110 , top_choice_flags(tcf)
111 , updated(true) {
112 debug_str = (debug_uch == nullptr) ? nullptr : new std::string();
113 if (pe != nullptr) {
115 if (pe->min_certainty < min_certainty) {
117 }
118 adapted += pe->adapted;
119 length += pe->length;
121 if (debug_uch != nullptr) {
122 *debug_str += *(pe->debug_str);
123 }
124 }
125 if (debug_str != nullptr && debug_uch != nullptr) {
126 *debug_str += debug_uch;
127 }
128 }
130 delete dawg_info;
131 delete ngram_info;
132 delete debug_str;
133 }
136 static int Compare(const void *e1, const void *e2) {
137 const ViterbiStateEntry *ve1 = *static_cast<const ViterbiStateEntry *const *>(e1);
138 const ViterbiStateEntry *ve2 = *static_cast<const ViterbiStateEntry *const *>(e2);
139 return (ve1->cost < ve2->cost) ? -1 : 1;
140 }
141 inline bool Consistent() const {
142 if (dawg_info != nullptr && consistency_info.NumInconsistentCase() == 0) {
143 return true;
144 }
146 }
149 bool HasAlnumChoice(const UNICHARSET &unicharset) {
150 if (curr_b == nullptr) {
151 return false;
152 }
153 UNICHAR_ID unichar_id = curr_b->unichar_id();
154 if (unicharset.get_isalpha(unichar_id) || unicharset.get_isdigit(unichar_id)) {
155 return true;
156 }
157 return false;
158 }
159 void Print(const char *msg) const;
160
167
171
175
178 std::string *debug_str;
179
182 float cost;
183
189 int length;
193
197
198 bool updated;
199};
200
202
203
206 : viterbi_state_entries_prunable_length(0)
207 , viterbi_state_entries_prunable_max_cost(FLT_MAX)
208 , viterbi_state_entries_length(0) {}
210
212 void Clear();
213
214 void Print(const char *msg);
215
217 ViterbiStateEntry_LIST viterbi_state_entries;
223};
224
227 explicit BestChoiceBundle(int matrix_dimension) : updated(false), best_vse(nullptr) {
228 beam.reserve(matrix_dimension);
229 for (int i = 0; i < matrix_dimension; ++i) {
230 beam.push_back(new LanguageModelState);
231 }
232 }
234 for (auto &state : beam) {
235 delete state;
236 }
237 }
238
246 std::vector<LanguageModelState *> beam;
249};
250
251} // namespace tesseract
252
253#endif // TESSERACT_WORDREC_LANGUAGE_MODEL_DEFS_H_
#define ELISTIZEH(CLASSNAME)
Definition: elst.h:803
const char * p
unsigned char LanguageModelFlagsType
Used for expressing various language model flags.
Definition: lm_state.h:35
int UNICHAR_ID
Definition: unichar.h:34
std::vector< DANGERR_INFO > DANGERR
Definition: stopper.h:47
PermuterType
Definition: ratngs.h:235
std::string Print(const T &value)
UNICHAR_ID unichar_id() const
Definition: ratngs.h:81
bool get_isalpha(UNICHAR_ID unichar_id) const
Definition: unicharset.h:497
bool get_isdigit(UNICHAR_ID unichar_id) const
Definition: unicharset.h:524
LanguageModelDawgInfo(const DawgPositionVector *a, PermuterType pt)
Definition: lm_state.h:60
DawgPositionVector active_dawgs
Definition: lm_state.h:62
float ngram_and_classifier_cost
-[ ln(P_classifier(path)) + scale_factor * ln(P_ngram_model(path)) ]
Definition: lm_state.h:87
float ngram_cost
-ln(P_ngram_model(path))
Definition: lm_state.h:85
LanguageModelNgramInfo(const char *c, int l, bool p, float nc, float ncc)
Definition: lm_state.h:69
LanguageModelDawgInfo * dawg_info
Definition: lm_state.h:170
float outline_length
length of the outline so far
Definition: lm_state.h:190
BLOB_CHOICE * curr_b
Pointers to BLOB_CHOICE and parent ViterbiStateEntry (not owned by this).
Definition: lm_state.h:162
AssociateStats associate_stats
character widths/gaps/seams
Definition: lm_state.h:192
ViterbiStateEntry * competing_vse
Definition: lm_state.h:166
int length
number of characters on the path
Definition: lm_state.h:189
void Print(const char *msg) const
Definition: lm_state.cpp:24
ViterbiStateEntry * parent_vse
Definition: lm_state.h:163
LanguageModelNgramInfo * ngram_info
Definition: lm_state.h:174
LanguageModelFlagsType top_choice_flags
Definition: lm_state.h:196
int adapted
number of BLOB_CHOICES from adapted templates
Definition: lm_state.h:188
static int Compare(const void *e1, const void *e2)
Definition: lm_state.h:136
float ratings_sum
sum of ratings of character on the path
Definition: lm_state.h:186
bool updated
set to true if the entry has just been created/updated
Definition: lm_state.h:198
LMConsistencyInfo consistency_info
path consistency info
Definition: lm_state.h:191
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:93
float min_certainty
minimum certainty on the path
Definition: lm_state.h:187
bool HasAlnumChoice(const UNICHARSET &unicharset)
Definition: lm_state.h:149
Struct to store information maintained by various language model components.
Definition: lm_state.h:204
float viterbi_state_entries_prunable_max_cost
Definition: lm_state.h:220
int viterbi_state_entries_length
Total number of entries in viterbi_state_entries.
Definition: lm_state.h:222
int viterbi_state_entries_prunable_length
Number and max cost of prunable paths in viterbi_state_entries.
Definition: lm_state.h:219
ViterbiStateEntry_LIST viterbi_state_entries
Storage for the Viterbi state.
Definition: lm_state.h:217
Bundle together all the things pertaining to the best choice/state.
Definition: lm_state.h:226
std::vector< LanguageModelState * > beam
Definition: lm_state.h:246
DANGERR fixpt
Places to try to fix the word suggested by ambiguity checking.
Definition: lm_state.h:242
ViterbiStateEntry * best_vse
Best ViterbiStateEntry and BLOB_CHOICE.
Definition: lm_state.h:248
bool updated
Flag to indicate whether anything was changed.
Definition: lm_state.h:240
BestChoiceBundle(int matrix_dimension)
Definition: lm_state.h:227