29 #pragma warning(disable:4244) // Conversion warnings 30 #pragma warning(disable:4800) // int/bool warnings 78 " direction %d word_end %d unichar_id %d, exploring node:\n",
80 if (node_ref != NO_EDGE) {
84 if (node_ref == NO_EDGE)
return false;
87 nodes_[node_ref]->forward_edges :
nodes_[node_ref]->backward_edges;
88 int vec_size = vec.
size();
94 while (start <= end) {
95 k = (start + end) >> 1;
99 *edge_ptr = &(vec[k]);
102 }
else if (compare == 1) {
109 for (
int i = 0; i < vec_size; ++i) {
115 *edge_ptr = &(edge_rec);
128 &(
nodes_[node1]->forward_edges) : &(
nodes_[node1]->backward_edges);
132 while (search_index < vec->size() &&
134 (*vec)[search_index]) == 1) {
138 search_index = vec->
size();
145 (*vec)[edge_index] = edge_rec;
146 }
else if (search_index < vec->size()) {
147 vec->
insert(edge_rec, search_index);
167 unichar_id, &back_edge_ptr, &back_edge_index));
179 if (word.
length() <= 0)
return false;
182 for (
int i = 0; i < word.
length(); ++i) {
190 bool marker_flag =
false;
193 inT32 still_finding_chars =
true;
194 inT32 word_end =
false;
195 bool add_failed =
false;
201 for (i = 0; i < word.
length() - 1; ++i) {
203 marker_flag = (repetitions != NULL) ? (*repetitions)[i] :
false;
205 if (still_finding_chars) {
207 unichar_id, &edge_ptr, &edge_index);
210 edge_index, last_node);
213 still_finding_chars =
false;
223 still_finding_chars =
false;
231 if (!still_finding_chars) {
235 if (the_next_node == 0) {
240 marker_flag, word_end, unichar_id)) {
245 last_node = the_next_node;
250 marker_flag = (repetitions != NULL) ? (*repetitions)[i] :
false;
252 if (still_finding_chars &&
254 unichar_id, &edge_ptr, &edge_index)) {
258 marker_flag, unichar_id);
264 !
add_new_edge(last_node, the_next_node, marker_flag,
true, unichar_id))
268 tprintf(
"Re-initializing document dictionary...\n");
283 static int sort_strings_by_dec_length(
const void* v1,
const void* v2) {
295 word_list.
sort(sort_strings_by_dec_length);
308 if (word_file == NULL)
return false;
320 tprintf(
"Read %d words so far\n", word_count);
324 tprintf(
"Skipping invalid word %s\n",
string);
329 tprintf(
"Read %d words total.\n", word_count);
336 for (
int i = 0; i < words.
size(); ++i) {
341 tprintf(
"Error: word '%s' not in DAWG after adding it\n",
370 bool is_alpha = unicharset.
get_isalpha(unichar_id);
392 }
else if (ch ==
'd') {
394 }
else if (ch ==
'n') {
396 }
else if (ch ==
'p') {
398 }
else if (ch ==
'a') {
400 }
else if (ch ==
'A') {
403 return INVALID_UNICHAR_ID;
410 tprintf(
"please call initialize_patterns() before read_pattern_list()\n");
414 FILE *pattern_file = fopen(
filename,
"rb");
415 if (pattern_file == NULL) {
420 int pattern_count = 0;
428 const char *str_ptr = string;
429 int step = unicharset.
step(str_ptr);
432 UNICHAR_ID curr_unichar_id = INVALID_UNICHAR_ID;
433 if (step == 1 && *str_ptr ==
'\\') {
435 if (*str_ptr ==
'\\') {
439 tprintf(
"Please provide at least %d concrete characters at the" 450 if (curr_unichar_id == INVALID_UNICHAR_ID) {
457 step = unicharset.
step(str_ptr);
459 if (step == 1 && *str_ptr ==
'\\' && *(str_ptr+1) ==
'*') {
460 repetitions_vec[repetitions_vec.
size()-1] =
true;
462 step = unicharset.
step(str_ptr);
466 tprintf(
"Invalid user pattern %s\n",
string);
471 tprintf(
"Inserting expanded user pattern %s\n",
477 tprintf(
"Error: failed to insert pattern '%s'\n",
string);
485 fclose(pattern_file);
494 unichar_id, &edge_ptr, &edge_index));
502 }
else if (node1 == 0) {
530 for (
int i = 0; i <
nodes_.
size(); i++) reduced_nodes[i] = 0;
532 delete[] reduced_nodes;
543 node_ref_map[i+1] = node_ref_map[i] +
nodes_[i]->forward_edges.
size();
545 int num_forward_edges = node_ref_map[i];
554 for (j = 0; j < end; ++j) {
565 delete[] node_ref_map;
575 tprintf(
"\nCollapsing node %d:\n", node);
599 curr_word_end, curr_unichar_id);
602 curr_word_end, curr_unichar_id,
603 &edge_ptr, &edge_index));
610 next_node2_num_edges, next_node2);
626 bool did_something =
false;
627 for (
int i = edge_index; i < backward_edges->
size() - 1; ++i) {
629 UNICHAR_ID curr_unichar_id = INVALID_UNICHAR_ID;
630 while (i < backward_edges->size()) {
631 if (!
DeadEdge((*backward_edges)[i])) {
633 if (curr_unichar_id != unichar_id)
return did_something;
638 if (i == backward_edges->
size())
break;
639 const EDGE_RECORD &edge_rec = (*backward_edges)[i];
641 for (
int j = i + 1; j < backward_edges->
size(); ++j) {
642 const EDGE_RECORD &next_edge_rec = (*backward_edges)[j];
643 if (
DeadEdge(next_edge_rec))
continue;
645 if (next_id != unichar_id)
break;
651 did_something =
true;
656 return did_something;
660 int num_edges = edges->
size();
661 if (num_edges <= 1)
return;
664 for (
int i = 0; i < num_edges; ++i) {
669 for (
int i = 0; i < num_edges; ++i)
670 (*edges)[i] = sort_vec[i].data;
683 while (edge_index < backward_edges.
size()) {
684 if (
DeadEdge(backward_edges[edge_index]))
continue;
688 &backward_edges, reduced_nodes));
689 while (++edge_index < backward_edges.
size()) {
691 if (!
DeadEdge(backward_edges[edge_index]) &&
id != unichar_id)
break;
694 reduced_nodes[node] =
true;
701 for (
int i = 0; i < backward_edges.
size(); ++i) {
702 if (
DeadEdge(backward_edges[i]))
continue;
711 if (node == NO_EDGE)
return;
716 for (
int dir = 0; dir < 2; ++dir) {
725 for (i = 0; (dir == 0 ? i < num_fwd : i < num_bkw) &&
726 i < max_num_edges; ++i) {
731 if (dir == 0 ? i < num_fwd : i < num_bkw)
tprintf(
"...");
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.
static const char kAlphanumPatternUnicode[]
void add_word_ending(EDGE_RECORD *edge, NODE_REF the_next_node, bool repeats, UNICHAR_ID unichar_id)
bool marker_flag_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns the marker flag of this edge.
void print_node(NODE_REF node, int max_num_edges) const
bool read_pattern_list(const char *filename, const UNICHARSET &unicharset)
bool get_isalpha(UNICHAR_ID unichar_id) const
#define MAX_NODE_EDGES_DISPLAY
bool read_word_list(const char *filename, const UNICHARSET &unicharset, Trie::RTLReversePolicy reverse_policy, GenericVector< STRING > *words)
void initialize_patterns(UNICHARSET *unicharset)
static const int kSaneNumConcreteChars
static const char kUpperPatternUnicode[]
UNICHAR_ID alpha_pattern_
const STRING & unichar_string() const
bool read_and_add_word_list(const char *filename, const UNICHARSET &unicharset, Trie::RTLReversePolicy reverse)
UNICHAR_ID unichar_id(int index) const
void KillEdge(EDGE_RECORD *edge_rec) const
NODE_REF next_node(EDGE_REF edge_ref) const
void print_all(const char *msg, int max_num_edges)
void insert(T t, int index)
bool add_word_to_dawg(const WERD_CHOICE &word, const GenericVector< bool > *repetitions)
bool contains_unichar_id(UNICHAR_ID unichar_id) const
UNICHAR_ID character_class_to_pattern(char ch)
UNICHAR_ID upper_pattern_
void unichar_id_to_patterns(UNICHAR_ID unichar_id, const UNICHARSET &unicharset, GenericVector< UNICHAR_ID > *vec) 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.
bool DeadEdge(const EDGE_RECORD &edge_rec) const
static const char kPuncPatternUnicode[]
static const char kAlphaPatternUnicode[]
bool get_ispunctuation(UNICHAR_ID unichar_id) const
UNICHAR_ID digit_pattern_
bool add_edge_linkage(NODE_REF node1, NODE_REF node2, bool repeats, int direction, bool word_end, UNICHAR_ID unichar_id)
static const char kDigitPatternUnicode[]
bool add_new_edge(NODE_REF node1, NODE_REF node2, bool repeats, bool word_end, UNICHAR_ID unichar_id)
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
static const char kLowerPatternUnicode[]
UNICHAR_ID unichar_id_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns UNICHAR_ID recorded in this edge.
void print_edge_rec(const EDGE_RECORD &edge_rec) const
void remove_edge_linkage(NODE_REF node1, NODE_REF node2, int direction, bool word_end, UNICHAR_ID unichar_id)
bool get_isupper(UNICHAR_ID unichar_id) const
EDGE_REF edge_char_of(NODE_REF node_ref, UNICHAR_ID unichar_id, bool word_end) const
const char *const RTLReversePolicyNames[]
void link_edge(EDGE_RECORD *edge, NODE_REF nxt, bool repeats, int direction, bool word_end, UNICHAR_ID unichar_id)
void reverse_and_mirror_unichar_ids()
void delete_data_pointers()
void TESS_API unichar_insert(const char *const unichar_repr)
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.
int direction(EDGEPT *point)
UNICHAR_ID TESS_API unichar_to_id(const char *const unichar_repr) const
bool add_word_list(const GenericVector< STRING > &words, const UNICHARSET &unicharset)
const char kDoNotReverse[]
UNICHAR_ID alphanum_pattern_
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
bool reduce_lettered_edges(EDGE_INDEX edge_index, UNICHAR_ID unichar_id, NODE_REF node, EDGE_VECTOR *backward_edges, NODE_MARKER reduced_nodes)
static const char * get_reverse_policy_name(RTLReversePolicy reverse_policy)
const char kReverseIfHasRTL[]
NODE_REF next_node_from_edge_rec(const EDGE_RECORD &edge_rec) const
Returns the next node visited by following this edge.
bool eliminate_redundant_edges(NODE_REF node, const EDGE_RECORD &edge1, const EDGE_RECORD &edge2)
bool word_in_dawg(const WERD_CHOICE &word) const
Returns true if the given word is in the Dawg.
SquishedDawg * trie_to_dawg()
void remove_edge(NODE_REF node1, NODE_REF node2, bool word_end, UNICHAR_ID unichar_id)
bool get_isdigit(UNICHAR_ID unichar_id) const
EDGE_VECTOR backward_edges
bool get_islower(UNICHAR_ID unichar_id) const
bool initialized_patterns_
const char kForceReverse[]
bool has_rtl_unichar_id() const
void reduce_node_input(NODE_REF node, NODE_MARKER reduced_nodes)
const STRING debug_string() const
EDGE_VECTOR forward_edges
void chomp_string(char *str)
UNICHAR_ID lower_pattern_
GenericVector< EDGE_INDEX > root_back_freelist_
void sort_edges(EDGE_VECTOR *edges)
bool can_be_eliminated(const EDGE_RECORD &edge_rec)
int step(const char *str) const
void append_unichar_id(UNICHAR_ID unichar_id, int blob_count, float rating, float certainty)