All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
tesseract::SearchNode Class Reference

#include <search_node.h>

Public Member Functions

 SearchNode (CubeRecoContext *cntxt, SearchNode *parent_node, int char_reco_cost, LangModEdge *edge, int col_idx)
 
 ~SearchNode ()
 
bool UpdateParent (SearchNode *new_parent, int new_reco_cost, LangModEdge *new_edge)
 
char_32 * PathString ()
 
const char_32 * NodeString ()
 
void SetString (char_32 *str)
 
int CharRecoCost ()
 
int BestPathRecoCost ()
 
int BestPathLength ()
 
int BestCost ()
 
int BestRecoCost ()
 
int ColIdx ()
 
SearchNode * ParentNode ()
 
LangModEdge * LangModelEdge ()
 
int LangModCost ()
 

Static Public Member Functions

static bool IdenticalPath (SearchNode *node1, SearchNode *node2)
 
static int SearchNodeComparer (const void *node1, const void *node2)
 

Detailed Description

Definition at line 35 of file search_node.h.

Constructor & Destructor Documentation

tesseract::SearchNode::SearchNode ( CubeRecoContext *  cntxt,
SearchNode *  parent_node,
int  char_reco_cost,
LangModEdge *  edge,
int  col_idx 
)

Definition at line 32 of file search_node.cpp.

33  {
34  // copy data members
35  cntxt_ = cntxt;
36  lang_mod_edge_ = edge;
37  col_idx_ = col_idx;
38  parent_node_ = parent_node;
39  char_reco_cost_ = char_reco_cost;
40 
41  // the string of this node is the same as that of the language model edge
42  str_ = (edge == NULL ? NULL : edge->EdgeString());
43 
44  // compute best path total reco cost
45  best_path_reco_cost_ = (parent_node_ == NULL) ? 0 :
46  parent_node_->CharRecoCost() + parent_node_->BestPathRecoCost();
47 
48  // update best path length
49  best_path_len_ = (parent_node_ == NULL) ?
50  1 : parent_node_->BestPathLength() + 1;
51  if (edge != NULL && edge->IsRoot() && parent_node_ != NULL) {
52  best_path_len_++;
53  }
54 
55  // compute best reco cost mean cost
56  mean_char_reco_cost_ = static_cast<int>(
57  (best_path_reco_cost_ + char_reco_cost_) /
58  static_cast<double>(best_path_len_));
59 
60  // get language model cost
61  int lm_cost = LangModCost(lang_mod_edge_, parent_node_);
62 
63  // compute aggregate best cost
64  best_cost_ = static_cast<int>(cntxt_->Params()->RecoWgt() *
65  (best_path_reco_cost_ + char_reco_cost_) /
66  static_cast<double>(best_path_len_)
67  ) + lm_cost;
68 }
TuningParams * Params() const
double RecoWgt() const
Definition: tuning_params.h:48
#define NULL
Definition: host.h:144
tesseract::SearchNode::~SearchNode ( )

Definition at line 70 of file search_node.cpp.

70  {
71  if (lang_mod_edge_ != NULL) {
72  delete lang_mod_edge_;
73  }
74 }
#define NULL
Definition: host.h:144

Member Function Documentation

int tesseract::SearchNode::BestCost ( )
inline

Definition at line 63 of file search_node.h.

63 { return best_cost_; }
int tesseract::SearchNode::BestPathLength ( )
inline

Definition at line 60 of file search_node.h.

60 { return best_path_len_; }
int tesseract::SearchNode::BestPathRecoCost ( )
inline

Definition at line 58 of file search_node.h.

58 { return best_path_reco_cost_; }
int tesseract::SearchNode::BestRecoCost ( )
inline

Definition at line 66 of file search_node.h.

66 { return mean_char_reco_cost_ ; }
int tesseract::SearchNode::CharRecoCost ( )
inline

Definition at line 55 of file search_node.h.

55 { return char_reco_cost_; }
int tesseract::SearchNode::ColIdx ( )
inline

Definition at line 68 of file search_node.h.

68 { return col_idx_; }
bool tesseract::SearchNode::IdenticalPath ( SearchNode *  node1,
SearchNode *  node2 
)
static

Definition at line 178 of file search_node.cpp.

178  {
179  if (node1 != NULL && node2 != NULL &&
180  node1->best_path_len_ != node2->best_path_len_) {
181  return false;
182  }
183 
184  // backtrack until either a root or a NULL edge is reached
185  while (node1 != NULL && node2 != NULL) {
186  if (node1->str_ != node2->str_) {
187  return false;
188  }
189 
190  // stop if either nodes is a root
191  if (node1->LangModelEdge()->IsRoot() || node2->LangModelEdge()->IsRoot()) {
192  break;
193  }
194 
195  node1 = node1->parent_node_;
196  node2 = node2->parent_node_;
197  }
198 
199  return ((node1 == NULL && node2 == NULL) ||
200  (node1 != NULL && node1->LangModelEdge()->IsRoot() &&
201  node2 != NULL && node2->LangModelEdge()->IsRoot()));
202 }
#define NULL
Definition: host.h:144
int tesseract::SearchNode::LangModCost ( )
inline

Definition at line 71 of file search_node.h.

71 { return LangModCost(lang_mod_edge_, parent_node_); }
LangModEdge* tesseract::SearchNode::LangModelEdge ( )
inline

Definition at line 70 of file search_node.h.

70 { return lang_mod_edge_;}
const char_32* tesseract::SearchNode::NodeString ( )
inline

Definition at line 51 of file search_node.h.

51 { return str_; }
SearchNode* tesseract::SearchNode::ParentNode ( )
inline

Definition at line 69 of file search_node.h.

69 { return parent_node_; }
char_32 * tesseract::SearchNode::PathString ( )

Definition at line 129 of file search_node.cpp.

129  {
130  SearchNode *node = this;
131 
132  // compute string length
133  int len = 0;
134 
135  while (node != NULL) {
136  if (node->str_ != NULL) {
137  len += CubeUtils::StrLen(node->str_);
138  }
139 
140  // if the edge is a root and does not have a NULL parent, account for space
141  LangModEdge *lm_edge = node->LangModelEdge();
142  if (lm_edge != NULL && lm_edge->IsRoot() && node->ParentNode() != NULL) {
143  len++;
144  }
145 
146  node = node->parent_node_;
147  }
148 
149  char_32 *char_ptr = new char_32[len + 1];
150  if (char_ptr == NULL) {
151  return NULL;
152  }
153 
154  int ch_idx = len;
155 
156  node = this;
157  char_ptr[ch_idx--] = 0;
158 
159  while (node != NULL) {
160  int str_len = ((node->str_ == NULL) ? 0 : CubeUtils::StrLen(node->str_));
161  while (str_len > 0) {
162  char_ptr[ch_idx--] = node->str_[--str_len];
163  }
164 
165  // if the edge is a root and does not have a NULL parent, insert a space
166  LangModEdge *lm_edge = node->LangModelEdge();
167  if (lm_edge != NULL && lm_edge->IsRoot() && node->ParentNode() != NULL) {
168  char_ptr[ch_idx--] = (char_32)' ';
169  }
170 
171  node = node->parent_node_;
172  }
173 
174  return char_ptr;
175 }
static int StrLen(const char_32 *str)
Definition: cube_utils.cpp:54
signed int char_32
Definition: string_32.h:40
#define NULL
Definition: host.h:144
SearchNode(CubeRecoContext *cntxt, SearchNode *parent_node, int char_reco_cost, LangModEdge *edge, int col_idx)
Definition: search_node.cpp:32
static int tesseract::SearchNode::SearchNodeComparer ( const void *  node1,
const void *  node2 
)
inlinestatic

Definition at line 75 of file search_node.h.

75  {
76  return (*(reinterpret_cast<SearchNode * const *>(node1)))->best_cost_ -
77  (*(reinterpret_cast<SearchNode * const *>(node2)))->best_cost_;
78  }
SearchNode(CubeRecoContext *cntxt, SearchNode *parent_node, int char_reco_cost, LangModEdge *edge, int col_idx)
Definition: search_node.cpp:32
void tesseract::SearchNode::SetString ( char_32 *  str)
inline

Definition at line 52 of file search_node.h.

52 { str_ = str; }
bool tesseract::SearchNode::UpdateParent ( SearchNode *  new_parent,
int  new_reco_cost,
LangModEdge *  new_edge 
)

Definition at line 77 of file search_node.cpp.

78  {
79  if (lang_mod_edge_ == NULL) {
80  if (new_edge != NULL) {
81  return false;
82  }
83  } else {
84  // to update the parent_node, we have to have the same target
85  // state and char
86  if (new_edge == NULL || !lang_mod_edge_->IsIdentical(new_edge) ||
87  !SearchNode::IdenticalPath(parent_node_, new_parent)) {
88  return false;
89  }
90  }
91 
92  // compute the path cost and combined cost of the new path
93  int new_best_path_reco_cost;
94  int new_cost;
95  int new_best_path_len;
96 
97  new_best_path_reco_cost = (new_parent == NULL) ?
98  0 : new_parent->BestPathRecoCost() + new_parent->CharRecoCost();
99 
100  new_best_path_len =
101  (new_parent == NULL) ? 1 : new_parent->BestPathLength() + 1;
102 
103  // compute the new language model cost
104  int new_lm_cost = LangModCost(new_edge, new_parent);
105 
106  new_cost = static_cast<int>(cntxt_->Params()->RecoWgt() *
107  (new_best_path_reco_cost + new_reco_cost) /
108  static_cast<double>(new_best_path_len)
109  ) + new_lm_cost;
110 
111  // update if it is better (less) than the current one
112  if (best_cost_ > new_cost) {
113  parent_node_ = new_parent;
114  char_reco_cost_ = new_reco_cost;
115  best_path_reco_cost_ = new_best_path_reco_cost;
116  best_path_len_ = new_best_path_len;
117  mean_char_reco_cost_ = static_cast<int>(
118  (best_path_reco_cost_ + char_reco_cost_) /
119  static_cast<double>(best_path_len_));
120  best_cost_ = static_cast<int>(cntxt_->Params()->RecoWgt() *
121  (best_path_reco_cost_ + char_reco_cost_) /
122  static_cast<double>(best_path_len_)
123  ) + new_lm_cost;
124  return true;
125  }
126  return false;
127 }
virtual bool IsIdentical(LangModEdge *edge) const =0
static bool IdenticalPath(SearchNode *node1, SearchNode *node2)
TuningParams * Params() const
double RecoWgt() const
Definition: tuning_params.h:48
#define NULL
Definition: host.h:144

The documentation for this class was generated from the following files: