42 #define NO_EDGE (int64_t) 0xffffffffffffffffi64 45 #define NO_EDGE (int64_t) 0xffffffffffffffffll 85 #define FORWARD_EDGE (int32_t) 0 86 #define BACKWARD_EDGE (int32_t) 1 87 #define MAX_NODE_EDGES_DISPLAY (int64_t) 100 88 #define MARKER_FLAG (int64_t) 1 89 #define DIRECTION_FLAG (int64_t) 2 90 #define WERD_END_FLAG (int64_t) 4 91 #define LETTER_START_BIT 0 92 #define NUM_FLAG_BITS 3 93 #define REFFORMAT "%" PRId64 102 static const char kWildcard[] =
"*";
145 bool enable_wildcard)
const;
161 bool word_end)
const = 0;
166 bool word_end)
const = 0;
259 curr_word_end, curr_unichar_id))
return 0;
260 if (unichar_id > curr_unichar_id)
return 1;
261 if (unichar_id == curr_unichar_id) {
262 if (
next_node > curr_next_node)
return 1;
264 if (word_end > curr_word_end)
return 1;
278 return ((unichar_id == other_unichar_id) &&
280 (!word_end || (word_end == other_word_end)));
285 void init(
int unicharset_size);
391 const char *debug_msg) {
393 if (
data_[i] == new_pos)
return false;
424 num_forward_edges_in_node0 = num_forward_edges(0);
431 num_edges_(num_edges) {
432 init(unicharset_size);
433 num_forward_edges_in_node0 = num_forward_edges(0);
434 if (debug_level > 3) print_all(
"SquishedDawg:");
440 if (!read_squished_dawg(fp))
return false;
441 num_forward_edges_in_node0 = num_forward_edges(0);
449 bool word_end)
const;
454 bool word_end)
const {
456 if (!edge_occupied(edge) || edge == NO_EDGE)
return;
457 assert(forward_edge(edge));
462 }
while (!last_edge(edge++));
495 tprintf(
"Error serializing %s\n", filename);
499 tprintf(
"Error writing file %s\n", filename);
511 inline void set_empty_edge(
EDGE_REF edge_ref) {
515 inline void clear_all_edges() {
516 for (
int edge = 0; edge < num_edges_; edge++) set_empty_edge(edge);
519 inline void clear_marker_flag(
EDGE_REF edge_ref) {
523 inline bool forward_edge(
EDGE_REF edge_ref)
const {
524 return (edge_occupied(edge_ref) &&
528 inline bool backward_edge(
EDGE_REF edge_ref)
const {
529 return (edge_occupied(edge_ref) &&
533 inline bool edge_occupied(
EDGE_REF edge_ref)
const {
537 inline bool last_edge(
EDGE_REF edge_ref)
const {
542 int32_t num_forward_edges(
NODE_REF node)
const;
545 bool read_squished_dawg(TFile *file);
548 void print_edge(
EDGE_REF edge)
const;
551 void print_all(
const char* msg) {
552 tprintf(
"\n__________________________\n%s\n", msg);
553 for (
int i = 0; i < num_edges_; ++i) print_edge(i);
554 tprintf(
"__________________________\n");
557 std::unique_ptr<EDGE_REF[]> build_node_map(int32_t *num_nodes)
const;
562 int num_forward_edges_in_node0;
567 #endif // DICT_DAWG_H_
NodeChild(UNICHAR_ID id, EDGE_REF ref)
void OpenWrite(GenericVector< char > *data)
bool write_squished_dawg(const char *filename)
void print_node(NODE_REF node, int max_num_edges) const
virtual void unichar_id_to_patterns(UNICHAR_ID unichar_id, const UNICHARSET &unicharset, GenericVector< UNICHAR_ID > *vec) const
virtual UNICHAR_ID edge_letter(EDGE_REF edge_ref) const =0
Returns UNICHAR_ID stored in the edge indicated by the given EDGE_REF.
int direction_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns the direction flag of this edge.
UNICHAR_ID unichar_id_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns UNICHAR_ID recorded in this edge.
bool prefix_in_dawg(const WERD_CHOICE &prefix, bool requires_complete) const
void iterate_words_rec(const WERD_CHOICE &word_so_far, NODE_REF to_explore, TessCallback1< const WERD_CHOICE *> *cb) const
bool word_in_dawg(const WERD_CHOICE &word) const
Returns true if the given word is in the Dawg.
UNICHAR_ID edge_letter(EDGE_REF edge_ref) const
Returns UNICHAR_ID stored in the edge indicated by the given EDGE_REF.
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.
bool add_unique(const DawgPosition &new_pos, bool debug, const char *debug_msg)
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.
PermuterType perm_
Permuter code that should be used if the word is found in this Dawg.
int given_greater_than_edge_rec(NODE_REF next_node, bool word_end, UNICHAR_ID unichar_id, const EDGE_RECORD &edge_rec) const
static const int16_t kDawgMagicNumber
Magic number to determine endianness when reading the Dawg from file.
SquishedDawg(DawgType type, const STRING &lang, PermuterType perm, int debug_level)
bool operator==(const DawgPosition &other)
bool write_squished_dawg(TFile *file)
Writes the squished/reduced Dawg to a file.
SquishedDawg(const char *filename, DawgType type, const STRING &lang, PermuterType perm, int debug_level)
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.
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.
virtual void print_node(NODE_REF node, int max_num_edges) const =0
bool marker_flag_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns the marker flag of this edge.
virtual EDGE_REF pattern_loop_edge(EDGE_REF edge_ref, UNICHAR_ID unichar_id, bool word_end) const
DLLSYM void tprintf(const char *format,...)
bool match_words(WERD_CHOICE *word, int32_t index, NODE_REF node, UNICHAR_ID wildcard) const
virtual void unichar_ids_of(NODE_REF node, NodeChildVector *vec, bool word_end) const =0
bool Open(const STRING &filename, FileReader reader)
int push_back(DawgPosition object)
void iterate_words(const UNICHARSET &unicharset, TessCallback1< const WERD_CHOICE *> *cb) const
void init(int unicharset_size)
bool CloseWrite(const STRING &filename, FileWriter writer)
virtual NODE_REF next_node(EDGE_REF edge_ref) const =0
bool end_of_word_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns true if this edge marks the end of a word.
DawgPosition(int dawg_idx, EDGE_REF dawgref, int punc_idx, EDGE_REF puncref, bool backtopunc)
static const UNICHAR_ID kPatternUnicharID
const STRING & lang() const
SquishedDawg(EDGE_ARRAY edges, int num_edges, DawgType type, const STRING &lang, PermuterType perm, int unicharset_size, int debug_level)
NODE_REF next_node_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns the next node visited by following this edge.
PermuterType permuter() const
int check_for_words(const char *filename, const UNICHARSET &unicharset, bool enable_wildcard) const
NODE_REF next_node(EDGE_REF edge) 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
virtual bool end_of_word(EDGE_REF edge_ref) const =0
Dawg(DawgType type, const STRING &lang, PermuterType perm, int debug_level)
bool end_of_word(EDGE_REF edge_ref) const
void unichar_ids_of(NODE_REF node, NodeChildVector *vec, bool word_end) const