tesseract  3.03
/usr/local/google/home/jbreiden/tesseract-ocr-read-only/wordrec/lm_pain_points.h
Go to the documentation of this file.
00001 
00002 // File:        lm_pain_points.h
00003 // Description: Functions that utilize the knowledge about the properties
00004 //              of the paths explored by the segmentation search in order
00005 //              to generate "pain points" - the locations in the ratings
00006 //              matrix which should be classified next.
00007 // Author:      Rika Antonova
00008 // Created:     Mon Jun 20 11:26:43 PST 2012
00009 //
00010 // (C) Copyright 2012, Google Inc.
00011 // Licensed under the Apache License, Version 2.0 (the "License");
00012 // you may not use this file except in compliance with the License.
00013 // You may obtain a copy of the License at
00014 // http://www.apache.org/licenses/LICENSE-2.0
00015 // Unless required by applicable law or agreed to in writing, software
00016 // distributed under the License is distributed on an "AS IS" BASIS,
00017 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00018 // See the License for the specific language governing permissions and
00019 // limitations under the License.
00020 //
00022 
00023 #ifndef TESSERACT_WORDREC_PAIN_POINTS_H_
00024 #define TESSERACT_WORDREC_PAIN_POINTS_H_
00025 
00026 #include "associate.h"
00027 #include "dict.h"
00028 #include "genericheap.h"
00029 #include "lm_state.h"
00030 
00031 namespace tesseract {
00032 
00033 // Heap of pain points used for determining where to chop/join.
00034 typedef GenericHeap<MatrixCoordPair> PainPointHeap;
00035 
00036 // Types of pain points (ordered in the decreasing level of importance).
00037 enum LMPainPointsType {
00038   LM_PPTYPE_BLAMER,
00039   LM_PPTYPE_AMBIG,
00040   LM_PPTYPE_PATH,
00041   LM_PPTYPE_SHAPE,
00042 
00043   LM_PPTYPE_NUM
00044 };
00045 
00046 static const char * const LMPainPointsTypeName[] = {
00047     "LM_PPTYPE_BLAMER",
00048     "LM_PPTYPE_AMBIGS",
00049     "LM_PPTYPE_PATH",
00050     "LM_PPTYPE_SHAPE",
00051 };
00052 
00053 class LMPainPoints {
00054  public:
00055 
00056   static const float kDefaultPainPointPriorityAdjustment;
00057   // If there is a significant drop in character ngram probability or a
00058   // dangerous ambiguity make the thresholds on what blob combinations
00059   // can be classified looser.
00060   static const float kLooseMaxCharWhRatio;
00061   // Returns a description of the type of a pain point.
00062   static const char* PainPointDescription(LMPainPointsType type) {
00063     return LMPainPointsTypeName[type];
00064   }
00065 
00066   LMPainPoints(int max, float rat, bool fp, const Dict *d, int deb) :
00067     max_heap_size_(max), max_char_wh_ratio_(rat), fixed_pitch_(fp),
00068     dict_(d), debug_level_(deb) {}
00069   ~LMPainPoints() {}
00070 
00071   // Returns true if the heap of pain points of pp_type is not empty().
00072   inline bool HasPainPoints(LMPainPointsType pp_type) const {
00073     return !pain_points_heaps_[pp_type].empty();
00074   }
00075 
00076   // Dequeues the next pain point from the pain points queue and copies
00077   // its contents and priority to *pp and *priority.
00078   // Returns LM_PPTYPE_NUM if pain points queue is empty, otherwise the type.
00079   LMPainPointsType Deque(MATRIX_COORD *pp, float *priority);
00080 
00081   // Clears pain points heap.
00082   void Clear() {
00083     for (int h = 0; h < LM_PPTYPE_NUM; ++h) pain_points_heaps_[h].clear();
00084   }
00085 
00086   // For each cell, generate a "pain point" if the cell is not classified
00087   // and has a left or right neighbor that was classified.
00088   void GenerateInitial(WERD_RES *word_res);
00089 
00090   // Generate pain points from the given path.
00091   void GenerateFromPath(float rating_cert_scale, ViterbiStateEntry *vse,
00092                         WERD_RES *word_res);
00093 
00094   // Generate pain points from dangerous ambiguities in best choice.
00095   void GenerateFromAmbigs(const DANGERR &fixpt, ViterbiStateEntry *vse,
00096                           WERD_RES *word_res);
00097 
00098   // Generate a pain point for the blamer.
00099   bool GenerateForBlamer(double max_char_wh_ratio, WERD_RES *word_res,
00100                          int col, int row) {
00101     return GeneratePainPoint(col, row, LM_PPTYPE_BLAMER, 0.0, false,
00102                              max_char_wh_ratio, word_res);
00103   }
00104 
00105   // Adds a pain point to classify chunks_record->ratings(col, row).
00106   // Returns true if a new pain point was added to an appropriate heap.
00107   // Pain point priority is set to special_priority for pain points of
00108   // LM_PPTYPE_AMBIG or LM_PPTYPE_PATH, for other pain points
00109   // AssociateStats::gap_sum is used.
00110   bool GeneratePainPoint(int col, int row, LMPainPointsType pp_type,
00111                          float special_priority, bool ok_to_extend,
00112                          float max_char_wh_ratio,
00113                          WERD_RES *word_res);
00114 
00115   // Adjusts the pain point coordinates to cope with expansion of the ratings
00116   // matrix due to a split of the blob with the given index.
00117   void RemapForSplit(int index);
00118 
00119  private:
00120   // Priority queues containing pain points generated by the language model
00121   // The priority is set by the language model components, adjustments like
00122   // seam cost and width priority are factored into the priority.
00123   PainPointHeap pain_points_heaps_[LM_PPTYPE_NUM];
00124   // Maximum number of points to keep in the heap.
00125   int max_heap_size_;
00126   // Maximum character width/height ratio.
00127   float max_char_wh_ratio_;
00128   // Set to true if fixed pitch should be assumed.
00129   bool fixed_pitch_;
00130   // Cached pointer to dictionary.
00131   const Dict *dict_;
00132   // Debug level for print statements.
00133   int debug_level_;
00134 };
00135 
00136 }  // namespace tesseract
00137 
00138 #endif  // TESSERACT_WORDREC_PAIN_POINTS_H_
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines