47 if (best_choice.
length() == 0)
return false;
51 bool is_case_ok =
case_ok(best_choice);
54 const char *xht =
"UNKNOWN";
55 switch (xheight_consistency) {
56 case XH_GOOD: xht =
"NORMAL";
break;
59 default: xht =
"UNKNOWN";
61 tprintf(
"\nStopper: %s (word=%c, case=%c, xht_ok=%s=[%g,%g])\n",
63 (is_valid_word ?
'y' :
'n'),
64 (is_case_ok ?
'y' :
'n'),
70 if (reject_offset_ <= 0.0f && !is_valid_word)
return false;
71 if (is_valid_word && is_case_ok) {
80 tprintf(
"Stopper: Rating = %4.1f, Certainty = %4.1f, Threshold = %4.1f\n",
84 best_choice.
certainty() > CertaintyThreshold &&
90 tprintf(
"AcceptableChoice() returned false" 91 " (no_dang_ambig:%d cert:%.4g thresh:%g uniform:%d)\n",
106 tprintf(
"\nRejecter: %s (word=%c, case=%c, unambig=%c, multiple=%c)\n",
125 tprintf(
"Rejecter: Certainty = %4.1f, Threshold = %4.1f ",
142 bool fix_replaceable,
145 tprintf(
"\nRunning NoDangerousAmbig() for %s\n",
153 bool ambigs_found =
false;
169 for (
int pass = 0; pass < (fix_replaceable ? 2 : 1); ++pass) {
170 bool replace = (fix_replaceable && pass == 0);
178 for (i = 0; i < best_choice->
length(); ++i) {
179 auto *lst =
new BLOB_CHOICE_LIST();
180 BLOB_CHOICE_IT lst_it(lst);
184 ambig_blob_choices.push_back(lst);
188 int wrong_ngram_index;
191 for (i = 0; i < best_choice->
length(); blob_index += best_choice->
state(i),
195 tprintf(
"Looking for %s ngrams starting with %s:\n",
196 replace ?
"replaceable" :
"ambiguous",
199 int num_wrong_blobs = best_choice->
state(i);
200 wrong_ngram_index = 0;
201 wrong_ngram[wrong_ngram_index] = curr_unichar_id;
202 if (curr_unichar_id == INVALID_UNICHAR_ID ||
203 curr_unichar_id >= table.
size() ||
204 table[curr_unichar_id] ==
nullptr) {
207 AmbigSpec_IT spec_it(table[curr_unichar_id]);
208 for (spec_it.mark_cycle_pt(); !spec_it.cycled_list();) {
209 const AmbigSpec *ambig_spec = spec_it.data();
210 wrong_ngram[wrong_ngram_index+1] = INVALID_UNICHAR_ID;
216 tprintf(
"current ngram from spec: ");
218 tprintf(
"comparison result: %d\n", compare);
222 if (fixpt !=
nullptr) {
225 blob_index, blob_index + num_wrong_blobs, replace,
229 tprintf(
"fixpt+=(%d %d %d %d %s)\n", blob_index,
230 blob_index + num_wrong_blobs,
false,
239 tprintf(
"replace ambiguity with %s : ",
247 best_choice, ratings);
256 for (
int tmp_index = 0; tmp_index <= wrong_ngram_index;
265 BLOB_CHOICE_IT bc_it(ambig_blob_choices[i+tmp_index]);
272 }
else if (compare == -1) {
274 ((next_index = wrong_ngram_index+1+i) < best_choice->
length())) {
277 wrong_ngram[++wrong_ngram_index] =
279 num_wrong_blobs += best_choice->
state(next_index);
294 tprintf(
"\nResulting ambig_blob_choices:\n");
295 for (i = 0; i < ambig_blob_choices.length(); ++i) {
301 ambigs_found = (alt_word->
rating() < 0.0);
304 tprintf (
"Stopper: Possible ambiguous word = %s\n",
307 if (fixpt !=
nullptr) {
313 for (i = 0; i < alt_word->
length(); ++i) {
315 bool replacement_is_ngram =
318 if (replacement_is_ngram) {
321 int step = uchset.
step(str);
324 int end_i = orig_i + alt_word->
state(i);
325 if (alt_word->
state(i) > 1 ||
326 (orig_i + 1 == end_i && replacement_is_ngram)) {
329 for (
int j = 0; j < orig_i; ++j)
330 blob_start += best_choice->
state(j);
331 int blob_end = blob_start;
332 for (
int j = orig_i; j < end_i; ++j)
333 blob_end += best_choice->
state(j);
335 replacement_is_ngram, leftmost_id));
337 tprintf(
"fixpt->dangerous+=(%d %d %d %d %s)\n", orig_i, end_i,
338 true, replacement_is_ngram,
342 orig_i += alt_word->
state(i);
348 if (output_ambig_words_file_ !=
nullptr) {
349 fprintf(output_ambig_words_file_,
"\n");
352 ambig_blob_choices.delete_data_pointers();
353 return !ambigs_found;
359 reject_offset_ = 0.0;
369 int num_blobs_to_replace = 0;
370 int begin_blob_index = 0;
374 float new_rating = 0.0f;
375 float new_certainty = 0.0f;
377 for (i = 0; i < wrong_ngram_begin_index + wrong_ngram_size; ++i) {
378 if (i >= wrong_ngram_begin_index) {
379 int num_blobs = werd_choice->
state(i);
380 int col = begin_blob_index + num_blobs_to_replace;
381 int row = col + num_blobs - 1;
382 BLOB_CHOICE_LIST* choices = ratings->
get(col, row);
386 new_rating += old_choice->
rating();
387 new_certainty += old_choice->
certainty();
388 num_blobs_to_replace += num_blobs;
390 begin_blob_index += werd_choice->
state(i);
393 new_certainty /= wrong_ngram_size;
396 begin_blob_index + num_blobs_to_replace - 1);
397 if (!coord.
Valid(*ratings)) {
400 if (ratings->
get(coord.
col, coord.
row) ==
nullptr)
401 ratings->
put(coord.
col, coord.
row,
new BLOB_CHOICE_LIST);
402 BLOB_CHOICE_LIST* new_choices = ratings->
get(coord.
col, coord.
row);
404 if (choice !=
nullptr) {
406 if (new_rating < choice->rating())
408 if (new_certainty < choice->certainty())
419 BLOB_CHOICE_IT it (new_choices);
420 it.add_to_end(choice);
424 for (
int replaced_count = 0; replaced_count < wrong_ngram_size;
426 if (replaced_count + 1 == wrong_ngram_size) {
428 num_blobs_to_replace, choice);
434 werd_choice->
print(
"ReplaceAmbig() ");
435 tprintf(
"Modified blob_choices: ");
441 int shortest = INT32_MAX;
443 for (
int w = 0; w < WordChoice.
length(); ++w) {
446 }
else if (curr_len > 0) {
447 if (curr_len < shortest) shortest = curr_len;
451 if (curr_len > 0 && curr_len < shortest) {
453 }
else if (shortest == INT32_MAX) {
461 float WorstCertainty = FLT_MAX;
462 float CertaintyThreshold;
463 double TotalCertainty;
464 double TotalCertaintySquared;
467 int word_length = word.
length();
472 TotalCertainty = TotalCertaintySquared = 0.0;
473 for (
int i = 0; i < word_length; ++i) {
475 TotalCertainty += Certainty;
476 TotalCertaintySquared +=
static_cast<double>(Certainty) * Certainty;
477 if (Certainty < WorstCertainty)
478 WorstCertainty = Certainty;
483 TotalCertainty -= WorstCertainty;
484 TotalCertaintySquared -=
static_cast<double>(WorstCertainty) * WorstCertainty;
486 Mean = TotalCertainty / word_length;
487 Variance = ((word_length * TotalCertaintySquared -
488 TotalCertainty * TotalCertainty) /
489 (word_length * (word_length - 1)));
492 StdDev = sqrt(Variance);
498 if (word.
certainty() < CertaintyThreshold) {
500 tprintf(
"Stopper: Non-uniform certainty = %4.1f" 501 " (m=%4.1f, s=%4.1f, t=%4.1f)\n",
void set_blob_choice(int index, int blob_count, const BLOB_CHOICE *blob_choice)
void SettupStopperPass2()
Sets up stopper variables in preparation for the second pass.
int valid_word(const WERD_CHOICE &word, bool numbers_ok) const
void set_rating(float newrat)
int step(const char *str) const
UNICHAR_ID wrong_ngram[MAX_AMBIG_SIZE+1]
bool AcceptableResult(WERD_RES *word) const
const STRING & unichar_string() const
float min_x_height() const
WERD_CHOICE * dawg_permute_and_select(const BLOB_CHOICE_LIST_VECTOR &char_choices, float rating_limit)
UNICHAR_ID correct_fragments[MAX_AMBIG_SIZE+1]
const UnicharAmbigs & getUnicharAmbigs() const
WERD_CHOICE_LIST best_choices
UNICHAR_ID correct_ngram_id
const char * id_to_unichar(UNICHAR_ID id) const
void set_matrix_cell(int col, int row)
const UnicharAmbigsVector & replace_ambigs() const
bool stopper_no_acceptable_choices
static int compare(const UNICHAR_ID *ptr1, const UNICHAR_ID *ptr2)
double stopper_allowable_character_badness
int stopper_smallword_size
BLOB_CHOICE * FindMatchingChoice(UNICHAR_ID char_id, BLOB_CHOICE_LIST *bc_list)
bool get_isalpha(UNICHAR_ID unichar_id) const
void set_classifier(BlobChoiceClassifier classifier)
static void print(const UNICHAR_ID array[], const UNICHARSET &unicharset)
void print_ratings_list(const char *msg, BLOB_CHOICE_LIST *ratings, const UNICHARSET ¤t_unicharset)
double stopper_nondict_certainty_base
void EndDangerousAmbigs()
UNICHAR_ID unichar_to_id(const char *const unichar_repr) const
void remove_unichar_id(int index)
void put(ICOORD pos, const T &thing)
const char * string() const
DLLSYM void tprintf(const char *format,...)
void IncreaseBandSize(int bandwidth)
void SettupStopperPass1()
Sets up stopper variables in preparation for the first pass.
bool AcceptableChoice(const WERD_CHOICE &best_choice, XHeightConsistencyEnum xheight_consistency)
Returns true if the given best_choice is good enough to stop.
bool dangerous_ambig_found() const
UNICHAR_ID unichar_id(int index) const
WERD_CHOICE * best_choice
void ReplaceAmbig(int wrong_ngram_begin_index, int wrong_ngram_size, UNICHAR_ID correct_ngram_id, WERD_CHOICE *werd_choice, MATRIX *ratings)
const UNICHARSET & getUnicharset() const
int case_ok(const WERD_CHOICE &word) const
Check a string to see if it matches a set of lexical rules.
bool Valid(const MATRIX &m) const
bool NoDangerousAmbig(WERD_CHOICE *BestChoice, DANGERR *fixpt, bool fix_replaceable, MATRIX *ratings)
int UniformCertainties(const WERD_CHOICE &word)
double stopper_phase2_certainty_rejection_offset
const UnicharAmbigsVector & dang_ambigs() const
float max_x_height() const
const STRING debug_string() const
double stopper_certainty_per_char
bool get_isngram(UNICHAR_ID unichar_id) const
const UNICHARSET * unicharset() const
int state(int index) const
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.
int LengthOfShortestAlphaRun(const WERD_CHOICE &WordChoice) const
Returns the length of the shortest alpha run in WordChoice.
void set_certainty(float newrat)
float Mean(PROTOTYPE *Proto, uint16_t Dimension)
void set_unichar_id(UNICHAR_ID newunichar_id)