tesseract  4.1.0
WERD_CHOICE Class Reference

#include <ratngs.h>

Inheritance diagram for WERD_CHOICE:
ELIST_LINK

Public Member Functions

 WERD_CHOICE (const UNICHARSET *unicharset)
 
 WERD_CHOICE (const UNICHARSET *unicharset, int reserved)
 
 WERD_CHOICE (const char *src_string, const char *src_lengths, float src_rating, float src_certainty, uint8_t src_permuter, const UNICHARSET &unicharset)
 
 WERD_CHOICE (const char *src_string, const UNICHARSET &unicharset)
 
 WERD_CHOICE (const WERD_CHOICE &word)
 
 ~WERD_CHOICE ()
 
const UNICHARSETunicharset () const
 
int length () const
 
float adjust_factor () const
 
void set_adjust_factor (float factor)
 
const UNICHAR_IDunichar_ids () const
 
UNICHAR_ID unichar_id (int index) const
 
int state (int index) const
 
tesseract::ScriptPos BlobPosition (int index) const
 
float rating () const
 
float certainty () const
 
float certainty (int index) const
 
float min_x_height () const
 
float max_x_height () const
 
void set_x_heights (float min_height, float max_height)
 
uint8_t permuter () const
 
const char * permuter_name () const
 
BLOB_CHOICE_LIST * blob_choices (int index, MATRIX *ratings) const
 
MATRIX_COORD MatrixCoord (int index) const
 
void set_unichar_id (UNICHAR_ID unichar_id, int index)
 
bool dangerous_ambig_found () const
 
void set_dangerous_ambig_found_ (bool value)
 
void set_rating (float new_val)
 
void set_certainty (float new_val)
 
void set_permuter (uint8_t perm)
 
void set_length (int len)
 
void double_the_size ()
 Make more space in unichar_id_ and fragment_lengths_ arrays. More...
 
void init (int reserved)
 
void init (const char *src_string, const char *src_lengths, float src_rating, float src_certainty, uint8_t src_permuter)
 
void make_bad ()
 Set the fields in this choice to be default (bad) values. More...
 
void append_unichar_id_space_allocated (UNICHAR_ID unichar_id, int blob_count, float rating, float certainty)
 
void append_unichar_id (UNICHAR_ID unichar_id, int blob_count, float rating, float certainty)
 
void set_unichar_id (UNICHAR_ID unichar_id, int blob_count, float rating, float certainty, int index)
 
void set_blob_choice (int index, int blob_count, const BLOB_CHOICE *blob_choice)
 
bool contains_unichar_id (UNICHAR_ID unichar_id) const
 
void remove_unichar_ids (int index, int num)
 
void remove_last_unichar_id ()
 
void remove_unichar_id (int index)
 
bool has_rtl_unichar_id () const
 
void reverse_and_mirror_unichar_ids ()
 
void punct_stripped (int *start_core, int *end_core) const
 
void GetNonSuperscriptSpan (int *start, int *end) const
 
WERD_CHOICE shallow_copy (int start, int end) const
 
void string_and_lengths (STRING *word_str, STRING *word_lengths_str) const
 
const STRING debug_string () const
 
bool ContainsAnyNonSpaceDelimited () const
 
bool IsAllSpaces () const
 
bool set_unichars_in_script_order (bool in_script_order)
 
bool unichars_in_script_order () const
 
const STRINGunichar_string () const
 
const STRINGunichar_lengths () const
 
void SetScriptPositions (bool small_caps, TWERD *word, int debug=0)
 
void SetScriptPositions (const tesseract::ScriptPos *positions, int length)
 
void SetAllScriptPositions (tesseract::ScriptPos position)
 
int GetTopScriptID () const
 
void UpdateStateForSplit (int blob_position)
 
int TotalOfStates () const
 
void print () const
 
void print (const char *msg) const
 
void print_state (const char *msg) const
 
void DisplaySegmentation (TWERD *word)
 
WERD_CHOICEoperator+= (const WERD_CHOICE &second)
 
WERD_CHOICEoperator= (const WERD_CHOICE &source)
 
- Public Member Functions inherited from ELIST_LINK
 ELIST_LINK ()
 
 ELIST_LINK (const ELIST_LINK &)
 
void operator= (const ELIST_LINK &)
 

Static Public Member Functions

static const char * permuter_name (uint8_t permuter)
 
static tesseract::ScriptPos ScriptPositionOf (bool print_debug, const UNICHARSET &unicharset, const TBOX &blob_box, UNICHAR_ID unichar_id)
 

Static Public Attributes

static const float kBadRating = 100000.0
 

Detailed Description

Definition at line 273 of file ratngs.h.

Constructor & Destructor Documentation

WERD_CHOICE::WERD_CHOICE ( const UNICHARSET unicharset)
inline

Definition at line 278 of file ratngs.h.

279  : unicharset_(unicharset) { this->init(8); }
void init(int reserved)
Definition: ratngs.h:409
WERD_CHOICE::WERD_CHOICE ( const UNICHARSET unicharset,
int  reserved 
)
inline

Definition at line 280 of file ratngs.h.

281  : unicharset_(unicharset) { this->init(reserved); }
void init(int reserved)
Definition: ratngs.h:409
WERD_CHOICE::WERD_CHOICE ( const char *  src_string,
const char *  src_lengths,
float  src_rating,
float  src_certainty,
uint8_t  src_permuter,
const UNICHARSET unicharset 
)
inline

Definition at line 282 of file ratngs.h.

288  : unicharset_(&unicharset) {
289  this->init(src_string, src_lengths, src_rating,
290  src_certainty, src_permuter);
291  }
void init(int reserved)
Definition: ratngs.h:409
WERD_CHOICE::WERD_CHOICE ( const char *  src_string,
const UNICHARSET unicharset 
)

WERD_CHOICE::WERD_CHOICE

Constructor to build a WERD_CHOICE from the given string. The function assumes that src_string is not nullptr.

Definition at line 218 of file ratngs.cpp.

220  : unicharset_(&unicharset){
221  GenericVector<UNICHAR_ID> encoding;
222  GenericVector<char> lengths;
223  std::string cleaned = unicharset.CleanupString(src_string);
224  if (unicharset.encode_string(cleaned.c_str(), true, &encoding, &lengths,
225  nullptr)) {
226  lengths.push_back('\0');
227  STRING src_lengths = &lengths[0];
228  this->init(cleaned.c_str(), src_lengths.string(), 0.0, 0.0, NO_PERM);
229  } else { // There must have been an invalid unichar in the string.
230  this->init(8);
231  this->make_bad();
232  }
233 }
void init(int reserved)
Definition: ratngs.h:409
Definition: strngs.h:45
static std::string CleanupString(const char *utf8_str)
Definition: unicharset.h:246
const char * string() const
Definition: strngs.cpp:194
void make_bad()
Set the fields in this choice to be default (bad) values.
Definition: ratngs.h:443
bool encode_string(const char *str, bool give_up_on_failure, GenericVector< UNICHAR_ID > *encoding, GenericVector< char > *lengths, int *encoded_length) const
Definition: unicharset.cpp:259
WERD_CHOICE::WERD_CHOICE ( const WERD_CHOICE word)
inline

Definition at line 293 of file ratngs.h.

294  : ELIST_LINK(word), unicharset_(word.unicharset_) {
295  this->init(word.length());
296  this->operator=(word);
297  }
void init(int reserved)
Definition: ratngs.h:409
int length() const
Definition: ratngs.h:303
WERD_CHOICE & operator=(const WERD_CHOICE &source)
Definition: ratngs.cpp:521
ELIST_LINK()
Definition: elst.h:90
WERD_CHOICE::~WERD_CHOICE ( )

WERD_CHOICE::~WERD_CHOICE

Definition at line 276 of file ratngs.cpp.

276  {
277  delete[] unichar_ids_;
278  delete[] script_pos_;
279  delete[] state_;
280  delete[] certainties_;
281 }

Member Function Documentation

float WERD_CHOICE::adjust_factor ( ) const
inline

Definition at line 306 of file ratngs.h.

306  {
307  return adjust_factor_;
308  }
void WERD_CHOICE::append_unichar_id ( UNICHAR_ID  unichar_id,
int  blob_count,
float  rating,
float  certainty 
)

append_unichar_id

Make sure there is enough space in the word for the new unichar id and call append_unichar_id_space_allocated().

Definition at line 468 of file ratngs.cpp.

470  {
471  if (length_ == reserved_) {
472  this->double_the_size();
473  }
475  rating, certainty);
476 }
void append_unichar_id_space_allocated(UNICHAR_ID unichar_id, int blob_count, float rating, float certainty)
Definition: ratngs.h:452
float rating() const
Definition: ratngs.h:327
void double_the_size()
Make more space in unichar_id_ and fragment_lengths_ arrays.
Definition: ratngs.h:387
float certainty() const
Definition: ratngs.h:330
UNICHAR_ID unichar_id(int index) const
Definition: ratngs.h:315
void WERD_CHOICE::append_unichar_id_space_allocated ( UNICHAR_ID  unichar_id,
int  blob_count,
float  rating,
float  certainty 
)
inline

This function assumes that there is enough space reserved in the WERD_CHOICE for adding another unichar. This is an efficient alternative to append_unichar_id().

Definition at line 452 of file ratngs.h.

454  {
455  assert(reserved_ > length_);
456  length_++;
457  this->set_unichar_id(unichar_id, blob_count,
458  rating, certainty, length_-1);
459  }
float rating() const
Definition: ratngs.h:327
void set_unichar_id(UNICHAR_ID unichar_id, int index)
Definition: ratngs.h:359
float certainty() const
Definition: ratngs.h:330
UNICHAR_ID unichar_id(int index) const
Definition: ratngs.h:315
BLOB_CHOICE_LIST * WERD_CHOICE::blob_choices ( int  index,
MATRIX ratings 
) const

Definition at line 290 of file ratngs.cpp.

290  {
291  MATRIX_COORD coord = MatrixCoord(index);
292  BLOB_CHOICE_LIST* result = ratings->get(coord.col, coord.row);
293  if (result == nullptr) {
294  result = new BLOB_CHOICE_LIST;
295  ratings->put(coord.col, coord.row, result);
296  }
297  return result;
298 }
MATRIX_COORD MatrixCoord(int index) const
Definition: ratngs.cpp:302
T get(ICOORD pos) const
Definition: matrix.h:231
void put(ICOORD pos, const T &thing)
Definition: matrix.h:223
tesseract::ScriptPos WERD_CHOICE::BlobPosition ( int  index) const
inline

Definition at line 322 of file ratngs.h.

322  {
323  if (index < 0 || index >= length_)
324  return tesseract::SP_NORMAL;
325  return script_pos_[index];
326  }
float WERD_CHOICE::certainty ( ) const
inline

Definition at line 330 of file ratngs.h.

330  {
331  return certainty_;
332  }
float WERD_CHOICE::certainty ( int  index) const
inline

Definition at line 333 of file ratngs.h.

333  {
334  return certainties_[index];
335  }
bool WERD_CHOICE::contains_unichar_id ( UNICHAR_ID  unichar_id) const

contains_unichar_id

Returns true if unichar_ids_ contain the given unichar_id, false otherwise.

Definition at line 326 of file ratngs.cpp.

326  {
327  for (int i = 0; i < length_; ++i) {
328  if (unichar_ids_[i] == unichar_id) {
329  return true;
330  }
331  }
332  return false;
333 }
UNICHAR_ID unichar_id(int index) const
Definition: ratngs.h:315
bool WERD_CHOICE::ContainsAnyNonSpaceDelimited ( ) const
inline

Definition at line 514 of file ratngs.h.

514  {
515  for (int i = 0; i < length_; ++i) {
516  if (!unicharset_->IsSpaceDelimited(unichar_ids_[i])) return true;
517  }
518  return false;
519  }
bool IsSpaceDelimited(UNICHAR_ID unichar_id) const
Definition: unicharset.h:652
bool WERD_CHOICE::dangerous_ambig_found ( ) const
inline

Definition at line 363 of file ratngs.h.

363  {
364  return dangerous_ambig_found_;
365  }
const STRING WERD_CHOICE::debug_string ( ) const
inline

Definition at line 505 of file ratngs.h.

505  {
506  STRING word_str;
507  for (int i = 0; i < length_; ++i) {
508  word_str += unicharset_->debug_str(unichar_ids_[i]);
509  word_str += " ";
510  }
511  return word_str;
512  }
Definition: strngs.h:45
STRING debug_str(UNICHAR_ID id) const
Definition: unicharset.cpp:343
void WERD_CHOICE::DisplaySegmentation ( TWERD word)

Definition at line 761 of file ratngs.cpp.

761  {
762 #ifndef GRAPHICS_DISABLED
763  // Number of different colors to draw with.
764  const int kNumColors = 6;
765  static ScrollView *segm_window = nullptr;
766  // Check the state against the static prev_drawn_state.
767  static GenericVector<int> prev_drawn_state;
768  bool already_done = prev_drawn_state.size() == length_;
769  if (!already_done) prev_drawn_state.init_to_size(length_, 0);
770  for (int i = 0; i < length_; ++i) {
771  if (prev_drawn_state[i] != state_[i]) {
772  already_done = false;
773  }
774  prev_drawn_state[i] = state_[i];
775  }
776  if (already_done || word->blobs.empty()) return;
777 
778  // Create the window if needed.
779  if (segm_window == nullptr) {
780  segm_window = new ScrollView("Segmentation", 5, 10, 500, 256,
781  2000.0, 256.0, true);
782  } else {
783  segm_window->Clear();
784  }
785 
786  TBOX bbox;
787  int blob_index = 0;
788  for (int c = 0; c < length_; ++c) {
789  auto color =
790  static_cast<ScrollView::Color>(c % kNumColors + 3);
791  for (int i = 0; i < state_[c]; ++i, ++blob_index) {
792  TBLOB* blob = word->blobs[blob_index];
793  bbox += blob->bounding_box();
794  blob->plot(segm_window, color, color);
795  }
796  }
797  segm_window->ZoomToRectangle(bbox.left(), bbox.top(),
798  bbox.right(), bbox.bottom());
799  segm_window->Update();
800  window_wait(segm_window);
801 #endif
802 }
int16_t top() const
Definition: rect.h:58
Definition: rect.h:34
GenericVector< TBLOB * > blobs
Definition: blobs.h:438
void ZoomToRectangle(int x1, int y1, int x2, int y2)
Definition: scrollview.cpp:757
TBOX bounding_box() const
Definition: blobs.cpp:472
Definition: blobs.h:263
void init_to_size(int size, const T &t)
static void Update()
Definition: scrollview.cpp:709
void plot(ScrollView *window, ScrollView::Color color, ScrollView::Color child_color)
Definition: blobs.cpp:514
char window_wait(ScrollView *win)
Definition: callcpp.cpp:103
void Clear()
Definition: scrollview.cpp:589
bool empty() const
Definition: genericvector.h:89
int16_t right() const
Definition: rect.h:79
int16_t bottom() const
Definition: rect.h:65
int16_t left() const
Definition: rect.h:72
int size() const
Definition: genericvector.h:70
void WERD_CHOICE::double_the_size ( )
inline

Make more space in unichar_id_ and fragment_lengths_ arrays.

Definition at line 387 of file ratngs.h.

387  {
388  if (reserved_ > 0) {
390  reserved_, unichar_ids_);
392  reserved_, script_pos_);
394  reserved_, state_);
396  reserved_, certainties_);
397  reserved_ *= 2;
398  } else {
399  unichar_ids_ = new UNICHAR_ID[1];
400  script_pos_ = new tesseract::ScriptPos[1];
401  state_ = new int[1];
402  certainties_ = new float[1];
403  reserved_ = 1;
404  }
405  }
int UNICHAR_ID
Definition: unichar.h:34
static T * double_the_size_memcpy(int current_size, T *data)
void WERD_CHOICE::GetNonSuperscriptSpan ( int *  start,
int *  end 
) const

Definition at line 397 of file ratngs.cpp.

397  {
398  int end = length();
399  while (end > 0 &&
400  unicharset_->get_isdigit(unichar_ids_[end - 1]) &&
402  end--;
403  }
404  int start = 0;
405  while (start < end &&
406  unicharset_->get_isdigit(unichar_ids_[start]) &&
408  start++;
409  }
410  *pstart = start;
411  *pend = end;
412 }
int length() const
Definition: ratngs.h:303
bool get_isdigit(UNICHAR_ID unichar_id) const
Definition: unicharset.h:512
tesseract::ScriptPos BlobPosition(int index) const
Definition: ratngs.h:322
int WERD_CHOICE::GetTopScriptID ( ) const

Definition at line 667 of file ratngs.cpp.

667  {
668  int max_script = unicharset_->get_script_table_size();
669  int *sid = new int[max_script];
670  int x;
671  for (x = 0; x < max_script; x++) sid[x] = 0;
672  for (x = 0; x < length_; ++x) {
673  int script_id = unicharset_->get_script(unichar_id(x));
674  sid[script_id]++;
675  }
676  if (unicharset_->han_sid() != unicharset_->null_sid()) {
677  // Add the Hiragana & Katakana counts to Han and zero them out.
678  if (unicharset_->hiragana_sid() != unicharset_->null_sid()) {
679  sid[unicharset_->han_sid()] += sid[unicharset_->hiragana_sid()];
680  sid[unicharset_->hiragana_sid()] = 0;
681  }
682  if (unicharset_->katakana_sid() != unicharset_->null_sid()) {
683  sid[unicharset_->han_sid()] += sid[unicharset_->katakana_sid()];
684  sid[unicharset_->katakana_sid()] = 0;
685  }
686  }
687  // Note that high script ID overrides lower one on a tie, thus biasing
688  // towards non-Common script (if sorted that way in unicharset file).
689  int max_sid = 0;
690  for (x = 1; x < max_script; x++)
691  if (sid[x] >= sid[max_sid]) max_sid = x;
692  if (sid[max_sid] < length_ / 2)
693  max_sid = unicharset_->null_sid();
694  delete[] sid;
695  return max_sid;
696 }
int null_sid() const
Definition: unicharset.h:884
int hiragana_sid() const
Definition: unicharset.h:890
int get_script_table_size() const
Definition: unicharset.h:849
UNICHAR_ID unichar_id(int index) const
Definition: ratngs.h:315
int get_script(UNICHAR_ID unichar_id) const
Definition: unicharset.h:663
int han_sid() const
Definition: unicharset.h:889
int katakana_sid() const
Definition: unicharset.h:891
bool WERD_CHOICE::has_rtl_unichar_id ( ) const

has_rtl_unichar_id

Returns true if unichar_ids contain at least one "strongly" RTL unichar.

Definition at line 431 of file ratngs.cpp.

431  {
432  int i;
433  for (i = 0; i < length_; ++i) {
434  UNICHARSET::Direction dir = unicharset_->get_direction(unichar_ids_[i]);
435  if (dir == UNICHARSET::U_RIGHT_TO_LEFT ||
437  return true;
438  }
439  }
440  return false;
441 }
Direction get_direction(UNICHAR_ID unichar_id) const
Definition: unicharset.h:690
void WERD_CHOICE::init ( int  reserved)
inline

Initializes WERD_CHOICE - reserves length slots in unichar_ids_ and fragment_length_ arrays. Sets other values to default (blank) values.

Definition at line 409 of file ratngs.h.

409  {
410  reserved_ = reserved;
411  if (reserved > 0) {
412  unichar_ids_ = new UNICHAR_ID[reserved];
413  script_pos_ = new tesseract::ScriptPos[reserved];
414  state_ = new int[reserved];
415  certainties_ = new float[reserved];
416  } else {
417  unichar_ids_ = nullptr;
418  script_pos_ = nullptr;
419  state_ = nullptr;
420  certainties_ = nullptr;
421  }
422  length_ = 0;
423  adjust_factor_ = 1.0f;
424  rating_ = 0.0;
425  certainty_ = FLT_MAX;
426  min_x_height_ = 0.0f;
427  max_x_height_ = FLT_MAX;
428  permuter_ = NO_PERM;
429  unichars_in_script_order_ = false; // Tesseract is strict left-to-right.
430  dangerous_ambig_found_ = false;
431  }
int UNICHAR_ID
Definition: unichar.h:34
void WERD_CHOICE::init ( const char *  src_string,
const char *  src_lengths,
float  src_rating,
float  src_certainty,
uint8_t  src_permuter 
)

Helper function to build a WERD_CHOICE from the given string, fragment lengths, rating, certainty and permuter. The function assumes that src_string is not nullptr. src_lengths argument could be nullptr, in which case the unichars in src_string are assumed to all be of length 1.

WERD_CHOICE::init

Helper function to build a WERD_CHOICE from the given string, fragment lengths, rating, certainty and permuter.

The function assumes that src_string is not nullptr. src_lengths argument could be nullptr, in which case the unichars in src_string are assumed to all be of length 1.

Definition at line 245 of file ratngs.cpp.

249  {
250  int src_string_len = strlen(src_string);
251  if (src_string_len == 0) {
252  this->init(8);
253  } else {
254  this->init(src_lengths ? strlen(src_lengths): src_string_len);
255  length_ = reserved_;
256  int offset = 0;
257  for (int i = 0; i < length_; ++i) {
258  int unichar_length = src_lengths ? src_lengths[i] : 1;
259  unichar_ids_[i] =
260  unicharset_->unichar_to_id(src_string+offset, unichar_length);
261  state_[i] = 1;
262  certainties_[i] = src_certainty;
263  offset += unichar_length;
264  }
265  }
266  adjust_factor_ = 1.0f;
267  rating_ = src_rating;
268  certainty_ = src_certainty;
269  permuter_ = src_permuter;
270  dangerous_ambig_found_ = false;
271 }
void init(int reserved)
Definition: ratngs.h:409
UNICHAR_ID unichar_to_id(const char *const unichar_repr) const
Definition: unicharset.cpp:210
bool WERD_CHOICE::IsAllSpaces ( ) const
inline

Definition at line 521 of file ratngs.h.

521  {
522  for (int i = 0; i < length_; ++i) {
523  if (unichar_ids_[i] != UNICHAR_SPACE) return false;
524  }
525  return true;
526  }
int WERD_CHOICE::length ( ) const
inline

Definition at line 303 of file ratngs.h.

303  {
304  return length_;
305  }
void WERD_CHOICE::make_bad ( )
inline

Set the fields in this choice to be default (bad) values.

Definition at line 443 of file ratngs.h.

443  {
444  length_ = 0;
445  rating_ = kBadRating;
446  certainty_ = -FLT_MAX;
447  }
static const float kBadRating
Definition: ratngs.h:275
MATRIX_COORD WERD_CHOICE::MatrixCoord ( int  index) const

Definition at line 302 of file ratngs.cpp.

302  {
303  int col = 0;
304  for (int i = 0; i < index; ++i)
305  col += state_[i];
306  int row = col + state_[index] - 1;
307  return MATRIX_COORD(col, row);
308 }
float WERD_CHOICE::max_x_height ( ) const
inline

Definition at line 339 of file ratngs.h.

339  {
340  return max_x_height_;
341  }
float WERD_CHOICE::min_x_height ( ) const
inline

Definition at line 336 of file ratngs.h.

336  {
337  return min_x_height_;
338  }
WERD_CHOICE & WERD_CHOICE::operator+= ( const WERD_CHOICE second)

WERD_CHOICE::operator+=

Cat a second word rating on the end of this current one. The ratings are added and the confidence is the min. If the permuters are NOT the same the permuter is set to COMPOUND_PERM

Definition at line 485 of file ratngs.cpp.

485  {
486  ASSERT_HOST(unicharset_ == second.unicharset_);
487  while (reserved_ < length_ + second.length()) {
488  this->double_the_size();
489  }
490  const UNICHAR_ID *other_unichar_ids = second.unichar_ids();
491  for (int i = 0; i < second.length(); ++i) {
492  unichar_ids_[length_ + i] = other_unichar_ids[i];
493  state_[length_ + i] = second.state_[i];
494  certainties_[length_ + i] = second.certainties_[i];
495  script_pos_[length_ + i] = second.BlobPosition(i);
496  }
497  length_ += second.length();
498  if (second.adjust_factor_ > adjust_factor_)
499  adjust_factor_ = second.adjust_factor_;
500  rating_ += second.rating(); // add ratings
501  if (second.certainty() < certainty_) // take min
502  certainty_ = second.certainty();
503  if (second.dangerous_ambig_found_)
504  dangerous_ambig_found_ = true;
505  if (permuter_ == NO_PERM) {
506  permuter_ = second.permuter();
507  } else if (second.permuter() != NO_PERM &&
508  second.permuter() != permuter_) {
509  permuter_ = COMPOUND_PERM;
510  }
511  return *this;
512 }
const UNICHAR_ID * unichar_ids() const
Definition: ratngs.h:312
int length() const
Definition: ratngs.h:303
float rating() const
Definition: ratngs.h:327
void double_the_size()
Make more space in unichar_id_ and fragment_lengths_ arrays.
Definition: ratngs.h:387
float certainty() const
Definition: ratngs.h:330
#define ASSERT_HOST(x)
Definition: errcode.h:88
int UNICHAR_ID
Definition: unichar.h:34
tesseract::ScriptPos BlobPosition(int index) const
Definition: ratngs.h:322
uint8_t permuter() const
Definition: ratngs.h:346
WERD_CHOICE & WERD_CHOICE::operator= ( const WERD_CHOICE source)

WERD_CHOICE::operator=

Allocate enough memory to hold a copy of source and copy over all the information from source to this WERD_CHOICE.

Definition at line 521 of file ratngs.cpp.

521  {
522  while (reserved_ < source.length()) {
523  this->double_the_size();
524  }
525 
526  unicharset_ = source.unicharset_;
527  const UNICHAR_ID *other_unichar_ids = source.unichar_ids();
528  for (int i = 0; i < source.length(); ++i) {
529  unichar_ids_[i] = other_unichar_ids[i];
530  state_[i] = source.state_[i];
531  certainties_[i] = source.certainties_[i];
532  script_pos_[i] = source.BlobPosition(i);
533  }
534  length_ = source.length();
535  adjust_factor_ = source.adjust_factor_;
536  rating_ = source.rating();
537  certainty_ = source.certainty();
538  min_x_height_ = source.min_x_height();
539  max_x_height_ = source.max_x_height();
540  permuter_ = source.permuter();
541  dangerous_ambig_found_ = source.dangerous_ambig_found_;
542  return *this;
543 }
const UNICHAR_ID * unichar_ids() const
Definition: ratngs.h:312
float min_x_height() const
Definition: ratngs.h:336
int length() const
Definition: ratngs.h:303
float rating() const
Definition: ratngs.h:327
void double_the_size()
Make more space in unichar_id_ and fragment_lengths_ arrays.
Definition: ratngs.h:387
float certainty() const
Definition: ratngs.h:330
int UNICHAR_ID
Definition: unichar.h:34
tesseract::ScriptPos BlobPosition(int index) const
Definition: ratngs.h:322
float max_x_height() const
Definition: ratngs.h:339
uint8_t permuter() const
Definition: ratngs.h:346
uint8_t WERD_CHOICE::permuter ( ) const
inline

Definition at line 346 of file ratngs.h.

346  {
347  return permuter_;
348  }
const char * WERD_CHOICE::permuter_name ( uint8_t  permuter)
static

Definition at line 194 of file ratngs.cpp.

194  {
195  return kPermuterTypeNames[permuter];
196 }
uint8_t permuter() const
Definition: ratngs.h:346
const char * WERD_CHOICE::permuter_name ( ) const

Definition at line 283 of file ratngs.cpp.

283  {
284  return kPermuterTypeNames[permuter_];
285 }
void WERD_CHOICE::print ( ) const
inline

Definition at line 580 of file ratngs.h.

580 { this->print(""); }
void print() const
Definition: ratngs.h:580
void WERD_CHOICE::print ( const char *  msg) const

WERD_CHOICE::print

Print WERD_CHOICE to stdout.

Definition at line 724 of file ratngs.cpp.

724  {
725  tprintf("%s : ", msg);
726  for (int i = 0; i < length_; ++i) {
727  tprintf("%s", unicharset_->id_to_unichar(unichar_ids_[i]));
728  }
729  tprintf(" : R=%g, C=%g, F=%g, Perm=%d, xht=[%g,%g], ambig=%d\n",
730  rating_, certainty_, adjust_factor_, permuter_,
731  min_x_height_, max_x_height_, dangerous_ambig_found_);
732  tprintf("pos");
733  for (int i = 0; i < length_; ++i) {
734  tprintf("\t%s", ScriptPosToString(script_pos_[i]));
735  }
736  tprintf("\nstr");
737  for (int i = 0; i < length_; ++i) {
738  tprintf("\t%s", unicharset_->id_to_unichar(unichar_ids_[i]));
739  }
740  tprintf("\nstate:");
741  for (int i = 0; i < length_; ++i) {
742  tprintf("\t%d ", state_[i]);
743  }
744  tprintf("\nC");
745  for (int i = 0; i < length_; ++i) {
746  tprintf("\t%.3f", certainties_[i]);
747  }
748  tprintf("\n");
749 }
const char * id_to_unichar(UNICHAR_ID id) const
Definition: unicharset.cpp:291
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:36
const char * ScriptPosToString(enum ScriptPos script_pos)
Definition: ratngs.cpp:200
void WERD_CHOICE::print_state ( const char *  msg) const

Definition at line 752 of file ratngs.cpp.

752  {
753  tprintf("%s", msg);
754  for (int i = 0; i < length_; ++i)
755  tprintf(" %d", state_[i]);
756  tprintf("\n");
757 }
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:36
void WERD_CHOICE::punct_stripped ( int *  start,
int *  end 
) const

punct_stripped

Returns the half-open interval of unichar_id indices [start, end) which enclose the core portion of this word – the part after stripping punctuation from the left and right.

Definition at line 383 of file ratngs.cpp.

383  {
384  *start = 0;
385  *end = length() - 1;
386  while (*start < length() &&
387  unicharset()->get_ispunctuation(unichar_id(*start))) {
388  (*start)++;
389  }
390  while (*end > -1 &&
391  unicharset()->get_ispunctuation(unichar_id(*end))) {
392  (*end)--;
393  }
394  (*end)++;
395 }
int length() const
Definition: ratngs.h:303
UNICHAR_ID unichar_id(int index) const
Definition: ratngs.h:315
const UNICHARSET * unicharset() const
Definition: ratngs.h:300
float WERD_CHOICE::rating ( ) const
inline

Definition at line 327 of file ratngs.h.

327  {
328  return rating_;
329  }
void WERD_CHOICE::remove_last_unichar_id ( )
inline

Definition at line 483 of file ratngs.h.

483 { --length_; }
void WERD_CHOICE::remove_unichar_id ( int  index)
inline

Definition at line 484 of file ratngs.h.

484  {
485  this->remove_unichar_ids(index, 1);
486  }
void remove_unichar_ids(int index, int num)
Definition: ratngs.cpp:342
void WERD_CHOICE::remove_unichar_ids ( int  start,
int  num 
)

remove_unichar_ids

Removes num unichar ids starting from index start from unichar_ids_ and updates length_ and fragment_lengths_ to reflect this change. Note: this function does not modify rating_ and certainty_.

Definition at line 342 of file ratngs.cpp.

342  {
343  ASSERT_HOST(start >= 0 && start + num <= length_);
344  // Accumulate the states to account for the merged blobs.
345  for (int i = 0; i < num; ++i) {
346  if (start > 0)
347  state_[start - 1] += state_[start + i];
348  else if (start + num < length_)
349  state_[start + num] += state_[start + i];
350  }
351  for (int i = start; i + num < length_; ++i) {
352  unichar_ids_[i] = unichar_ids_[i + num];
353  script_pos_[i] = script_pos_[i + num];
354  state_[i] = state_[i + num];
355  certainties_[i] = certainties_[i + num];
356  }
357  length_ -= num;
358 }
#define ASSERT_HOST(x)
Definition: errcode.h:88
void WERD_CHOICE::reverse_and_mirror_unichar_ids ( )

reverse_and_mirror_unichar_ids

Reverses and mirrors unichars in unichar_ids.

Definition at line 365 of file ratngs.cpp.

365  {
366  for (int i = 0; i < length_ / 2; ++i) {
367  UNICHAR_ID tmp_id = unichar_ids_[i];
368  unichar_ids_[i] = unicharset_->get_mirror(unichar_ids_[length_-1-i]);
369  unichar_ids_[length_-1-i] = unicharset_->get_mirror(tmp_id);
370  }
371  if (length_ % 2 != 0) {
372  unichar_ids_[length_/2] = unicharset_->get_mirror(unichar_ids_[length_/2]);
373  }
374 }
UNICHAR_ID get_mirror(UNICHAR_ID unichar_id) const
Definition: unicharset.h:697
int UNICHAR_ID
Definition: unichar.h:34
ScriptPos WERD_CHOICE::ScriptPositionOf ( bool  print_debug,
const UNICHARSET unicharset,
const TBOX blob_box,
UNICHAR_ID  unichar_id 
)
static

Definition at line 629 of file ratngs.cpp.

632  {
634  int top = blob_box.top();
635  int bottom = blob_box.bottom();
636  int min_bottom, max_bottom, min_top, max_top;
637  unicharset.get_top_bottom(unichar_id,
638  &min_bottom, &max_bottom,
639  &min_top, &max_top);
640 
641  int sub_thresh_top = min_top - kMinSubscriptOffset;
642  int sub_thresh_bot = kBlnBaselineOffset - kMinSubscriptOffset;
643  int sup_thresh_bot = max_bottom + kMinSuperscriptOffset;
644  if (bottom <= kMaxDropCapBottom) {
645  retval = tesseract::SP_DROPCAP;
646  } else if (top < sub_thresh_top && bottom < sub_thresh_bot) {
647  retval = tesseract::SP_SUBSCRIPT;
648  } else if (bottom > sup_thresh_bot) {
649  retval = tesseract::SP_SUPERSCRIPT;
650  }
651 
652  if (print_debug) {
653  const char *pos = ScriptPosToString(retval);
654  tprintf("%s Character %s[bot:%d top: %d] "
655  "bot_range[%d,%d] top_range[%d, %d] "
656  "sub_thresh[bot:%d top:%d] sup_thresh_bot %d\n",
657  pos, unicharset.id_to_unichar(unichar_id),
658  bottom, top,
659  min_bottom, max_bottom, min_top, max_top,
660  sub_thresh_bot, sub_thresh_top,
661  sup_thresh_bot);
662  }
663  return retval;
664 }
int16_t top() const
Definition: rect.h:58
const int kMinSubscriptOffset
Definition: ratngs.cpp:43
const char * id_to_unichar(UNICHAR_ID id) const
Definition: unicharset.cpp:291
const int kBlnBaselineOffset
Definition: normalis.h:25
const int kMinSuperscriptOffset
Definition: ratngs.cpp:45
void get_top_bottom(UNICHAR_ID unichar_id, int *min_bottom, int *max_bottom, int *min_top, int *max_top) const
Definition: unicharset.h:568
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:36
const int kMaxDropCapBottom
Definition: ratngs.cpp:47
int16_t bottom() const
Definition: rect.h:65
UNICHAR_ID unichar_id(int index) const
Definition: ratngs.h:315
const char * ScriptPosToString(enum ScriptPos script_pos)
Definition: ratngs.cpp:200
void WERD_CHOICE::set_adjust_factor ( float  factor)
inline

Definition at line 309 of file ratngs.h.

309  {
310  adjust_factor_ = factor;
311  }
void WERD_CHOICE::set_blob_choice ( int  index,
int  blob_count,
const BLOB_CHOICE blob_choice 
)

Definition at line 312 of file ratngs.cpp.

313  {
314  unichar_ids_[index] = blob_choice->unichar_id();
315  script_pos_[index] = tesseract::SP_NORMAL;
316  state_[index] = blob_count;
317  certainties_[index] = blob_choice->certainty();
318 }
UNICHAR_ID unichar_id() const
Definition: ratngs.h:77
float certainty() const
Definition: ratngs.h:83
void WERD_CHOICE::set_certainty ( float  new_val)
inline

Definition at line 372 of file ratngs.h.

372  {
373  certainty_ = new_val;
374  }
void WERD_CHOICE::set_dangerous_ambig_found_ ( bool  value)
inline

Definition at line 366 of file ratngs.h.

366  {
367  dangerous_ambig_found_ = value;
368  }
void WERD_CHOICE::set_length ( int  len)
inline

Definition at line 381 of file ratngs.h.

381  {
382  ASSERT_HOST(reserved_ >= len);
383  length_ = len;
384  }
#define ASSERT_HOST(x)
Definition: errcode.h:88
void WERD_CHOICE::set_permuter ( uint8_t  perm)
inline

Definition at line 375 of file ratngs.h.

375  {
376  permuter_ = perm;
377  }
void WERD_CHOICE::set_rating ( float  new_val)
inline

Definition at line 369 of file ratngs.h.

369  {
370  rating_ = new_val;
371  }
void WERD_CHOICE::set_unichar_id ( UNICHAR_ID  unichar_id,
int  index 
)
inline

Definition at line 359 of file ratngs.h.

359  {
360  assert(index < length_);
361  unichar_ids_[index] = unichar_id;
362  }
UNICHAR_ID unichar_id(int index) const
Definition: ratngs.h:315
void WERD_CHOICE::set_unichar_id ( UNICHAR_ID  unichar_id,
int  blob_count,
float  rating,
float  certainty,
int  index 
)
inline

Definition at line 464 of file ratngs.h.

465  {
466  assert(index < length_);
467  unichar_ids_[index] = unichar_id;
468  state_[index] = blob_count;
469  certainties_[index] = certainty;
470  script_pos_[index] = tesseract::SP_NORMAL;
471  rating_ += rating;
472  if (certainty < certainty_) {
473  certainty_ = certainty;
474  }
475  }
float rating() const
Definition: ratngs.h:327
float certainty() const
Definition: ratngs.h:330
UNICHAR_ID unichar_id(int index) const
Definition: ratngs.h:315
bool WERD_CHOICE::set_unichars_in_script_order ( bool  in_script_order)
inline

Definition at line 531 of file ratngs.h.

531  {
532  return unichars_in_script_order_ = in_script_order;
533  }
void WERD_CHOICE::set_x_heights ( float  min_height,
float  max_height 
)
inline

Definition at line 342 of file ratngs.h.

342  {
343  min_x_height_ = min_height;
344  max_x_height_ = max_height;
345  }
void WERD_CHOICE::SetAllScriptPositions ( tesseract::ScriptPos  position)

Definition at line 623 of file ratngs.cpp.

623  {
624  for (int i = 0; i < length_; ++i)
625  script_pos_[i] = position;
626 }
void WERD_CHOICE::SetScriptPositions ( bool  small_caps,
TWERD word,
int  debug = 0 
)

Definition at line 550 of file ratngs.cpp.

550  {
551  // Initialize to normal.
552  for (int i = 0; i < length_; ++i)
553  script_pos_[i] = tesseract::SP_NORMAL;
554  if (word->blobs.empty() || word->NumBlobs() != TotalOfStates()) {
555  return;
556  }
557 
558  int position_counts[4] = { 0, 0, 0, 0 };
559 
560  int chunk_index = 0;
561  for (int blob_index = 0; blob_index < length_; ++blob_index, ++chunk_index) {
562  TBLOB* tblob = word->blobs[chunk_index];
563  int uni_id = unichar_id(blob_index);
564  TBOX blob_box = tblob->bounding_box();
565  if (state_ != nullptr) {
566  for (int i = 1; i < state_[blob_index]; ++i) {
567  ++chunk_index;
568  tblob = word->blobs[chunk_index];
569  blob_box += tblob->bounding_box();
570  }
571  }
572  script_pos_[blob_index] = ScriptPositionOf(false, *unicharset_, blob_box,
573  uni_id);
574  if (small_caps && script_pos_[blob_index] != tesseract::SP_DROPCAP) {
575  script_pos_[blob_index] = tesseract::SP_NORMAL;
576  }
577  position_counts[script_pos_[blob_index]]++;
578  }
579  // If almost everything looks like a superscript or subscript,
580  // we most likely just got the baseline wrong.
581  if (position_counts[tesseract::SP_SUBSCRIPT] > 0.75 * length_ ||
582  position_counts[tesseract::SP_SUPERSCRIPT] > 0.75 * length_) {
583  if (debug >= 2) {
584  tprintf("Most characters of %s are subscript or superscript.\n"
585  "That seems wrong, so I'll assume we got the baseline wrong\n",
586  unichar_string().string());
587  }
588  for (int i = 0; i < length_; i++) {
589  ScriptPos sp = script_pos_[i];
591  position_counts[sp]--;
592  position_counts[tesseract::SP_NORMAL]++;
593  script_pos_[i] = tesseract::SP_NORMAL;
594  }
595  }
596  }
597 
598  if ((debug >= 1 && position_counts[tesseract::SP_NORMAL] < length_) ||
599  debug >= 2) {
600  tprintf("SetScriptPosition on %s\n", unichar_string().string());
601  int chunk_index = 0;
602  for (int blob_index = 0; blob_index < length_; ++blob_index) {
603  if (debug >= 2 || script_pos_[blob_index] != tesseract::SP_NORMAL) {
604  TBLOB* tblob = word->blobs[chunk_index];
605  ScriptPositionOf(true, *unicharset_, tblob->bounding_box(),
606  unichar_id(blob_index));
607  }
608  chunk_index += state_ != nullptr ? state_[blob_index] : 1;
609  }
610  }
611 }
Definition: rect.h:34
GenericVector< TBLOB * > blobs
Definition: blobs.h:438
TBOX bounding_box() const
Definition: blobs.cpp:472
const STRING & unichar_string() const
Definition: ratngs.h:541
Definition: blobs.h:263
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:36
bool empty() const
Definition: genericvector.h:89
int NumBlobs() const
Definition: blobs.h:427
UNICHAR_ID unichar_id(int index) const
Definition: ratngs.h:315
int TotalOfStates() const
Definition: ratngs.cpp:711
static tesseract::ScriptPos ScriptPositionOf(bool print_debug, const UNICHARSET &unicharset, const TBOX &blob_box, UNICHAR_ID unichar_id)
Definition: ratngs.cpp:629
void WERD_CHOICE::SetScriptPositions ( const tesseract::ScriptPos positions,
int  length 
)

Definition at line 613 of file ratngs.cpp.

614  {
615  ASSERT_HOST(length == length_);
616  if (positions != script_pos_) {
617  delete [] script_pos_;
618  script_pos_ = new ScriptPos[length];
619  memcpy(script_pos_, positions, sizeof(positions[0]) * length);
620  }
621 }
int length() const
Definition: ratngs.h:303
#define ASSERT_HOST(x)
Definition: errcode.h:88
WERD_CHOICE WERD_CHOICE::shallow_copy ( int  start,
int  end 
) const

Definition at line 414 of file ratngs.cpp.

414  {
415  ASSERT_HOST(start >= 0 && start <= length_);
416  ASSERT_HOST(end >= 0 && end <= length_);
417  if (end < start) { end = start; }
418  WERD_CHOICE retval(unicharset_, end - start);
419  for (int i = start; i < end; i++) {
420  retval.append_unichar_id_space_allocated(
421  unichar_ids_[i], state_[i], 0.0f, certainties_[i]);
422  }
423  return retval;
424 }
#define ASSERT_HOST(x)
Definition: errcode.h:88
int WERD_CHOICE::state ( int  index) const
inline

Definition at line 319 of file ratngs.h.

319  {
320  return state_[index];
321  }
void WERD_CHOICE::string_and_lengths ( STRING word_str,
STRING word_lengths_str 
) const

string_and_lengths

Populates the given word_str with unichars from unichar_ids and and word_lengths_str with the corresponding unichar lengths.

Definition at line 449 of file ratngs.cpp.

450  {
451  *word_str = "";
452  if (word_lengths_str != nullptr) *word_lengths_str = "";
453  for (int i = 0; i < length_; ++i) {
454  const char *ch = unicharset_->id_to_unichar_ext(unichar_ids_[i]);
455  *word_str += ch;
456  if (word_lengths_str != nullptr) {
457  *word_lengths_str += strlen(ch);
458  }
459  }
460 }
const char * id_to_unichar_ext(UNICHAR_ID id) const
Definition: unicharset.cpp:299
int WERD_CHOICE::TotalOfStates ( ) const

Definition at line 711 of file ratngs.cpp.

711  {
712  int total_chunks = 0;
713  for (int i = 0; i < length_; ++i) {
714  total_chunks += state_[i];
715  }
716  return total_chunks;
717 }
UNICHAR_ID WERD_CHOICE::unichar_id ( int  index) const
inline

Definition at line 315 of file ratngs.h.

315  {
316  assert(index < length_);
317  return unichar_ids_[index];
318  }
const UNICHAR_ID* WERD_CHOICE::unichar_ids ( ) const
inline

Definition at line 312 of file ratngs.h.

312  {
313  return unichar_ids_;
314  }
const STRING& WERD_CHOICE::unichar_lengths ( ) const
inline

Definition at line 548 of file ratngs.h.

548  {
549  this->string_and_lengths(&unichar_string_, &unichar_lengths_);
550  return unichar_lengths_;
551  }
void string_and_lengths(STRING *word_str, STRING *word_lengths_str) const
Definition: ratngs.cpp:449
const STRING& WERD_CHOICE::unichar_string ( ) const
inline

Definition at line 541 of file ratngs.h.

541  {
542  this->string_and_lengths(&unichar_string_, &unichar_lengths_);
543  return unichar_string_;
544  }
void string_and_lengths(STRING *word_str, STRING *word_lengths_str) const
Definition: ratngs.cpp:449
bool WERD_CHOICE::unichars_in_script_order ( ) const
inline

Definition at line 535 of file ratngs.h.

535  {
536  return unichars_in_script_order_;
537  }
const UNICHARSET* WERD_CHOICE::unicharset ( ) const
inline

Definition at line 300 of file ratngs.h.

300  {
301  return unicharset_;
302  }
void WERD_CHOICE::UpdateStateForSplit ( int  blob_position)

Definition at line 699 of file ratngs.cpp.

699  {
700  int total_chunks = 0;
701  for (int i = 0; i < length_; ++i) {
702  total_chunks += state_[i];
703  if (total_chunks > blob_position) {
704  ++state_[i];
705  return;
706  }
707  }
708 }

Member Data Documentation

const float WERD_CHOICE::kBadRating = 100000.0
static

Definition at line 275 of file ratngs.h.


The documentation for this class was generated from the following files: