tesseract  4.00.00dev
rejctmap.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * File: rejctmap.cpp (Formerly rejmap.c)
3  * Description: REJ and REJMAP class functions.
4  * Author: Phil Cheatle
5  * Created: Thu Jun 9 13:46:38 BST 1994
6  *
7  * (C) Copyright 1994, Hewlett-Packard Ltd.
8  ** Licensed under the Apache License, Version 2.0 (the "License");
9  ** you may not use this file except in compliance with the License.
10  ** You may obtain a copy of the License at
11  ** http://www.apache.org/licenses/LICENSE-2.0
12  ** Unless required by applicable law or agreed to in writing, software
13  ** distributed under the License is distributed on an "AS IS" BASIS,
14  ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  ** See the License for the specific language governing permissions and
16  ** limitations under the License.
17  *
18  **********************************************************************/
19 
20 #include "host.h"
21 #include "rejctmap.h"
22 #include "params.h"
23 
24 BOOL8 REJ::perm_rejected() { //Is char perm reject?
25  return (flag (R_TESS_FAILURE) ||
26  flag (R_SMALL_XHT) ||
27  flag (R_EDGE_CHAR) ||
28  flag (R_1IL_CONFLICT) ||
29  flag (R_POSTNN_1IL) ||
30  flag (R_REJ_CBLOB) ||
32 }
33 
34 
35 BOOL8 REJ::rej_before_nn_accept() {
36  return flag (R_POOR_MATCH) ||
39 }
40 
41 
42 BOOL8 REJ::rej_between_nn_and_mm() {
43  return flag (R_HYPHEN) ||
44  flag (R_DUBIOUS) ||
46 }
47 
48 
49 BOOL8 REJ::rej_between_mm_and_quality_accept() {
50  return flag (R_BAD_QUALITY);
51 }
52 
53 
54 BOOL8 REJ::rej_between_quality_and_minimal_rej_accept() {
55  return flag (R_DOC_REJ) ||
57 }
58 
59 
60 BOOL8 REJ::rej_before_mm_accept() {
61  return rej_between_nn_and_mm () ||
62  (rej_before_nn_accept () &&
64 }
65 
66 
67 BOOL8 REJ::rej_before_quality_accept() {
68  return rej_between_mm_and_quality_accept () ||
69  (!flag (R_MM_ACCEPT) && rej_before_mm_accept ());
70 }
71 
72 
73 BOOL8 REJ::rejected() { //Is char rejected?
75  return FALSE;
76  else
77  return (perm_rejected () ||
78  rej_between_quality_and_minimal_rej_accept () ||
79  (!flag (R_QUALITY_ACCEPT) && rej_before_quality_accept ()));
80 }
81 
82 
83 BOOL8 REJ::accept_if_good_quality() { //potential rej?
84  return (rejected () &&
85  !perm_rejected () &&
86  flag (R_BAD_PERMUTER) &&
87  !flag (R_POOR_MATCH) &&
90  (!rej_between_nn_and_mm () &&
91  !rej_between_mm_and_quality_accept () &&
92  !rej_between_quality_and_minimal_rej_accept ()));
93 }
94 
95 
96 void REJ::setrej_tess_failure() { //Tess generated blank
97  set_flag(R_TESS_FAILURE);
98 }
99 
100 
101 void REJ::setrej_small_xht() { //Small xht char/wd
102  set_flag(R_SMALL_XHT);
103 }
104 
105 
106 void REJ::setrej_edge_char() { //Close to image edge
107  set_flag(R_EDGE_CHAR);
108 }
109 
110 
111 void REJ::setrej_1Il_conflict() { //Initial reject map
112  set_flag(R_1IL_CONFLICT);
113 }
114 
115 
116 void REJ::setrej_postNN_1Il() { //1Il after NN
117  set_flag(R_POSTNN_1IL);
118 }
119 
120 
121 void REJ::setrej_rej_cblob() { //Insert duff blob
122  set_flag(R_REJ_CBLOB);
123 }
124 
125 
126 void REJ::setrej_mm_reject() { //Matrix matcher
127  set_flag(R_MM_REJECT);
128 }
129 
130 
131 void REJ::setrej_bad_repetition() { //Odd repeated char
132  set_flag(R_BAD_REPETITION);
133 }
134 
135 
136 void REJ::setrej_poor_match() { //Failed Rays heuristic
137  set_flag(R_POOR_MATCH);
138 }
139 
140 
142  //TEMP reject_word
143  set_flag(R_NOT_TESS_ACCEPTED);
144 }
145 
146 
148  //TEMP reject_word
149  set_flag(R_CONTAINS_BLANKS);
150 }
151 
152 
153 void REJ::setrej_bad_permuter() { //POTENTIAL reject_word
154  set_flag(R_BAD_PERMUTER);
155 }
156 
157 
158 void REJ::setrej_hyphen() { //PostNN dubious hyphen or .
159  set_flag(R_HYPHEN);
160 }
161 
162 
163 void REJ::setrej_dubious() { //PostNN dubious limit
164  set_flag(R_DUBIOUS);
165 }
166 
167 
168 void REJ::setrej_no_alphanums() { //TEMP reject_word
169  set_flag(R_NO_ALPHANUMS);
170 }
171 
172 
173 void REJ::setrej_mostly_rej() { //TEMP reject_word
174  set_flag(R_MOSTLY_REJ);
175 }
176 
177 
178 void REJ::setrej_xht_fixup() { //xht fixup
179  set_flag(R_XHT_FIXUP);
180 }
181 
182 
183 void REJ::setrej_bad_quality() { //TEMP reject_word
184  set_flag(R_BAD_QUALITY);
185 }
186 
187 
188 void REJ::setrej_doc_rej() { //TEMP reject_word
189  set_flag(R_DOC_REJ);
190 }
191 
192 
193 void REJ::setrej_block_rej() { //TEMP reject_word
194  set_flag(R_BLOCK_REJ);
195 }
196 
197 
198 void REJ::setrej_row_rej() { //TEMP reject_word
199  set_flag(R_ROW_REJ);
200 }
201 
202 
203 void REJ::setrej_unlv_rej() { //TEMP reject_word
204  set_flag(R_UNLV_REJ);
205 }
206 
207 
208 void REJ::setrej_hyphen_accept() { //NN Flipped a char
209  set_flag(R_HYPHEN_ACCEPT);
210 }
211 
212 
213 void REJ::setrej_nn_accept() { //NN Flipped a char
214  set_flag(R_NN_ACCEPT);
215 }
216 
217 
218 void REJ::setrej_mm_accept() { //Matrix matcher
219  set_flag(R_MM_ACCEPT);
220 }
221 
222 
223 void REJ::setrej_quality_accept() { //Quality flip a char
224  set_flag(R_QUALITY_ACCEPT);
225 }
226 
227 
229  //Accept all except blank
230  set_flag(R_MINIMAL_REJ_ACCEPT);
231 }
232 
233 
234 void REJ::full_print(FILE *fp) {
235  fprintf (fp, "R_TESS_FAILURE: %s\n", flag (R_TESS_FAILURE) ? "T" : "F");
236  fprintf (fp, "R_SMALL_XHT: %s\n", flag (R_SMALL_XHT) ? "T" : "F");
237  fprintf (fp, "R_EDGE_CHAR: %s\n", flag (R_EDGE_CHAR) ? "T" : "F");
238  fprintf (fp, "R_1IL_CONFLICT: %s\n", flag (R_1IL_CONFLICT) ? "T" : "F");
239  fprintf (fp, "R_POSTNN_1IL: %s\n", flag (R_POSTNN_1IL) ? "T" : "F");
240  fprintf (fp, "R_REJ_CBLOB: %s\n", flag (R_REJ_CBLOB) ? "T" : "F");
241  fprintf (fp, "R_MM_REJECT: %s\n", flag (R_MM_REJECT) ? "T" : "F");
242  fprintf (fp, "R_BAD_REPETITION: %s\n", flag (R_BAD_REPETITION) ? "T" : "F");
243  fprintf (fp, "R_POOR_MATCH: %s\n", flag (R_POOR_MATCH) ? "T" : "F");
244  fprintf (fp, "R_NOT_TESS_ACCEPTED: %s\n",
245  flag (R_NOT_TESS_ACCEPTED) ? "T" : "F");
246  fprintf (fp, "R_CONTAINS_BLANKS: %s\n",
247  flag (R_CONTAINS_BLANKS) ? "T" : "F");
248  fprintf (fp, "R_BAD_PERMUTER: %s\n", flag (R_BAD_PERMUTER) ? "T" : "F");
249  fprintf (fp, "R_HYPHEN: %s\n", flag (R_HYPHEN) ? "T" : "F");
250  fprintf (fp, "R_DUBIOUS: %s\n", flag (R_DUBIOUS) ? "T" : "F");
251  fprintf (fp, "R_NO_ALPHANUMS: %s\n", flag (R_NO_ALPHANUMS) ? "T" : "F");
252  fprintf (fp, "R_MOSTLY_REJ: %s\n", flag (R_MOSTLY_REJ) ? "T" : "F");
253  fprintf (fp, "R_XHT_FIXUP: %s\n", flag (R_XHT_FIXUP) ? "T" : "F");
254  fprintf (fp, "R_BAD_QUALITY: %s\n", flag (R_BAD_QUALITY) ? "T" : "F");
255  fprintf (fp, "R_DOC_REJ: %s\n", flag (R_DOC_REJ) ? "T" : "F");
256  fprintf (fp, "R_BLOCK_REJ: %s\n", flag (R_BLOCK_REJ) ? "T" : "F");
257  fprintf (fp, "R_ROW_REJ: %s\n", flag (R_ROW_REJ) ? "T" : "F");
258  fprintf (fp, "R_UNLV_REJ: %s\n", flag (R_UNLV_REJ) ? "T" : "F");
259  fprintf (fp, "R_HYPHEN_ACCEPT: %s\n", flag (R_HYPHEN_ACCEPT) ? "T" : "F");
260  fprintf (fp, "R_NN_ACCEPT: %s\n", flag (R_NN_ACCEPT) ? "T" : "F");
261  fprintf (fp, "R_MM_ACCEPT: %s\n", flag (R_MM_ACCEPT) ? "T" : "F");
262  fprintf (fp, "R_QUALITY_ACCEPT: %s\n", flag (R_QUALITY_ACCEPT) ? "T" : "F");
263  fprintf (fp, "R_MINIMAL_REJ_ACCEPT: %s\n",
264  flag (R_MINIMAL_REJ_ACCEPT) ? "T" : "F");
265 }
266 
267 
268 //The REJMAP class has been hacked to use malloc instead of new [].
269 //This is to reduce memory fragmentation only as it is rather kludgy.
270 // malloc by-passes the call to the constructor of REJ on each
271 // array element. Although the constructor is empty, the BITS16 members
272 // do have a constructor which sets all the flags to 0. The memset
273 // replaces this functionality.
274 
275 REJMAP::REJMAP( //classwise copy
276  const REJMAP &source) {
277  REJ *to;
278  REJ *from = source.ptr;
279  int i;
280 
281  len = source.length ();
282 
283  if (len > 0) {
284  ptr = (REJ *) malloc(len * sizeof (REJ));
285  to = ptr;
286  for (i = 0; i < len; i++) {
287  *to = *from;
288  to++;
289  from++;
290  }
291  }
292  else
293  ptr = NULL;
294 }
295 
296 
297 REJMAP & REJMAP::operator= ( //assign REJMAP
298 const REJMAP & source //from this
299 ) {
300  REJ *
301  to;
302  REJ *
303  from = source.ptr;
304  int
305  i;
306 
307  initialise (source.len);
308  to = ptr;
309  for (i = 0; i < len; i++) {
310  *to = *from;
311  to++;
312  from++;
313  }
314  return *this;
315 }
316 
317 
318 void REJMAP::initialise( //Redefine map
319  inT16 length) {
320  free(ptr);
321  len = length;
322  if (len > 0)
323  ptr = (REJ *) calloc(len, sizeof(REJ));
324  else
325  ptr = NULL;
326 }
327 
328 
329 inT16 REJMAP::accept_count() { //How many accepted?
330  int i;
331  inT16 count = 0;
332 
333  for (i = 0; i < len; i++) {
334  if (ptr[i].accepted ())
335  count++;
336  }
337  return count;
338 }
339 
340 
341 BOOL8 REJMAP::recoverable_rejects() { //Any non perm rejs?
342  int i;
343 
344  for (i = 0; i < len; i++) {
345  if (ptr[i].recoverable ())
346  return TRUE;
347  }
348  return FALSE;
349 }
350 
351 
353  int i;
354 
355  for (i = 0; i < len; i++) {
356  if (ptr[i].accept_if_good_quality ())
357  return TRUE;
358  }
359  return FALSE;
360 }
361 
362 
363 void REJMAP::remove_pos( //Cut out an element
364  inT16 pos //element to remove
365  ) {
366  REJ *new_ptr; //new, smaller map
367  int i;
368 
369  ASSERT_HOST (pos >= 0);
370  ASSERT_HOST (pos < len);
371  ASSERT_HOST (len > 0);
372 
373  len--;
374  if (len > 0)
375  new_ptr = (REJ *) malloc(len * sizeof(REJ));
376  else
377  new_ptr = NULL;
378 
379  for (i = 0; i < pos; i++)
380  new_ptr[i] = ptr[i]; //copy pre pos
381 
382  for (; pos < len; pos++)
383  new_ptr[pos] = ptr[pos + 1]; //copy post pos
384 
385  //delete old map
386  free(ptr);
387  ptr = new_ptr;
388 }
389 
390 
391 void REJMAP::print(FILE *fp) {
392  int i;
393  char buff[512];
394 
395  for (i = 0; i < len; i++) {
396  buff[i] = ptr[i].display_char ();
397  }
398  buff[i] = '\0';
399  fprintf (fp, "\"%s\"", buff);
400 }
401 
402 
403 void REJMAP::full_print(FILE *fp) {
404  int i;
405 
406  for (i = 0; i < len; i++) {
407  ptr[i].full_print (fp);
408  fprintf (fp, "\n");
409  }
410 }
411 
412 
413 void REJMAP::rej_word_small_xht() { //Reject whole word
414  int i;
415 
416  for (i = 0; i < len; i++) {
417  ptr[i].setrej_small_xht ();
418  }
419 }
420 
421 
422 void REJMAP::rej_word_tess_failure() { //Reject whole word
423  int i;
424 
425  for (i = 0; i < len; i++) {
426  ptr[i].setrej_tess_failure ();
427  }
428 }
429 
430 
431 void REJMAP::rej_word_not_tess_accepted() { //Reject whole word
432  int i;
433 
434  for (i = 0; i < len; i++) {
435  if (ptr[i].accepted()) ptr[i].setrej_not_tess_accepted();
436  }
437 }
438 
439 
440 void REJMAP::rej_word_contains_blanks() { //Reject whole word
441  int i;
442 
443  for (i = 0; i < len; i++) {
444  if (ptr[i].accepted()) ptr[i].setrej_contains_blanks();
445  }
446 }
447 
448 
449 void REJMAP::rej_word_bad_permuter() { //Reject whole word
450  int i;
451 
452  for (i = 0; i < len; i++) {
453  if (ptr[i].accepted()) ptr[i].setrej_bad_permuter ();
454  }
455 }
456 
457 
458 void REJMAP::rej_word_xht_fixup() { //Reject whole word
459  int i;
460 
461  for (i = 0; i < len; i++) {
462  if (ptr[i].accepted()) ptr[i].setrej_xht_fixup();
463  }
464 }
465 
466 
467 void REJMAP::rej_word_no_alphanums() { //Reject whole word
468  int i;
469 
470  for (i = 0; i < len; i++) {
471  if (ptr[i].accepted()) ptr[i].setrej_no_alphanums();
472  }
473 }
474 
475 
476 void REJMAP::rej_word_mostly_rej() { //Reject whole word
477  int i;
478 
479  for (i = 0; i < len; i++) {
480  if (ptr[i].accepted()) ptr[i].setrej_mostly_rej();
481  }
482 }
483 
484 
485 void REJMAP::rej_word_bad_quality() { //Reject whole word
486  int i;
487 
488  for (i = 0; i < len; i++) {
489  if (ptr[i].accepted()) ptr[i].setrej_bad_quality();
490  }
491 }
492 
493 
494 void REJMAP::rej_word_doc_rej() { //Reject whole word
495  int i;
496 
497  for (i = 0; i < len; i++) {
498  if (ptr[i].accepted()) ptr[i].setrej_doc_rej();
499  }
500 }
501 
502 
503 void REJMAP::rej_word_block_rej() { //Reject whole word
504  int i;
505 
506  for (i = 0; i < len; i++) {
507  if (ptr[i].accepted()) ptr[i].setrej_block_rej();
508  }
509 }
510 
511 
512 void REJMAP::rej_word_row_rej() { //Reject whole word
513  int i;
514 
515  for (i = 0; i < len; i++) {
516  if (ptr[i].accepted()) ptr[i].setrej_row_rej();
517  }
518 }
void setrej_bad_repetition()
Definition: rejctmap.cpp:131
void rej_word_small_xht()
Definition: rejctmap.cpp:413
void setrej_mm_accept()
Definition: rejctmap.cpp:218
BOOL8 flag(REJ_FLAGS rej_flag)
Definition: rejctmap.h:135
void setrej_quality_accept()
Definition: rejctmap.cpp:223
void remove_pos(inT16 pos)
Definition: rejctmap.cpp:363
void rej_word_no_alphanums()
Definition: rejctmap.cpp:467
BOOL8 accepted()
Definition: rejctmap.h:157
#define TRUE
Definition: capi.h:45
void rej_word_block_rej()
Definition: rejctmap.cpp:503
void rej_word_mostly_rej()
Definition: rejctmap.cpp:476
void print(FILE *fp)
Definition: rejctmap.cpp:391
void setrej_mm_reject()
Definition: rejctmap.cpp:126
void setrej_bad_quality()
Definition: rejctmap.cpp:183
BOOL8 perm_rejected()
Definition: rejctmap.cpp:24
void setrej_bad_permuter()
Definition: rejctmap.cpp:153
void setrej_xht_fixup()
Definition: rejctmap.cpp:178
void setrej_no_alphanums()
Definition: rejctmap.cpp:168
void setrej_edge_char()
Definition: rejctmap.cpp:106
void setrej_small_xht()
Definition: rejctmap.cpp:101
void setrej_block_rej()
Definition: rejctmap.cpp:193
void rej_word_xht_fixup()
Definition: rejctmap.cpp:458
void setrej_minimal_rej_accept()
Definition: rejctmap.cpp:228
void setrej_mostly_rej()
Definition: rejctmap.cpp:173
int16_t inT16
Definition: host.h:36
void setrej_nn_accept()
Definition: rejctmap.cpp:213
#define ASSERT_HOST(x)
Definition: errcode.h:84
BOOL8 accept_if_good_quality()
Definition: rejctmap.cpp:83
inT16 accept_count()
Definition: rejctmap.cpp:329
BOOL8 rejected()
Definition: rejctmap.cpp:73
inT32 length() const
Definition: rejctmap.h:235
void setrej_1Il_conflict()
Definition: rejctmap.cpp:111
void full_print(FILE *fp)
Definition: rejctmap.cpp:234
void setrej_not_tess_accepted()
Definition: rejctmap.cpp:141
unsigned char BOOL8
Definition: host.h:44
#define FALSE
Definition: capi.h:46
void setrej_poor_match()
Definition: rejctmap.cpp:136
void setrej_postNN_1Il()
Definition: rejctmap.cpp:116
void setrej_row_rej()
Definition: rejctmap.cpp:198
BOOL8 recoverable_rejects()
Definition: rejctmap.cpp:341
void setrej_unlv_rej()
Definition: rejctmap.cpp:203
BOOL8 quality_recoverable_rejects()
Definition: rejctmap.cpp:352
void rej_word_bad_quality()
Definition: rejctmap.cpp:485
void setrej_rej_cblob()
Definition: rejctmap.cpp:121
void setrej_hyphen()
Definition: rejctmap.cpp:158
void setrej_contains_blanks()
Definition: rejctmap.cpp:147
void setrej_doc_rej()
Definition: rejctmap.cpp:188
void rej_word_bad_permuter()
Definition: rejctmap.cpp:449
void full_print(FILE *fp)
Definition: rejctmap.cpp:403
REJMAP & operator=(const REJMAP &source)
Definition: rejctmap.cpp:297
void rej_word_tess_failure()
Definition: rejctmap.cpp:422
BOOL8 recoverable()
Definition: rejctmap.h:164
void setrej_tess_failure()
Definition: rejctmap.cpp:96
void rej_word_row_rej()
Definition: rejctmap.cpp:512
void setrej_dubious()
Definition: rejctmap.cpp:163
void initialise(inT16 length)
Definition: rejctmap.cpp:318
void rej_word_doc_rej()
Definition: rejctmap.cpp:494
int count(LIST var_list)
Definition: oldlist.cpp:103
REJMAP()
Definition: rejctmap.h:210
void rej_word_contains_blanks()
Definition: rejctmap.cpp:440
Definition: rejctmap.h:99
void setrej_hyphen_accept()
Definition: rejctmap.cpp:208
void rej_word_not_tess_accepted()
Definition: rejctmap.cpp:431