-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Fixed the GetUTF8Text method to return nullptr instead of asserting #4451
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zdenop
wants to merge
1
commit into
main
Choose a base branch
from
warp2_test-20250810
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+52
−28
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,7 +49,9 @@ char *LTRResultIterator::GetUTF8Text(PageIteratorLevel level) const { | |
std::string text; | ||
PAGE_RES_IT res_it(*it_); | ||
WERD_CHOICE *best_choice = res_it.word()->best_choice; | ||
ASSERT_HOST(best_choice != nullptr); | ||
if (best_choice == nullptr) { | ||
return nullptr; // No recognition results available | ||
} | ||
if (level == RIL_SYMBOL) { | ||
text = res_it.word()->BestUTF8(blob_index_, false); | ||
} else if (level == RIL_WORD) { | ||
|
@@ -61,7 +63,9 @@ char *LTRResultIterator::GetUTF8Text(PageIteratorLevel level) const { | |
do { // for each text line in a paragraph | ||
do { // for each word in a text line | ||
best_choice = res_it.word()->best_choice; | ||
ASSERT_HOST(best_choice != nullptr); | ||
if (best_choice == nullptr) { | ||
break; // Skip words without recognition results | ||
} | ||
text += best_choice->unichar_string(); | ||
text += " "; | ||
res_it.forward(); | ||
|
@@ -104,37 +108,47 @@ float LTRResultIterator::Confidence(PageIteratorLevel level) const { | |
case RIL_BLOCK: | ||
do { | ||
best_choice = res_it.word()->best_choice; | ||
mean_certainty += best_choice->certainty(); | ||
++certainty_count; | ||
if (best_choice != nullptr) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are there situations where the condition is false? |
||
mean_certainty += best_choice->certainty(); | ||
++certainty_count; | ||
} | ||
res_it.forward(); | ||
} while (res_it.block() == res_it.prev_block()); | ||
break; | ||
case RIL_PARA: | ||
do { | ||
best_choice = res_it.word()->best_choice; | ||
mean_certainty += best_choice->certainty(); | ||
++certainty_count; | ||
if (best_choice != nullptr) { | ||
mean_certainty += best_choice->certainty(); | ||
++certainty_count; | ||
} | ||
res_it.forward(); | ||
} while (res_it.block() == res_it.prev_block() && | ||
res_it.row()->row->para() == res_it.prev_row()->row->para()); | ||
break; | ||
case RIL_TEXTLINE: | ||
do { | ||
best_choice = res_it.word()->best_choice; | ||
mean_certainty += best_choice->certainty(); | ||
++certainty_count; | ||
if (best_choice != nullptr) { | ||
mean_certainty += best_choice->certainty(); | ||
++certainty_count; | ||
} | ||
res_it.forward(); | ||
} while (res_it.row() == res_it.prev_row()); | ||
break; | ||
case RIL_WORD: | ||
best_choice = res_it.word()->best_choice; | ||
mean_certainty = best_choice->certainty(); | ||
certainty_count = 1; | ||
if (best_choice != nullptr) { | ||
mean_certainty = best_choice->certainty(); | ||
certainty_count = 1; | ||
} | ||
break; | ||
case RIL_SYMBOL: | ||
best_choice = res_it.word()->best_choice; | ||
mean_certainty = best_choice->certainty(blob_index_); | ||
certainty_count = 1; | ||
if (best_choice != nullptr) { | ||
mean_certainty = best_choice->certainty(blob_index_); | ||
certainty_count = 1; | ||
} | ||
} | ||
if (certainty_count > 0) { | ||
mean_certainty /= certainty_count; | ||
|
@@ -226,8 +240,8 @@ StrongScriptDirection LTRResultIterator::WordDirection() const { | |
|
||
// Returns true if the current word was found in a dictionary. | ||
bool LTRResultIterator::WordIsFromDictionary() const { | ||
if (it_->word() == nullptr) { | ||
return false; // Already at the end! | ||
if (it_->word() == nullptr || it_->word()->best_choice == nullptr) { | ||
return false; // Already at the end or no recognition results! | ||
} | ||
int permuter = it_->word()->best_choice->permuter(); | ||
return permuter == SYSTEM_DAWG_PERM || permuter == FREQ_DAWG_PERM || permuter == USER_DAWG_PERM; | ||
|
@@ -243,8 +257,8 @@ int LTRResultIterator::BlanksBeforeWord() const { | |
|
||
// Returns true if the current word is numeric. | ||
bool LTRResultIterator::WordIsNumeric() const { | ||
if (it_->word() == nullptr) { | ||
return false; // Already at the end! | ||
if (it_->word() == nullptr || it_->word()->best_choice == nullptr) { | ||
return false; // Already at the end or no recognition results! | ||
} | ||
int permuter = it_->word()->best_choice->permuter(); | ||
return permuter == NUMBER_PERM; | ||
|
@@ -315,8 +329,11 @@ char *LTRResultIterator::WordNormedUTF8Text() const { | |
if (it_->word() == nullptr) { | ||
return nullptr; // Already at the end! | ||
} | ||
std::string ocr_text; | ||
WERD_CHOICE *best_choice = it_->word()->best_choice; | ||
if (best_choice == nullptr) { | ||
return nullptr; // No recognition results available | ||
} | ||
std::string ocr_text; | ||
const UNICHARSET *unicharset = it_->word()->uch_set; | ||
for (unsigned i = 0; i < best_choice->length(); ++i) { | ||
ocr_text += unicharset->get_normed_unichar(best_choice->unichar_id(i)); | ||
|
@@ -341,7 +358,7 @@ const char *LTRResultIterator::WordLattice(int *lattice_size) const { | |
// If iterating at a higher level object than symbols, eg words, then | ||
// this will return the attributes of the first symbol in that word. | ||
bool LTRResultIterator::SymbolIsSuperscript() const { | ||
if (cblob_it_ == nullptr && it_->word() != nullptr) { | ||
if (cblob_it_ == nullptr && it_->word() != nullptr && it_->word()->best_choice != nullptr) { | ||
return it_->word()->best_choice->BlobPosition(blob_index_) == SP_SUPERSCRIPT; | ||
} | ||
return false; | ||
|
@@ -351,7 +368,7 @@ bool LTRResultIterator::SymbolIsSuperscript() const { | |
// If iterating at a higher level object than symbols, eg words, then | ||
// this will return the attributes of the first symbol in that word. | ||
bool LTRResultIterator::SymbolIsSubscript() const { | ||
if (cblob_it_ == nullptr && it_->word() != nullptr) { | ||
if (cblob_it_ == nullptr && it_->word() != nullptr && it_->word()->best_choice != nullptr) { | ||
return it_->word()->best_choice->BlobPosition(blob_index_) == SP_SUBSCRIPT; | ||
} | ||
return false; | ||
|
@@ -361,7 +378,7 @@ bool LTRResultIterator::SymbolIsSubscript() const { | |
// If iterating at a higher level object than symbols, eg words, then | ||
// this will return the attributes of the first symbol in that word. | ||
bool LTRResultIterator::SymbolIsDropcap() const { | ||
if (cblob_it_ == nullptr && it_->word() != nullptr) { | ||
if (cblob_it_ == nullptr && it_->word() != nullptr && it_->word()->best_choice != nullptr) { | ||
return it_->word()->best_choice->BlobPosition(blob_index_) == SP_DROPCAP; | ||
} | ||
return false; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there an example which triggers the assertion here? I am not sure whether the
break
is the right solution here. Maybe the loop should continue at line 71.