tesseract  4.0.0-1-g2a2b
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 143 of file ambigs.h.

Constructor & Destructor Documentation

◆ UnicharAmbigs()

tesseract::UnicharAmbigs::UnicharAmbigs ( )
default

◆ ~UnicharAmbigs()

tesseract::UnicharAmbigs::~UnicharAmbigs ( )
inline

Definition at line 146 of file ambigs.h.

146  {
147  replace_ambigs_.delete_data_pointers();
148  dang_ambigs_.delete_data_pointers();
149  one_to_one_definite_ambigs_.delete_data_pointers();
150  }
void delete_data_pointers()

Member Function Documentation

◆ AmbigsForAdaption()

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

Definition at line 190 of file ambigs.h.

191  {
192  if (ambigs_for_adaption_.empty()) return nullptr;
193  return ambigs_for_adaption_[unichar_id];
194  }

◆ dang_ambigs()

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

Definition at line 152 of file ambigs.h.

152 { return dang_ambigs_; }

◆ InitUnicharAmbigs()

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

Definition at line 49 of file ambigs.cpp.

50  {
51  for (int i = 0; i < unicharset.size(); ++i) {
52  replace_ambigs_.push_back(nullptr);
53  dang_ambigs_.push_back(nullptr);
54  one_to_one_definite_ambigs_.push_back(nullptr);
55  if (use_ambigs_for_adaption) {
56  ambigs_for_adaption_.push_back(nullptr);
57  reverse_ambigs_for_adaption_.push_back(nullptr);
58  }
59  }
60 }
int size() const
Definition: unicharset.h:336
int push_back(T object)

◆ 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 70 of file ambigs.cpp.

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

◆ LoadUniversal()

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

Definition at line 63 of file ambigs.cpp.

64  {
65  TFile file;
66  if (!file.Open(kUniversalAmbigsFile, ksizeofUniversalAmbigsFile)) return;
67  LoadUnicharAmbigs(encoder_set, &file, 0, false, unicharset);
68 }
const char kUniversalAmbigsFile[]
const int ksizeofUniversalAmbigsFile
void LoadUnicharAmbigs(const UNICHARSET &encoder_set, TFile *ambigs_file, int debug_level, bool use_ambigs_for_adaption, UNICHARSET *unicharset)
Definition: ambigs.cpp:70

◆ OneToOneDefiniteAmbigs()

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

Definition at line 179 of file ambigs.h.

180  {
181  if (one_to_one_definite_ambigs_.empty()) return nullptr;
182  return one_to_one_definite_ambigs_[unichar_id];
183  }

◆ replace_ambigs()

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

Definition at line 153 of file ambigs.h.

153 { return replace_ambigs_; }

◆ ReverseAmbigsForAdaption()

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

Definition at line 199 of file ambigs.h.

200  {
201  if (reverse_ambigs_for_adaption_.empty()) return nullptr;
202  return reverse_ambigs_for_adaption_[unichar_id];
203  }

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