45 bool requires_complete)
const {
46 if (word.
length() == 0)
return !requires_complete;
48 int end_index = word.
length() - 1;
49 for (
int i = 0; i < end_index; i++) {
51 if (edge == NO_EDGE) {
71 bool enable_wildcard)
const {
72 if (filename ==
nullptr)
return 0;
79 word_file = fopen(filename,
"r");
80 if (word_file ==
nullptr) {
81 tprintf(
"Error: Could not open file %s\n", filename);
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);
120 std::unique_ptr<TessCallback1<const WERD_CHOICE *>> shim(
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++));
227int32_t SquishedDawg::num_forward_edges(
NODE_REF node)
const {
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 =
" ";
253 const char *direction;
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++));
298void 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" :
" "),
311bool SquishedDawg::read_squished_dawg(TFile *file) {
317 if (!file->DeSerialize(&magic))
return false;
323 int32_t unicharset_size;
324 if (!file->DeSerialize(&unicharset_size))
return false;
325 if (!file->DeSerialize(&num_edges_))
return false;
330 if (!file->DeSerialize(&edges_[0], num_edges_))
return false;
332 tprintf(
"type: %d lang: %s perm: %d unicharset_size: %d num_edges: %d\n",
334 for (
EDGE_REF edge = 0; edge < num_edges_; ++edge) print_edge(edge);
339std::unique_ptr<EDGE_REF[]> SquishedDawg::build_node_map(
340 int32_t *num_nodes)
const {
342 std::unique_ptr<EDGE_REF[]> node_map(
new EDGE_REF[num_edges_]);
343 int32_t node_counter;
346 for (edge = 0; edge < num_edges_; edge++)
349 node_counter = num_forward_edges(0);
352 for (edge = 0; edge < num_edges_; edge++) {
354 if (forward_edge(edge)) {
356 node_map[edge] = (edge ? node_counter : 0);
357 num_edges = num_forward_edges(edge);
358 if (edge != 0) node_counter += num_edges;
360 if (edge >= num_edges_)
break;
361 if (backward_edge(edge))
while (!last_edge(edge++));
371 int32_t node_count = 0;
377 std::unique_ptr<EDGE_REF[]> node_map(build_node_map(&node_count));
381 if (!file->
Serialize(&magic))
return false;
386 for (edge=0; edge < num_edges_; edge++)
387 if (forward_edge(edge))
391 if (!file->
Serialize(&num_edges))
return false;
394 tprintf(
"%d nodes in DAWG\n", node_count);
395 tprintf(
"%d edges in DAWG\n", num_edges);
398 for (edge = 0; edge < num_edges_; edge++) {
399 if (forward_edge(edge)) {
402 set_next_node(edge, node_map[old_index]);
403 temp_record = edges_[edge];
404 if (!file->
Serialize(&temp_record))
return false;
405 set_next_node(edge, old_index);
406 }
while (!last_edge(edge++));
408 if (edge >= num_edges_)
break;
409 if (backward_edge(edge))
410 while (!last_edge(edge++));
void chomp_string(char *str)
_ConstTessMemberResultCallback_5_0< false, R, T1, P1, P2, P3, P4, P5 >::base * NewPermanentTessCallback(const T1 *obj, R(T2::*member)(P1, P2, P3, P4, P5) const, typename Identity< P1 >::type p1, typename Identity< P2 >::type p2, typename Identity< P3 >::type p3, typename Identity< P4 >::type p4, typename Identity< P5 >::type p5)
DLLSYM void tprintf(const char *format,...)
#define MAX_NODE_EDGES_DISPLAY
void string_and_lengths(STRING *word_str, STRING *word_lengths_str) const
UNICHAR_ID unichar_id(int index) const
void set_unichar_id(UNICHAR_ID unichar_id, int index)
bool contains_unichar_id(UNICHAR_ID unichar_id) const
void append_unichar_id(UNICHAR_ID unichar_id, int blob_count, float rating, float certainty)
bool Serialize(const char *data, size_t count=1)
const char * string() const
UNICHAR_ID unichar_to_id(const char *const unichar_repr) const
NODE_REF next_node_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns the next node visited by following this edge.
bool prefix_in_dawg(const WERD_CHOICE &prefix, bool requires_complete) const
int given_greater_than_edge_rec(NODE_REF next_node, bool word_end, UNICHAR_ID unichar_id, const EDGE_RECORD &edge_rec) 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.
virtual bool end_of_word(EDGE_REF edge_ref) const =0
void init(int unicharset_size)
bool match_words(WERD_CHOICE *word, int32_t index, NODE_REF node, UNICHAR_ID 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.
void iterate_words_rec(const WERD_CHOICE &word_so_far, NODE_REF to_explore, TessCallback1< const WERD_CHOICE * > *cb) const
UNICHAR_ID unichar_id_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns UNICHAR_ID recorded in this edge.
int check_for_words(const char *filename, const UNICHARSET &unicharset, bool enable_wildcard) const
PermuterType perm_
Permuter code that should be used if the word is found in this 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.
virtual NODE_REF next_node(EDGE_REF edge_ref) const =0
void iterate_words(const UNICHARSET &unicharset, TessCallback1< const WERD_CHOICE * > *cb) const
static const int16_t kDawgMagicNumber
Magic number to determine endianness when reading the Dawg from file.
bool end_of_word(EDGE_REF edge_ref) const override
UNICHAR_ID edge_letter(EDGE_REF edge_ref) const override
Returns UNICHAR_ID stored in the edge indicated by the given EDGE_REF.
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.
NODE_REF next_node(EDGE_REF edge) const override
bool write_squished_dawg(TFile *file)
Writes the squished/reduced Dawg to a file.
void print_node(NODE_REF node, int max_num_edges) const override