tesseract  4.00.00dev
segsearch.cpp
Go to the documentation of this file.
1 // File: segsearch.cpp
3 // Description: Segmentation search functions.
4 // Author: Daria Antonova
5 // Created: Mon Jun 23 11:26:43 PDT 2008
6 //
7 // (C) Copyright 2009, 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 #include "wordrec.h"
21 
22 #include "associate.h"
23 #include "language_model.h"
24 #include "matrix.h"
25 #include "params.h"
26 #include "lm_pain_points.h"
27 #include "ratngs.h"
28 
29 namespace tesseract {
30 
31 void Wordrec::DoSegSearch(WERD_RES* word_res) {
32  BestChoiceBundle best_choice_bundle(word_res->ratings->dimension());
33  // Run Segmentation Search.
34  SegSearch(word_res, &best_choice_bundle, NULL);
35 }
36 
38  BestChoiceBundle* best_choice_bundle,
39  BlamerBundle* blamer_bundle) {
44  // Compute scaling factor that will help us recover blob outline length
45  // from classifier rating and certainty for the blob.
46  float rating_cert_scale = -1.0 * getDict().certainty_scale / rating_scale;
48  InitialSegSearch(word_res, &pain_points, &pending, best_choice_bundle,
49  blamer_bundle);
50 
51  if (!SegSearchDone(0)) { // find a better choice
52  if (chop_enable && word_res->chopped_word != NULL) {
53  improve_by_chopping(rating_cert_scale, word_res, best_choice_bundle,
54  blamer_bundle, &pain_points, &pending);
55  }
56  if (chop_debug) SEAM::PrintSeams("Final seam list:", word_res->seam_array);
57 
58  if (blamer_bundle != NULL &&
59  !blamer_bundle->ChoiceIsCorrect(word_res->best_choice)) {
60  blamer_bundle->SetChopperBlame(word_res, wordrec_debug_blamer);
61  }
62  }
63  // Keep trying to find a better path by fixing the "pain points".
64 
65  MATRIX_COORD pain_point;
66  float pain_point_priority;
67  int num_futile_classifications = 0;
68  STRING blamer_debug;
69  while (wordrec_enable_assoc &&
70  (!SegSearchDone(num_futile_classifications) ||
71  (blamer_bundle != NULL &&
72  blamer_bundle->GuidedSegsearchStillGoing()))) {
73  // Get the next valid "pain point".
74  bool found_nothing = true;
75  LMPainPointsType pp_type;
76  while ((pp_type = pain_points.Deque(&pain_point, &pain_point_priority)) !=
77  LM_PPTYPE_NUM) {
78  if (!pain_point.Valid(*word_res->ratings)) {
79  word_res->ratings->IncreaseBandSize(
80  pain_point.row - pain_point.col + 1);
81  }
82  if (pain_point.Valid(*word_res->ratings) &&
83  !word_res->ratings->Classified(pain_point.col, pain_point.row,
84  getDict().WildcardID())) {
85  found_nothing = false;
86  break;
87  }
88  }
89  if (found_nothing) {
90  if (segsearch_debug_level > 0) tprintf("Pain points queue is empty\n");
91  break;
92  }
93  ProcessSegSearchPainPoint(pain_point_priority, pain_point,
95  &pending, word_res, &pain_points, blamer_bundle);
96 
97  UpdateSegSearchNodes(rating_cert_scale, pain_point.col, &pending,
98  word_res, &pain_points, best_choice_bundle,
99  blamer_bundle);
100  if (!best_choice_bundle->updated) ++num_futile_classifications;
101 
102  if (segsearch_debug_level > 0) {
103  tprintf("num_futile_classifications %d\n", num_futile_classifications);
104  }
105 
106  best_choice_bundle->updated = false; // reset updated
107 
108  // See if it's time to terminate SegSearch or time for starting a guided
109  // search for the true path to find the blame for the incorrect best_choice.
110  if (SegSearchDone(num_futile_classifications) &&
111  blamer_bundle != NULL &&
112  blamer_bundle->GuidedSegsearchNeeded(word_res->best_choice)) {
113  InitBlamerForSegSearch(word_res, &pain_points, blamer_bundle,
114  &blamer_debug);
115  }
116  } // end while loop exploring alternative paths
117  if (blamer_bundle != NULL) {
118  blamer_bundle->FinishSegSearch(word_res->best_choice,
119  wordrec_debug_blamer, &blamer_debug);
120  }
121 
122  if (segsearch_debug_level > 0) {
123  tprintf("Done with SegSearch (AcceptableChoiceFound: %d)\n",
125  }
126 }
127 
128 // Setup and run just the initial segsearch on an established matrix,
129 // without doing any additional chopping or joining.
130 // (Internal factored version that can be used as part of the main SegSearch.)
131 void Wordrec::InitialSegSearch(WERD_RES* word_res, LMPainPoints* pain_points,
133  BestChoiceBundle* best_choice_bundle,
134  BlamerBundle* blamer_bundle) {
135  if (segsearch_debug_level > 0) {
136  tprintf("Starting SegSearch on ratings matrix%s:\n",
137  wordrec_enable_assoc ? " (with assoc)" : "");
138  word_res->ratings->print(getDict().getUnicharset());
139  }
140 
141  pain_points->GenerateInitial(word_res);
142 
143  // Compute scaling factor that will help us recover blob outline length
144  // from classifier rating and certainty for the blob.
145  float rating_cert_scale = -1.0 * getDict().certainty_scale / rating_scale;
146 
149  segsearch_max_char_wh_ratio, rating_cert_scale);
150 
151  // Initialize blamer-related information: map character boxes recorded in
152  // blamer_bundle->norm_truth_word to the corresponding i,j indices in the
153  // ratings matrix. We expect this step to succeed, since when running the
154  // chopper we checked that the correct chops are present.
155  if (blamer_bundle != NULL) {
156  blamer_bundle->SetupCorrectSegmentation(word_res->chopped_word,
158  }
159 
160  // pending[col] tells whether there is update work to do to combine
161  // best_choice_bundle->beam[col - 1] with some BLOB_CHOICEs in matrix[col, *].
162  // As the language model state is updated, pending entries are modified to
163  // minimize duplication of work. It is important that during the update the
164  // children are considered in the non-decreasing order of their column, since
165  // this guarantees that all the parents would be up to date before an update
166  // of a child is done.
167  pending->init_to_size(word_res->ratings->dimension(), SegSearchPending());
168 
169  // Search the ratings matrix for the initial best path.
170  (*pending)[0].SetColumnClassified();
171  UpdateSegSearchNodes(rating_cert_scale, 0, pending, word_res,
172  pain_points, best_choice_bundle, blamer_bundle);
173 }
174 
176  float rating_cert_scale,
177  int starting_col,
179  WERD_RES *word_res,
180  LMPainPoints *pain_points,
181  BestChoiceBundle *best_choice_bundle,
182  BlamerBundle *blamer_bundle) {
183  MATRIX *ratings = word_res->ratings;
184  ASSERT_HOST(ratings->dimension() == pending->size());
185  ASSERT_HOST(ratings->dimension() == best_choice_bundle->beam.size());
186  for (int col = starting_col; col < ratings->dimension(); ++col) {
187  if (!(*pending)[col].WorkToDo()) continue;
188  int first_row = col;
189  int last_row = MIN(ratings->dimension() - 1,
190  col + ratings->bandwidth() - 1);
191  if ((*pending)[col].SingleRow() >= 0) {
192  first_row = last_row = (*pending)[col].SingleRow();
193  }
194  if (segsearch_debug_level > 0) {
195  tprintf("\n\nUpdateSegSearchNodes: col=%d, rows=[%d,%d], alljust=%d\n",
196  col, first_row, last_row,
197  (*pending)[col].IsRowJustClassified(MAX_INT32));
198  }
199  // Iterate over the pending list for this column.
200  for (int row = first_row; row <= last_row; ++row) {
201  // Update language model state of this child+parent pair.
202  BLOB_CHOICE_LIST *current_node = ratings->get(col, row);
203  LanguageModelState *parent_node =
204  col == 0 ? NULL : best_choice_bundle->beam[col - 1];
205  if (current_node != NULL &&
206  language_model_->UpdateState((*pending)[col].IsRowJustClassified(row),
207  col, row, current_node, parent_node,
208  pain_points, word_res,
209  best_choice_bundle, blamer_bundle) &&
210  row + 1 < ratings->dimension()) {
211  // Since the language model state of this entry changed, process all
212  // the child column.
213  (*pending)[row + 1].RevisitWholeColumn();
214  if (segsearch_debug_level > 0) {
215  tprintf("Added child col=%d to pending\n", row + 1);
216  }
217  } // end if UpdateState.
218  } // end for row.
219  } // end for col.
220  if (best_choice_bundle->best_vse != NULL) {
221  ASSERT_HOST(word_res->StatesAllValid());
222  if (best_choice_bundle->best_vse->updated) {
223  pain_points->GenerateFromPath(rating_cert_scale,
224  best_choice_bundle->best_vse, word_res);
225  if (!best_choice_bundle->fixpt.empty()) {
226  pain_points->GenerateFromAmbigs(best_choice_bundle->fixpt,
227  best_choice_bundle->best_vse, word_res);
228  }
229  }
230  }
231  // The segsearch is completed. Reset all updated flags on all VSEs and reset
232  // all pendings.
233  for (int col = 0; col < pending->size(); ++col) {
234  (*pending)[col].Clear();
235  ViterbiStateEntry_IT
236  vse_it(&best_choice_bundle->beam[col]->viterbi_state_entries);
237  for (vse_it.mark_cycle_pt(); !vse_it.cycled_list(); vse_it.forward()) {
238  vse_it.data()->updated = false;
239  }
240  }
241 }
242 
244  float pain_point_priority,
245  const MATRIX_COORD &pain_point, const char* pain_point_type,
246  GenericVector<SegSearchPending>* pending, WERD_RES *word_res,
247  LMPainPoints *pain_points, BlamerBundle *blamer_bundle) {
248  if (segsearch_debug_level > 0) {
249  tprintf("Classifying pain point %s priority=%.4f, col=%d, row=%d\n",
250  pain_point_type, pain_point_priority,
251  pain_point.col, pain_point.row);
252  }
253  ASSERT_HOST(pain_points != NULL);
254  MATRIX *ratings = word_res->ratings;
255  // Classify blob [pain_point.col pain_point.row]
256  if (!pain_point.Valid(*ratings)) {
257  ratings->IncreaseBandSize(pain_point.row + 1 - pain_point.col);
258  }
259  ASSERT_HOST(pain_point.Valid(*ratings));
260  BLOB_CHOICE_LIST *classified = classify_piece(word_res->seam_array,
261  pain_point.col, pain_point.row,
262  pain_point_type,
263  word_res->chopped_word,
264  blamer_bundle);
265  BLOB_CHOICE_LIST *lst = ratings->get(pain_point.col, pain_point.row);
266  if (lst == NULL) {
267  ratings->put(pain_point.col, pain_point.row, classified);
268  } else {
269  // We can not delete old BLOB_CHOICEs, since they might contain
270  // ViterbiStateEntries that are parents of other "active" entries.
271  // Thus if the matrix cell already contains classifications we add
272  // the new ones to the beginning of the list.
273  BLOB_CHOICE_IT it(lst);
274  it.add_list_before(classified);
275  delete classified; // safe to delete, since empty after add_list_before()
276  classified = NULL;
277  }
278 
279  if (segsearch_debug_level > 0) {
280  print_ratings_list("Updated ratings matrix with a new entry:",
281  ratings->get(pain_point.col, pain_point.row),
282  getDict().getUnicharset());
283  ratings->print(getDict().getUnicharset());
284  }
285 
286  // Insert initial "pain points" to join the newly classified blob
287  // with its left and right neighbors.
288  if (classified != NULL && !classified->empty()) {
289  if (pain_point.col > 0) {
290  pain_points->GeneratePainPoint(
291  pain_point.col - 1, pain_point.row, LM_PPTYPE_SHAPE, 0.0,
292  true, segsearch_max_char_wh_ratio, word_res);
293  }
294  if (pain_point.row + 1 < ratings->dimension()) {
295  pain_points->GeneratePainPoint(
296  pain_point.col, pain_point.row + 1, LM_PPTYPE_SHAPE, 0.0,
297  true, segsearch_max_char_wh_ratio, word_res);
298  }
299  }
300  (*pending)[pain_point.col].SetBlobClassified(pain_point.row);
301 }
302 
303 // Resets enough of the results so that the Viterbi search is re-run.
304 // Needed when the n-gram model is enabled, as the multi-length comparison
305 // implementation will re-value existing paths to worse values.
307  BestChoiceBundle* best_choice_bundle,
309  // TODO(rays) More refactoring required here.
310  // Delete existing viterbi states.
311  for (int col = 0; col < best_choice_bundle->beam.size(); ++col) {
312  best_choice_bundle->beam[col]->Clear();
313  }
314  // Reset best_choice_bundle.
315  word_res->ClearWordChoices();
316  best_choice_bundle->best_vse = NULL;
317  // Clear out all existing pendings and add a new one for the first column.
318  (*pending)[0].SetColumnClassified();
319  for (int i = 1; i < pending->size(); ++i)
320  (*pending)[i].Clear();
321 }
322 
324  LMPainPoints *pain_points,
325  BlamerBundle *blamer_bundle,
326  STRING *blamer_debug) {
327  pain_points->Clear(); // Clear pain points heap.
329  pain_points, &LMPainPoints::GenerateForBlamer,
330  static_cast<double>(segsearch_max_char_wh_ratio), word_res);
331  blamer_bundle->InitForSegSearch(word_res->best_choice, word_res->ratings,
332  getDict().WildcardID(), wordrec_debug_blamer,
333  blamer_debug, pp_cb);
334  delete pp_cb;
335 }
336 
337 } // namespace tesseract
bool empty() const
Definition: genericvector.h:91
#define MIN(x, y)
Definition: ndminx.h:28
void FinishSegSearch(const WERD_CHOICE *best_choice, bool debug, STRING *debug_str)
Definition: blamer.cpp:506
int segsearch_max_pain_points
Definition: wordrec.h:171
PointerVector< LanguageModelState > beam
Definition: lm_state.h:231
void DoSegSearch(WERD_RES *word_res)
Definition: segsearch.cpp:31
double certainty_scale
Definition: dict.h:611
void put(ICOORD pos, const T &thing)
Definition: matrix.h:219
bool GeneratePainPoint(int col, int row, LMPainPointsType pp_type, float special_priority, bool ok_to_extend, float max_char_wh_ratio, WERD_RES *word_res)
TWERD * chopped_word
Definition: pageres.h:201
void ClearWordChoices()
Definition: pageres.cpp:1175
void print_ratings_list(const char *msg, BLOB_CHOICE_LIST *ratings, const UNICHARSET &current_unicharset)
Definition: ratngs.cpp:822
bool wordrec_enable_assoc
Definition: wordrec.h:130
void GenerateInitial(WERD_RES *word_res)
int dimension() const
Definition: matrix.h:528
WERD_CHOICE * prev_word_best_choice_
Definition: wordrec.h:412
void IncreaseBandSize(int bandwidth)
Definition: matrix.cpp:49
#define MAX_INT32
Definition: host.h:62
_ConstTessMemberResultCallback_0_0< false, R, T1 >::base * NewPermanentTessCallback(const T1 *obj, R(T2::*member)() const)
Definition: tesscallback.h:116
virtual Dict & getDict()
Definition: classify.h:65
LanguageModel * language_model_
Definition: wordrec.h:407
Struct to store information maintained by various language model components.
Definition: lm_state.h:193
ViterbiStateEntry * best_vse
Best ViterbiStateEntry and BLOB_CHOICE.
Definition: lm_state.h:233
MATRIX * ratings
Definition: pageres.h:215
void InitForWord(const WERD_CHOICE *prev_word, bool fixed_pitch, float max_char_wh_ratio, float rating_cert_scale)
int size() const
Definition: genericvector.h:72
WERD_CHOICE * best_choice
Definition: pageres.h:219
virtual BLOB_CHOICE_LIST * classify_piece(const GenericVector< SEAM *> &seams, inT16 start, inT16 end, const char *description, TWERD *word, BlamerBundle *blamer_bundle)
Definition: pieces.cpp:56
bool GuidedSegsearchStillGoing() const
Definition: blamer.cpp:501
#define tprintf(...)
Definition: tprintf.h:31
void UpdateSegSearchNodes(float rating_cert_scale, int starting_col, GenericVector< SegSearchPending > *pending, WERD_RES *word_res, LMPainPoints *pain_points, BestChoiceBundle *best_choice_bundle, BlamerBundle *blamer_bundle)
Definition: segsearch.cpp:175
void print(const UNICHARSET &unicharset) const
Definition: matrix.cpp:112
LMPainPointsType Deque(MATRIX_COORD *pp, float *priority)
static const char * PainPointDescription(LMPainPointsType type)
T get(ICOORD pos) const
Definition: matrix.h:227
bool GuidedSegsearchNeeded(const WERD_CHOICE *best_choice) const
Definition: blamer.cpp:461
bool assume_fixed_pitch_char_segment
Definition: wordrec.h:161
void ProcessSegSearchPainPoint(float pain_point_priority, const MATRIX_COORD &pain_point, const char *pain_point_type, GenericVector< SegSearchPending > *pending, WERD_RES *word_res, LMPainPoints *pain_points, BlamerBundle *blamer_bundle)
Definition: segsearch.cpp:243
void SetChopperBlame(const WERD_RES *word, bool debug)
Definition: blamer.cpp:310
static void PrintSeams(const char *label, const GenericVector< SEAM *> &seams)
Definition: seam.cpp:173
Definition: strngs.h:45
bool Valid(const MATRIX &m) const
Definition: matrix.h:608
Definition: matrix.h:570
#define ASSERT_HOST(x)
Definition: errcode.h:84
int bandwidth() const
Definition: matrix.h:530
double segsearch_max_char_wh_ratio
Definition: wordrec.h:175
int segsearch_debug_level
Definition: wordrec.h:169
void SetupCorrectSegmentation(const TWERD *word, bool debug)
Definition: blamer.cpp:407
void GenerateFromPath(float rating_cert_scale, ViterbiStateEntry *vse, WERD_RES *word_res)
DANGERR fixpt
Places to try to fix the word suggested by ambiguity checking.
Definition: lm_state.h:227
bool updated
Flag to indicate whether anything was changed.
Definition: lm_state.h:225
void improve_by_chopping(float rating_cert_scale, WERD_RES *word, BestChoiceBundle *best_choice_bundle, BlamerBundle *blamer_bundle, LMPainPoints *pain_points, GenericVector< SegSearchPending > *pending)
Definition: chopper.cpp:456
void SegSearch(WERD_RES *word_res, BestChoiceBundle *best_choice_bundle, BlamerBundle *blamer_bundle)
Definition: segsearch.cpp:37
void GenerateFromAmbigs(const DANGERR &fixpt, ViterbiStateEntry *vse, WERD_RES *word_res)
bool Classified(int col, int row, int wildcard_id) const
Definition: matrix.cpp:36
bool UpdateState(bool just_classified, int curr_col, int curr_row, BLOB_CHOICE_LIST *curr_list, LanguageModelState *parent_node, LMPainPoints *pain_points, WERD_RES *word_res, BestChoiceBundle *best_choice_bundle, BlamerBundle *blamer_bundle)
bool StatesAllValid()
Definition: pageres.cpp:450
void ResetNGramSearch(WERD_RES *word_res, BestChoiceBundle *best_choice_bundle, GenericVector< SegSearchPending > *pending)
Definition: segsearch.cpp:306
void InitBlamerForSegSearch(WERD_RES *word_res, LMPainPoints *pain_points, BlamerBundle *blamer_bundle, STRING *blamer_debug)
Definition: segsearch.cpp:323
bool ChoiceIsCorrect(const WERD_CHOICE *word_choice) const
Definition: blamer.cpp:111
void init_to_size(int size, T t)
const UNICHARSET & getUnicharset() const
Definition: dict.h:97
void InitForSegSearch(const WERD_CHOICE *best_choice, MATRIX *ratings, UNICHAR_ID wildcard_id, bool debug, STRING *debug_str, TessResultCallback2< bool, int, int > *pp_cb)
Definition: blamer.cpp:473
GenericVector< SEAM * > seam_array
Definition: pageres.h:203
void InitialSegSearch(WERD_RES *word_res, LMPainPoints *pain_points, GenericVector< SegSearchPending > *pending, BestChoiceBundle *best_choice_bundle, BlamerBundle *blamer_bundle)
Definition: segsearch.cpp:131
Bundle together all the things pertaining to the best choice/state.
Definition: lm_state.h:215
bool SegSearchDone(int num_futile_classifications)
Definition: wordrec.h:422
bool wordrec_debug_blamer
Definition: wordrec.h:167
bool GenerateForBlamer(double max_char_wh_ratio, WERD_RES *word_res, int col, int row)