40 #pragma warning(disable:4244) // Conversion warnings
41 #pragma warning(disable:4800) // int/bool warnings
58 if (best_choice.
length() == 0)
return false;
65 const char *xht =
"UNKNOWN";
66 switch (xheight_consistency) {
67 case XH_GOOD: xht =
"NORMAL";
break;
70 default: xht =
"UNKNOWN";
72 tprintf(
"\nStopper: %s (word=%c, case=%c, xht_ok=%s=[%g,%g])\n",
74 (is_valid_word ?
'y' :
'n'),
75 (is_case_ok ?
'y' :
'n'),
81 if (reject_offset_ <= 0.0f && !is_valid_word)
return false;
82 if (is_valid_word && is_case_ok) {
91 tprintf(
"Stopper: Rating = %4.1f, Certainty = %4.1f, Threshold = %4.1f\n",
95 best_choice.
certainty() > CertaintyThreshold &&
101 tprintf(
"AcceptableChoice() returned false"
102 " (no_dang_ambig:%d cert:%.4g thresh:%g uniform:%d)\n",
117 tprintf(
"\nRejecter: %s (word=%c, case=%c, unambig=%c, multiple=%c)\n",
137 tprintf(
"Rejecter: Certainty = %4.1f, Threshold = %4.1f ",
154 bool fix_replaceable,
157 tprintf(
"\nRunning NoDangerousAmbig() for %s\n",
165 bool ambigs_found =
false;
181 for (
int pass = 0; pass < (fix_replaceable ? 2 : 1); ++pass) {
182 bool replace = (fix_replaceable && pass == 0);
190 for (i = 0; i < best_choice->
length(); ++i) {
191 BLOB_CHOICE_LIST *lst =
new BLOB_CHOICE_LIST();
192 BLOB_CHOICE_IT lst_it(lst);
200 int wrong_ngram_index;
203 for (i = 0; i < best_choice->
length(); blob_index += best_choice->
state(i),
207 tprintf(
"Looking for %s ngrams starting with %s:\n",
208 replace ?
"replaceable" :
"ambiguous",
211 int num_wrong_blobs = best_choice->
state(i);
212 wrong_ngram_index = 0;
213 wrong_ngram[wrong_ngram_index] = curr_unichar_id;
214 if (curr_unichar_id == INVALID_UNICHAR_ID ||
215 curr_unichar_id >= table.
size() ||
216 table[curr_unichar_id] ==
NULL) {
219 AmbigSpec_IT spec_it(table[curr_unichar_id]);
220 for (spec_it.mark_cycle_pt(); !spec_it.cycled_list();) {
221 const AmbigSpec *ambig_spec = spec_it.data();
222 wrong_ngram[wrong_ngram_index+1] = INVALID_UNICHAR_ID;
228 tprintf(
"current ngram from spec: ");
230 tprintf(
"comparison result: %d\n", compare);
237 blob_index, blob_index + num_wrong_blobs, replace,
241 tprintf(
"fixpt+=(%d %d %d %d %s)\n", blob_index,
242 blob_index + num_wrong_blobs,
false,
251 tprintf(
"replace ambiguity with %s : ",
259 best_choice, ratings);
268 for (
int tmp_index = 0; tmp_index <= wrong_ngram_index;
277 BLOB_CHOICE_IT bc_it(ambig_blob_choices[i+tmp_index]);
284 }
else if (compare == -1) {
286 ((next_index = wrong_ngram_index+1+i) < best_choice->
length())) {
289 wrong_ngram[++wrong_ngram_index] =
291 num_wrong_blobs += best_choice->
state(next_index);
306 tprintf(
"\nResulting ambig_blob_choices:\n");
307 for (i = 0; i < ambig_blob_choices.
length(); ++i) {
313 ambigs_found = (alt_word->
rating() < 0.0);
316 tprintf (
"Stopper: Possible ambiguous word = %s\n",
325 for (i = 0; i < alt_word->
length(); ++i) {
327 bool replacement_is_ngram =
330 if (replacement_is_ngram) {
333 int step = uchset.
step(str);
336 int end_i = orig_i + alt_word->
state(i);
337 if (alt_word->
state(i) > 1 ||
338 (orig_i + 1 == end_i && replacement_is_ngram)) {
341 for (
int j = 0; j < orig_i; ++j)
342 blob_start += best_choice->
state(j);
343 int blob_end = blob_start;
344 for (
int j = orig_i; j < end_i; ++j)
345 blob_end += best_choice->
state(j);
347 replacement_is_ngram, leftmost_id));
349 tprintf(
"fixpt->dangerous+=(%d %d %d %d %s)\n", orig_i, end_i,
350 true, replacement_is_ngram,
354 orig_i += alt_word->
state(i);
360 if (output_ambig_words_file_ !=
NULL) {
361 fprintf(output_ambig_words_file_,
"\n");
365 return !ambigs_found;
371 reject_offset_ = 0.0;
381 int num_blobs_to_replace = 0;
382 int begin_blob_index = 0;
386 float new_rating = 0.0f;
387 float new_certainty = 0.0f;
389 for (i = 0; i < wrong_ngram_begin_index + wrong_ngram_size; ++i) {
390 if (i >= wrong_ngram_begin_index) {
391 int num_blobs = werd_choice->
state(i);
392 int col = begin_blob_index + num_blobs_to_replace;
393 int row = col + num_blobs - 1;
394 BLOB_CHOICE_LIST* choices = ratings->
get(col, row);
398 new_rating += old_choice->
rating();
399 new_certainty += old_choice->
certainty();
400 num_blobs_to_replace += num_blobs;
402 begin_blob_index += werd_choice->
state(i);
405 new_certainty /= wrong_ngram_size;
408 begin_blob_index + num_blobs_to_replace - 1);
409 if (!coord.
Valid(*ratings)) {
413 ratings->
put(coord.
col, coord.
row,
new BLOB_CHOICE_LIST);
414 BLOB_CHOICE_LIST* new_choices = ratings->
get(coord.
col, coord.
row);
416 if (choice !=
NULL) {
418 if (new_rating < choice->rating())
420 if (new_certainty < choice->certainty())
431 BLOB_CHOICE_IT it (new_choices);
432 it.add_to_end(choice);
436 for (
int replaced_count = 0; replaced_count < wrong_ngram_size;
438 if (replaced_count + 1 == wrong_ngram_size) {
440 num_blobs_to_replace, choice);
446 werd_choice->
print(
"ReplaceAmbig() ");
447 tprintf(
"Modified blob_choices: ");
455 for (
int w = 0; w < WordChoice.
length(); ++w) {
458 }
else if (curr_len > 0) {
459 if (curr_len < shortest) shortest = curr_len;
463 if (curr_len > 0 && curr_len < shortest) {
474 float CertaintyThreshold;
479 int word_length = word.
length();
484 TotalCertainty = TotalCertaintySquared = 0.0;
485 for (
int i = 0; i < word_length; ++i) {
487 TotalCertainty += Certainty;
488 TotalCertaintySquared += Certainty * Certainty;
489 if (Certainty < WorstCertainty)
490 WorstCertainty = Certainty;
495 TotalCertainty -= WorstCertainty;
496 TotalCertaintySquared -= WorstCertainty * WorstCertainty;
498 Mean = TotalCertainty / word_length;
499 Variance = ((word_length * TotalCertaintySquared -
500 TotalCertainty * TotalCertainty) /
501 (word_length * (word_length - 1)));
504 StdDev = sqrt(Variance);
510 if (word.
certainty() < CertaintyThreshold) {
512 tprintf(
"Stopper: Non-uniform certainty = %4.1f"
513 " (m=%4.1f, s=%4.1f, t=%4.1f)\n",
void ReplaceAmbig(int wrong_ngram_begin_index, int wrong_ngram_size, UNICHAR_ID correct_ngram_id, WERD_CHOICE *werd_choice, MATRIX *ratings)
double stopper_phase2_certainty_rejection_offset
WERD_CHOICE_LIST best_choices
void remove_unichar_id(int index)
const UNICHAR_ID unichar_to_id(const char *const unichar_repr) const
static int compare(const UNICHAR_ID array1[], const UNICHAR_ID array2[])
void EndDangerousAmbigs()
WERD_CHOICE * best_choice
T get(int column, int row) const
int valid_word(const WERD_CHOICE &word, bool numbers_ok) const
double stopper_allowable_character_badness
const UnicharAmbigsVector & replace_ambigs() const
UNICHAR_ID wrong_ngram[MAX_AMBIG_SIZE+1]
int stopper_smallword_size
BLOB_CHOICE * FindMatchingChoice(UNICHAR_ID char_id, BLOB_CHOICE_LIST *bc_list)
void put(int column, int row, const T &thing)
float min_x_height() const
bool dangerous_ambig_found() const
void print_ratings_list(const char *msg, BLOB_CHOICE_LIST *ratings, const UNICHARSET ¤t_unicharset)
const STRING & unichar_string() const
double stopper_certainty_per_char
bool AcceptableChoice(const WERD_CHOICE &best_choice, XHeightConsistencyEnum xheight_consistency)
Returns true if the given best_choice is good enough to stop.
void SettupStopperPass1()
Sets up stopper variables in preparation for the first pass.
int LengthOfShortestAlphaRun(const WERD_CHOICE &WordChoice)
Returns the length of the shortest alpha run in WordChoice.
int state(int index) const
void set_classifier(BlobChoiceClassifier classifier)
static bool valid_word_permuter(uinT8 perm, bool numbers_ok)
Check all the DAWGs to see if this word is in any of them.
int case_ok(const WERD_CHOICE &word, const UNICHARSET &unicharset)
Check a string to see if it matches a set of lexical rules.
const UnicharAmbigs & getUnicharAmbigs() const
void set_certainty(float newrat)
bool get_isngram(UNICHAR_ID unichar_id) const
const UNICHAR_ID unichar_id(int index) const
void delete_data_pointers()
const char *const id_to_unichar(UNICHAR_ID id) const
bool NoDangerousAmbig(WERD_CHOICE *BestChoice, DANGERR *fixpt, bool fix_replaceable, MATRIX *ratings)
double stopper_nondict_certainty_base
const STRING debug_string() const
bool Valid(const MATRIX &m) const
WERD_CHOICE * dawg_permute_and_select(const BLOB_CHOICE_LIST_VECTOR &char_choices, float rating_limit)
void set_matrix_cell(int col, int row)
void SettupStopperPass2()
Sets up stopper variables in preparation for the second pass.
const UnicharAmbigsVector & dang_ambigs() const
bool stopper_no_acceptable_choices
UNICHAR_ID correct_fragments[MAX_AMBIG_SIZE+1]
float max_x_height() const
void set_blob_choice(int index, int blob_count, const BLOB_CHOICE *blob_choice)
UNICHAR_ID correct_ngram_id
FLOAT32 Mean(PROTOTYPE *Proto, uinT16 Dimension)
const UNICHARSET & getUnicharset() const
static void print(const UNICHAR_ID array[], const UNICHARSET &unicharset)
bool get_isalpha(UNICHAR_ID unichar_id) const
bool AcceptableResult(WERD_RES *word)
int step(const char *str) const
void set_rating(float newrat)
const char * string() const
int UniformCertainties(const WERD_CHOICE &word)
void set_unichar_id(UNICHAR_ID newunichar_id)
void IncreaseBandSize(int bandwidth)