tesseract  5.0.0-alpha-619-ge9db
tesseract::UnicharAmbigs Class Reference

#include <ambigs.h>

Public Member Functions

 UnicharAmbigs ()=default
 
 ~UnicharAmbigs ()
 
const UnicharAmbigsVectordang_ambigs () const
 
const UnicharAmbigsVectorreplace_ambigs () const
 
void InitUnicharAmbigs (const UNICHARSET &unicharset, bool use_ambigs_for_adaption)
 
void LoadUniversal (const UNICHARSET &encoder_set, UNICHARSET *unicharset)
 
void LoadUnicharAmbigs (const UNICHARSET &encoder_set, TFile *ambigs_file, int debug_level, bool use_ambigs_for_adaption, UNICHARSET *unicharset)
 
const UnicharIdVectorOneToOneDefiniteAmbigs (UNICHAR_ID unichar_id) const
 
const UnicharIdVectorAmbigsForAdaption (UNICHAR_ID unichar_id) const
 
const UnicharIdVectorReverseAmbigsForAdaption (UNICHAR_ID unichar_id) const
 

Detailed Description

Definition at line 136 of file ambigs.h.

Constructor & Destructor Documentation

◆ UnicharAmbigs()

tesseract::UnicharAmbigs::UnicharAmbigs ( )
default

◆ ~UnicharAmbigs()

tesseract::UnicharAmbigs::~UnicharAmbigs ( )
inline

Definition at line 139 of file ambigs.h.

139  {
140  replace_ambigs_.delete_data_pointers();
141  dang_ambigs_.delete_data_pointers();
142  one_to_one_definite_ambigs_.delete_data_pointers();
143  }

Member Function Documentation

◆ AmbigsForAdaption()

const UnicharIdVector* tesseract::UnicharAmbigs::AmbigsForAdaption ( UNICHAR_ID  unichar_id) const
inline

Definition at line 183 of file ambigs.h.

184  {
185  if (ambigs_for_adaption_.empty()) return nullptr;
186  return ambigs_for_adaption_[unichar_id];
187  }

◆ dang_ambigs()

const UnicharAmbigsVector& tesseract::UnicharAmbigs::dang_ambigs ( ) const
inline

Definition at line 145 of file ambigs.h.

145 { return dang_ambigs_; }

◆ InitUnicharAmbigs()

void tesseract::UnicharAmbigs::InitUnicharAmbigs ( const UNICHARSET unicharset,
bool  use_ambigs_for_adaption 
)

Definition at line 54 of file ambigs.cpp.

55  {
56  for (int i = 0; i < unicharset.size(); ++i) {
57  replace_ambigs_.push_back(nullptr);
58  dang_ambigs_.push_back(nullptr);
59  one_to_one_definite_ambigs_.push_back(nullptr);
60  if (use_ambigs_for_adaption) {
61  ambigs_for_adaption_.push_back(nullptr);
62  reverse_ambigs_for_adaption_.push_back(nullptr);
63  }
64  }
65 }

◆ LoadUnicharAmbigs()

void tesseract::UnicharAmbigs::LoadUnicharAmbigs ( const UNICHARSET encoder_set,
TFile ambigs_file,
int  debug_level,
bool  use_ambigs_for_adaption,
UNICHARSET unicharset 
)

Definition at line 75 of file ambigs.cpp.

79  {
80  int i, j;
81  UnicharIdVector *adaption_ambigs_entry;
82  if (debug_level) tprintf("Reading ambiguities\n");
83 
84  int test_ambig_part_size;
85  int replacement_ambig_part_size;
86  // The space for buffer is allocated on the heap to avoid
87  // GCC frame size warning.
88  const int kBufferSize = 10 + 2 * kMaxAmbigStringSize;
89  char *buffer = new char[kBufferSize];
90  char replacement_string[kMaxAmbigStringSize];
91  UNICHAR_ID test_unichar_ids[MAX_AMBIG_SIZE + 1];
92  int line_num = 0;
93  int type = NOT_AMBIG;
94 
95  // Determine the version of the ambigs file.
96  int version = 0;
97  ASSERT_HOST(ambig_file->FGets(buffer, kBufferSize) != nullptr &&
98  strlen(buffer) > 0);
99  if (*buffer == 'v') {
100  version = static_cast<int>(strtol(buffer+1, nullptr, 10));
101  ++line_num;
102  } else {
103  ambig_file->Rewind();
104  }
105  while (ambig_file->FGets(buffer, kBufferSize) != nullptr) {
106  chomp_string(buffer);
107  if (debug_level > 2) tprintf("read line %s\n", buffer);
108  ++line_num;
109  if (!ParseAmbiguityLine(line_num, version, debug_level, encoder_set,
110  buffer, &test_ambig_part_size, test_unichar_ids,
111  &replacement_ambig_part_size,
112  replacement_string, &type)) continue;
113  // Construct AmbigSpec and add it to the appropriate AmbigSpec_LIST.
114  auto *ambig_spec = new AmbigSpec();
115  if (!InsertIntoTable((type == REPLACE_AMBIG) ? replace_ambigs_
116  : dang_ambigs_,
117  test_ambig_part_size, test_unichar_ids,
118  replacement_ambig_part_size, replacement_string, type,
119  ambig_spec, unicharset))
120  continue;
121 
122  // Update one_to_one_definite_ambigs_.
123  if (test_ambig_part_size == 1 &&
124  replacement_ambig_part_size == 1 && type == DEFINITE_AMBIG) {
125  if (one_to_one_definite_ambigs_[test_unichar_ids[0]] == nullptr) {
126  one_to_one_definite_ambigs_[test_unichar_ids[0]] = new UnicharIdVector();
127  }
128  one_to_one_definite_ambigs_[test_unichar_ids[0]]->push_back(
129  ambig_spec->correct_ngram_id);
130  }
131  // Update ambigs_for_adaption_.
132  if (use_ambigs_for_adaption) {
133  GenericVector<UNICHAR_ID> encoding;
134  // Silently ignore invalid strings, as before, so it is safe to use a
135  // universal ambigs file.
136  if (unicharset->encode_string(replacement_string, true, &encoding,
137  nullptr, nullptr)) {
138  for (i = 0; i < test_ambig_part_size; ++i) {
139  if (ambigs_for_adaption_[test_unichar_ids[i]] == nullptr) {
140  ambigs_for_adaption_[test_unichar_ids[i]] = new UnicharIdVector();
141  }
142  adaption_ambigs_entry = ambigs_for_adaption_[test_unichar_ids[i]];
143  for (int r = 0; r < encoding.size(); ++r) {
144  UNICHAR_ID id_to_insert = encoding[r];
145  ASSERT_HOST(id_to_insert != INVALID_UNICHAR_ID);
146  // Add the new unichar id to adaption_ambigs_entry (only if the
147  // vector does not already contain it) keeping it in sorted order.
148  for (j = 0; j < adaption_ambigs_entry->size() &&
149  (*adaption_ambigs_entry)[j] > id_to_insert; ++j);
150  if (j < adaption_ambigs_entry->size()) {
151  if ((*adaption_ambigs_entry)[j] != id_to_insert) {
152  adaption_ambigs_entry->insert(id_to_insert, j);
153  }
154  } else {
155  adaption_ambigs_entry->push_back(id_to_insert);
156  }
157  }
158  }
159  }
160  }
161  }
162  delete[] buffer;
163 
164  // Fill in reverse_ambigs_for_adaption from ambigs_for_adaption vector.
165  if (use_ambigs_for_adaption) {
166  for (i = 0; i < ambigs_for_adaption_.size(); ++i) {
167  adaption_ambigs_entry = ambigs_for_adaption_[i];
168  if (adaption_ambigs_entry == nullptr) continue;
169  for (j = 0; j < adaption_ambigs_entry->size(); ++j) {
170  UNICHAR_ID ambig_id = (*adaption_ambigs_entry)[j];
171  if (reverse_ambigs_for_adaption_[ambig_id] == nullptr) {
172  reverse_ambigs_for_adaption_[ambig_id] = new UnicharIdVector();
173  }
174  reverse_ambigs_for_adaption_[ambig_id]->push_back(i);
175  }
176  }
177  }
178 
179  // Print what was read from the input file.
180  if (debug_level > 1) {
181  for (int tbl = 0; tbl < 2; ++tbl) {
182  const UnicharAmbigsVector &print_table =
183  (tbl == 0) ? replace_ambigs_ : dang_ambigs_;
184  for (i = 0; i < print_table.size(); ++i) {
185  AmbigSpec_LIST *lst = print_table[i];
186  if (lst == nullptr) continue;
187  if (!lst->empty()) {
188  tprintf("%s Ambiguities for %s:\n",
189  (tbl == 0) ? "Replaceable" : "Dangerous",
190  unicharset->debug_str(i).c_str());
191  }
192  AmbigSpec_IT lst_it(lst);
193  for (lst_it.mark_cycle_pt(); !lst_it.cycled_list(); lst_it.forward()) {
194  AmbigSpec *ambig_spec = lst_it.data();
195  tprintf("wrong_ngram:");
196  UnicharIdArrayUtils::print(ambig_spec->wrong_ngram, *unicharset);
197  tprintf("correct_fragments:");
198  UnicharIdArrayUtils::print(ambig_spec->correct_fragments, *unicharset);
199  }
200  }
201  }
202  if (use_ambigs_for_adaption) {
203  for (int vec_id = 0; vec_id < 2; ++vec_id) {
204  const GenericVector<UnicharIdVector *> &vec = (vec_id == 0) ?
205  ambigs_for_adaption_ : reverse_ambigs_for_adaption_;
206  for (i = 0; i < vec.size(); ++i) {
207  adaption_ambigs_entry = vec[i];
208  if (adaption_ambigs_entry != nullptr) {
209  tprintf("%sAmbigs for adaption for %s:\n",
210  (vec_id == 0) ? "" : "Reverse ",
211  unicharset->debug_str(i).c_str());
212  for (j = 0; j < adaption_ambigs_entry->size(); ++j) {
213  tprintf("%s ", unicharset->debug_str(
214  (*adaption_ambigs_entry)[j]).c_str());
215  }
216  tprintf("\n");
217  }
218  }
219  }
220  }
221  }
222 }

◆ LoadUniversal()

void tesseract::UnicharAmbigs::LoadUniversal ( const UNICHARSET encoder_set,
UNICHARSET unicharset 
)

Definition at line 68 of file ambigs.cpp.

69  {
70  TFile file;
72  LoadUnicharAmbigs(encoder_set, &file, 0, false, unicharset);
73 }

◆ OneToOneDefiniteAmbigs()

const UnicharIdVector* tesseract::UnicharAmbigs::OneToOneDefiniteAmbigs ( UNICHAR_ID  unichar_id) const
inline

Definition at line 172 of file ambigs.h.

173  {
174  if (one_to_one_definite_ambigs_.empty()) return nullptr;
175  return one_to_one_definite_ambigs_[unichar_id];
176  }

◆ replace_ambigs()

const UnicharAmbigsVector& tesseract::UnicharAmbigs::replace_ambigs ( ) const
inline

Definition at line 146 of file ambigs.h.

146 { return replace_ambigs_; }

◆ ReverseAmbigsForAdaption()

const UnicharIdVector* tesseract::UnicharAmbigs::ReverseAmbigsForAdaption ( UNICHAR_ID  unichar_id) const
inline

Definition at line 192 of file ambigs.h.

193  {
194  if (reverse_ambigs_for_adaption_.empty()) return nullptr;
195  return reverse_ambigs_for_adaption_[unichar_id];
196  }

The documentation for this class was generated from the following files:
GenericVector::delete_data_pointers
void delete_data_pointers()
Definition: genericvector.h:872
UNICHARSET::encode_string
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:258
tesseract::DEFINITE_AMBIG
Definition: ambigs.h:40
tesseract::File::Open
static FILE * Open(const std::string &filename, const std::string &mode)
Definition: fileio.cpp:54
ASSERT_HOST
#define ASSERT_HOST(x)
Definition: errcode.h:87
tesseract::UnicharAmbigsVector
GenericVector< AmbigSpec_LIST * > UnicharAmbigsVector
Definition: ambigs.h:134
tesseract::UnicharAmbigs::LoadUnicharAmbigs
void LoadUnicharAmbigs(const UNICHARSET &encoder_set, TFile *ambigs_file, int debug_level, bool use_ambigs_for_adaption, UNICHARSET *unicharset)
Definition: ambigs.cpp:75
tesseract::kMaxAmbigStringSize
const int kMaxAmbigStringSize
Definition: ambigs.cpp:41
MAX_AMBIG_SIZE
#define MAX_AMBIG_SIZE
Definition: ambigs.h:31
tesseract::NOT_AMBIG
Definition: ambigs.h:38
tesseract::UnicharIdVector
GenericVector< UNICHAR_ID > UnicharIdVector
Definition: ambigs.h:35
GenericVector::push_back
int push_back(T object)
Definition: genericvector.h:799
UNICHARSET::debug_str
STRING debug_str(UNICHAR_ID id) const
Definition: unicharset.cpp:342
STRING::c_str
const char * c_str() const
Definition: strngs.cpp:192
file
Definition: include_gunit.h:22
chomp_string
void chomp_string(char *str)
Definition: helpers.h:75
tesseract::kUniversalAmbigsFile
const char kUniversalAmbigsFile[]
Definition: universalambigs.cpp:24
UNICHAR_ID
int UNICHAR_ID
Definition: unichar.h:36
GenericVector< UNICHAR_ID >
tesseract::UnicharIdArrayUtils::print
static void print(const UNICHAR_ID array[], const UNICHARSET &unicharset)
Definition: ambigs.h:91
tesseract::ksizeofUniversalAmbigsFile
const int ksizeofUniversalAmbigsFile
Definition: universalambigs.cpp:19036
tesseract::REPLACE_AMBIG
Definition: ambigs.h:39
tprintf
DLLSYM void tprintf(const char *format,...)
Definition: tprintf.cpp:34
tesstrain_utils.type
type
Definition: tesstrain_utils.py:141
GenericVector::size
int size() const
Definition: genericvector.h:71
UNICHARSET::size
int size() const
Definition: unicharset.h:341