Tesseract  3.02
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
tesseract::Trie Class Reference

#include <trie.h>

Inheritance diagram for tesseract::Trie:
tesseract::Dawg

Public Types

enum  RTLReversePolicy { RRP_DO_NO_REVERSE, RRP_REVERSE_IF_HAS_RTL, RRP_FORCE_REVERSE }
 

Public Member Functions

 Trie (DawgType type, const STRING &lang, PermuterType perm, uinT64 max_num_edges, int unicharset_size, int debug_level)
 
virtual ~Trie ()
 
void clear ()
 
EDGE_REF edge_char_of (NODE_REF node_ref, UNICHAR_ID unichar_id, bool word_end) const
 
void unichar_ids_of (NODE_REF node, NodeChildVector *vec) const
 
NODE_REF next_node (EDGE_REF edge_ref) const
 
bool end_of_word (EDGE_REF edge_ref) const
 
UNICHAR_ID edge_letter (EDGE_REF edge_ref) const
 
void print_node (NODE_REF node, int max_num_edges) const
 
SquishedDawgtrie_to_dawg ()
 
bool read_word_list (const char *filename, const UNICHARSET &unicharset, Trie::RTLReversePolicy reverse)
 
bool read_pattern_list (const char *filename, const UNICHARSET &unicharset)
 
void initialize_patterns (UNICHARSET *unicharset)
 
void unichar_id_to_patterns (UNICHAR_ID unichar_id, const UNICHARSET &unicharset, GenericVector< UNICHAR_ID > *vec) const
 
virtual EDGE_REF pattern_loop_edge (EDGE_REF edge_ref, UNICHAR_ID unichar_id, bool word_end) const
 
bool add_word_to_dawg (const WERD_CHOICE &word, const GenericVector< bool > *repetitions)
 
bool add_word_to_dawg (const WERD_CHOICE &word)
 
- Public Member Functions inherited from tesseract::Dawg
DawgType type () const
 
const STRINGlang () const
 
PermuterType permuter () const
 
virtual ~Dawg ()
 
bool word_in_dawg (const WERD_CHOICE &word) const
 Returns true if the given word is in the Dawg. More...
 
int check_for_words (const char *filename, const UNICHARSET &unicharset, bool enable_wildcard) const
 
void iterate_words (const UNICHARSET &unicharset, TessCallback1< const char * > *cb) const
 

Static Public Member Functions

static const char * get_reverse_policy_name (RTLReversePolicy reverse_policy)
 

Static Public Attributes

static const int kSaneNumConcreteChars = 4
 
static const char kAlphaPatternUnicode [] = "\u2000"
 
static const char kDigitPatternUnicode [] = "\u2001"
 
static const char kAlphanumPatternUnicode [] = "\u2002"
 
static const char kPuncPatternUnicode [] = "\u2003"
 
static const char kLowerPatternUnicode [] = "\u2004"
 
static const char kUpperPatternUnicode [] = "\u2005"
 
- Static Public Attributes inherited from tesseract::Dawg
static const inT16 kDawgMagicNumber = 42
 Magic number to determine endianness when reading the Dawg from file. More...
 
static const UNICHAR_ID kPatternUnicharID = 0
 

Protected Member Functions

EDGE_RECORDderef_edge_ref (EDGE_REF edge_ref) const
 
EDGE_REF make_edge_ref (NODE_REF node_index, EDGE_INDEX edge_index) const
 
void link_edge (EDGE_RECORD *edge, NODE_REF nxt, bool repeats, int direction, bool word_end, UNICHAR_ID unichar_id)
 
void print_edge_rec (const EDGE_RECORD &edge_rec) const
 
bool can_be_eliminated (const EDGE_RECORD &edge_rec)
 
void print_all (const char *msg, int max_num_edges)
 
bool edge_char_of (NODE_REF node_ref, NODE_REF next_node, int direction, bool word_end, UNICHAR_ID unichar_id, EDGE_RECORD **edge_ptr, EDGE_INDEX *edge_index) const
 
bool add_edge_linkage (NODE_REF node1, NODE_REF node2, bool repeats, int direction, bool word_end, UNICHAR_ID unichar_id)
 
bool add_new_edge (NODE_REF node1, NODE_REF node2, bool repeats, bool word_end, UNICHAR_ID unichar_id)
 
void add_word_ending (EDGE_RECORD *edge, NODE_REF the_next_node, bool repeats, UNICHAR_ID unichar_id)
 
NODE_REF new_dawg_node ()
 
void remove_edge_linkage (NODE_REF node1, NODE_REF node2, int direction, bool word_end, UNICHAR_ID unichar_id)
 
void remove_edge (NODE_REF node1, NODE_REF node2, bool word_end, UNICHAR_ID unichar_id)
 
bool eliminate_redundant_edges (NODE_REF node, const EDGE_RECORD &edge1, const EDGE_RECORD &edge2)
 
bool reduce_lettered_edges (EDGE_INDEX edge_index, UNICHAR_ID unichar_id, NODE_REF node, const EDGE_VECTOR &backward_edges, NODE_MARKER reduced_nodes)
 
void sort_edges (EDGE_VECTOR *edges)
 
void reduce_node_input (NODE_REF node, NODE_MARKER reduced_nodes)
 
UNICHAR_ID character_class_to_pattern (char ch)
 
- Protected Member Functions inherited from tesseract::Dawg
 Dawg ()
 
NODE_REF next_node_from_edge_rec (const EDGE_RECORD &edge_rec) const
 Returns the next node visited by following this edge. More...
 
bool marker_flag_from_edge_rec (const EDGE_RECORD &edge_rec) const
 Returns the marker flag of this edge. More...
 
int direction_from_edge_rec (const EDGE_RECORD &edge_rec) const
 Returns the direction flag of this edge. More...
 
bool end_of_word_from_edge_rec (const EDGE_RECORD &edge_rec) const
 Returns true if this edge marks the end of a word. More...
 
UNICHAR_ID unichar_id_from_edge_rec (const EDGE_RECORD &edge_rec) const
 Returns UNICHAR_ID recorded in this edge. More...
 
void set_next_node_in_edge_rec (EDGE_RECORD *edge_rec, EDGE_REF value)
 Sets the next node link for this edge in the Dawg. More...
 
void set_marker_flag_in_edge_rec (EDGE_RECORD *edge_rec)
 Sets this edge record to be the last one in a sequence of edges. More...
 
int given_greater_than_edge_rec (NODE_REF next_node, bool word_end, UNICHAR_ID unichar_id, const EDGE_RECORD &edge_rec) const
 
bool edge_rec_match (NODE_REF next_node, bool word_end, UNICHAR_ID unichar_id, NODE_REF other_next_node, bool other_word_end, UNICHAR_ID other_unichar_id) const
 
void init (DawgType type, const STRING &lang, PermuterType perm, int unicharset_size, int debug_level)
 
bool match_words (WERD_CHOICE *word, inT32 index, NODE_REF node, UNICHAR_ID wildcard) const
 
void iterate_words_rec (const WERD_CHOICE &word_so_far, NODE_REF to_explore, TessCallback1< const char * > *cb) const
 

Protected Attributes

TRIE_NODES nodes_
 
uinT64 num_edges_
 
uinT64 max_num_edges_
 
uinT64 deref_direction_mask_
 
uinT64 deref_node_index_mask_
 
bool initialized_patterns_
 
UNICHAR_ID alpha_pattern_
 
UNICHAR_ID digit_pattern_
 
UNICHAR_ID alphanum_pattern_
 
UNICHAR_ID punc_pattern_
 
UNICHAR_ID lower_pattern_
 
UNICHAR_ID upper_pattern_
 
- Protected Attributes inherited from tesseract::Dawg
DawgType type_
 
STRING lang_
 
PermuterType perm_
 Permuter code that should be used if the word is found in this Dawg. More...
 
int unicharset_size_
 
int flag_start_bit_
 
int next_node_start_bit_
 
uinT64 next_node_mask_
 
uinT64 flags_mask_
 
uinT64 letter_mask_
 
int debug_level_
 

Detailed Description

Concrete class for Trie data structure that allows to store a list of words (extends Dawg base class) as well as dynamically add new words. This class stores a vector of pointers to TRIE_NODE_RECORDs, each of which has a vector of forward and backward edges.

Definition at line 62 of file trie.h.

Member Enumeration Documentation

Enumerator
RRP_DO_NO_REVERSE 
RRP_REVERSE_IF_HAS_RTL 
RRP_FORCE_REVERSE 

Definition at line 64 of file trie.h.

Constructor & Destructor Documentation

tesseract::Trie::Trie ( DawgType  type,
const STRING lang,
PermuterType  perm,
uinT64  max_num_edges,
int  unicharset_size,
int  debug_level 
)
inline

Definition at line 89 of file trie.h.

90  {
91  init(type, lang, perm, unicharset_size, debug_level);
92  num_edges_ = 0;
93  max_num_edges_ = max_num_edges;
95  new_dawg_node(); // need to allocate node 0
96  initialized_patterns_ = false;
97  }
uinT64 letter_mask_
Definition: dawg.h:296
uinT64 max_num_edges_
Definition: trie.h:397
DawgType type() const
Definition: dawg.h:131
bool initialized_patterns_
Definition: trie.h:402
uinT64 num_edges_
Definition: trie.h:396
NODE_REF new_dawg_node()
Definition: trie.cpp:261
void init(DawgType type, const STRING &lang, PermuterType perm, int unicharset_size, int debug_level)
Definition: dawg.cpp:159
uinT64 deref_node_index_mask_
Definition: trie.h:399
virtual tesseract::Trie::~Trie ( )
inlinevirtual

Definition at line 98 of file trie.h.

void delete_data_pointers()
TRIE_NODES nodes_
Definition: trie.h:395

Member Function Documentation

bool tesseract::Trie::add_edge_linkage ( NODE_REF  node1,
NODE_REF  node2,
bool  repeats,
int  direction,
bool  word_end,
UNICHAR_ID  unichar_id 
)
protected

Definition at line 123 of file trie.cpp.

125  {
126  if (num_edges_ == max_num_edges_) return false;
127  EDGE_VECTOR *vec = (direction == FORWARD_EDGE) ?
128  &(nodes_[node1]->forward_edges) : &(nodes_[node1]->backward_edges);
129  int search_index;
130  if (node1 == 0) {
131  search_index = 0; // find the index to make the add sorted
132  while (search_index < vec->size() &&
133  given_greater_than_edge_rec(node2, word_end, unichar_id,
134  (*vec)[search_index]) == 1) {
135  search_index++;
136  }
137  } else {
138  search_index = vec->size(); // add is unsorted, so index does not matter
139  }
140  EDGE_RECORD edge_rec;
141  link_edge(&edge_rec, node2, marker_flag, direction, word_end, unichar_id);
142  if (search_index < vec->size()) {
143  vec->insert(edge_rec, search_index);
144  } else {
145  vec->push_back(edge_rec);
146  }
147  if (debug_level_ > 1) {
148  tprintf("new edge in nodes_[" REFFORMAT "]: ", node1);
149  print_edge_rec(edge_rec);
150  tprintf("\n");
151  }
152  num_edges_++;
153  return true;
154 }
void print_edge_rec(const EDGE_RECORD &edge_rec) const
Definition: trie.h:293
#define REFFORMAT
Definition: dawg.h:92
uinT64 max_num_edges_
Definition: trie.h:397
int given_greater_than_edge_rec(NODE_REF next_node, bool word_end, UNICHAR_ID unichar_id, const EDGE_RECORD &edge_rec) const
Definition: dawg.h:232
int push_back(T object)
uinT64 num_edges_
Definition: trie.h:396
void insert(T t, int index)
TRIE_NODES nodes_
Definition: trie.h:395
int debug_level_
Definition: dawg.h:298
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:41
void link_edge(EDGE_RECORD *edge, NODE_REF nxt, bool repeats, int direction, bool word_end, UNICHAR_ID unichar_id)
Definition: trie.h:282
int size() const
Definition: genericvector.h:59
#define FORWARD_EDGE
Definition: dawg.h:84
uinT64 EDGE_RECORD
Definition: dawg.h:50
int direction(EDGEPT *point)
Definition: vecfuncs.cpp:43
bool tesseract::Trie::add_new_edge ( NODE_REF  node1,
NODE_REF  node2,
bool  repeats,
bool  word_end,
UNICHAR_ID  unichar_id 
)
inlineprotected

Definition at line 332 of file trie.h.

333  {
334  return (add_edge_linkage(node1, node2, repeats, FORWARD_EDGE,
335  word_end, unichar_id) &&
336  add_edge_linkage(node2, node1, repeats, BACKWARD_EDGE,
337  word_end, unichar_id));
338  }
bool add_edge_linkage(NODE_REF node1, NODE_REF node2, bool repeats, int direction, bool word_end, UNICHAR_ID unichar_id)
Definition: trie.cpp:123
#define BACKWARD_EDGE
Definition: dawg.h:85
#define FORWARD_EDGE
Definition: dawg.h:84
void tesseract::Trie::add_word_ending ( EDGE_RECORD edge,
NODE_REF  the_next_node,
bool  repeats,
UNICHAR_ID  unichar_id 
)
protected

Definition at line 156 of file trie.cpp.

159  {
160  EDGE_RECORD *back_edge_ptr;
161  EDGE_INDEX back_edge_index;
162  ASSERT_HOST(edge_char_of(the_next_node, NO_EDGE, BACKWARD_EDGE, false,
163  unichar_id, &back_edge_ptr, &back_edge_index));
164  if (marker_flag) {
165  *back_edge_ptr |= (MARKER_FLAG << flag_start_bit_);
166  *edge_ptr |= (MARKER_FLAG << flag_start_bit_);
167  }
168  // Mark both directions as end of word.
169  *back_edge_ptr |= (WERD_END_FLAG << flag_start_bit_);
170  *edge_ptr |= (WERD_END_FLAG << flag_start_bit_);
171 }
#define WERD_END_FLAG
Definition: dawg.h:89
EDGE_REF edge_char_of(NODE_REF node_ref, UNICHAR_ID unichar_id, bool word_end) const
Definition: trie.h:104
#define BACKWARD_EDGE
Definition: dawg.h:85
int flag_start_bit_
Definition: dawg.h:292
inT64 EDGE_INDEX
Definition: trie.h:32
#define MARKER_FLAG
Definition: dawg.h:87
#define ASSERT_HOST(x)
Definition: errcode.h:84
uinT64 EDGE_RECORD
Definition: dawg.h:50
bool tesseract::Trie::add_word_to_dawg ( const WERD_CHOICE word,
const GenericVector< bool > *  repetitions 
)

Definition at line 173 of file trie.cpp.

174  {
175  if (word.length() <= 0) return false; // can't add empty words
176  if (repetitions != NULL) ASSERT_HOST(repetitions->size() == word.length());
177  // Make sure the word does not contain invalid unchar ids.
178  for (int i = 0; i < word.length(); ++i) {
179  if (word.unichar_id(i) < 0 ||
180  word.unichar_id(i) >= unicharset_size_) return false;
181  }
182 
183  EDGE_RECORD *edge_ptr;
184  NODE_REF last_node = 0;
185  NODE_REF the_next_node;
186  bool marker_flag = false;
187  EDGE_INDEX edge_index;
188  int i;
189  inT32 still_finding_chars = true;
190  inT32 word_end = false;
191  bool add_failed = false;
192  bool found;
193 
194  if (debug_level_ > 1) word.print("\nAdding word: ");
195 
196  UNICHAR_ID unichar_id;
197  for (i = 0; i < word.length() - 1; ++i) {
198  unichar_id = word.unichar_id(i);
199  marker_flag = (repetitions != NULL) ? (*repetitions)[i] : false;
200  if (debug_level_ > 1) tprintf("Adding letter %d\n", unichar_id);
201  if (still_finding_chars) {
202  found = edge_char_of(last_node, NO_EDGE, FORWARD_EDGE, word_end,
203  unichar_id, &edge_ptr, &edge_index);
204  if (found && debug_level_ > 1) {
205  tprintf("exploring edge " REFFORMAT " in node " REFFORMAT "\n",
206  edge_index, last_node);
207  }
208  if (!found) {
209  still_finding_chars = false;
210  } else if (next_node_from_edge_rec(*edge_ptr) == 0) {
211  word_end = true;
212  still_finding_chars = false;
213  remove_edge(last_node, 0, word_end, unichar_id);
214  } else {
215  if (marker_flag) set_marker_flag_in_edge_rec(edge_ptr);
216  last_node = next_node_from_edge_rec(*edge_ptr);
217  }
218  }
219  if (!still_finding_chars) {
220  the_next_node = new_dawg_node();
221  if (debug_level_ > 1)
222  tprintf("adding node " REFFORMAT "\n", the_next_node);
223  if (the_next_node == 0) {
224  add_failed = true;
225  break;
226  }
227  if (!add_new_edge(last_node, the_next_node,
228  marker_flag, word_end, unichar_id)) {
229  add_failed = true;
230  break;
231  }
232  word_end = false;
233  last_node = the_next_node;
234  }
235  }
236  the_next_node = 0;
237  unichar_id = word.unichar_id(i);
238  marker_flag = (repetitions != NULL) ? (*repetitions)[i] : false;
239  if (debug_level_ > 1) tprintf("Adding letter %d\n", unichar_id);
240  if (still_finding_chars &&
241  edge_char_of(last_node, NO_EDGE, FORWARD_EDGE, false,
242  unichar_id, &edge_ptr, &edge_index)) {
243  // An extension of this word already exists in the trie, so we
244  // only have to add the ending flags in both directions.
245  add_word_ending(edge_ptr, next_node_from_edge_rec(*edge_ptr),
246  marker_flag, unichar_id);
247  } else {
248  if (!add_failed &&
249  !add_new_edge(last_node, the_next_node, marker_flag, true, unichar_id))
250  add_failed = true;
251  }
252  if (add_failed) {
253  tprintf("Re-initializing document dictionary...\n");
254  clear();
255  return false;
256  } else {
257  return true;
258  }
259 }
int length() const
Definition: ratngs.h:214
#define REFFORMAT
Definition: dawg.h:92
int UNICHAR_ID
Definition: unichar.h:31
void add_word_ending(EDGE_RECORD *edge, NODE_REF the_next_node, bool repeats, UNICHAR_ID unichar_id)
Definition: trie.cpp:156
void clear()
Definition: trie.cpp:65
bool add_new_edge(NODE_REF node1, NODE_REF node2, bool repeats, bool word_end, UNICHAR_ID unichar_id)
Definition: trie.h:332
#define NULL
Definition: host.h:144
NODE_REF next_node_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns the next node visited by following this edge.
Definition: dawg.h:194
int inT32
Definition: host.h:102
EDGE_REF edge_char_of(NODE_REF node_ref, UNICHAR_ID unichar_id, bool word_end) const
Definition: trie.h:104
NODE_REF new_dawg_node()
Definition: trie.cpp:261
inT64 EDGE_INDEX
Definition: trie.h:32
const void print() const
Definition: ratngs.h:406
int debug_level_
Definition: dawg.h:298
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:41
inT64 NODE_REF
Definition: dawg.h:55
int size() const
Definition: genericvector.h:59
#define FORWARD_EDGE
Definition: dawg.h:84
#define ASSERT_HOST(x)
Definition: errcode.h:84
int unicharset_size_
Definition: dawg.h:291
uinT64 EDGE_RECORD
Definition: dawg.h:50
const UNICHAR_ID unichar_id(int index) const
Definition: ratngs.h:220
void remove_edge(NODE_REF node1, NODE_REF node2, bool word_end, UNICHAR_ID unichar_id)
Definition: trie.h:357
void set_marker_flag_in_edge_rec(EDGE_RECORD *edge_rec)
Sets this edge record to be the last one in a sequence of edges.
Definition: dawg.h:222
bool tesseract::Trie::add_word_to_dawg ( const WERD_CHOICE word)
inline

Definition at line 245 of file trie.h.

245  {
246  return add_word_to_dawg(word, NULL);
247  }
bool add_word_to_dawg(const WERD_CHOICE &word, const GenericVector< bool > *repetitions)
Definition: trie.cpp:173
#define NULL
Definition: host.h:144
bool tesseract::Trie::can_be_eliminated ( const EDGE_RECORD edge_rec)
inlineprotected

Definition at line 302 of file trie.h.

302  {
303  NODE_REF node_ref = next_node_from_edge_rec(edge_rec);
304  return (node_ref != NO_EDGE &&
305  nodes_[static_cast<int>(node_ref)]->forward_edges.size() == 1);
306  }
NODE_REF next_node_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns the next node visited by following this edge.
Definition: dawg.h:194
TRIE_NODES nodes_
Definition: trie.h:395
inT64 NODE_REF
Definition: dawg.h:55
int size() const
Definition: genericvector.h:59
UNICHAR_ID tesseract::Trie::character_class_to_pattern ( char  ch)
protected

Definition at line 346 of file trie.cpp.

346  {
347  if (ch == 'c') {
348  return alpha_pattern_;
349  } else if (ch == 'd') {
350  return digit_pattern_;
351  } else if (ch == 'n') {
352  return alphanum_pattern_;
353  } else if (ch == 'p') {
354  return punc_pattern_;
355  } else if (ch == 'a') {
356  return lower_pattern_;
357  } else if (ch == 'A') {
358  return upper_pattern_;
359  } else {
360  return INVALID_UNICHAR_ID;
361  }
362 }
UNICHAR_ID punc_pattern_
Definition: trie.h:406
UNICHAR_ID lower_pattern_
Definition: trie.h:407
UNICHAR_ID alpha_pattern_
Definition: trie.h:403
UNICHAR_ID alphanum_pattern_
Definition: trie.h:405
UNICHAR_ID digit_pattern_
Definition: trie.h:404
UNICHAR_ID upper_pattern_
Definition: trie.h:408
void tesseract::Trie::clear ( )

Definition at line 65 of file trie.cpp.

65  {
67  nodes_.clear();
68  num_edges_ = 0;
69  new_dawg_node(); // Need to allocate node 0.
70 }
void delete_data_pointers()
virtual void clear()
uinT64 num_edges_
Definition: trie.h:396
TRIE_NODES nodes_
Definition: trie.h:395
NODE_REF new_dawg_node()
Definition: trie.cpp:261
EDGE_RECORD* tesseract::Trie::deref_edge_ref ( EDGE_REF  edge_ref) const
inlineprotected

Definition at line 267 of file trie.h.

267  {
268  int edge_index = static_cast<int>(
269  (edge_ref & letter_mask_) >> LETTER_START_BIT);
270  int node_index = static_cast<int>(
271  (edge_ref & deref_node_index_mask_) >> flag_start_bit_);
272  TRIE_NODE_RECORD *node_rec = nodes_[node_index];
273  return &(node_rec->forward_edges[edge_index]);
274  }
uinT64 letter_mask_
Definition: dawg.h:296
#define LETTER_START_BIT
Definition: dawg.h:90
TRIE_NODES nodes_
Definition: trie.h:395
int flag_start_bit_
Definition: dawg.h:292
EDGE_VECTOR forward_edges
Definition: trie.h:49
uinT64 deref_node_index_mask_
Definition: trie.h:399
EDGE_REF tesseract::Trie::edge_char_of ( NODE_REF  node_ref,
UNICHAR_ID  unichar_id,
bool  word_end 
) const
inlinevirtual

Returns the edge that corresponds to the letter out of this node.

Implements tesseract::Dawg.

Definition at line 104 of file trie.h.

105  {
106  EDGE_RECORD *edge_ptr;
107  EDGE_INDEX edge_index;
108  if (!edge_char_of(node_ref, NO_EDGE, FORWARD_EDGE, word_end, unichar_id,
109  &edge_ptr, &edge_index)) return NO_EDGE;
110  return make_edge_ref(node_ref, edge_index);
111  }
EDGE_REF edge_char_of(NODE_REF node_ref, UNICHAR_ID unichar_id, bool word_end) const
Definition: trie.h:104
EDGE_REF make_edge_ref(NODE_REF node_index, EDGE_INDEX edge_index) const
Definition: trie.h:276
inT64 EDGE_INDEX
Definition: trie.h:32
#define FORWARD_EDGE
Definition: dawg.h:84
uinT64 EDGE_RECORD
Definition: dawg.h:50
bool tesseract::Trie::edge_char_of ( NODE_REF  node_ref,
NODE_REF  next_node,
int  direction,
bool  word_end,
UNICHAR_ID  unichar_id,
EDGE_RECORD **  edge_ptr,
EDGE_INDEX edge_index 
) const
protected

Definition at line 72 of file trie.cpp.

74  {
75  if (debug_level_ == 3) {
76  tprintf("edge_char_of() given node_ref " REFFORMAT " next_node " REFFORMAT
77  " direction %d word_end %d unichar_id %d, exploring node:\n",
78  node_ref, next_node, direction, word_end, unichar_id);
79  if (node_ref != NO_EDGE) {
80  print_node(node_ref, nodes_[node_ref]->forward_edges.size());
81  }
82  }
83  if (node_ref == NO_EDGE) return false;
84  assert(node_ref < nodes_.size());
85  EDGE_VECTOR &vec = (direction == FORWARD_EDGE) ?
86  nodes_[node_ref]->forward_edges : nodes_[node_ref]->backward_edges;
87  int vec_size = vec.size();
88  if (node_ref == 0) { // binary search
89  EDGE_INDEX start = 0;
90  EDGE_INDEX end = vec_size - 1;
91  EDGE_INDEX k;
92  int compare;
93  while (start <= end) {
94  k = (start + end) >> 1; // (start + end) / 2
95  compare = given_greater_than_edge_rec(next_node, word_end,
96  unichar_id, vec[k]);
97  if (compare == 0) { // given == vec[k]
98  *edge_ptr = &(vec[k]);
99  *edge_index = k;
100  return true;
101  } else if (compare == 1) { // given > vec[k]
102  start = k + 1;
103  } else { // given < vec[k]
104  end = k - 1;
105  }
106  }
107  } else { // linear search
108  for (int i = 0; i < vec_size; ++i) {
109  EDGE_RECORD &edge_rec = vec[i];
110  if (edge_rec_match(next_node, word_end, unichar_id,
111  next_node_from_edge_rec(edge_rec),
112  end_of_word_from_edge_rec(edge_rec),
113  unichar_id_from_edge_rec(edge_rec))) {
114  *edge_ptr = &(edge_rec);
115  *edge_index = i;
116  return true;
117  }
118  }
119  }
120  return false; // not found
121 }
#define REFFORMAT
Definition: dawg.h:92
int given_greater_than_edge_rec(NODE_REF next_node, bool word_end, UNICHAR_ID unichar_id, const EDGE_RECORD &edge_rec) const
Definition: dawg.h:232
NODE_REF next_node(EDGE_REF edge_ref) const
Definition: trie.h:130
NODE_REF next_node_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns the next node visited by following this edge.
Definition: dawg.h:194
bool edge_rec_match(NODE_REF next_node, bool word_end, UNICHAR_ID unichar_id, NODE_REF other_next_node, bool other_word_end, UNICHAR_ID other_unichar_id) const
Definition: dawg.h:253
TRIE_NODES nodes_
Definition: trie.h:395
inT64 EDGE_INDEX
Definition: trie.h:32
int debug_level_
Definition: dawg.h:298
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:41
int size() const
Definition: genericvector.h:59
#define FORWARD_EDGE
Definition: dawg.h:84
UNICHAR_ID unichar_id_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns UNICHAR_ID recorded in this edge.
Definition: dawg.h:211
uinT64 EDGE_RECORD
Definition: dawg.h:50
int direction(EDGEPT *point)
Definition: vecfuncs.cpp:43
void print_node(NODE_REF node, int max_num_edges) const
Definition: trie.cpp:648
bool end_of_word_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns true if this edge marks the end of a word.
Definition: dawg.h:207
UNICHAR_ID tesseract::Trie::edge_letter ( EDGE_REF  edge_ref) const
inlinevirtual

Returns UNICHAR_ID stored in the edge indicated by the given EDGE_REF.

Implements tesseract::Dawg.

Definition at line 145 of file trie.h.

145  {
146  if (edge_ref == NO_EDGE || num_edges_ == 0) return INVALID_UNICHAR_ID;
147  return unichar_id_from_edge_rec(*deref_edge_ref(edge_ref));
148  }
uinT64 num_edges_
Definition: trie.h:396
EDGE_RECORD * deref_edge_ref(EDGE_REF edge_ref) const
Definition: trie.h:267
UNICHAR_ID unichar_id_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns UNICHAR_ID recorded in this edge.
Definition: dawg.h:211
bool tesseract::Trie::eliminate_redundant_edges ( NODE_REF  node,
const EDGE_RECORD edge1,
const EDGE_RECORD edge2 
)
protected

Definition at line 512 of file trie.cpp.

514  {
515  if (debug_level_ > 1) {
516  tprintf("\nCollapsing node %d:\n", node);
518  tprintf("Candidate edges: ");
519  print_edge_rec(edge1);
520  tprintf(", ");
521  print_edge_rec(edge2);
522  tprintf("\n\n");
523  }
524  NODE_REF next_node1 = next_node_from_edge_rec(edge1);
525  NODE_REF next_node2 = next_node_from_edge_rec(edge2);
526  TRIE_NODE_RECORD *next_node2_ptr = nodes_[next_node2];
527  // Translate all edges going to/from next_node2 to go to/from next_node1.
528  EDGE_RECORD *edge_ptr = NULL;
529  EDGE_INDEX edge_index;
530  int i;
531  // Remove the backward link in node to next_node2.
532  const EDGE_RECORD &fwd_edge = next_node2_ptr->forward_edges[0];
533  remove_edge_linkage(node, next_node2, BACKWARD_EDGE,
534  end_of_word_from_edge_rec(fwd_edge),
535  unichar_id_from_edge_rec(fwd_edge));
536  // Copy all the backward links in next_node2 to node next_node1
537  for (i = 0; i < next_node2_ptr->backward_edges.size(); ++i) {
538  const EDGE_RECORD &bkw_edge = next_node2_ptr->backward_edges[i];
539  NODE_REF curr_next_node = next_node_from_edge_rec(bkw_edge);
540  UNICHAR_ID curr_unichar_id = unichar_id_from_edge_rec(bkw_edge);
541  int curr_word_end = end_of_word_from_edge_rec(bkw_edge);
542  bool marker_flag = marker_flag_from_edge_rec(bkw_edge);
543  add_edge_linkage(next_node1, curr_next_node, marker_flag, BACKWARD_EDGE,
544  curr_word_end, curr_unichar_id);
545  // Relocate the corresponding forward edge in curr_next_node
546  ASSERT_HOST(edge_char_of(curr_next_node, next_node2, FORWARD_EDGE,
547  curr_word_end, curr_unichar_id,
548  &edge_ptr, &edge_index));
549  set_next_node_in_edge_rec(edge_ptr, next_node1);
550  }
551  int next_node2_num_edges = (next_node2_ptr->forward_edges.size() +
552  next_node2_ptr->backward_edges.size());
553  if (debug_level_ > 1) {
554  tprintf("removed %d edges from node " REFFORMAT "\n",
555  next_node2_num_edges, next_node2);
556  }
557  next_node2_ptr->forward_edges.clear();
558  next_node2_ptr->backward_edges.clear();
559  num_edges_ -= next_node2_num_edges;
560  return true;
561 }
void print_edge_rec(const EDGE_RECORD &edge_rec) const
Definition: trie.h:293
#define REFFORMAT
Definition: dawg.h:92
EDGE_VECTOR backward_edges
Definition: trie.h:50
int UNICHAR_ID
Definition: unichar.h:31
virtual void clear()
#define NULL
Definition: host.h:144
bool add_edge_linkage(NODE_REF node1, NODE_REF node2, bool repeats, int direction, bool word_end, UNICHAR_ID unichar_id)
Definition: trie.cpp:123
NODE_REF next_node_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns the next node visited by following this edge.
Definition: dawg.h:194
EDGE_REF edge_char_of(NODE_REF node_ref, UNICHAR_ID unichar_id, bool word_end) const
Definition: trie.h:104
uinT64 num_edges_
Definition: trie.h:396
TRIE_NODES nodes_
Definition: trie.h:395
#define BACKWARD_EDGE
Definition: dawg.h:85
inT64 EDGE_INDEX
Definition: trie.h:32
bool marker_flag_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns the marker flag of this edge.
Definition: dawg.h:198
int debug_level_
Definition: dawg.h:298
void set_next_node_in_edge_rec(EDGE_RECORD *edge_rec, EDGE_REF value)
Sets the next node link for this edge in the Dawg.
Definition: dawg.h:216
#define MAX_NODE_EDGES_DISPLAY
Definition: dawg.h:86
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:41
inT64 NODE_REF
Definition: dawg.h:55
int size() const
Definition: genericvector.h:59
#define FORWARD_EDGE
Definition: dawg.h:84
EDGE_VECTOR forward_edges
Definition: trie.h:49
UNICHAR_ID unichar_id_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns UNICHAR_ID recorded in this edge.
Definition: dawg.h:211
void remove_edge_linkage(NODE_REF node1, NODE_REF node2, int direction, bool word_end, UNICHAR_ID unichar_id)
Definition: trie.cpp:446
#define ASSERT_HOST(x)
Definition: errcode.h:84
uinT64 EDGE_RECORD
Definition: dawg.h:50
void print_node(NODE_REF node, int max_num_edges) const
Definition: trie.cpp:648
bool end_of_word_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns true if this edge marks the end of a word.
Definition: dawg.h:207
bool tesseract::Trie::end_of_word ( EDGE_REF  edge_ref) const
inlinevirtual

Returns true if the edge indicated by the given EDGE_REF marks the end of a word.

Implements tesseract::Dawg.

Definition at line 139 of file trie.h.

139  {
140  if (edge_ref == NO_EDGE || num_edges_ == 0) return false;
141  return end_of_word_from_edge_rec(*deref_edge_ref(edge_ref));
142  }
uinT64 num_edges_
Definition: trie.h:396
EDGE_RECORD * deref_edge_ref(EDGE_REF edge_ref) const
Definition: trie.h:267
bool end_of_word_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns true if this edge marks the end of a word.
Definition: dawg.h:207
const char * tesseract::Trie::get_reverse_policy_name ( RTLReversePolicy  reverse_policy)
static

Definition at line 60 of file trie.cpp.

60  {
61  return RTLReversePolicyNames[reverse_policy];
62 }
const char *const RTLReversePolicyNames[]
Definition: trie.cpp:47
void tesseract::Trie::initialize_patterns ( UNICHARSET unicharset)

Definition at line 307 of file trie.cpp.

307  {
314  unicharset->unichar_insert(kPuncPatternUnicode);
320  initialized_patterns_ = true;
321  unicharset_size_ = unicharset->size();
322 }
UNICHAR_ID punc_pattern_
Definition: trie.h:406
UNICHAR_ID lower_pattern_
Definition: trie.h:407
UNICHAR_ID alpha_pattern_
Definition: trie.h:403
const UNICHAR_ID unichar_to_id(const char *const unichar_repr) const
Definition: unicharset.cpp:176
static const char kPuncPatternUnicode[]
Definition: trie.h:78
int size() const
Definition: unicharset.h:264
static const char kUpperPatternUnicode[]
Definition: trie.h:80
bool initialized_patterns_
Definition: trie.h:402
UNICHAR_ID alphanum_pattern_
Definition: trie.h:405
static const char kAlphaPatternUnicode[]
Definition: trie.h:75
static const char kLowerPatternUnicode[]
Definition: trie.h:79
static const char kDigitPatternUnicode[]
Definition: trie.h:76
void unichar_insert(const char *const unichar_repr)
Definition: unicharset.cpp:511
static const char kAlphanumPatternUnicode[]
Definition: trie.h:77
UNICHAR_ID digit_pattern_
Definition: trie.h:404
UNICHAR_ID upper_pattern_
Definition: trie.h:408
int unicharset_size_
Definition: dawg.h:291
void tesseract::Trie::link_edge ( EDGE_RECORD edge,
NODE_REF  nxt,
bool  repeats,
int  direction,
bool  word_end,
UNICHAR_ID  unichar_id 
)
inlineprotected

Sets up this edge record to the requested values.

Definition at line 282 of file trie.h.

283  {
284  EDGE_RECORD flags = 0;
285  if (repeats) flags |= MARKER_FLAG;
286  if (word_end) flags |= WERD_END_FLAG;
287  if (direction == BACKWARD_EDGE) flags |= DIRECTION_FLAG;
288  *edge = ((nxt << next_node_start_bit_) |
289  (static_cast<EDGE_RECORD>(flags) << flag_start_bit_) |
290  (static_cast<EDGE_RECORD>(unichar_id) << LETTER_START_BIT));
291  }
#define WERD_END_FLAG
Definition: dawg.h:89
#define LETTER_START_BIT
Definition: dawg.h:90
#define BACKWARD_EDGE
Definition: dawg.h:85
int flag_start_bit_
Definition: dawg.h:292
#define MARKER_FLAG
Definition: dawg.h:87
#define DIRECTION_FLAG
Definition: dawg.h:88
int next_node_start_bit_
Definition: dawg.h:293
uinT64 EDGE_RECORD
Definition: dawg.h:50
int direction(EDGEPT *point)
Definition: vecfuncs.cpp:43
EDGE_REF tesseract::Trie::make_edge_ref ( NODE_REF  node_index,
EDGE_INDEX  edge_index 
) const
inlineprotected

Constructs EDGE_REF from the given node_index and edge_index.

Definition at line 276 of file trie.h.

277  {
278  return ((node_index << flag_start_bit_) |
279  (edge_index << LETTER_START_BIT));
280  }
#define LETTER_START_BIT
Definition: dawg.h:90
int flag_start_bit_
Definition: dawg.h:292
NODE_REF tesseract::Trie::new_dawg_node ( )
protected

Definition at line 261 of file trie.cpp.

261  {
262  TRIE_NODE_RECORD *node = new TRIE_NODE_RECORD();
263  if (node == NULL) return 0; // failed to create new node
264  nodes_.push_back(node);
265  return nodes_.length() - 1;
266 }
#define NULL
Definition: host.h:144
int push_back(T object)
TRIE_NODES nodes_
Definition: trie.h:395
int length() const
Definition: genericvector.h:63
NODE_REF tesseract::Trie::next_node ( EDGE_REF  edge_ref) const
inlinevirtual

Returns the next node visited by following the edge indicated by the given EDGE_REF.

Implements tesseract::Dawg.

Definition at line 130 of file trie.h.

130  {
131  if (edge_ref == NO_EDGE || num_edges_ == 0) return NO_EDGE;
132  return next_node_from_edge_rec(*deref_edge_ref(edge_ref));
133  }
NODE_REF next_node_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns the next node visited by following this edge.
Definition: dawg.h:194
uinT64 num_edges_
Definition: trie.h:396
EDGE_RECORD * deref_edge_ref(EDGE_REF edge_ref) const
Definition: trie.h:267
virtual EDGE_REF tesseract::Trie::pattern_loop_edge ( EDGE_REF  edge_ref,
UNICHAR_ID  unichar_id,
bool  word_end 
) const
inlinevirtual

Returns the given EDGE_REF if the EDGE_RECORD that it points to has a self loop and the given unichar_id matches the unichar_id stored in the EDGE_RECORD, returns NO_EDGE otherwise.

Reimplemented from tesseract::Dawg.

Definition at line 223 of file trie.h.

225  {
226  if (edge_ref == NO_EDGE) return NO_EDGE;
227  EDGE_RECORD *edge_rec = deref_edge_ref(edge_ref);
228  return (marker_flag_from_edge_rec(*edge_rec) &&
229  unichar_id == unichar_id_from_edge_rec(*edge_rec) &&
230  word_end == end_of_word_from_edge_rec(*edge_rec)) ?
231  edge_ref : NO_EDGE;
232  }
EDGE_RECORD * deref_edge_ref(EDGE_REF edge_ref) const
Definition: trie.h:267
bool marker_flag_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns the marker flag of this edge.
Definition: dawg.h:198
UNICHAR_ID unichar_id_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns UNICHAR_ID recorded in this edge.
Definition: dawg.h:211
uinT64 EDGE_RECORD
Definition: dawg.h:50
bool end_of_word_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns true if this edge marks the end of a word.
Definition: dawg.h:207
void tesseract::Trie::print_all ( const char *  msg,
int  max_num_edges 
)
inlineprotected

Definition at line 310 of file trie.h.

310  {
311  tprintf("\n__________________________\n%s\n", msg);
312  for (int i = 0; i < nodes_.size(); ++i) print_node(i, max_num_edges);
313  tprintf("__________________________\n");
314  }
TRIE_NODES nodes_
Definition: trie.h:395
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:41
int size() const
Definition: genericvector.h:59
void print_node(NODE_REF node, int max_num_edges) const
Definition: trie.cpp:648
void tesseract::Trie::print_edge_rec ( const EDGE_RECORD edge_rec) const
inlineprotected

Prints the given EDGE_RECORD.

Definition at line 293 of file trie.h.

293  {
294  tprintf("|" REFFORMAT "|%s%s%s|%d|", next_node_from_edge_rec(edge_rec),
295  marker_flag_from_edge_rec(edge_rec) ? "R," : "",
296  (direction_from_edge_rec(edge_rec) == FORWARD_EDGE) ? "F" : "B",
297  end_of_word_from_edge_rec(edge_rec) ? ",E" : "",
298  unichar_id_from_edge_rec(edge_rec));
299  }
#define REFFORMAT
Definition: dawg.h:92
NODE_REF next_node_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns the next node visited by following this edge.
Definition: dawg.h:194
bool marker_flag_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns the marker flag of this edge.
Definition: dawg.h:198
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:41
#define FORWARD_EDGE
Definition: dawg.h:84
UNICHAR_ID unichar_id_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns UNICHAR_ID recorded in this edge.
Definition: dawg.h:211
int direction_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns the direction flag of this edge.
Definition: dawg.h:202
bool end_of_word_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns true if this edge marks the end of a word.
Definition: dawg.h:207
void tesseract::Trie::print_node ( NODE_REF  node,
int  max_num_edges 
) const
virtual

Prints the contents of the node indicated by the given NODE_REF. At most max_num_edges will be printed.

Implements tesseract::Dawg.

Definition at line 648 of file trie.cpp.

648  {
649  if (node == NO_EDGE) return; // nothing to print
650  TRIE_NODE_RECORD *node_ptr = nodes_[node];
651  int num_fwd = node_ptr->forward_edges.size();
652  int num_bkw = node_ptr->backward_edges.size();
653  EDGE_VECTOR *vec;
654  for (int dir = 0; dir < 2; ++dir) {
655  if (dir == 0) {
656  vec = &(node_ptr->forward_edges);
657  tprintf(REFFORMAT " (%d %d): ", node, num_fwd, num_bkw);
658  } else {
659  vec = &(node_ptr->backward_edges);
660  tprintf("\t");
661  }
662  int i;
663  for (i = 0; (dir == 0 ? i < num_fwd : i < num_bkw) &&
664  i < max_num_edges; ++i) {
665  print_edge_rec((*vec)[i]);
666  tprintf(" ");
667  }
668  if (dir == 0 ? i < num_fwd : i < num_bkw) tprintf("...");
669  tprintf("\n");
670  }
671 }
void print_edge_rec(const EDGE_RECORD &edge_rec) const
Definition: trie.h:293
#define REFFORMAT
Definition: dawg.h:92
EDGE_VECTOR backward_edges
Definition: trie.h:50
TRIE_NODES nodes_
Definition: trie.h:395
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:41
int size() const
Definition: genericvector.h:59
EDGE_VECTOR forward_edges
Definition: trie.h:49
bool tesseract::Trie::read_pattern_list ( const char *  filename,
const UNICHARSET unicharset 
)

Definition at line 364 of file trie.cpp.

365  {
366  if (!initialized_patterns_) {
367  tprintf("please call initialize_patterns() before read_pattern_list()\n");
368  return false;
369  }
370 
371  FILE *pattern_file = open_file (filename, "r");
372  if (pattern_file == NULL) {
373  tprintf("Error opening pattern file %s\n", filename);
374  return false;
375  }
376 
377  int pattern_count = 0;
378  char string[CHARS_PER_LINE];
379  while (fgets(string, CHARS_PER_LINE, pattern_file) != NULL) {
380  chomp_string(string); // remove newline
381  // Parse the pattern and construct a unichar id vector.
382  // Record the number of repetitions of each unichar in the parallel vector.
383  WERD_CHOICE word(&unicharset);
384  GenericVector<bool> repetitions_vec;
385  const char *str_ptr = string;
386  int step = unicharset.step(str_ptr);
387  bool failed = false;
388  while (step > 0) {
389  UNICHAR_ID curr_unichar_id = INVALID_UNICHAR_ID;
390  if (step == 1 && *str_ptr == '\\') {
391  ++str_ptr;
392  if (*str_ptr == '\\') { // regular '\' unichar that was escaped
393  curr_unichar_id = unicharset.unichar_to_id(str_ptr, step);
394  } else {
395  if (word.length() < kSaneNumConcreteChars) {
396  tprintf("Please provide at least %d concrete characters at the"
397  " beginning of the pattern\n", kSaneNumConcreteChars);
398  failed = true;
399  break;
400  }
401  // Parse character class from expression.
402  curr_unichar_id = character_class_to_pattern(*str_ptr);
403  }
404  } else {
405  curr_unichar_id = unicharset.unichar_to_id(str_ptr, step);
406  }
407  if (curr_unichar_id == INVALID_UNICHAR_ID) {
408  failed = true;
409  break; // failed to parse this pattern
410  }
411  word.append_unichar_id(curr_unichar_id, 1, 0.0, 0.0);
412  repetitions_vec.push_back(false);
413  str_ptr += step;
414  step = unicharset.step(str_ptr);
415  // Check if there is a repetition pattern specified after this unichar.
416  if (step == 1 && *str_ptr == '\\' && *(str_ptr+1) == '*') {
417  repetitions_vec[repetitions_vec.size()-1] = true;
418  str_ptr += 2;
419  step = unicharset.step(str_ptr);
420  }
421  }
422  if (failed) {
423  tprintf("Invalid user pattern %s\n", string);
424  continue;
425  }
426  // Insert the pattern into the trie.
427  if (debug_level_ > 2) {
428  tprintf("Inserting expanded user pattern %s\n",
429  word.debug_string().string());
430  }
431  if (!this->word_in_dawg(word)) {
432  this->add_word_to_dawg(word, &repetitions_vec);
433  if (!this->word_in_dawg(word)) {
434  tprintf("Error: failed to insert pattern '%s'\n", string);
435  }
436  }
437  ++pattern_count;
438  }
439  if (debug_level_) {
440  tprintf("Read %d valid patterns from %s\n", pattern_count, filename);
441  }
442  fclose(pattern_file);
443  return true;
444 }
int UNICHAR_ID
Definition: unichar.h:31
const UNICHAR_ID unichar_to_id(const char *const unichar_repr) const
Definition: unicharset.cpp:176
FILE * open_file(const char *filename, const char *mode)
Definition: cutil.cpp:82
UNICHAR_ID character_class_to_pattern(char ch)
Definition: trie.cpp:346
bool initialized_patterns_
Definition: trie.h:402
bool add_word_to_dawg(const WERD_CHOICE &word, const GenericVector< bool > *repetitions)
Definition: trie.cpp:173
#define NULL
Definition: host.h:144
bool word_in_dawg(const WERD_CHOICE &word) const
Returns true if the given word is in the Dawg.
Definition: dawg.cpp:48
int push_back(T object)
#define CHARS_PER_LINE
Definition: cutil.h:57
void chomp_string(char *str)
Definition: helpers.h:32
int debug_level_
Definition: dawg.h:298
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:41
static const int kSaneNumConcreteChars
Definition: trie.h:71
int size() const
Definition: genericvector.h:59
int step(const char *str) const
Definition: unicharset.cpp:192
bool tesseract::Trie::read_word_list ( const char *  filename,
const UNICHARSET unicharset,
Trie::RTLReversePolicy  reverse 
)

Definition at line 268 of file trie.cpp.

270  {
271  FILE *word_file;
272  char string[CHARS_PER_LINE];
273  int word_count = 0;
274 
275  word_file = open_file (filename, "r");
276 
277  while (fgets(string, CHARS_PER_LINE, word_file) != NULL) {
278  chomp_string(string); // remove newline
279  WERD_CHOICE word(string, unicharset);
280  if ((reverse_policy == RRP_REVERSE_IF_HAS_RTL &&
281  word.has_rtl_unichar_id()) ||
282  reverse_policy == RRP_FORCE_REVERSE) {
283  word.reverse_and_mirror_unichar_ids();
284  }
285  ++word_count;
286  if (debug_level_ && word_count % 10000 == 0)
287  tprintf("Read %d words so far\n", word_count);
288  if (word.length() != 0 && !word.contains_unichar_id(INVALID_UNICHAR_ID)) {
289  if (!this->word_in_dawg(word)) {
290  this->add_word_to_dawg(word);
291  if (!this->word_in_dawg(word)) {
292  tprintf("Error: word '%s' not in DAWG after adding it\n", string);
293  return false;
294  }
295  }
296  } else if (debug_level_) {
297  tprintf("Skipping invalid word %s\n", string);
298  if (debug_level_ >= 3) word.print();
299  }
300  }
301  if (debug_level_)
302  tprintf("Read %d words total.\n", word_count);
303  fclose(word_file);
304  return true;
305 }
FILE * open_file(const char *filename, const char *mode)
Definition: cutil.cpp:82
bool add_word_to_dawg(const WERD_CHOICE &word, const GenericVector< bool > *repetitions)
Definition: trie.cpp:173
#define NULL
Definition: host.h:144
bool word_in_dawg(const WERD_CHOICE &word) const
Returns true if the given word is in the Dawg.
Definition: dawg.cpp:48
#define CHARS_PER_LINE
Definition: cutil.h:57
void chomp_string(char *str)
Definition: helpers.h:32
int debug_level_
Definition: dawg.h:298
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:41
bool tesseract::Trie::reduce_lettered_edges ( EDGE_INDEX  edge_index,
UNICHAR_ID  unichar_id,
NODE_REF  node,
const EDGE_VECTOR backward_edges,
NODE_MARKER  reduced_nodes 
)
protected

Definition at line 563 of file trie.cpp.

567  {
568  if (debug_level_ > 1)
569  tprintf("reduce_lettered_edges(edge=" REFFORMAT ")\n", edge_index);
570  // Compare each of the edge pairs with the given unichar_id.
571  bool did_something = false;
572  for (int i = edge_index; i < backward_edges.size() - 1; ++i) {
573  // Find the first edge that can be eliminated.
574  UNICHAR_ID curr_unichar_id = INVALID_UNICHAR_ID;
575  while (i < backward_edges.size() &&
576  ((curr_unichar_id = unichar_id_from_edge_rec(backward_edges[i])) ==
577  unichar_id) &&
578  !can_be_eliminated(backward_edges[i])) ++i;
579  if (i == backward_edges.size() || curr_unichar_id != unichar_id) break;
580  const EDGE_RECORD &edge_rec = backward_edges[i];
581  // Compare it to the rest of the edges with the given unichar_id.
582  for (int j = i + 1; j < backward_edges.size(); ++j) {
583  const EDGE_RECORD &next_edge_rec = backward_edges[j];
584  if (unichar_id_from_edge_rec(next_edge_rec) != unichar_id) break;
585  if (end_of_word_from_edge_rec(next_edge_rec) ==
586  end_of_word_from_edge_rec(edge_rec) &&
587  can_be_eliminated(next_edge_rec) &&
588  eliminate_redundant_edges(node, edge_rec, next_edge_rec)) {
589  reduced_nodes[next_node_from_edge_rec(edge_rec)] = 0;
590  did_something = true;
591  --j; // do not increment j if next_edge_rec was removed
592  }
593  }
594  }
595  return did_something;
596 }
#define REFFORMAT
Definition: dawg.h:92
int UNICHAR_ID
Definition: unichar.h:31
bool eliminate_redundant_edges(NODE_REF node, const EDGE_RECORD &edge1, const EDGE_RECORD &edge2)
Definition: trie.cpp:512
NODE_REF next_node_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns the next node visited by following this edge.
Definition: dawg.h:194
bool can_be_eliminated(const EDGE_RECORD &edge_rec)
Definition: trie.h:302
int debug_level_
Definition: dawg.h:298
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:41
int size() const
Definition: genericvector.h:59
UNICHAR_ID unichar_id_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns UNICHAR_ID recorded in this edge.
Definition: dawg.h:211
uinT64 EDGE_RECORD
Definition: dawg.h:50
bool end_of_word_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns true if this edge marks the end of a word.
Definition: dawg.h:207
void tesseract::Trie::reduce_node_input ( NODE_REF  node,
NODE_MARKER  reduced_nodes 
)
protected

Eliminates any redundant edges from this node in the Trie.

Definition at line 615 of file trie.cpp.

616  {
617  if (debug_level_ > 1) {
618  tprintf("reduce_node_input(node=" REFFORMAT ")\n", node);
620  }
621 
622  EDGE_VECTOR &backward_edges = nodes_[node]->backward_edges;
623  if (node != 0) sort_edges(&backward_edges);
624  EDGE_INDEX edge_index = 0;
625  while (edge_index < backward_edges.size()) {
626  UNICHAR_ID unichar_id =
627  unichar_id_from_edge_rec(backward_edges[edge_index]);
628  while (reduce_lettered_edges(edge_index, unichar_id, node,
629  backward_edges, reduced_nodes));
630  while (++edge_index < backward_edges.size() &&
631  unichar_id_from_edge_rec(backward_edges[edge_index]) == unichar_id);
632  }
633  reduced_nodes[node] = true; // mark as reduced
634 
635  if (debug_level_ > 1) {
636  tprintf("Node " REFFORMAT " after reduction:\n", node);
638  }
639 
640  for (int i = 0; i < backward_edges.size(); ++i) {
641  NODE_REF next_node = next_node_from_edge_rec(backward_edges[i]);
642  if (next_node != 0 && !reduced_nodes[next_node]) {
643  reduce_node_input(next_node, reduced_nodes);
644  }
645  }
646 }
#define REFFORMAT
Definition: dawg.h:92
int UNICHAR_ID
Definition: unichar.h:31
void reduce_node_input(NODE_REF node, NODE_MARKER reduced_nodes)
Definition: trie.cpp:615
bool reduce_lettered_edges(EDGE_INDEX edge_index, UNICHAR_ID unichar_id, NODE_REF node, const EDGE_VECTOR &backward_edges, NODE_MARKER reduced_nodes)
Definition: trie.cpp:563
NODE_REF next_node(EDGE_REF edge_ref) const
Definition: trie.h:130
NODE_REF next_node_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns the next node visited by following this edge.
Definition: dawg.h:194
TRIE_NODES nodes_
Definition: trie.h:395
void sort_edges(EDGE_VECTOR *edges)
Definition: trie.cpp:598
inT64 EDGE_INDEX
Definition: trie.h:32
int debug_level_
Definition: dawg.h:298
#define MAX_NODE_EDGES_DISPLAY
Definition: dawg.h:86
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:41
inT64 NODE_REF
Definition: dawg.h:55
int size() const
Definition: genericvector.h:59
UNICHAR_ID unichar_id_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns UNICHAR_ID recorded in this edge.
Definition: dawg.h:211
void print_node(NODE_REF node, int max_num_edges) const
Definition: trie.cpp:648
void tesseract::Trie::remove_edge ( NODE_REF  node1,
NODE_REF  node2,
bool  word_end,
UNICHAR_ID  unichar_id 
)
inlineprotected

Definition at line 357 of file trie.h.

358  {
359  remove_edge_linkage(node1, node2, FORWARD_EDGE, word_end, unichar_id);
360  remove_edge_linkage(node2, node1, BACKWARD_EDGE, word_end, unichar_id);
361  }
#define BACKWARD_EDGE
Definition: dawg.h:85
#define FORWARD_EDGE
Definition: dawg.h:84
void remove_edge_linkage(NODE_REF node1, NODE_REF node2, int direction, bool word_end, UNICHAR_ID unichar_id)
Definition: trie.cpp:446
void tesseract::Trie::remove_edge_linkage ( NODE_REF  node1,
NODE_REF  node2,
int  direction,
bool  word_end,
UNICHAR_ID  unichar_id 
)
protected

Definition at line 446 of file trie.cpp.

447  {
448  EDGE_RECORD *edge_ptr = NULL;
449  EDGE_INDEX edge_index = 0;
450  ASSERT_HOST(edge_char_of(node1, node2, direction, word_end,
451  unichar_id, &edge_ptr, &edge_index));
452  if (debug_level_ > 1) {
453  tprintf("removed edge in nodes_[" REFFORMAT "]: ", node1);
454  print_edge_rec(*edge_ptr);
455  tprintf("\n");
456  }
457  if (direction == FORWARD_EDGE) {
458  nodes_[node1]->forward_edges.remove(edge_index);
459  } else {
460  nodes_[node1]->backward_edges.remove(edge_index);
461  }
462  --num_edges_;
463 }
void print_edge_rec(const EDGE_RECORD &edge_rec) const
Definition: trie.h:293
#define REFFORMAT
Definition: dawg.h:92
#define NULL
Definition: host.h:144
EDGE_REF edge_char_of(NODE_REF node_ref, UNICHAR_ID unichar_id, bool word_end) const
Definition: trie.h:104
uinT64 num_edges_
Definition: trie.h:396
TRIE_NODES nodes_
Definition: trie.h:395
inT64 EDGE_INDEX
Definition: trie.h:32
int debug_level_
Definition: dawg.h:298
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:41
#define FORWARD_EDGE
Definition: dawg.h:84
virtual void remove(int index)
#define ASSERT_HOST(x)
Definition: errcode.h:84
uinT64 EDGE_RECORD
Definition: dawg.h:50
int direction(EDGEPT *point)
Definition: vecfuncs.cpp:43
void tesseract::Trie::sort_edges ( EDGE_VECTOR edges)
protected

Order num_edges of consequtive EDGE_RECORDS in the given EDGE_VECTOR in increasing order of unichar ids. This function is normally called for all edges in a single node, and since number of edges in each node is usually quite small, selection sort is used.

Definition at line 598 of file trie.cpp.

598  {
599  int num_edges = edges->size();
600  if (num_edges <= 1) return;
601  for (int i = 0; i < num_edges - 1; ++i) {
602  int min = i;
603  for (int j = (i + 1); j < num_edges; ++j) {
604  if (unichar_id_from_edge_rec((*edges)[j]) <
605  unichar_id_from_edge_rec((*edges)[min])) min = j;
606  }
607  if (i != min) {
608  EDGE_RECORD temp = (*edges)[i];
609  (*edges)[i] = (*edges)[min];
610  (*edges)[min] = temp;
611  }
612  }
613 }
int size() const
Definition: genericvector.h:59
UNICHAR_ID unichar_id_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns UNICHAR_ID recorded in this edge.
Definition: dawg.h:211
uinT64 EDGE_RECORD
Definition: dawg.h:50
SquishedDawg * tesseract::Trie::trie_to_dawg ( )

Definition at line 465 of file trie.cpp.

465  {
466  if (debug_level_ > 2) {
467  print_all("Before reduction:", MAX_NODE_EDGES_DISPLAY);
468  }
469  NODE_MARKER reduced_nodes = new bool[nodes_.size()];
470  for (int i = 0; i < nodes_.size(); i++) reduced_nodes[i] = 0;
471  this->reduce_node_input(0, reduced_nodes);
472  delete[] reduced_nodes;
473 
474  if (debug_level_ > 2) {
475  print_all("After reduction:", MAX_NODE_EDGES_DISPLAY);
476  }
477  // Build a translation map from node indices in nodes_ vector to
478  // their target indices in EDGE_ARRAY.
479  NODE_REF *node_ref_map = new NODE_REF[nodes_.size() + 1];
480  int i, j;
481  node_ref_map[0] = 0;
482  for (i = 0; i < nodes_.size(); ++i) {
483  node_ref_map[i+1] = node_ref_map[i] + nodes_[i]->forward_edges.size();
484  }
485  int num_forward_edges = node_ref_map[i];
486 
487  // Convert nodes_ vector into EDGE_ARRAY translating the next node references
488  // in edges using node_ref_map. Empty nodes and backward edges are dropped.
489  EDGE_ARRAY edge_array =
490  (EDGE_ARRAY)memalloc(num_forward_edges * sizeof(EDGE_RECORD));
491  EDGE_ARRAY edge_array_ptr = edge_array;
492  for (i = 0; i < nodes_.size(); ++i) {
493  TRIE_NODE_RECORD *node_ptr = nodes_[i];
494  int end = node_ptr->forward_edges.size();
495  for (j = 0; j < end; ++j) {
496  EDGE_RECORD &edge_rec = node_ptr->forward_edges[j];
497  NODE_REF node_ref = next_node_from_edge_rec(edge_rec);
498  ASSERT_HOST(node_ref < nodes_.size());
499  UNICHAR_ID unichar_id = unichar_id_from_edge_rec(edge_rec);
500  link_edge(edge_array_ptr, node_ref_map[node_ref], false, FORWARD_EDGE,
501  end_of_word_from_edge_rec(edge_rec), unichar_id);
502  if (j == end - 1) set_marker_flag_in_edge_rec(edge_array_ptr);
503  ++edge_array_ptr;
504  }
505  }
506  delete[] node_ref_map;
507 
508  return new SquishedDawg(edge_array, num_forward_edges, type_, lang_,
510 }
PermuterType perm_
Permuter code that should be used if the word is found in this Dawg.
Definition: dawg.h:286
int UNICHAR_ID
Definition: unichar.h:31
int * memalloc(int size)
Definition: freelist.cpp:22
void reduce_node_input(NODE_REF node, NODE_MARKER reduced_nodes)
Definition: trie.cpp:615
NODE_REF next_node_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns the next node visited by following this edge.
Definition: dawg.h:194
void print_all(const char *msg, int max_num_edges)
Definition: trie.h:310
TRIE_NODES nodes_
Definition: trie.h:395
EDGE_RECORD * EDGE_ARRAY
Definition: dawg.h:53
int debug_level_
Definition: dawg.h:298
#define MAX_NODE_EDGES_DISPLAY
Definition: dawg.h:86
inT64 NODE_REF
Definition: dawg.h:55
void link_edge(EDGE_RECORD *edge, NODE_REF nxt, bool repeats, int direction, bool word_end, UNICHAR_ID unichar_id)
Definition: trie.h:282
DawgType type_
Definition: dawg.h:283
int size() const
Definition: genericvector.h:59
#define FORWARD_EDGE
Definition: dawg.h:84
STRING lang_
Definition: dawg.h:284
EDGE_VECTOR forward_edges
Definition: trie.h:49
UNICHAR_ID unichar_id_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns UNICHAR_ID recorded in this edge.
Definition: dawg.h:211
bool * NODE_MARKER
Definition: trie.h:45
#define ASSERT_HOST(x)
Definition: errcode.h:84
int unicharset_size_
Definition: dawg.h:291
uinT64 EDGE_RECORD
Definition: dawg.h:50
bool end_of_word_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns true if this edge marks the end of a word.
Definition: dawg.h:207
void set_marker_flag_in_edge_rec(EDGE_RECORD *edge_rec)
Sets this edge record to be the last one in a sequence of edges.
Definition: dawg.h:222
void tesseract::Trie::unichar_id_to_patterns ( UNICHAR_ID  unichar_id,
const UNICHARSET unicharset,
GenericVector< UNICHAR_ID > *  vec 
) const
virtual

Fills vec with unichar ids that represent the character classes of the given unichar_id.

Reimplemented from tesseract::Dawg.

Definition at line 324 of file trie.cpp.

326  {
327  bool is_alpha = unicharset.get_isalpha(unichar_id);
328  if (is_alpha) {
331  if (unicharset.get_islower(unichar_id)) {
333  } else if (unicharset.get_isupper(unichar_id)) {
335  }
336  }
337  if (unicharset.get_isdigit(unichar_id)) {
339  if (!is_alpha) vec->push_back(alphanum_pattern_);
340  }
341  if (unicharset.get_ispunctuation(unichar_id)) {
342  vec->push_back(punc_pattern_);
343  }
344 }
bool get_isalpha(UNICHAR_ID unichar_id) const
Definition: unicharset.h:392
UNICHAR_ID punc_pattern_
Definition: trie.h:406
UNICHAR_ID lower_pattern_
Definition: trie.h:407
UNICHAR_ID alpha_pattern_
Definition: trie.h:403
bool get_ispunctuation(UNICHAR_ID unichar_id) const
Definition: unicharset.h:420
UNICHAR_ID alphanum_pattern_
Definition: trie.h:405
bool get_islower(UNICHAR_ID unichar_id) const
Definition: unicharset.h:399
bool get_isupper(UNICHAR_ID unichar_id) const
Definition: unicharset.h:406
int push_back(T object)
bool get_isdigit(UNICHAR_ID unichar_id) const
Definition: unicharset.h:413
UNICHAR_ID digit_pattern_
Definition: trie.h:404
UNICHAR_ID upper_pattern_
Definition: trie.h:408
void tesseract::Trie::unichar_ids_of ( NODE_REF  node,
NodeChildVector vec 
) const
inlinevirtual

Fills the given NodeChildVector with all the unichar ids (and the corresponding EDGE_REFs) for which there is an edge out of this node.

Implements tesseract::Dawg.

Definition at line 117 of file trie.h.

117  {
118  const EDGE_VECTOR &forward_edges =
119  nodes_[static_cast<int>(node)]->forward_edges;
120  for (int i = 0; i < forward_edges.size(); ++i) {
121  vec->push_back(NodeChild(unichar_id_from_edge_rec(forward_edges[i]),
122  make_edge_ref(node, i)));
123  }
124  }
int push_back(T object)
TRIE_NODES nodes_
Definition: trie.h:395
EDGE_REF make_edge_ref(NODE_REF node_index, EDGE_INDEX edge_index) const
Definition: trie.h:276
int size() const
Definition: genericvector.h:59
UNICHAR_ID unichar_id_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns UNICHAR_ID recorded in this edge.
Definition: dawg.h:211

Member Data Documentation

UNICHAR_ID tesseract::Trie::alpha_pattern_
protected

Definition at line 403 of file trie.h.

UNICHAR_ID tesseract::Trie::alphanum_pattern_
protected

Definition at line 405 of file trie.h.

uinT64 tesseract::Trie::deref_direction_mask_
protected

Definition at line 398 of file trie.h.

uinT64 tesseract::Trie::deref_node_index_mask_
protected

Definition at line 399 of file trie.h.

UNICHAR_ID tesseract::Trie::digit_pattern_
protected

Definition at line 404 of file trie.h.

bool tesseract::Trie::initialized_patterns_
protected

Definition at line 402 of file trie.h.

const char tesseract::Trie::kAlphanumPatternUnicode = "\u2002"
static

Definition at line 77 of file trie.h.

const char tesseract::Trie::kAlphaPatternUnicode = "\u2000"
static

Definition at line 75 of file trie.h.

const char tesseract::Trie::kDigitPatternUnicode = "\u2001"
static

Definition at line 76 of file trie.h.

const char tesseract::Trie::kLowerPatternUnicode = "\u2004"
static

Definition at line 79 of file trie.h.

const char tesseract::Trie::kPuncPatternUnicode = "\u2003"
static

Definition at line 78 of file trie.h.

const int tesseract::Trie::kSaneNumConcreteChars = 4
static

Definition at line 71 of file trie.h.

const char tesseract::Trie::kUpperPatternUnicode = "\u2005"
static

Definition at line 80 of file trie.h.

UNICHAR_ID tesseract::Trie::lower_pattern_
protected

Definition at line 407 of file trie.h.

uinT64 tesseract::Trie::max_num_edges_
protected

Definition at line 397 of file trie.h.

TRIE_NODES tesseract::Trie::nodes_
protected

Definition at line 395 of file trie.h.

uinT64 tesseract::Trie::num_edges_
protected

Definition at line 396 of file trie.h.

UNICHAR_ID tesseract::Trie::punc_pattern_
protected

Definition at line 406 of file trie.h.

UNICHAR_ID tesseract::Trie::upper_pattern_
protected

Definition at line 408 of file trie.h.


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