tesseract  3.05.01
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

◆ SearchNode()

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

◆ ~SearchNode()

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 }

Member Function Documentation

◆ BestCost()

int tesseract::SearchNode::BestCost ( )
inline

Definition at line 63 of file search_node.h.

63 { return best_cost_; }

◆ BestPathLength()

int tesseract::SearchNode::BestPathLength ( )
inline

Definition at line 60 of file search_node.h.

60 { return best_path_len_; }

◆ BestPathRecoCost()

int tesseract::SearchNode::BestPathRecoCost ( )
inline

Definition at line 58 of file search_node.h.

58 { return best_path_reco_cost_; }

◆ BestRecoCost()

int tesseract::SearchNode::BestRecoCost ( )
inline

Definition at line 66 of file search_node.h.

66 { return mean_char_reco_cost_ ; }

◆ CharRecoCost()

int tesseract::SearchNode::CharRecoCost ( )
inline

Definition at line 55 of file search_node.h.

55 { return char_reco_cost_; }

◆ ColIdx()

int tesseract::SearchNode::ColIdx ( )
inline

Definition at line 68 of file search_node.h.

68 { return col_idx_; }

◆ IdenticalPath()

bool tesseract::SearchNode::IdenticalPath ( SearchNode *  node1,
SearchNode *  node2 
)
static

Definition at line 175 of file search_node.cpp.

175  {
176  if (node1 != NULL && node2 != NULL &&
177  node1->best_path_len_ != node2->best_path_len_) {
178  return false;
179  }
180 
181  // backtrack until either a root or a NULL edge is reached
182  while (node1 != NULL && node2 != NULL) {
183  if (node1->str_ != node2->str_) {
184  return false;
185  }
186 
187  // stop if either nodes is a root
188  if (node1->LangModelEdge()->IsRoot() || node2->LangModelEdge()->IsRoot()) {
189  break;
190  }
191 
192  node1 = node1->parent_node_;
193  node2 = node2->parent_node_;
194  }
195 
196  return ((node1 == NULL && node2 == NULL) ||
197  (node1 != NULL && node1->LangModelEdge()->IsRoot() &&
198  node2 != NULL && node2->LangModelEdge()->IsRoot()));
199 }

◆ LangModCost()

int tesseract::SearchNode::LangModCost ( )
inline

Definition at line 71 of file search_node.h.

71 { return LangModCost(lang_mod_edge_, parent_node_); }

◆ LangModelEdge()

LangModEdge* tesseract::SearchNode::LangModelEdge ( )
inline

Definition at line 70 of file search_node.h.

70 { return lang_mod_edge_;}

◆ NodeString()

const char_32* tesseract::SearchNode::NodeString ( )
inline

Definition at line 51 of file search_node.h.

51 { return str_; }

◆ ParentNode()

SearchNode* tesseract::SearchNode::ParentNode ( )
inline

Definition at line 69 of file search_node.h.

69 { return parent_node_; }

◆ PathString()

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 
151  int ch_idx = len;
152 
153  node = this;
154  char_ptr[ch_idx--] = 0;
155 
156  while (node != NULL) {
157  int str_len = ((node->str_ == NULL) ? 0 : CubeUtils::StrLen(node->str_));
158  while (str_len > 0) {
159  char_ptr[ch_idx--] = node->str_[--str_len];
160  }
161 
162  // if the edge is a root and does not have a NULL parent, insert a space
163  LangModEdge *lm_edge = node->LangModelEdge();
164  if (lm_edge != NULL && lm_edge->IsRoot() && node->ParentNode() != NULL) {
165  char_ptr[ch_idx--] = (char_32)' ';
166  }
167 
168  node = node->parent_node_;
169  }
170 
171  return char_ptr;
172 }
static int StrLen(const char_32 *str)
Definition: cube_utils.cpp:54
SearchNode(CubeRecoContext *cntxt, SearchNode *parent_node, int char_reco_cost, LangModEdge *edge, int col_idx)
Definition: search_node.cpp:32
signed int char_32
Definition: string_32.h:40

◆ SearchNodeComparer()

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  }

◆ SetString()

void tesseract::SearchNode::SetString ( char_32 *  str)
inline

Definition at line 52 of file search_node.h.

52 { str_ = str; }

◆ UpdateParent()

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 }
TuningParams * Params() const
static bool IdenticalPath(SearchNode *node1, SearchNode *node2)
double RecoWgt() const
Definition: tuning_params.h:48
virtual bool IsIdentical(LangModEdge *edge) const =0

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