Skip to content

Commit 6ac2ff0

Browse files
committed
trying to add tessedit_char_whitelist etc. again:
- ignore matrix outputs in ComputeTopN if they belong to a disabled unichar_id - pass UNICHARSET refs to check that - in SetBlackAndWhitelist, also update the unicharset of the lstm_recognizer_ instance, if any
1 parent fe5c82f commit 6ac2ff0

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

src/ccmain/tesseractclass.cpp

+6
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,12 @@ void Tesseract::SetBlackAndWhitelist() {
619619
unicharset.set_black_and_whitelist(tessedit_char_blacklist.string(),
620620
tessedit_char_whitelist.string(),
621621
tessedit_char_unblacklist.string());
622+
if (lstm_recognizer_) {
623+
UNICHARSET& lstm_unicharset = const_cast<UNICHARSET&> (lstm_recognizer_->GetUnicharset());
624+
lstm_unicharset.set_black_and_whitelist(tessedit_char_blacklist.string(),
625+
tessedit_char_whitelist.string(),
626+
tessedit_char_unblacklist.string());
627+
}
622628
// Black and white lists should apply to all loaded classifiers.
623629
for (int i = 0; i < sub_langs_.size(); ++i) {
624630
sub_langs_[i]->unicharset.set_black_and_whitelist(

src/lstm/recodebeam.cpp

+10-3
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ void RecodeBeamSearch::Decode(const NetworkIO& output, double dict_ratio,
8787
if (lstm_choice_mode)
8888
timesteps.clear();
8989
for (int t = 0; t < width; ++t) {
90-
ComputeTopN(output.f(t), output.NumFeatures(), kBeamWidths[0]);
90+
ComputeTopN(output.f(t), output.NumFeatures(), kBeamWidths[0], charset);
9191
DecodeStep(output.f(t), t, dict_ratio, cert_offset, worst_dict_cert,
9292
charset);
9393
if (lstm_choice_mode) {
@@ -102,7 +102,7 @@ void RecodeBeamSearch::Decode(const GENERIC_2D_ARRAY<float>& output,
102102
beam_size_ = 0;
103103
int width = output.dim1();
104104
for (int t = 0; t < width; ++t) {
105-
ComputeTopN(output[t], output.dim2(), kBeamWidths[0]);
105+
ComputeTopN(output[t], output.dim2(), kBeamWidths[0], charset);
106106
DecodeStep(output[t], t, dict_ratio, cert_offset, worst_dict_cert, charset);
107107
}
108108
}
@@ -456,12 +456,19 @@ WERD_RES* RecodeBeamSearch::InitializeWord(bool leading_space,
456456
// Fills top_n_flags_ with bools that are true iff the corresponding output
457457
// is one of the top_n.
458458
void RecodeBeamSearch::ComputeTopN(const float* outputs, int num_outputs,
459-
int top_n) {
459+
int top_n, const UNICHARSET* charset) {
460460
top_n_flags_.init_to_size(num_outputs, TN_ALSO_RAN);
461461
top_code_ = -1;
462462
second_code_ = -1;
463463
top_heap_.clear();
464464
for (int i = 0; i < num_outputs; ++i) {
465+
// Decode label via recoder_.
466+
RecodedCharID code;
467+
code.Set(0, i);
468+
int label = recoder_.DecodeUnichar(code);
469+
if (label != INVALID_UNICHAR_ID && // not part of a bigger code.
470+
!charset->get_enabled(label)) // disabled
471+
continue;
465472
if (top_heap_.size() < top_n || outputs[i] > top_heap_.PeekTop().key) {
466473
TopPair entry(outputs[i], i);
467474
top_heap_.Push(&entry);

src/lstm/recodebeam.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ class RecodeBeamSearch {
293293

294294
// Fills top_n_flags_ with bools that are true iff the corresponding output
295295
// is one of the top_n.
296-
void ComputeTopN(const float* outputs, int num_outputs, int top_n);
296+
void ComputeTopN(const float* outputs, int num_outputs, int top_n, const UNICHARSET* unicharset);
297297

298298
// Adds the computation for the current time-step to the beam. Call at each
299299
// time-step in sequence from left to right. outputs is the activation vector

0 commit comments

Comments
 (0)