Tesseract  3.02
tesseract-ocr/cube/search_column.h
Go to the documentation of this file.
00001 /**********************************************************************
00002  * File:        search_column.h
00003  * Description: Declaration of the Beam Search Column Class
00004  * Author:    Ahmad Abdulkader
00005  * Created:   2008
00006  *
00007  * (C) Copyright 2008, Google Inc.
00008  ** Licensed under the Apache License, Version 2.0 (the "License");
00009  ** you may not use this file except in compliance with the License.
00010  ** You may obtain a copy of the License at
00011  ** http://www.apache.org/licenses/LICENSE-2.0
00012  ** Unless required by applicable law or agreed to in writing, software
00013  ** distributed under the License is distributed on an "AS IS" BASIS,
00014  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015  ** See the License for the specific language governing permissions and
00016  ** limitations under the License.
00017  *
00018  **********************************************************************/
00019 
00020 // The SearchColumn class abstracts a column in the lattice that is created
00021 // by the BeamSearch during the recognition process
00022 // The class holds the lattice nodes. New nodes are added by calls to AddNode
00023 // made from the BeamSearch
00024 // The class maintains a hash table of the nodes to be able to lookup nodes
00025 // quickly using their lang_mod_edge. This is needed to merge similar paths
00026 // in the lattice
00027 
00028 #ifndef SEARCH_COLUMN_H
00029 #define SEARCH_COLUMN_H
00030 
00031 #include "search_node.h"
00032 #include "lang_mod_edge.h"
00033 #include "cube_reco_context.h"
00034 
00035 namespace tesseract {
00036 
00037 class SearchColumn {
00038  public:
00039   SearchColumn(int col_idx, int max_node_cnt);
00040   ~SearchColumn();
00041   // Accessor functions
00042   inline int ColIdx() const { return col_idx_; }
00043   inline int NodeCount() const { return node_cnt_; }
00044   inline SearchNode **Nodes() const { return node_array_; }
00045 
00046   // Prune the nodes if necessary. Pruning is done such that a max
00047   // number of nodes is kept, i.e., the beam width
00048   void Prune();
00049   SearchNode *AddNode(LangModEdge *edge, int score,
00050                       SearchNode *parent, CubeRecoContext *cntxt);
00051   // Returns the node with the least cost
00052   SearchNode *BestNode();
00053   // Sort the lattice nodes. Needed for visualization
00054   void Sort();
00055   // Free up the Hash Table. Added to be called by the Beam Search after
00056   // a column is pruned to reduce memory foot print
00057   void FreeHashTable() {
00058     if (node_hash_table_ != NULL) {
00059       delete node_hash_table_;
00060       node_hash_table_ = NULL;
00061     }
00062   }
00063 
00064  private:
00065   static const int kNodeAllocChunk = 1024;
00066   static const int kScoreBins = 1024;
00067   bool init_;
00068   int min_cost_;
00069   int max_cost_;
00070   int max_node_cnt_;
00071   int node_cnt_;
00072   int col_idx_;
00073   int score_bins_[kScoreBins];
00074   SearchNode **node_array_;
00075   SearchNodeHashTable *node_hash_table_;
00076 
00077   // Free node array and hash table
00078   void Cleanup();
00079   // Create hash table
00080   bool Init();
00081 };
00082 }
00083 
00084 #endif  // SEARCH_COLUMN_H