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 {
76 if (filename == NULL)
return 0;
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) {
197 bool word_end)
const {
201 EDGE_REF end = num_forward_edges_in_node0 - 1;
203 while (start <= end) {
204 edge = (start + end) >> 1;
206 unichar_id, edges_[edge]);
209 }
else if (compare == 1) {
216 if (edge != NO_EDGE && edge_occupied(edge)) {
221 }
while (!last_edge(edge++));
231 if (forward_edge (edge)) {
234 }
while (!last_edge(edge++));
241 if (node == NO_EDGE)
return;
244 const char *forward_string =
"FORWARD";
245 const char *backward_string =
" ";
247 const char *last_string =
"LAST";
248 const char *not_last_string =
" ";
250 const char *eow_string =
"EOW";
251 const char *not_eow_string =
" ";
259 if (edge_occupied(edge)) {
262 forward_edge(edge) ? forward_string : backward_string;
263 is_last = last_edge(edge) ? last_string : not_last_string;
264 eow =
end_of_word(edge) ? eow_string : not_eow_string;
269 direction, is_last, eow);
271 if (edge - node > max_num_edges)
return;
272 }
while (!last_edge(edge++));
274 if (edge < num_edges_ &&
275 edge_occupied(edge) && backward_edge(edge)) {
278 forward_edge(edge) ? forward_string : backward_string;
279 is_last = last_edge(edge) ? last_string : not_last_string;
280 eow =
end_of_word(edge) ? eow_string : not_eow_string;
284 ", unichar_id = %d, %s %s %s\n",
286 direction, is_last, eow);
289 }
while (!last_edge(edge++));
298 void SquishedDawg::print_edge(
EDGE_REF edge)
const {
299 if (edge == NO_EDGE) {
303 ", unichar_id = '%d', %s %s %s\n", edge,
305 (forward_edge(edge) ?
"FORWARD" :
" "),
306 (last_edge(edge) ?
"LAST" :
" "),
311 bool SquishedDawg::read_squished_dawg(
TFile *file) {
317 if (file->
FReadEndian(&magic,
sizeof(magic), 1) != 1)
return false;
323 inT32 unicharset_size;
324 if (file->
FReadEndian(&unicharset_size,
sizeof(unicharset_size), 1) != 1)
326 if (file->
FReadEndian(&num_edges_,
sizeof(num_edges_), 1) != 1)
return false;
331 if (file->
FReadEndian(&edges_[0],
sizeof(edges_[0]), num_edges_) !=
335 tprintf(
"type: %d lang: %s perm: %d unicharset_size: %d num_edges: %d\n",
337 for (
EDGE_REF edge = 0; edge < num_edges_; ++edge) print_edge(edge);
342 NODE_MAP SquishedDawg::build_node_map(
inT32 *num_nodes)
const {
350 for (edge = 0; edge < num_edges_; edge++)
351 node_map [edge] = -1;
353 node_counter = num_forward_edges(0);
356 for (edge = 0; edge < num_edges_; edge++) {
358 if (forward_edge(edge)) {
360 node_map[edge] = (edge ? node_counter : 0);
361 num_edges = num_forward_edges(edge);
362 if (edge != 0) node_counter += num_edges;
364 if (edge >= num_edges_)
break;
365 if (backward_edge(edge))
while (!last_edge(edge++));
375 inT32 node_count = 0;
382 node_map = build_node_map(&node_count);
386 fwrite(&magic,
sizeof(
inT16), 1, file);
391 for (edge=0; edge < num_edges_; edge++)
392 if (forward_edge(edge))
395 fwrite(&num_edges,
sizeof(
inT32), 1, file);
398 tprintf(
"%d nodes in DAWG\n", node_count);
399 tprintf(
"%d edges in DAWG\n", num_edges);
402 for (edge = 0; edge < num_edges_; edge++) {
403 if (forward_edge(edge)) {
406 set_next_node(edge, node_map[old_index]);
407 temp_record = edges_[edge];
408 fwrite(&(temp_record),
sizeof(
EDGE_RECORD), 1, file);
409 set_next_node(edge, old_index);
410 }
while (!last_edge(edge++));
412 if (edge >= num_edges_)
break;
413 if (backward_edge(edge))
414 while (!last_edge(edge++));
virtual NODE_REF next_node(EDGE_REF edge_ref) const =0
UNICHAR_ID unichar_id(int index) 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.
_ConstTessMemberResultCallback_0_0< false, R, T1 >::base * NewPermanentTessCallback(const T1 *obj, R(T2::*member)() 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.
bool match_words(WERD_CHOICE *word, inT32 index, NODE_REF node, UNICHAR_ID wildcard) const
void iterate_words(const UNICHARSET &unicharset, TessCallback1< const WERD_CHOICE *> *cb) const
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.
int direction(EDGEPT *point)
const char * string() const
int FReadEndian(void *buffer, int size, int count)
void init(int unicharset_size)
NODE_REF next_node_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns the next node visited by following this edge.
void append_unichar_id(UNICHAR_ID unichar_id, int blob_count, float rating, float certainty)
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
bool word_in_dawg(const WERD_CHOICE &word) const
Returns true if the given word is in the Dawg.
void string_and_lengths(STRING *word_str, STRING *word_lengths_str) const
virtual bool end_of_word(EDGE_REF edge_ref) const =0
FILE * open_file(const char *filename, const char *mode)
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.
void chomp_string(char *str)
#define MAX_NODE_EDGES_DISPLAY
void CallWithUTF8(TessCallback1< const char *> *cb, const WERD_CHOICE *wc)
bool contains_unichar_id(UNICHAR_ID unichar_id) const
int given_greater_than_edge_rec(NODE_REF next_node, bool word_end, UNICHAR_ID unichar_id, const EDGE_RECORD &edge_rec) const
void set_unichar_id(UNICHAR_ID unichar_id, int index)
bool prefix_in_dawg(const WERD_CHOICE &prefix, bool requires_complete) const
static const inT16 kDawgMagicNumber
Magic number to determine endianness when reading the Dawg from file.
void write_squished_dawg(FILE *file)
Writes the squished/reduced Dawg to a file.
PermuterType perm_
Permuter code that should be used if the word is found in this Dawg.
UNICHAR_ID unichar_to_id(const char *const unichar_repr) const
void print_node(NODE_REF node, int max_num_edges) const
UNICHAR_ID unichar_id_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns UNICHAR_ID recorded in this edge.