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 std::unique_ptr<EDGE_REF[]> SquishedDawg::build_node_map(
343 inT32 *num_nodes)
const {
345 std::unique_ptr<EDGE_REF[]> node_map(
new EDGE_REF[num_edges_]);
349 for (edge = 0; edge < num_edges_; edge++)
352 node_counter = num_forward_edges(0);
355 for (edge = 0; edge < num_edges_; edge++) {
357 if (forward_edge(edge)) {
359 node_map[edge] = (edge ? node_counter : 0);
360 num_edges = num_forward_edges(edge);
361 if (edge != 0) node_counter += num_edges;
363 if (edge >= num_edges_)
break;
364 if (backward_edge(edge))
while (!last_edge(edge++));
374 inT32 node_count = 0;
380 std::unique_ptr<EDGE_REF[]> node_map(build_node_map(&node_count));
384 if (file->
FWrite(&magic,
sizeof(magic), 1) != 1)
return false;
390 for (edge=0; edge < num_edges_; edge++)
391 if (forward_edge(edge))
395 if (file->
FWrite(&num_edges,
sizeof(num_edges), 1) != 1)
return false;
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 if (file->
FWrite(&temp_record,
sizeof(temp_record), 1) != 1)
410 set_next_node(edge, old_index);
411 }
while (!last_edge(edge++));
413 if (edge >= num_edges_)
break;
414 if (backward_edge(edge))
415 while (!last_edge(edge++));
#define MAX_NODE_EDGES_DISPLAY
int FWrite(const void *buffer, int size, int count)
void iterate_words_rec(const WERD_CHOICE &word_so_far, NODE_REF to_explore, TessCallback1< const WERD_CHOICE *> *cb) const
bool write_squished_dawg(TFile *file)
Writes the squished/reduced Dawg to a file.
NODE_REF next_node_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns the next node visited by following this edge.
void CallWithUTF8(TessCallback1< const char *> *cb, const WERD_CHOICE *wc)
bool end_of_word_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns true if this edge marks the end of a word.
virtual bool end_of_word(EDGE_REF edge_ref) const =0
_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)
FILE * open_file(const char *filename, const char *mode)
static const inT16 kDawgMagicNumber
Magic number to determine endianness when reading the Dawg from file.
UNICHAR_ID unichar_id(int index) const
int check_for_words(const char *filename, const UNICHARSET &unicharset, bool enable_wildcard) const
int direction(EDGEPT *point)
void append_unichar_id(UNICHAR_ID unichar_id, int blob_count, float rating, float certainty)
void chomp_string(char *str)
PermuterType perm_
Permuter code that should be used if the word is found in this Dawg.
const char * string() const
void iterate_words(const UNICHARSET &unicharset, TessCallback1< const WERD_CHOICE *> *cb) const
void init(int unicharset_size)
int given_greater_than_edge_rec(NODE_REF next_node, bool word_end, UNICHAR_ID unichar_id, const EDGE_RECORD &edge_rec) 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.
UNICHAR_ID unichar_to_id(const char *const unichar_repr) const
void print_node(NODE_REF node, int max_num_edges) const
bool contains_unichar_id(UNICHAR_ID unichar_id) const
UNICHAR_ID unichar_id_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns UNICHAR_ID recorded in this edge.
bool word_in_dawg(const WERD_CHOICE &word) const
Returns true if the given word is in the Dawg.
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.
int FReadEndian(void *buffer, int size, int count)
virtual NODE_REF next_node(EDGE_REF edge_ref) const =0
void string_and_lengths(STRING *word_str, STRING *word_lengths_str) const
virtual void unichar_ids_of(NODE_REF node, NodeChildVector *vec, bool word_end) const =0
bool prefix_in_dawg(const WERD_CHOICE &prefix, bool requires_complete) 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