Tesseract 3.02

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
word_size_model.h
Go to the documentation of this file.
1 /**********************************************************************
2 * File: word_size_model.h
3 * Description: Declaration of the Word Size Model Class
4 * Author: Ahmad Abdulkader
5 * Created: 2008
6 *
7 * (C) Copyright 2008, Google Inc.
8 ** Licensed under the Apache License, Version 2.0 (the "License");
9 ** you may not use this file except in compliance with the License.
10 ** You may obtain a copy of the License at
11 ** http://www.apache.org/licenses/LICENSE-2.0
12 ** Unless required by applicable law or agreed to in writing, software
13 ** distributed under the License is distributed on an "AS IS" BASIS,
14 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 ** See the License for the specific language governing permissions and
16 ** limitations under the License.
17 *
18 **********************************************************************/
19
20 // The WordSizeModel class abstracts the geometrical relationships
21 // between characters/shapes in the same word (presumeably of the same font)
22 // A non-parametric bigram model describes the three geometrical properties of a
23 // character pair:
24 // 1- Normalized Width
25 // 2- Normalized Top
26 // 3- Normalized Height
27 // These dimensions are computed for each character pair in a word. These are
28 // then compared to the same information for each of the fonts that the size
29 // model knows about. The WordSizeCost is the cost of the font that matches
30 // best.
31
32 #ifndef WORD_SIZE_MODEL_H
33 #define WORD_SIZE_MODEL_H
34
35 #include <string>
36 #include "char_samp.h"
37 #include "char_set.h"
38
39 namespace tesseract {
40 struct PairSizeInfo {
41 int delta_top;
42 int wid_0;
43 int hgt_0;
44 int wid_1;
45 int hgt_1;
46 };
47
48 struct FontPairSizeInfo {
49 string font_name;
50 PairSizeInfo **pair_size_info;
51 };
52
53 class WordSizeModel {
54 public:
55 WordSizeModel(CharSet *, bool contextual);
56 virtual ~WordSizeModel();
57 static WordSizeModel *Create(const string &data_file_path,
58 const string &lang,
59 CharSet *char_set,
60 bool contextual);
61 // Given a word and number of unichars, return the size cost,
62 // minimized over all fonts in the size model.
63 int Cost(CharSamp **samp_array, int samp_cnt) const;
64 // Given dimensions of a pair of character samples and a font size
65 // model for that character pair, return the pair's size cost for
66 // the font.
67 static double PairCost(int width_0, int height_0, int top_0,
68 int width_1, int height_1, int top_1,
69 const PairSizeInfo& pair_info);
70 bool Save(string file_name);
71 // Number of fonts in size model.
72 inline int FontCount() const {
73 return font_pair_size_models_.size();
74 }
75 inline const FontPairSizeInfo *FontInfo() const {
76 return &font_pair_size_models_[0];
77 }
78 // Helper functions to convert between size codes, class id and position
79 // codes
80 static inline int SizeCode(int cls_id, int start, int end) {
81 return (cls_id << 2) + (end << 1) + start;
82 }
83
84 private:
85 // Scaling constant used to convert floating point ratios in size table
86 // to fixed point
87 static const int kShapeModelScale = 1000;
88 static const int kExpectedTokenCount = 10;
89
90 // Language properties
91 bool contextual_;
92 CharSet *char_set_;
93 // Size ratios table
94 vector<FontPairSizeInfo> font_pair_size_models_;
95
96 // Initialize the word size model object
97 bool Init(const string &data_file_path, const string &lang);
98 };
99 }
100 #endif // WORD_SIZE_MODEL_H