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

#include <dawg.h>

Inheritance diagram for tesseract::SquishedDawg:
tesseract::Dawg

Public Member Functions

 SquishedDawg (FILE *file, DawgType type, const STRING &lang, PermuterType perm, int debug_level)
 
 SquishedDawg (const char *filename, DawgType type, const STRING &lang, PermuterType perm, int debug_level)
 
 SquishedDawg (EDGE_ARRAY edges, int num_edges, DawgType type, const STRING &lang, PermuterType perm, int unicharset_size, int debug_level)
 
 ~SquishedDawg ()
 
int NumEdges ()
 
EDGE_REF edge_char_of (NODE_REF node, UNICHAR_ID unichar_id, bool word_end) const
 Returns the edge that corresponds to the letter out of this node. More...
 
void unichar_ids_of (NODE_REF node, NodeChildVector *vec) const
 
NODE_REF next_node (EDGE_REF edge) const
 
bool end_of_word (EDGE_REF edge_ref) const
 
UNICHAR_ID edge_letter (EDGE_REF edge_ref) const
 Returns UNICHAR_ID stored in the edge indicated by the given EDGE_REF. More...
 
void print_node (NODE_REF node, int max_num_edges) const
 
void write_squished_dawg (FILE *file)
 Writes the squished/reduced Dawg to a file. More...
 
void write_squished_dawg (const char *filename)
 
- 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
 
virtual 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
 

Additional Inherited Members

- 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 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 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 that can operate on a compacted (squished) Dawg (read, search and write to file). This class is read-only in the sense that new words can not be added to an instance of SquishedDawg. The underlying representation of the nodes and edges in SquishedDawg is stored as a contiguous EDGE_ARRAY (read from file or given as an argument to the constructor).

Definition at line 352 of file dawg.h.

Constructor & Destructor Documentation

tesseract::SquishedDawg::SquishedDawg ( FILE *  file,
DawgType  type,
const STRING lang,
PermuterType  perm,
int  debug_level 
)
inline

Definition at line 354 of file dawg.h.

355  {
356  read_squished_dawg(file, type, lang, perm, debug_level);
357  num_forward_edges_in_node0 = num_forward_edges(0);
358  }
DawgType type() const
Definition: dawg.h:131
tesseract::SquishedDawg::SquishedDawg ( const char *  filename,
DawgType  type,
const STRING lang,
PermuterType  perm,
int  debug_level 
)
inline

Definition at line 359 of file dawg.h.

360  {
361  FILE *file = fopen(filename, "rb");
362  if (file == NULL) {
363  tprintf("Failed to open dawg file %s\n", filename);
364  exit(1);
365  }
366  read_squished_dawg(file, type, lang, perm, debug_level);
367  num_forward_edges_in_node0 = num_forward_edges(0);
368  fclose(file);
369  }
DawgType type() const
Definition: dawg.h:131
#define NULL
Definition: host.h:144
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:41
tesseract::SquishedDawg::SquishedDawg ( EDGE_ARRAY  edges,
int  num_edges,
DawgType  type,
const STRING lang,
PermuterType  perm,
int  unicharset_size,
int  debug_level 
)
inline

Definition at line 370 of file dawg.h.

372  :
373  edges_(edges), num_edges_(num_edges) {
374  init(type, lang, perm, unicharset_size, debug_level);
375  num_forward_edges_in_node0 = num_forward_edges(0);
376  if (debug_level > 3) print_all("SquishedDawg:");
377  }
DawgType type() const
Definition: dawg.h:131
void init(DawgType type, const STRING &lang, PermuterType perm, int unicharset_size, int debug_level)
Definition: dawg.cpp:159
tesseract::SquishedDawg::~SquishedDawg ( )

Definition at line 181 of file dawg.cpp.

181 { memfree(edges_); }
void memfree(void *element)
Definition: freelist.cpp:30

Member Function Documentation

EDGE_REF tesseract::SquishedDawg::edge_char_of ( NODE_REF  node,
UNICHAR_ID  unichar_id,
bool  word_end 
) const
virtual

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

Implements tesseract::Dawg.

Definition at line 183 of file dawg.cpp.

185  {
186  EDGE_REF edge = node;
187  if (node == 0) { // binary search
188  EDGE_REF start = 0;
189  EDGE_REF end = num_forward_edges_in_node0 - 1;
190  int compare;
191  while (start <= end) {
192  edge = (start + end) >> 1; // (start + end) / 2
193  compare = given_greater_than_edge_rec(NO_EDGE, word_end,
194  unichar_id, edges_[edge]);
195  if (compare == 0) { // given == vec[k]
196  return edge;
197  } else if (compare == 1) { // given > vec[k]
198  start = edge + 1;
199  } else { // given < vec[k]
200  end = edge - 1;
201  }
202  }
203  } else { // linear search
204  if (edge != NO_EDGE && edge_occupied(edge)) {
205  do {
206  if ((unichar_id_from_edge_rec(edges_[edge]) == unichar_id) &&
207  (!word_end || end_of_word_from_edge_rec(edges_[edge])))
208  return (edge);
209  } while (!last_edge(edge++));
210  }
211  }
212  return (NO_EDGE); // not found
213 }
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
inT64 EDGE_REF
Definition: dawg.h:54
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 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::SquishedDawg::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 410 of file dawg.h.

410  {
411  return unichar_id_from_edge_rec((edges_[edge_ref]));
412  }
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::SquishedDawg::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 405 of file dawg.h.

405  {
406  return end_of_word_from_edge_rec((edges_[edge_ref]));
407  }
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
NODE_REF tesseract::SquishedDawg::next_node ( EDGE_REF  edge) const
inlinevirtual

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

Implements tesseract::Dawg.

Definition at line 399 of file dawg.h.

399  {
400  return next_node_from_edge_rec((edges_[edge]));
401  }
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 tesseract::SquishedDawg::NumEdges ( )
inline

Definition at line 380 of file dawg.h.

380 { return num_edges_; }
void tesseract::SquishedDawg::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 228 of file dawg.cpp.

228  {
229  if (node == NO_EDGE) return; // nothing to print
230 
231  EDGE_REF edge = node;
232  const char *forward_string = "FORWARD";
233  const char *backward_string = " ";
234 
235  const char *last_string = "LAST";
236  const char *not_last_string = " ";
237 
238  const char *eow_string = "EOW";
239  const char *not_eow_string = " ";
240 
241  const char *direction;
242  const char *is_last;
243  const char *eow;
244 
245  UNICHAR_ID unichar_id;
246 
247  if (edge_occupied(edge)) {
248  do {
249  direction =
250  forward_edge(edge) ? forward_string : backward_string;
251  is_last = last_edge(edge) ? last_string : not_last_string;
252  eow = end_of_word(edge) ? eow_string : not_eow_string;
253 
254  unichar_id = edge_letter(edge);
255  tprintf(REFFORMAT " : next = " REFFORMAT ", unichar_id = %d, %s %s %s\n",
256  edge, next_node(edge), unichar_id,
257  direction, is_last, eow);
258 
259  if (edge - node > max_num_edges) return;
260  } while (!last_edge(edge++));
261 
262  if (edge < num_edges_ &&
263  edge_occupied(edge) && backward_edge(edge)) {
264  do {
265  direction =
266  forward_edge(edge) ? forward_string : backward_string;
267  is_last = last_edge(edge) ? last_string : not_last_string;
268  eow = end_of_word(edge) ? eow_string : not_eow_string;
269 
270  unichar_id = edge_letter(edge);
271  tprintf(REFFORMAT " : next = " REFFORMAT
272  ", unichar_id = %d, %s %s %s\n",
273  edge, next_node(edge), unichar_id,
274  direction, is_last, eow);
275 
276  if (edge - node > MAX_NODE_EDGES_DISPLAY) return;
277  } while (!last_edge(edge++));
278  }
279  }
280  else {
281  tprintf(REFFORMAT " : no edges in this node\n", node);
282  }
283  tprintf("\n");
284 }
#define REFFORMAT
Definition: dawg.h:92
int UNICHAR_ID
Definition: unichar.h:31
bool end_of_word(EDGE_REF edge_ref) const
Definition: dawg.h:405
inT64 EDGE_REF
Definition: dawg.h:54
UNICHAR_ID edge_letter(EDGE_REF edge_ref) const
Returns UNICHAR_ID stored in the edge indicated by the given EDGE_REF.
Definition: dawg.h:410
#define MAX_NODE_EDGES_DISPLAY
Definition: dawg.h:86
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:41
NODE_REF next_node(EDGE_REF edge) const
Definition: dawg.h:399
int direction(EDGEPT *point)
Definition: vecfuncs.cpp:43
void tesseract::SquishedDawg::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 388 of file dawg.h.

388  {
389  EDGE_REF edge = node;
390  if (!edge_occupied(edge) || edge == NO_EDGE) return;
391  assert(forward_edge(edge)); // we don't expect any backward edges to
392  do { // be present when this funciton is called
393  vec->push_back(NodeChild(unichar_id_from_edge_rec(edges_[edge]), edge));
394  } while (!last_edge(edge++));
395  }
inT64 EDGE_REF
Definition: dawg.h:54
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 tesseract::SquishedDawg::write_squished_dawg ( FILE *  file)

Writes the squished/reduced Dawg to a file.

Definition at line 369 of file dawg.cpp.

369  {
370  EDGE_REF edge;
371  inT32 num_edges;
372  inT32 node_count = 0;
373  NODE_MAP node_map;
374  EDGE_REF old_index;
375  EDGE_RECORD temp_record;
376 
377  if (debug_level_) tprintf("write_squished_dawg\n");
378 
379  node_map = build_node_map(&node_count);
380 
381  // Write the magic number to help detecting a change in endianness.
382  inT16 magic = kDawgMagicNumber;
383  fwrite(&magic, sizeof(inT16), 1, file);
384  fwrite(&unicharset_size_, sizeof(inT32), 1, file);
385 
386  // Count the number of edges in this Dawg.
387  num_edges = 0;
388  for (edge=0; edge < num_edges_; edge++)
389  if (forward_edge(edge))
390  num_edges++;
391 
392  fwrite(&num_edges, sizeof(inT32), 1, file); // write edge count to file
393 
394  if (debug_level_) {
395  tprintf("%d nodes in DAWG\n", node_count);
396  tprintf("%d edges in DAWG\n", num_edges);
397  }
398 
399  for (edge = 0; edge < num_edges_; edge++) {
400  if (forward_edge(edge)) { // write forward edges
401  do {
402  old_index = next_node_from_edge_rec(edges_[edge]);
403  set_next_node(edge, node_map[old_index]);
404  temp_record = edges_[edge];
405  fwrite(&(temp_record), sizeof(EDGE_RECORD), 1, file);
406  set_next_node(edge, old_index);
407  } while (!last_edge(edge++));
408 
409  if (edge >= num_edges_) break;
410  if (backward_edge(edge)) // skip back links
411  while (!last_edge(edge++));
412 
413  edge--;
414  }
415  }
416  free(node_map);
417 }
static const inT16 kDawgMagicNumber
Magic number to determine endianness when reading the Dawg from file.
Definition: dawg.h:125
inT64 EDGE_REF
Definition: dawg.h:54
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
int debug_level_
Definition: dawg.h:298
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:41
short inT16
Definition: host.h:100
EDGE_REF * NODE_MAP
Definition: dawg.h:56
int unicharset_size_
Definition: dawg.h:291
uinT64 EDGE_RECORD
Definition: dawg.h:50
void tesseract::SquishedDawg::write_squished_dawg ( const char *  filename)
inline

Opens the file with the given filename and writes the squished/reduced Dawg to the file.

Definition at line 423 of file dawg.h.

423  {
424  FILE *file = fopen(filename, "wb");
425  if (file == NULL) {
426  tprintf("Error opening %s\n", filename);
427  exit(1);
428  }
429  this->write_squished_dawg(file);
430  fclose(file);
431  }
#define NULL
Definition: host.h:144
void write_squished_dawg(FILE *file)
Writes the squished/reduced Dawg to a file.
Definition: dawg.cpp:369
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:41

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