41 #define NO_EDGE (inT64) 0xffffffffffffffffi64
44 #define NO_EDGE (inT64) 0xffffffffffffffffll
64 NodeChild(): unichar_id(INVALID_UNICHAR_ID), edge_ref(NO_EDGE) {}
84 #define FORWARD_EDGE (inT32) 0
85 #define BACKWARD_EDGE (inT32) 1
86 #define MAX_NODE_EDGES_DISPLAY (inT64) 100
87 #define MARKER_FLAG (inT64) 1
88 #define DIRECTION_FLAG (inT64) 2
89 #define WERD_END_FLAG (inT64) 4
90 #define LETTER_START_BIT 0
91 #define NUM_FLAG_BITS 3
92 #define REFFORMAT "%lld"
101 static const char kWildcard[] =
"*";
144 bool enable_wildcard)
const;
160 bool word_end)
const = 0;
165 bool word_end)
const = 0;
245 if (
edge_rec_match(next_node, word_end, unichar_id, curr_next_node,
246 curr_word_end, curr_unichar_id))
return 0;
247 if (unichar_id > curr_unichar_id)
return 1;
248 if (unichar_id == curr_unichar_id) {
249 if (next_node > curr_next_node)
return 1;
250 if (next_node == curr_next_node) {
251 if (word_end > curr_word_end)
return 1;
265 return ((unichar_id == other_unichar_id) &&
266 (next_node == NO_EDGE || next_node == other_next_node) &&
267 (!word_end || (word_end == other_word_end)));
273 PermuterType perm,
int unicharset_size,
int debug_level);
387 const char *debug_msg) {
389 if (
data_[i] == new_pos)
return false;
413 read_squished_dawg(file, type, lang, perm, debug_level);
414 num_forward_edges_in_node0 = num_forward_edges(0);
418 FILE *file = fopen(filename,
"rb");
420 tprintf(
"Failed to open dawg file %s\n", filename);
423 read_squished_dawg(file, type, lang, perm, debug_level);
424 num_forward_edges_in_node0 = num_forward_edges(0);
429 int unicharset_size,
int debug_level) :
430 edges_(edges), num_edges_(num_edges) {
431 init(type, lang, perm, unicharset_size, debug_level);
432 num_forward_edges_in_node0 = num_forward_edges(0);
433 if (debug_level > 3) print_all(
"SquishedDawg:");
441 bool word_end)
const;
446 bool word_end)
const {
448 if (!edge_occupied(edge) || edge == NO_EDGE)
return;
449 assert(forward_edge(edge));
454 }
while (!last_edge(edge++));
484 FILE *file = fopen(filename,
"wb");
486 tprintf(
"Error opening %s\n", filename);
499 inline void set_empty_edge(
EDGE_REF edge_ref) {
503 inline void clear_all_edges() {
504 for (
int edge = 0; edge < num_edges_; edge++) set_empty_edge(edge);
507 inline void clear_marker_flag(
EDGE_REF edge_ref) {
511 inline bool forward_edge(
EDGE_REF edge_ref)
const {
512 return (edge_occupied(edge_ref) &&
516 inline bool backward_edge(
EDGE_REF edge_ref)
const {
517 return (edge_occupied(edge_ref) &&
521 inline bool edge_occupied(
EDGE_REF edge_ref)
const {
525 inline bool last_edge(
EDGE_REF edge_ref)
const {
537 void print_edge(
EDGE_REF edge)
const;
540 void print_all(
const char* msg) {
541 tprintf(
"\n__________________________\n%s\n", msg);
542 for (
int i = 0; i < num_edges_; ++i) print_edge(i);
543 tprintf(
"__________________________\n");
552 int num_forward_edges_in_node0;
557 #endif // DICT_DAWG_H_
const STRING & lang() const
bool end_of_word_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns true if this edge marks the end of a word.
~DawgPositionVector()
Overload destructor, since clear() does not delete data_[] any 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.
virtual bool end_of_word(EDGE_REF edge_ref) const =0
DawgPosition(int dawg_idx, EDGE_REF dawgref, int punc_idx, EDGE_REF puncref, bool backtopunc)
int push_back(DawgPositionobject)
void print_node(NODE_REF node, int max_num_edges) const
NODE_REF next_node(EDGE_REF edge) const
void iterate_words_rec(const WERD_CHOICE &word_so_far, NODE_REF to_explore, TessCallback1< const WERD_CHOICE * > *cb) const
SquishedDawg(FILE *file, DawgType type, const STRING &lang, PermuterType perm, int debug_level)
UNICHAR_ID edge_letter(EDGE_REF edge_ref) const
Returns UNICHAR_ID stored in the edge indicated by the given EDGE_REF.
GenericVector< NodeChild > NodeChildVector
NODE_REF next_node_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns the next node visited by following this edge.
virtual void print_node(NODE_REF node, int max_num_edges) const =0
void write_squished_dawg(const char *filename)
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
bool end_of_word(EDGE_REF edge_ref) const
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.
static const UNICHAR_ID kPatternUnicharID
int check_for_words(const char *filename, const UNICHARSET &unicharset, bool enable_wildcard) const
bool word_in_dawg(const WERD_CHOICE &word) const
Returns true if the given word is in the Dawg.
void write_squished_dawg(FILE *file)
Writes the squished/reduced Dawg to a file.
virtual EDGE_REF edge_char_of(NODE_REF node, UNICHAR_ID unichar_id, bool word_end) const =0
Returns the edge that corresponds to the letter out of this node.
void iterate_words(const UNICHARSET &unicharset, TessCallback1< const WERD_CHOICE * > *cb) const
static const inT16 kDawgMagicNumber
Magic number to determine endianness when reading the Dawg from file.
bool prefix_in_dawg(const WERD_CHOICE &prefix, bool requires_complete) const
NodeChild(UNICHAR_ID id, EDGE_REF ref)
virtual void unichar_id_to_patterns(UNICHAR_ID unichar_id, const UNICHARSET &unicharset, GenericVector< UNICHAR_ID > *vec) const
bool match_words(WERD_CHOICE *word, inT32 index, NODE_REF node, UNICHAR_ID wildcard) const
UNICHAR_ID unichar_id_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns UNICHAR_ID recorded in this edge.
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.
GenericVector< SuccessorList * > SuccessorListsVector
int direction_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns the direction flag of this edge.
SquishedDawg(EDGE_ARRAY edges, int num_edges, DawgType type, const STRING &lang, PermuterType perm, int unicharset_size, int debug_level)
int given_greater_than_edge_rec(NODE_REF next_node, bool word_end, UNICHAR_ID unichar_id, const EDGE_RECORD &edge_rec) const
virtual EDGE_REF pattern_loop_edge(EDGE_REF edge_ref, UNICHAR_ID unichar_id, bool word_end) const
unsigned long long int uinT64
virtual void unichar_ids_of(NODE_REF node, NodeChildVector *vec, bool word_end) const =0
bool operator==(const DawgPosition &other)
GenericVector< int > SuccessorList
bool add_unique(const DawgPosition &new_pos, bool debug, const char *debug_msg)
bool marker_flag_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns the marker flag of this edge.
virtual UNICHAR_ID edge_letter(EDGE_REF edge_ref) const =0
Returns UNICHAR_ID stored in the edge indicated by the given EDGE_REF.
void init(DawgType type, const STRING &lang, PermuterType perm, int unicharset_size, int debug_level)
PermuterType perm_
Permuter code that should be used if the word is found in this Dawg.
PermuterType permuter() const
void unichar_ids_of(NODE_REF node, NodeChildVector *vec, bool word_end) const
virtual NODE_REF next_node(EDGE_REF edge_ref) const =0
SquishedDawg(const char *filename, DawgType type, const STRING &lang, PermuterType perm, int debug_level)