Go to the documentation of this file.
38 #define NO_EDGE (int64_t) 0xffffffffffffffffi64
41 #define NO_EDGE (int64_t) 0xffffffffffffffffll
81 #define FORWARD_EDGE (int32_t) 0
82 #define BACKWARD_EDGE (int32_t) 1
83 #define MAX_NODE_EDGES_DISPLAY (int64_t) 100
84 #define MARKER_FLAG (int64_t) 1
85 #define DIRECTION_FLAG (int64_t) 2
86 #define WERD_END_FLAG (int64_t) 4
87 #define LETTER_START_BIT 0
88 #define NUM_FLAG_BITS 3
89 #define REFFORMAT "%" PRId64
92 {
false,
true,
true,
false },
93 {
true,
false,
false,
false },
94 {
true,
false,
false,
false },
95 {
false,
false,
false,
false },
98 static const char kWildcard[] =
"*";
141 bool enable_wildcard)
const;
157 bool word_end)
const = 0;
162 bool word_end)
const = 0;
255 curr_word_end, curr_unichar_id))
return 0;
256 if (unichar_id > curr_unichar_id)
return 1;
257 if (unichar_id == curr_unichar_id) {
258 if (
next_node > curr_next_node)
return 1;
260 if (word_end > curr_word_end)
return 1;
274 return ((unichar_id == other_unichar_id) &&
276 (!word_end || (word_end == other_word_end)));
281 void init(
int unicharset_size);
385 const char *debug_msg) {
387 if (
data_[i] == new_pos)
return false;
418 num_forward_edges_in_node0 = num_forward_edges(0);
425 num_edges_(num_edges) {
426 init(unicharset_size);
427 num_forward_edges_in_node0 = num_forward_edges(0);
428 if (debug_level > 3) print_all(
"SquishedDawg:");
434 if (!read_squished_dawg(fp))
return false;
435 num_forward_edges_in_node0 = num_forward_edges(0);
443 bool word_end)
const override;
448 bool word_end)
const override {
450 if (!edge_occupied(edge) || edge == NO_EDGE)
return;
451 assert(forward_edge(edge));
456 }
while (!last_edge(edge++));
489 tprintf(
"Error serializing %s\n", filename);
493 tprintf(
"Error writing file %s\n", filename);
505 inline void set_empty_edge(
EDGE_REF edge_ref) {
509 inline void clear_all_edges() {
510 for (
int edge = 0; edge < num_edges_; edge++) set_empty_edge(edge);
513 inline void clear_marker_flag(
EDGE_REF edge_ref) {
517 inline bool forward_edge(
EDGE_REF edge_ref)
const {
518 return (edge_occupied(edge_ref) &&
522 inline bool backward_edge(
EDGE_REF edge_ref)
const {
523 return (edge_occupied(edge_ref) &&
527 inline bool edge_occupied(
EDGE_REF edge_ref)
const {
531 inline bool last_edge(
EDGE_REF edge_ref)
const {
536 int32_t num_forward_edges(
NODE_REF node)
const;
539 bool read_squished_dawg(TFile *file);
542 void print_edge(
EDGE_REF edge)
const;
545 void print_all(
const char* msg) {
546 tprintf(
"\n__________________________\n%s\n", msg);
547 for (
int i = 0; i < num_edges_; ++i) print_edge(i);
548 tprintf(
"__________________________\n");
551 std::unique_ptr<EDGE_REF[]> build_node_map(int32_t *num_nodes)
const;
555 int32_t num_edges_ = 0;
556 int num_forward_edges_in_node0 = 0;
561 #endif // DICT_DAWG_H_
int given_greater_than_edge_rec(NODE_REF next_node, bool word_end, UNICHAR_ID unichar_id, const EDGE_RECORD &edge_rec) const
NodeChild(UNICHAR_ID id, EDGE_REF ref)
SquishedDawg(const char *filename, DawgType type, const STRING &lang, PermuterType perm, int debug_level)
void iterate_words_rec(const WERD_CHOICE &word_so_far, NODE_REF to_explore, TessCallback1< const WERD_CHOICE * > *cb) const
PermuterType perm_
Permuter code that should be used if the word is found in this Dawg.
SquishedDawg(EDGE_ARRAY edges, int num_edges, DawgType type, const STRING &lang, PermuterType perm, int unicharset_size, int debug_level)
void init(int unicharset_size)
bool end_of_word(EDGE_REF edge_ref) const override
virtual UNICHAR_ID edge_letter(EDGE_REF edge_ref) const =0
Returns UNICHAR_ID stored in the edge indicated by the given EDGE_REF.
SquishedDawg(DawgType type, const STRING &lang, PermuterType perm, int debug_level)
bool match_words(WERD_CHOICE *word, int32_t index, NODE_REF node, UNICHAR_ID wildcard) const
bool add_unique(const DawgPosition &new_pos, bool debug, const char *debug_msg)
bool end_of_word_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns true if this edge marks the end of a word.
UNICHAR_ID unichar_id_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns UNICHAR_ID recorded in this edge.
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.
bool write_squished_dawg(const char *filename)
bool word_in_dawg(const WERD_CHOICE &word) const
Returns true if the given word is in the Dawg.
virtual bool end_of_word(EDGE_REF edge_ref) const =0
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
NODE_REF next_node_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns the next node visited by following this edge.
void print_node(NODE_REF node, int max_num_edges) const override
DawgPosition(int dawg_idx, EDGE_REF dawgref, int punc_idx, EDGE_REF puncref, bool backtopunc)
const STRING & lang() const
void unichar_ids_of(NODE_REF node, NodeChildVector *vec, bool word_end) const override
virtual NODE_REF next_node(EDGE_REF edge_ref) const =0
DLLSYM void tprintf(const char *format,...)
UNICHAR_ID edge_letter(EDGE_REF edge_ref) const override
Returns UNICHAR_ID stored in the edge indicated by the given EDGE_REF.
static const UNICHAR_ID kPatternUnicharID
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.
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.
virtual void print_node(NODE_REF node, int max_num_edges) const =0
virtual EDGE_REF pattern_loop_edge(EDGE_REF edge_ref, UNICHAR_ID unichar_id, bool word_end) const
EDGE_REF edge_char_of(NODE_REF node, UNICHAR_ID unichar_id, bool word_end) const override
Returns the edge that corresponds to the letter out of this node.
bool CloseWrite(const STRING &filename, FileWriter writer)
Dawg(DawgType type, const STRING &lang, PermuterType perm, int debug_level)
bool write_squished_dawg(TFile *file)
Writes the squished/reduced Dawg to a file.
virtual void unichar_ids_of(NODE_REF node, NodeChildVector *vec, bool word_end) const =0
void iterate_words(const UNICHARSET &unicharset, TessCallback1< const WERD_CHOICE * > *cb) const
void OpenWrite(GenericVector< char > *data)
int direction_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns the direction flag of this edge.
bool operator==(const DawgPosition &other)
int check_for_words(const char *filename, const UNICHARSET &unicharset, bool enable_wildcard) const
bool prefix_in_dawg(const WERD_CHOICE &prefix, bool requires_complete) const
bool Open(const STRING &filename, FileReader reader)
NODE_REF next_node(EDGE_REF edge) const override
PermuterType permuter() const
virtual void unichar_id_to_patterns(UNICHAR_ID unichar_id, const UNICHARSET &unicharset, GenericVector< UNICHAR_ID > *vec) const
bool marker_flag_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns the marker flag of this edge.
static const int16_t kDawgMagicNumber
Magic number to determine endianness when reading the Dawg from file.
int push_back(DawgPosition object)