30 #pragma warning(disable:4244) // Conversion warnings 31 #pragma warning(disable:4800) // int/bool warnings 49 bool requires_complete)
const {
50 if (word.
length() == 0)
return !requires_complete;
52 int end_index = word.
length() - 1;
53 for (
int i = 0; i < end_index; i++) {
55 if (edge == NO_EDGE) {
75 bool enable_wildcard)
const {
91 enable_wildcard ? wildcard : INVALID_UNICHAR_ID)) {
92 tprintf(
"Missing word: %s\n",
string);
96 tprintf(
"Failed to create a valid word from %s\n",
string);
131 for (
int i = 0; i < children.
size(); i++) {
149 if (wildcard != INVALID_UNICHAR_ID && word->
unichar_id(index) == wildcard) {
150 bool any_matched =
false;
153 for (
int i = 0; i < vec.
size(); ++i) {
161 word_end = index == word->
length() - 1;
163 if (edge != NO_EDGE) {
168 }
else if (node != 0) {
177 PermuterType perm,
int unicharset_size,
int debug_level) {
203 bool word_end)
const {
207 EDGE_REF end = num_forward_edges_in_node0 - 1;
209 while (start <= end) {
210 edge = (start + end) >> 1;
212 unichar_id, edges_[edge]);
215 }
else if (compare == 1) {
222 if (edge != NO_EDGE && edge_occupied(edge)) {
227 }
while (!last_edge(edge++));
237 if (forward_edge (edge)) {
240 }
while (!last_edge(edge++));
247 if (node == NO_EDGE)
return;
250 const char *forward_string =
"FORWARD";
251 const char *backward_string =
" ";
253 const char *last_string =
"LAST";
254 const char *not_last_string =
" ";
256 const char *eow_string =
"EOW";
257 const char *not_eow_string =
" ";
265 if (edge_occupied(edge)) {
268 forward_edge(edge) ? forward_string : backward_string;
269 is_last = last_edge(edge) ? last_string : not_last_string;
270 eow =
end_of_word(edge) ? eow_string : not_eow_string;
277 if (edge - node > max_num_edges)
return;
278 }
while (!last_edge(edge++));
280 if (edge < num_edges_ &&
281 edge_occupied(edge) && backward_edge(edge)) {
284 forward_edge(edge) ? forward_string : backward_string;
285 is_last = last_edge(edge) ? last_string : not_last_string;
286 eow =
end_of_word(edge) ? eow_string : not_eow_string;
290 ", unichar_id = %d, %s %s %s\n",
295 }
while (!last_edge(edge++));
304 void SquishedDawg::print_edge(
EDGE_REF edge)
const {
305 if (edge == NO_EDGE) {
309 ", unichar_id = '%d', %s %s %s\n", edge,
311 (forward_edge(edge) ?
"FORWARD" :
" "),
312 (last_edge(edge) ?
"LAST" :
" "),
317 void SquishedDawg::read_squished_dawg(FILE *file,
322 if (debug_level)
tprintf(
"Reading squished dawg\n");
327 fread(&magic,
sizeof(
inT16), 1, file);
331 fread(&unicharset_size,
sizeof(
inT32), 1, file);
332 fread(&num_edges_,
sizeof(
inT32), 1, file);
335 ReverseN(&unicharset_size,
sizeof(unicharset_size));
336 ReverseN(&num_edges_,
sizeof(num_edges_));
342 fread(&edges_[0],
sizeof(
EDGE_RECORD), num_edges_, file);
345 for (edge = 0; edge < num_edges_; ++edge) {
346 ReverseN(&edges_[edge],
sizeof(edges_[edge]));
349 if (debug_level > 2) {
350 tprintf(
"type: %d lang: %s perm: %d unicharset_size: %d num_edges: %d\n",
352 for (edge = 0; edge < num_edges_; ++edge)
357 NODE_MAP SquishedDawg::build_node_map(
inT32 *num_nodes)
const {
365 for (edge = 0; edge < num_edges_; edge++)
366 node_map [edge] = -1;
368 node_counter = num_forward_edges(0);
371 for (edge = 0; edge < num_edges_; edge++) {
373 if (forward_edge(edge)) {
375 node_map[edge] = (edge ? node_counter : 0);
376 num_edges = num_forward_edges(edge);
377 if (edge != 0) node_counter += num_edges;
379 if (edge >= num_edges_)
break;
380 if (backward_edge(edge))
while (!last_edge(edge++));
390 inT32 node_count = 0;
397 node_map = build_node_map(&node_count);
401 fwrite(&magic,
sizeof(
inT16), 1, file);
406 for (edge=0; edge < num_edges_; edge++)
407 if (forward_edge(edge))
410 fwrite(&num_edges,
sizeof(
inT32), 1, file);
413 tprintf(
"%d nodes in DAWG\n", node_count);
414 tprintf(
"%d edges in DAWG\n", num_edges);
417 for (edge = 0; edge < num_edges_; edge++) {
418 if (forward_edge(edge)) {
421 set_next_node(edge, node_map[old_index]);
422 temp_record = edges_[edge];
423 fwrite(&(temp_record),
sizeof(
EDGE_RECORD), 1, file);
424 set_next_node(edge, old_index);
425 }
while (!last_edge(edge++));
427 if (edge >= num_edges_)
break;
428 if (backward_edge(edge))
429 while (!last_edge(edge++));
bool prefix_in_dawg(const WERD_CHOICE &prefix, bool requires_complete) const
void string_and_lengths(STRING *word_str, STRING *word_lengths_str) 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.
#define MAX_NODE_EDGES_DISPLAY
NODE_REF next_node(EDGE_REF edge) const
FILE * open_file(const char *filename, const char *mode)
virtual bool end_of_word(EDGE_REF edge_ref) const =0
UNICHAR_ID unichar_id(int index) const
_ConstTessMemberResultCallback_0_0< false, R, T1 >::base * NewPermanentTessCallback(const T1 *obj, R(T2::*member)() const)
void set_unichar_id(UNICHAR_ID unichar_id, int index)
void write_squished_dawg(FILE *file)
Writes the squished/reduced Dawg to a file.
void print_node(NODE_REF node, int max_num_edges) const
bool contains_unichar_id(UNICHAR_ID unichar_id) const
static const inT16 kDawgMagicNumber
Magic number to determine endianness when reading the Dawg from file.
void CallWithUTF8(TessCallback1< const char *> *cb, const WERD_CHOICE *wc)
bool match_words(WERD_CHOICE *word, inT32 index, NODE_REF node, UNICHAR_ID wildcard) const
int given_greater_than_edge_rec(NODE_REF next_node, bool word_end, UNICHAR_ID unichar_id, const EDGE_RECORD &edge_rec) const
PermuterType perm_
Permuter code that should be used if the word is found in this Dawg.
UNICHAR_ID edge_letter(EDGE_REF edge_ref) const
Returns UNICHAR_ID stored in the edge indicated by the given EDGE_REF.
int check_for_words(const char *filename, const UNICHARSET &unicharset, bool enable_wildcard) 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.
const char * string() const
UNICHAR_ID unichar_id_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns UNICHAR_ID recorded in this edge.
void iterate_words(const UNICHARSET &unicharset, TessCallback1< const WERD_CHOICE *> *cb) const
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 NODE_REF next_node(EDGE_REF edge_ref) const =0
int direction(EDGEPT *point)
UNICHAR_ID TESS_API unichar_to_id(const char *const unichar_repr) const
bool end_of_word(EDGE_REF edge_ref) const
NODE_REF next_node_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns the next node visited by following this edge.
bool word_in_dawg(const WERD_CHOICE &word) const
Returns true if the given word is in the Dawg.
void iterate_words_rec(const WERD_CHOICE &word_so_far, NODE_REF to_explore, TessCallback1< const WERD_CHOICE *> *cb) const
virtual void unichar_ids_of(NODE_REF node, NodeChildVector *vec, bool word_end) const =0
void init(DawgType type, const STRING &lang, PermuterType perm, int unicharset_size, int debug_level)
const STRING & lang() const
void ReverseN(void *ptr, int num_bytes)
void chomp_string(char *str)
void append_unichar_id(UNICHAR_ID unichar_id, int blob_count, float rating, float certainty)