mirror of
https://github.com/tesseract-ocr/tesseract.git
synced 2025-06-07 18:02:40 +08:00
Reduce number of new / delete operations for class LanguageModel
Signed-off-by: Stefan Weil <sw@weilnetz.de>
This commit is contained in:
parent
562de89728
commit
e6d683923c
@ -118,20 +118,15 @@ LanguageModel::LanguageModel(const UnicityTable<FontInfo> *fontinfo_table,
|
||||
BOOL_INIT_MEMBER(language_model_use_sigmoidal_certainty, false,
|
||||
"Use sigmoidal score for certainty",
|
||||
dict->getCCUtil()->params()),
|
||||
dawg_args_(nullptr, new DawgPositionVector(), NO_PERM),
|
||||
fontinfo_table_(fontinfo_table), dict_(dict),
|
||||
fixed_pitch_(false), max_char_wh_ratio_(0.0),
|
||||
acceptable_choice_found_(false) {
|
||||
ASSERT_HOST(dict_ != NULL);
|
||||
dawg_args_ = new DawgArgs(NULL, new DawgPositionVector(), NO_PERM);
|
||||
very_beginning_active_dawgs_ = new DawgPositionVector();
|
||||
beginning_active_dawgs_ = new DawgPositionVector();
|
||||
}
|
||||
|
||||
LanguageModel::~LanguageModel() {
|
||||
delete very_beginning_active_dawgs_;
|
||||
delete beginning_active_dawgs_;
|
||||
delete dawg_args_->updated_dawgs;
|
||||
delete dawg_args_;
|
||||
delete dawg_args_.updated_dawgs;
|
||||
}
|
||||
|
||||
void LanguageModel::InitForWord(const WERD_CHOICE *prev_word,
|
||||
@ -144,10 +139,10 @@ void LanguageModel::InitForWord(const WERD_CHOICE *prev_word,
|
||||
correct_segmentation_explored_ = false;
|
||||
|
||||
// Initialize vectors with beginning DawgInfos.
|
||||
very_beginning_active_dawgs_->clear();
|
||||
dict_->init_active_dawgs(very_beginning_active_dawgs_, false);
|
||||
beginning_active_dawgs_->clear();
|
||||
dict_->default_dawgs(beginning_active_dawgs_, false);
|
||||
very_beginning_active_dawgs_.clear();
|
||||
dict_->init_active_dawgs(&very_beginning_active_dawgs_, false);
|
||||
beginning_active_dawgs_.clear();
|
||||
dict_->default_dawgs(&beginning_active_dawgs_, false);
|
||||
|
||||
// Fill prev_word_str_ with the last language_model_ngram_order
|
||||
// unichars from prev_word.
|
||||
@ -791,18 +786,18 @@ LanguageModelDawgInfo *LanguageModel::GenerateDawgInfo(
|
||||
// Initialize active_dawgs from parent_vse if it is not NULL.
|
||||
// Otherwise use very_beginning_active_dawgs_.
|
||||
if (parent_vse == NULL) {
|
||||
dawg_args_->active_dawgs = very_beginning_active_dawgs_;
|
||||
dawg_args_->permuter = NO_PERM;
|
||||
dawg_args_.active_dawgs = &very_beginning_active_dawgs_;
|
||||
dawg_args_.permuter = NO_PERM;
|
||||
} else {
|
||||
if (parent_vse->dawg_info == NULL) return NULL; // not a dict word path
|
||||
dawg_args_->active_dawgs = &parent_vse->dawg_info->active_dawgs;
|
||||
dawg_args_->permuter = parent_vse->dawg_info->permuter;
|
||||
dawg_args_.active_dawgs = &parent_vse->dawg_info->active_dawgs;
|
||||
dawg_args_.permuter = parent_vse->dawg_info->permuter;
|
||||
}
|
||||
|
||||
// Deal with hyphenated words.
|
||||
if (word_end && dict_->has_hyphen_end(b.unichar_id(), curr_col == 0)) {
|
||||
if (language_model_debug_level > 0) tprintf("Hyphenated word found\n");
|
||||
return new LanguageModelDawgInfo(dawg_args_->active_dawgs,
|
||||
return new LanguageModelDawgInfo(dawg_args_.active_dawgs,
|
||||
COMPOUND_PERM);
|
||||
}
|
||||
|
||||
@ -815,7 +810,7 @@ LanguageModelDawgInfo *LanguageModel::GenerateDawgInfo(
|
||||
// Do not allow compounding of words with lengths shorter than
|
||||
// language_model_min_compound_length
|
||||
if (parent_vse == NULL || word_end ||
|
||||
dawg_args_->permuter == COMPOUND_PERM ||
|
||||
dawg_args_.permuter == COMPOUND_PERM ||
|
||||
parent_vse->length < language_model_min_compound_length) return NULL;
|
||||
|
||||
int i;
|
||||
@ -835,7 +830,7 @@ LanguageModelDawgInfo *LanguageModel::GenerateDawgInfo(
|
||||
if (!has_word_ending) return NULL;
|
||||
|
||||
if (language_model_debug_level > 0) tprintf("Compound word found\n");
|
||||
return new LanguageModelDawgInfo(beginning_active_dawgs_, COMPOUND_PERM);
|
||||
return new LanguageModelDawgInfo(&beginning_active_dawgs_, COMPOUND_PERM);
|
||||
} // done dealing with compound words
|
||||
|
||||
LanguageModelDawgInfo *dawg_info = NULL;
|
||||
@ -850,22 +845,22 @@ LanguageModelDawgInfo *LanguageModel::GenerateDawgInfo(
|
||||
if (language_model_debug_level > 2)
|
||||
tprintf("Test Letter OK for unichar %d, normed %d\n",
|
||||
b.unichar_id(), normed_ids[i]);
|
||||
dict_->LetterIsOkay(dawg_args_, normed_ids[i],
|
||||
dict_->LetterIsOkay(&dawg_args_, normed_ids[i],
|
||||
word_end && i == normed_ids.size() - 1);
|
||||
if (dawg_args_->permuter == NO_PERM) {
|
||||
if (dawg_args_.permuter == NO_PERM) {
|
||||
break;
|
||||
} else if (i < normed_ids.size() - 1) {
|
||||
tmp_active_dawgs = *dawg_args_->updated_dawgs;
|
||||
dawg_args_->active_dawgs = &tmp_active_dawgs;
|
||||
tmp_active_dawgs = *dawg_args_.updated_dawgs;
|
||||
dawg_args_.active_dawgs = &tmp_active_dawgs;
|
||||
}
|
||||
if (language_model_debug_level > 2)
|
||||
tprintf("Letter was OK for unichar %d, normed %d\n",
|
||||
b.unichar_id(), normed_ids[i]);
|
||||
}
|
||||
dawg_args_->active_dawgs = NULL;
|
||||
if (dawg_args_->permuter != NO_PERM) {
|
||||
dawg_info = new LanguageModelDawgInfo(dawg_args_->updated_dawgs,
|
||||
dawg_args_->permuter);
|
||||
dawg_args_.active_dawgs = nullptr;
|
||||
if (dawg_args_.permuter != NO_PERM) {
|
||||
dawg_info = new LanguageModelDawgInfo(dawg_args_.updated_dawgs,
|
||||
dawg_args_.permuter);
|
||||
} else if (language_model_debug_level > 3) {
|
||||
tprintf("Letter %s not OK!\n",
|
||||
dict_->getUnicharset().id_to_unichar(b.unichar_id()));
|
||||
@ -1320,7 +1315,7 @@ void LanguageModel::UpdateBestChoice(
|
||||
// Update hyphen state if we are dealing with a dictionary word.
|
||||
if (vse->dawg_info != NULL) {
|
||||
if (dict_->has_hyphen_end(*word)) {
|
||||
dict_->set_hyphen_word(*word, *(dawg_args_->active_dawgs));
|
||||
dict_->set_hyphen_word(*word, *(dawg_args_.active_dawgs));
|
||||
} else {
|
||||
dict_->reset_hyphen_vars(true);
|
||||
}
|
||||
|
@ -361,7 +361,7 @@ class LanguageModel {
|
||||
|
||||
// Temporary DawgArgs struct that is re-used across different words to
|
||||
// avoid dynamic memory re-allocation (should be cleared before each use).
|
||||
DawgArgs *dawg_args_;
|
||||
DawgArgs dawg_args_;
|
||||
// Scaling for recovering blob outline length from rating and certainty.
|
||||
float rating_cert_scale_;
|
||||
|
||||
@ -392,8 +392,8 @@ class LanguageModel {
|
||||
STRING prev_word_str_;
|
||||
int prev_word_unichar_step_len_;
|
||||
// Active dawg vector.
|
||||
DawgPositionVector *very_beginning_active_dawgs_; // includes continuation
|
||||
DawgPositionVector *beginning_active_dawgs_;
|
||||
DawgPositionVector very_beginning_active_dawgs_; // includes continuation
|
||||
DawgPositionVector beginning_active_dawgs_;
|
||||
// Set to true if acceptable choice was discovered.
|
||||
// Note: it would be nice to use this to terminate the search once an
|
||||
// acceptable choices is found. However we do not do that and once an
|
||||
|
Loading…
Reference in New Issue
Block a user