00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef CHAR_SET_H
00029 #define CHAR_SET_H
00030
00031 #include <string.h>
00032 #include <string>
00033 #include <algorithm>
00034
00035 #include "string_32.h"
00036 #include "tessdatamanager.h"
00037 #include "unicharset.h"
00038 #include "cube_const.h"
00039
00040 namespace tesseract {
00041
00042 class CharSet {
00043 public:
00044 CharSet();
00045 ~CharSet();
00046
00047
00048 inline bool SharedUnicharset() { return (unicharset_map_ == NULL); }
00049
00050
00051
00052
00053
00054 inline int ClassID(const char_32 *str) const {
00055 int hash_val = Hash(str);
00056 if (hash_bin_size_[hash_val] == 0)
00057 return -1;
00058 for (int bin = 0; bin < hash_bin_size_[hash_val]; bin++) {
00059 if (class_strings_[hash_bins_[hash_val][bin]]->compare(str) == 0)
00060 return hash_bins_[hash_val][bin];
00061 }
00062 return -1;
00063 }
00064
00065 inline int ClassID(char_32 ch) const {
00066 int hash_val = Hash(ch);
00067 if (hash_bin_size_[hash_val] == 0)
00068 return -1;
00069 for (int bin = 0; bin < hash_bin_size_[hash_val]; bin++) {
00070 if ((*class_strings_[hash_bins_[hash_val][bin]])[0] == ch &&
00071 class_strings_[hash_bins_[hash_val][bin]]->length() == 1) {
00072 return hash_bins_[hash_val][bin];
00073 }
00074 }
00075 return -1;
00076 }
00077
00078
00079
00080 inline int UnicharID(const char_32 *str) const {
00081 int class_id = ClassID(str);
00082 if (class_id == INVALID_UNICHAR_ID)
00083 return INVALID_UNICHAR_ID;
00084 int unichar_id;
00085 if (unicharset_map_)
00086 unichar_id = unicharset_map_[class_id];
00087 else
00088 unichar_id = class_id;
00089 return unichar_id;
00090 }
00091
00092 inline int UnicharID(char_32 ch) const {
00093 int class_id = ClassID(ch);
00094 if (class_id == INVALID_UNICHAR_ID)
00095 return INVALID_UNICHAR_ID;
00096 int unichar_id;
00097 if (unicharset_map_)
00098 unichar_id = unicharset_map_[class_id];
00099 else
00100 unichar_id = class_id;
00101 return unichar_id;
00102 }
00103
00104 inline const char_32 * ClassString(int class_id) const {
00105 if (class_id < 0 || class_id >= class_cnt_) {
00106 return NULL;
00107 }
00108 return reinterpret_cast<const char_32 *>(class_strings_[class_id]->c_str());
00109 }
00110
00111 inline int ClassCount() const { return class_cnt_; }
00112
00113
00114
00115
00116 static CharSet *Create(TessdataManager *tessdata_manager,
00117 UNICHARSET *tess_unicharset);
00118
00119
00120
00121 UNICHARSET *InternalUnicharset() { return unicharset_; }
00122
00123 private:
00124
00125
00126
00127 static const int kHashBins = 3001;
00128 static const int kMaxHashSize = 16;
00129
00130
00131
00132 static inline int Hash(const char_32 *str) {
00133 unsigned long hash = 5381;
00134 int c;
00135 while ((c = *str++))
00136 hash = ((hash << 5) + hash) + c;
00137 return (hash%kHashBins);
00138 }
00139
00140 static inline int Hash(char_32 ch) {
00141 char_32 b[2];
00142 b[0] = ch;
00143 b[1] = 0;
00144 return Hash(b);
00145 }
00146
00147
00148
00149
00150 bool LoadSupportedCharList(FILE *fp, UNICHARSET *tess_unicharset);
00151
00152
00153 int class_cnt_;
00154
00155 int hash_bin_size_[kHashBins];
00156
00157 int hash_bins_[kHashBins][kMaxHashSize];
00158
00159 string_32 **class_strings_;
00160
00161 int *unicharset_map_;
00162
00163
00164 UNICHARSET cube_unicharset_;
00165
00166
00167
00168 UNICHARSET *unicharset_;
00169
00170 bool init_;
00171 };
00172 }
00173
00174 #endif // CHAR_SET_H