All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
tesseract::WordSizeModel Class Reference

#include <word_size_model.h>

Public Member Functions

 WordSizeModel (CharSet *, bool contextual)
 
virtual ~WordSizeModel ()
 
int Cost (CharSamp **samp_array, int samp_cnt) const
 
bool Save (string file_name)
 
int FontCount () const
 
const FontPairSizeInfoFontInfo () const
 

Static Public Member Functions

static WordSizeModelCreate (const string &data_file_path, const string &lang, CharSet *char_set, bool contextual)
 
static double PairCost (int width_0, int height_0, int top_0, int width_1, int height_1, int top_1, const PairSizeInfo &pair_info)
 
static int SizeCode (int cls_id, int start, int end)
 

Detailed Description

Definition at line 53 of file word_size_model.h.

Constructor & Destructor Documentation

tesseract::WordSizeModel::WordSizeModel ( CharSet char_set,
bool  contextual 
)

Definition at line 28 of file word_size_model.cpp.

28  {
29  char_set_ = char_set;
30  contextual_ = contextual;
31 }
tesseract::WordSizeModel::~WordSizeModel ( )
virtual

Definition at line 33 of file word_size_model.cpp.

33  {
34  for (int fnt = 0; fnt < font_pair_size_models_.size(); fnt++) {
35  FontPairSizeInfo fnt_info = font_pair_size_models_[fnt];
36  delete []fnt_info.pair_size_info[0];
37  delete []fnt_info.pair_size_info;
38  }
39 }

Member Function Documentation

int tesseract::WordSizeModel::Cost ( CharSamp **  samp_array,
int  samp_cnt 
) const

Definition at line 210 of file word_size_model.cpp.

210  {
211  if (samp_cnt < 2) {
212  return 0;
213  }
214  double best_dist = static_cast<double>(WORST_COST);
215  int best_fnt = -1;
216  for (int fnt = 0; fnt < font_pair_size_models_.size(); fnt++) {
217  const FontPairSizeInfo *fnt_info = &font_pair_size_models_[fnt];
218  double mean_dist = 0;
219  int pair_cnt = 0;
220 
221  for (int smp_0 = 0; smp_0 < samp_cnt; smp_0++) {
222  int cls_0 = char_set_->ClassID(samp_array[smp_0]->StrLabel());
223  if (cls_0 < 1) {
224  continue;
225  }
226  // compute size code for samp 0 based on class id and position
227  int size_code_0;
228  if (contextual_) {
229  size_code_0 = SizeCode(cls_0,
230  samp_array[smp_0]->FirstChar() == 0 ? 0 : 1,
231  samp_array[smp_0]->LastChar() == 0 ? 0 : 1);
232  } else {
233  size_code_0 = cls_0;
234  }
235 
236  int char0_height = samp_array[smp_0]->Height();
237  int char0_width = samp_array[smp_0]->Width();
238  int char0_top = samp_array[smp_0]->Top();
239 
240  for (int smp_1 = smp_0 + 1; smp_1 < samp_cnt; smp_1++) {
241  int cls_1 = char_set_->ClassID(samp_array[smp_1]->StrLabel());
242  if (cls_1 < 1) {
243  continue;
244  }
245  // compute size code for samp 0 based on class id and position
246  int size_code_1;
247  if (contextual_) {
248  size_code_1 = SizeCode(cls_1,
249  samp_array[smp_1]->FirstChar() == 0 ? 0 : 1,
250  samp_array[smp_1]->LastChar() == 0 ? 0 : 1);
251  } else {
252  size_code_1 = cls_1;
253  }
254  double dist = PairCost(
255  char0_width, char0_height, char0_top, samp_array[smp_1]->Width(),
256  samp_array[smp_1]->Height(), samp_array[smp_1]->Top(),
257  fnt_info->pair_size_info[size_code_0][size_code_1]);
258  if (dist > 0) {
259  mean_dist += dist;
260  pair_cnt++;
261  }
262  } // smp_1
263  } // smp_0
264  if (pair_cnt == 0) {
265  continue;
266  }
267  mean_dist /= pair_cnt;
268  if (best_fnt == -1 || mean_dist < best_dist) {
269  best_dist = mean_dist;
270  best_fnt = fnt;
271  }
272  }
273  if (best_fnt == -1) {
274  return static_cast<int>(WORST_COST);
275  } else {
276  return static_cast<int>(best_dist);
277  }
278 }
#define WORST_COST
Definition: cube_const.h:30
static int SizeCode(int cls_id, int start, int end)
int ClassID(const char_32 *str) const
Definition: char_set.h:54
static double PairCost(int width_0, int height_0, int top_0, int width_1, int height_1, int top_1, const PairSizeInfo &pair_info)
WordSizeModel * tesseract::WordSizeModel::Create ( const string &  data_file_path,
const string &  lang,
CharSet char_set,
bool  contextual 
)
static

Definition at line 41 of file word_size_model.cpp.

44  {
45  WordSizeModel *obj = new WordSizeModel(char_set, contextual);
46  if (!obj) {
47  fprintf(stderr, "Cube ERROR (WordSizeModel::Create): unable to allocate "
48  "new word size model object\n");
49  return NULL;
50  }
51 
52  if (!obj->Init(data_file_path, lang)) {
53  delete obj;
54  return NULL;
55  }
56  return obj;
57 }
WordSizeModel(CharSet *, bool contextual)
#define NULL
Definition: host.h:144
int tesseract::WordSizeModel::FontCount ( ) const
inline

Definition at line 72 of file word_size_model.h.

72  {
73  return font_pair_size_models_.size();
74  }
const FontPairSizeInfo* tesseract::WordSizeModel::FontInfo ( ) const
inline

Definition at line 75 of file word_size_model.h.

75  {
76  return &font_pair_size_models_[0];
77  }
double tesseract::WordSizeModel::PairCost ( int  width_0,
int  height_0,
int  top_0,
int  width_1,
int  height_1,
int  top_1,
const PairSizeInfo pair_info 
)
static

Definition at line 280 of file word_size_model.cpp.

282  {
283  double scale_factor = static_cast<double>(pair_info.hgt_0) /
284  static_cast<double>(height_0);
285  double dist = 0.0;
286  if (scale_factor > 0) {
287  double norm_width_0 = width_0 * scale_factor;
288  double norm_width_1 = width_1 * scale_factor;
289  double norm_height_1 = height_1 * scale_factor;
290  double norm_delta_top = (top_1 - top_0) * scale_factor;
291 
292  // accumulate the distance between the model character and the
293  // predicted one on all dimensions of the pair
294  dist += fabs(pair_info.wid_0 - norm_width_0);
295  dist += fabs(pair_info.wid_1 - norm_width_1);
296  dist += fabs(pair_info.hgt_1 - norm_height_1);
297  dist += fabs(pair_info.delta_top - norm_delta_top);
298  }
299  return dist;
300 }
bool tesseract::WordSizeModel::Save ( string  file_name)
static int tesseract::WordSizeModel::SizeCode ( int  cls_id,
int  start,
int  end 
)
inlinestatic

Definition at line 80 of file word_size_model.h.

80  {
81  return (cls_id << 2) + (end << 1) + start;
82  }

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