48 if (best_choice.
length() == 0)
return false;
55 const char *xht =
"UNKNOWN";
56 switch (xheight_consistency) {
57 case XH_GOOD: xht =
"NORMAL";
break;
60 default: xht =
"UNKNOWN";
62 tprintf(
"\nStopper: %s (word=%c, case=%c, xht_ok=%s=[%g,%g])\n",
64 (is_valid_word ?
'y' :
'n'),
65 (is_case_ok ?
'y' :
'n'),
71 if (reject_offset_ <= 0.0f && !is_valid_word)
return false;
72 if (is_valid_word && is_case_ok) {
81 tprintf(
"Stopper: Rating = %4.1f, Certainty = %4.1f, Threshold = %4.1f\n",
85 best_choice.
certainty() > CertaintyThreshold &&
91 tprintf(
"AcceptableChoice() returned false" 92 " (no_dang_ambig:%d cert:%.4g thresh:%g uniform:%d)\n",
107 tprintf(
"\nRejecter: %s (word=%c, case=%c, unambig=%c, multiple=%c)\n",
127 tprintf(
"Rejecter: Certainty = %4.1f, Threshold = %4.1f ",
144 bool fix_replaceable,
147 tprintf(
"\nRunning NoDangerousAmbig() for %s\n",
155 bool ambigs_found =
false;
171 for (
int pass = 0; pass < (fix_replaceable ? 2 : 1); ++pass) {
172 bool replace = (fix_replaceable && pass == 0);
180 for (i = 0; i < best_choice->
length(); ++i) {
181 BLOB_CHOICE_LIST *lst =
new BLOB_CHOICE_LIST();
182 BLOB_CHOICE_IT lst_it(lst);
190 int wrong_ngram_index;
193 for (i = 0; i < best_choice->
length(); blob_index += best_choice->
state(i),
197 tprintf(
"Looking for %s ngrams starting with %s:\n",
198 replace ?
"replaceable" :
"ambiguous",
201 int num_wrong_blobs = best_choice->
state(i);
202 wrong_ngram_index = 0;
203 wrong_ngram[wrong_ngram_index] = curr_unichar_id;
204 if (curr_unichar_id == INVALID_UNICHAR_ID ||
205 curr_unichar_id >= table.
size() ||
206 table[curr_unichar_id] ==
nullptr) {
209 AmbigSpec_IT spec_it(table[curr_unichar_id]);
210 for (spec_it.mark_cycle_pt(); !spec_it.cycled_list();) {
211 const AmbigSpec *ambig_spec = spec_it.data();
212 wrong_ngram[wrong_ngram_index+1] = INVALID_UNICHAR_ID;
218 tprintf(
"current ngram from spec: ");
220 tprintf(
"comparison result: %d\n", compare);
224 if (fixpt !=
nullptr) {
227 blob_index, blob_index + num_wrong_blobs, replace,
231 tprintf(
"fixpt+=(%d %d %d %d %s)\n", blob_index,
232 blob_index + num_wrong_blobs,
false,
241 tprintf(
"replace ambiguity with %s : ",
249 best_choice, ratings);
258 for (
int tmp_index = 0; tmp_index <= wrong_ngram_index;
267 BLOB_CHOICE_IT bc_it(ambig_blob_choices[i+tmp_index]);
274 }
else if (compare == -1) {
276 ((next_index = wrong_ngram_index+1+i) < best_choice->
length())) {
279 wrong_ngram[++wrong_ngram_index] =
281 num_wrong_blobs += best_choice->
state(next_index);
296 tprintf(
"\nResulting ambig_blob_choices:\n");
297 for (i = 0; i < ambig_blob_choices.
length(); ++i) {
303 ambigs_found = (alt_word->
rating() < 0.0);
306 tprintf (
"Stopper: Possible ambiguous word = %s\n",
309 if (fixpt !=
nullptr) {
315 for (i = 0; i < alt_word->
length(); ++i) {
317 bool replacement_is_ngram =
320 if (replacement_is_ngram) {
323 int step = uchset.
step(str);
326 int end_i = orig_i + alt_word->
state(i);
327 if (alt_word->
state(i) > 1 ||
328 (orig_i + 1 == end_i && replacement_is_ngram)) {
331 for (
int j = 0; j < orig_i; ++j)
332 blob_start += best_choice->
state(j);
333 int blob_end = blob_start;
334 for (
int j = orig_i; j < end_i; ++j)
335 blob_end += best_choice->
state(j);
337 replacement_is_ngram, leftmost_id));
339 tprintf(
"fixpt->dangerous+=(%d %d %d %d %s)\n", orig_i, end_i,
340 true, replacement_is_ngram,
344 orig_i += alt_word->
state(i);
350 if (output_ambig_words_file_ !=
nullptr) {
351 fprintf(output_ambig_words_file_,
"\n");
355 return !ambigs_found;
361 reject_offset_ = 0.0;
371 int num_blobs_to_replace = 0;
372 int begin_blob_index = 0;
376 float new_rating = 0.0f;
377 float new_certainty = 0.0f;
379 for (i = 0; i < wrong_ngram_begin_index + wrong_ngram_size; ++i) {
380 if (i >= wrong_ngram_begin_index) {
381 int num_blobs = werd_choice->
state(i);
382 int col = begin_blob_index + num_blobs_to_replace;
383 int row = col + num_blobs - 1;
384 BLOB_CHOICE_LIST* choices = ratings->
get(col, row);
388 new_rating += old_choice->
rating();
389 new_certainty += old_choice->
certainty();
390 num_blobs_to_replace += num_blobs;
392 begin_blob_index += werd_choice->
state(i);
395 new_certainty /= wrong_ngram_size;
398 begin_blob_index + num_blobs_to_replace - 1);
399 if (!coord.
Valid(*ratings)) {
402 if (ratings->
get(coord.
col, coord.
row) ==
nullptr)
403 ratings->
put(coord.
col, coord.
row,
new BLOB_CHOICE_LIST);
404 BLOB_CHOICE_LIST* new_choices = ratings->
get(coord.
col, coord.
row);
406 if (choice !=
nullptr) {
408 if (new_rating < choice->rating())
410 if (new_certainty < choice->certainty())
421 BLOB_CHOICE_IT it (new_choices);
422 it.add_to_end(choice);
426 for (
int replaced_count = 0; replaced_count < wrong_ngram_size;
428 if (replaced_count + 1 == wrong_ngram_size) {
430 num_blobs_to_replace, choice);
436 werd_choice->
print(
"ReplaceAmbig() ");
437 tprintf(
"Modified blob_choices: ");
443 int shortest = INT32_MAX;
445 for (
int w = 0; w < WordChoice.
length(); ++w) {
448 }
else if (curr_len > 0) {
449 if (curr_len < shortest) shortest = curr_len;
453 if (curr_len > 0 && curr_len < shortest) {
455 }
else if (shortest == INT32_MAX) {
463 float WorstCertainty = FLT_MAX;
464 float CertaintyThreshold;
465 double TotalCertainty;
466 double TotalCertaintySquared;
469 int word_length = word.
length();
474 TotalCertainty = TotalCertaintySquared = 0.0;
475 for (
int i = 0; i < word_length; ++i) {
477 TotalCertainty += Certainty;
478 TotalCertaintySquared +=
static_cast<double>(Certainty) * Certainty;
479 if (Certainty < WorstCertainty)
480 WorstCertainty = Certainty;
485 TotalCertainty -= WorstCertainty;
486 TotalCertaintySquared -=
static_cast<double>(WorstCertainty) * WorstCertainty;
488 Mean = TotalCertainty / word_length;
489 Variance = ((word_length * TotalCertaintySquared -
490 TotalCertainty * TotalCertainty) /
491 (word_length * (word_length - 1)));
494 StdDev = sqrt(Variance);
500 if (word.
certainty() < CertaintyThreshold) {
502 tprintf(
"Stopper: Non-uniform certainty = %4.1f" 503 " (m=%4.1f, s=%4.1f, t=%4.1f)\n",
WERD_CHOICE_LIST best_choices
int case_ok(const WERD_CHOICE &word, const UNICHARSET &unicharset) const
Check a string to see if it matches a set of lexical rules.
int UniformCertainties(const WERD_CHOICE &word)
void remove_unichar_id(int index)
int valid_word(const WERD_CHOICE &word, bool numbers_ok) const
void set_classifier(BlobChoiceClassifier classifier)
void SettupStopperPass1()
Sets up stopper variables in preparation for the first pass.
const UnicharAmbigs & getUnicharAmbigs() const
const char * string() const
int state(int index) const
void set_matrix_cell(int col, int row)
bool AcceptableChoice(const WERD_CHOICE &best_choice, XHeightConsistencyEnum xheight_consistency)
Returns true if the given best_choice is good enough to stop.
bool stopper_no_acceptable_choices
UNICHAR_ID unichar_to_id(const char *const unichar_repr) const
bool get_isalpha(UNICHAR_ID unichar_id) const
void ReplaceAmbig(int wrong_ngram_begin_index, int wrong_ngram_size, UNICHAR_ID correct_ngram_id, WERD_CHOICE *werd_choice, MATRIX *ratings)
void EndDangerousAmbigs()
static int compare(const UNICHAR_ID *ptr1, const UNICHAR_ID *ptr2)
bool Valid(const MATRIX &m) const
bool dangerous_ambig_found() const
float max_x_height() const
void IncreaseBandSize(int bandwidth)
bool get_isngram(UNICHAR_ID unichar_id) const
void set_certainty(float newrat)
static void print(const UNICHAR_ID array[], const UNICHARSET &unicharset)
void set_rating(float newrat)
void set_unichar_id(UNICHAR_ID newunichar_id)
void set_blob_choice(int index, int blob_count, const BLOB_CHOICE *blob_choice)
double stopper_allowable_character_badness
double stopper_nondict_certainty_base
const UnicharAmbigsVector & dang_ambigs() const
UNICHAR_ID unichar_id(int index) const
DLLSYM void tprintf(const char *format,...)
void put(ICOORD pos, const T &thing)
float Mean(PROTOTYPE *Proto, uint16_t Dimension)
int step(const char *str) const
float min_x_height() const
double stopper_certainty_per_char
UNICHAR_ID correct_ngram_id
UNICHAR_ID correct_fragments[MAX_AMBIG_SIZE+1]
const STRING debug_string() const
const char * id_to_unichar(UNICHAR_ID id) const
void delete_data_pointers()
const UNICHARSET & getUnicharset() const
const STRING & unichar_string() const
int stopper_smallword_size
bool AcceptableResult(WERD_RES *word) const
void print_ratings_list(const char *msg, BLOB_CHOICE_LIST *ratings, const UNICHARSET ¤t_unicharset)
double stopper_phase2_certainty_rejection_offset
BLOB_CHOICE * FindMatchingChoice(UNICHAR_ID char_id, BLOB_CHOICE_LIST *bc_list)
void SettupStopperPass2()
Sets up stopper variables in preparation for the second pass.
WERD_CHOICE * dawg_permute_and_select(const BLOB_CHOICE_LIST_VECTOR &char_choices, float rating_limit)
static bool valid_word_permuter(uint8_t perm, bool numbers_ok)
Check all the DAWGs to see if this word is in any of them.
UNICHAR_ID wrong_ngram[MAX_AMBIG_SIZE+1]
WERD_CHOICE * best_choice
int LengthOfShortestAlphaRun(const WERD_CHOICE &WordChoice) const
Returns the length of the shortest alpha run in WordChoice.
bool NoDangerousAmbig(WERD_CHOICE *BestChoice, DANGERR *fixpt, bool fix_replaceable, MATRIX *ratings)
const UnicharAmbigsVector & replace_ambigs() const