All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
tess_lang_mod_edge.h
Go to the documentation of this file.
1 /**********************************************************************
2  * File: tess_lang_mod_edge.h
3  * Description: Declaration of the Tesseract Language Model Edge Class
4  * Author: Ahmad Abdulkader
5  * Created: 2008
6  *
7  * (C) Copyright 2008, 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  *
18  **********************************************************************/
19 
20 // The TessLangModEdge models an edge in the Tesseract language models
21 // It inherits from the LangModEdge class
22 
23 #ifndef TESS_LANG_MOD_EDGE_H
24 #define TESS_LANG_MOD_EDGE_H
25 
26 #include "dawg.h"
27 #include "char_set.h"
28 
29 #include "lang_mod_edge.h"
30 #include "cube_reco_context.h"
31 #include "cube_utils.h"
32 
33 // Macros needed to identify punctuation in the langmodel state
34 #ifdef _HMSW32_H
35 #define LEAD_PUNC_EDGE_REF_MASK (inT64) 0x0000000100000000i64
36 #define TRAIL_PUNC_EDGE_REF_MASK (inT64) 0x0000000200000000i64
37 #define TRAIL_PUNC_REPEAT_MASK (inT64) 0xffff000000000000i64
38 #else
39 #define LEAD_PUNC_EDGE_REF_MASK (inT64) 0x0000000100000000ll
40 #define TRAIL_PUNC_EDGE_REF_MASK (inT64) 0x0000000200000000ll
41 #define TRAIL_PUNC_REPEAT_MASK (inT64) 0xffff000000000000ll
42 #endif
43 
44 // Number state machine macros
45 #define NUMBER_STATE_SHIFT 0
46 #define NUMBER_STATE_MASK 0x0000000fl
47 #define NUMBER_LITERAL_SHIFT 4
48 #define NUMBER_LITERAL_MASK 0x000000f0l
49 #define NUMBER_REPEAT_SHIFT 8
50 #define NUMBER_REPEAT_MASK 0x00000f00l
51 #define NUM_TRM -99
52 #define TRAIL_PUNC_REPEAT_SHIFT 48
53 
54 #define IsLeadingPuncEdge(edge_mask) \
55  ((edge_mask & LEAD_PUNC_EDGE_REF_MASK) != 0)
56 #define IsTrailingPuncEdge(edge_mask) \
57  ((edge_mask & TRAIL_PUNC_EDGE_REF_MASK) != 0)
58 #define TrailingPuncCount(edge_mask) \
59  ((edge_mask & TRAIL_PUNC_REPEAT_MASK) >> TRAIL_PUNC_REPEAT_SHIFT)
60 #define TrailingPuncEdgeMask(Cnt) \
61  (TRAIL_PUNC_EDGE_REF_MASK | ((Cnt) << TRAIL_PUNC_REPEAT_SHIFT))
62 
63 // State machine IDs
64 #define DAWG_OOD 0
65 #define DAWG_NUMBER 1
66 
67 namespace tesseract {
68 class TessLangModEdge : public LangModEdge {
69  public:
70  // Different ways of constructing a TessLangModEdge
71  TessLangModEdge(CubeRecoContext *cntxt, const Dawg *edge_array,
72  EDGE_REF edge, int class_id);
73  TessLangModEdge(CubeRecoContext *cntxt, const Dawg *edge_array,
74  EDGE_REF start_edge_idx, EDGE_REF end_edge_idx,
75  int class_id);
76  TessLangModEdge(CubeRecoContext *cntxt, int class_id);
78 
79  // Accessors
80  inline bool IsRoot() const {
81  return root_;
82  }
83  inline void SetRoot(bool flag) { root_ = flag; }
84 
85  inline bool IsOOD() const {
86  return (dawg_ == (Dawg *)DAWG_OOD);
87  }
88 
89  inline bool IsNumber() const {
90  return (dawg_ == (Dawg *)DAWG_NUMBER);
91  }
92 
93  inline bool IsEOW() const {
94  return (IsTerminal() || (dawg_->end_of_word(end_edge_) != 0));
95  }
96 
97  inline const Dawg *GetDawg() const { return dawg_; }
98  inline EDGE_REF StartEdge() const { return start_edge_; }
99  inline EDGE_REF EndEdge() const { return end_edge_; }
100  inline EDGE_REF EdgeMask() const { return edge_mask_; }
101  inline const char_32 * EdgeString() const { return str_; }
102  inline int ClassID () const { return class_id_; }
103  inline int PathCost() const { return path_cost_; }
104  inline void SetEdgeMask(EDGE_REF edge_mask) { edge_mask_ = edge_mask; }
105  inline void SetDawg(Dawg *dawg) { dawg_ = dawg; }
106  inline void SetStartEdge(EDGE_REF edge_idx) { start_edge_ = edge_idx; }
107  inline void SetEndEdge(EDGE_REF edge_idx) { end_edge_ = edge_idx; }
108 
109  // is this a terminal node:
110  // we can terminate at any OOD char, trailing punc or
111  // when the dawg terminates
112  inline bool IsTerminal() const {
113  return (IsOOD() || IsNumber() || IsTrailingPuncEdge(start_edge_) ||
114  dawg_->next_node(end_edge_) == 0);
115  }
116 
117  // How many signals does the LM provide for tuning. These are flags like:
118  // OOD or not, Number of not that are used by the training to compute
119  // extra costs for each word.
120  inline int SignalCnt() const {
121  return 2;
122  }
123 
124  // returns the weight assigned to a specified signal
125  inline double SignalWgt(int signal) const {
126  CubeTuningParams *params =
127  reinterpret_cast<CubeTuningParams *>(cntxt_->Params());
128  if (params != NULL) {
129  switch (signal) {
130  case 0:
131  return params->OODWgt();
132  break;
133 
134  case 1:
135  return params->NumWgt();
136  break;
137  }
138  }
139 
140  return 0.0;
141  }
142 
143  // sets the weight assigned to a specified signal: Used in training
144  void SetSignalWgt(int signal, double wgt) {
145  CubeTuningParams *params =
146  reinterpret_cast<CubeTuningParams *>(cntxt_->Params());
147  if (params != NULL) {
148  switch (signal) {
149  case 0:
150  params->SetOODWgt(wgt);
151  break;
152 
153  case 1:
154  params->SetNumWgt(wgt);
155  break;
156  }
157  }
158  }
159 
160  // returns the actual value of a specified signal
161  int Signal(int signal) {
162  switch (signal) {
163  case 0:
164  return IsOOD() ? MIN_PROB_COST : 0;
165  break;
166 
167  case 1:
168  return IsNumber() ? MIN_PROB_COST : 0;
169  break;
170 
171  default:
172  return 0;
173  }
174  }
175 
176  // returns the Hash value of the edge. Used by the SearchNode hash table
177  // to quickly lookup exisiting edges to converge during search
178  inline unsigned int Hash() const {
179  return static_cast<unsigned int>(
180  ((start_edge_ | end_edge_) ^ ((reinterpret_cast<uintptr_t>(dawg_)))) ^
181  ((unsigned int)edge_mask_) ^ class_id_);
182  }
183 
184  // A verbal description of the edge: Used by visualizers
185  char *Description() const;
186 
187  // Is this edge identical to the specified edge
188  inline bool IsIdentical(LangModEdge *lang_mod_edge) const {
189  return (class_id_ ==
190  reinterpret_cast<TessLangModEdge *>(lang_mod_edge)->class_id_ &&
191  str_ == reinterpret_cast<TessLangModEdge *>(lang_mod_edge)->str_ &&
192  dawg_ == reinterpret_cast<TessLangModEdge *>(lang_mod_edge)->dawg_ &&
193  start_edge_ ==
194  reinterpret_cast<TessLangModEdge *>(lang_mod_edge)->start_edge_ &&
195  end_edge_ ==
196  reinterpret_cast<TessLangModEdge *>(lang_mod_edge)->end_edge_ &&
197  edge_mask_ ==
198  reinterpret_cast<TessLangModEdge *>(lang_mod_edge)->edge_mask_);
199  }
200 
201  // Creates a set of fan-out edges for the specified edge
202  static int CreateChildren(CubeRecoContext *cntxt,
203  const Dawg *edges,
204  NODE_REF edge_reg,
205  LangModEdge **lm_edges);
206 
207  private:
208  bool root_;
209  CubeRecoContext *cntxt_;
210  const Dawg *dawg_;
211  EDGE_REF start_edge_;
212  EDGE_REF end_edge_;
213  EDGE_REF edge_mask_;
214  int path_cost_;
215  int class_id_;
216  const char_32 * str_;
217  // returns the cost of the lang_mod_edge
218  inline int Cost() const {
219  if (cntxt_ != NULL) {
220  CubeTuningParams *params =
221  reinterpret_cast<CubeTuningParams *>(cntxt_->Params());
222  if (dawg_ == (Dawg *)DAWG_OOD) {
223  return static_cast<int>(params->OODWgt() * MIN_PROB_COST);
224  } else if (dawg_ == (Dawg *)DAWG_NUMBER) {
225  return static_cast<int>(params->NumWgt() * MIN_PROB_COST);
226  }
227  }
228  return 0;
229  }
230 };
231 } // namespace tesseract
232 
233 #endif // TESS_LANG_MOD_EDGE_H
unsigned int Hash() const
virtual bool end_of_word(EDGE_REF edge_ref) const =0
#define IsTrailingPuncEdge(edge_mask)
const char_32 * EdgeString() const
TessLangModEdge(CubeRecoContext *cntxt, const Dawg *edge_array, EDGE_REF edge, int class_id)
#define DAWG_NUMBER
void SetEndEdge(EDGE_REF edge_idx)
bool IsIdentical(LangModEdge *lang_mod_edge) const
static int CreateChildren(CubeRecoContext *cntxt, const Dawg *edges, NODE_REF edge_reg, LangModEdge **lm_edges)
void SetStartEdge(EDGE_REF edge_idx)
void SetEdgeMask(EDGE_REF edge_mask)
#define MIN_PROB_COST
Definition: cube_const.h:26
TuningParams * Params() const
const Dawg * GetDawg() const
inT64 EDGE_REF
Definition: dawg.h:54
signed int char_32
Definition: string_32.h:40
double SignalWgt(int signal) const
#define NULL
Definition: host.h:144
inT64 NODE_REF
Definition: dawg.h:55
#define DAWG_OOD
virtual NODE_REF next_node(EDGE_REF edge_ref) const =0
void SetSignalWgt(int signal, double wgt)