All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
unicharset_training_utils.cpp
Go to the documentation of this file.
1 // File: unicharset_training_utils.cpp
3 // Description: Training utilities for UNICHARSET.
4 // Author: Ray Smith
5 // Created: Fri Oct 17 17:09:01 PDT 2014
6 //
7 // (C) Copyright 2014, 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 //
19 
21 
22 #include <stdlib.h>
23 #include <string.h>
24 #include <string>
25 
26 #include "fileio.h"
27 #include "genericvector.h"
28 #include "icuerrorcode.h"
29 #include "normstrngs.h"
30 #include "statistc.h"
31 #include "strngs.h"
32 #include "unicharset.h"
33 #include "unicode/uchar.h" // from libicu
34 #include "unicode/uscript.h" // from libicu
35 
36 namespace tesseract {
37 
38 // Helper sets the character attribute properties and sets up the script table.
39 // Does not set tops and bottoms.
40 void SetupBasicProperties(bool report_errors, UNICHARSET* unicharset) {
41  for (int unichar_id = 0; unichar_id < unicharset->size(); ++unichar_id) {
42  // Convert any custom ligatures.
43  const char* unichar_str = unicharset->id_to_unichar(unichar_id);
44  for (int i = 0; UNICHARSET::kCustomLigatures[i][0] != NULL; ++i) {
45  if (!strcmp(UNICHARSET::kCustomLigatures[i][1], unichar_str)) {
46  unichar_str = UNICHARSET::kCustomLigatures[i][0];
47  break;
48  }
49  }
50 
51  // Convert the unichar to UTF32 representation
52  GenericVector<char32> uni_vector;
53  tesseract::UTF8ToUTF32(unichar_str, &uni_vector);
54 
55  // Assume that if the property is true for any character in the string,
56  // then it holds for the whole "character".
57  bool unichar_isalpha = false;
58  bool unichar_islower = false;
59  bool unichar_isupper = false;
60  bool unichar_isdigit = false;
61  bool unichar_ispunct = false;
62 
63  for (int i = 0; i < uni_vector.size(); ++i) {
64  if (u_isalpha(uni_vector[i]))
65  unichar_isalpha = true;
66  if (u_islower(uni_vector[i]))
67  unichar_islower = true;
68  if (u_isupper(uni_vector[i]))
69  unichar_isupper = true;
70  if (u_isdigit(uni_vector[i]))
71  unichar_isdigit = true;
72  if (u_ispunct(uni_vector[i]))
73  unichar_ispunct = true;
74  }
75 
76  unicharset->set_isalpha(unichar_id, unichar_isalpha);
77  unicharset->set_islower(unichar_id, unichar_islower);
78  unicharset->set_isupper(unichar_id, unichar_isupper);
79  unicharset->set_isdigit(unichar_id, unichar_isdigit);
80  unicharset->set_ispunctuation(unichar_id, unichar_ispunct);
81 
83  unicharset->set_script(unichar_id, uscript_getName(
84  uscript_getScript(uni_vector[0], err)));
85 
86  const int num_code_points = uni_vector.size();
87  // Obtain the lower/upper case if needed and record it in the properties.
88  unicharset->set_other_case(unichar_id, unichar_id);
89  if (unichar_islower || unichar_isupper) {
90  GenericVector<char32> other_case(num_code_points, 0);
91  for (int i = 0; i < num_code_points; ++i) {
92  // TODO(daria): Ideally u_strToLower()/ustrToUpper() should be used.
93  // However since they deal with UChars (so need a conversion function
94  // from char32 or UTF8string) and require a meaningful locale string,
95  // for now u_tolower()/u_toupper() are used.
96  other_case[i] = unichar_islower ? u_toupper(uni_vector[i]) :
97  u_tolower(uni_vector[i]);
98  }
99  STRING other_case_uch;
100  tesseract::UTF32ToUTF8(other_case, &other_case_uch);
101  UNICHAR_ID other_case_id =
102  unicharset->unichar_to_id(other_case_uch.c_str());
103  if (other_case_id != INVALID_UNICHAR_ID) {
104  unicharset->set_other_case(unichar_id, other_case_id);
105  } else if (unichar_id >= SPECIAL_UNICHAR_CODES_COUNT && report_errors) {
106  tprintf("Other case %s of %s is not in unicharset\n",
107  other_case_uch.c_str(), unichar_str);
108  }
109  }
110 
111  // Set RTL property and obtain mirror unichar ID from ICU.
112  GenericVector<char32> mirrors(num_code_points, 0);
113  for (int i = 0; i < num_code_points; ++i) {
114  mirrors[i] = u_charMirror(uni_vector[i]);
115  if (i == 0) { // set directionality to that of the 1st code point
116  unicharset->set_direction(unichar_id,
117  static_cast<UNICHARSET::Direction>(
118  u_charDirection(uni_vector[i])));
119  }
120  }
121  STRING mirror_uch;
122  tesseract::UTF32ToUTF8(mirrors, &mirror_uch);
123  UNICHAR_ID mirror_uch_id = unicharset->unichar_to_id(mirror_uch.c_str());
124  if (mirror_uch_id != INVALID_UNICHAR_ID) {
125  unicharset->set_mirror(unichar_id, mirror_uch_id);
126  } else if (report_errors) {
127  tprintf("Mirror %s of %s is not in unicharset\n",
128  mirror_uch.c_str(), unichar_str);
129  }
130 
131  // Record normalized version of this unichar.
132  STRING normed_str = tesseract::NormalizeUTF8String(unichar_str);
133  if (unichar_id != 0 && normed_str.length() > 0) {
134  unicharset->set_normed(unichar_id, normed_str.c_str());
135  } else {
136  unicharset->set_normed(unichar_id, unichar_str);
137  }
138  ASSERT_HOST(unicharset->get_other_case(unichar_id) < unicharset->size());
139  }
140  unicharset->post_load_setup();
141 }
142 
143 // Helper to set the properties for an input unicharset file, writes to the
144 // output file. If an appropriate script unicharset can be found in the
145 // script_dir directory, then the tops and bottoms are expanded using the
146 // script unicharset.
147 // If non-empty, xheight data for the fonts are written to the xheights_file.
148 void SetPropertiesForInputFile(const string& script_dir,
149  const string& input_unicharset_file,
150  const string& output_unicharset_file,
151  const string& output_xheights_file) {
152  UNICHARSET unicharset;
153 
154  // Load the input unicharset
155  unicharset.load_from_file(input_unicharset_file.c_str());
156  tprintf("Loaded unicharset of size %d from file %s\n", unicharset.size(),
157  input_unicharset_file.c_str());
158 
159  // Set unichar properties
160  tprintf("Setting unichar properties\n");
161  SetupBasicProperties(true, &unicharset);
162  string xheights_str;
163  for (int s = 0; s < unicharset.get_script_table_size(); ++s) {
164  // Load the unicharset for the script if available.
165  string filename = script_dir + "/" +
166  unicharset.get_script_from_script_id(s) + ".unicharset";
167  UNICHARSET script_set;
168  if (script_set.load_from_file(filename.c_str())) {
169  unicharset.SetPropertiesFromOther(script_set);
170  }
171  // Load the xheights for the script if available.
172  filename = script_dir + "/" + unicharset.get_script_from_script_id(s) +
173  ".xheights";
174  string script_heights;
175  if (File::ReadFileToString(filename, &script_heights))
176  xheights_str += script_heights;
177  }
178  if (!output_xheights_file.empty())
179  File::WriteStringToFileOrDie(xheights_str, output_xheights_file);
180  for (int c = SPECIAL_UNICHAR_CODES_COUNT; c < unicharset.size(); ++c) {
181  if (unicharset.PropertiesIncomplete(c)) {
182  tprintf("Warning: properties incomplete for index %d = %s\n",
183  c, unicharset.id_to_unichar(c));
184  }
185  }
186 
187  // Write the output unicharset
188  tprintf("Writing unicharset to file %s\n", output_unicharset_file.c_str());
189  unicharset.save_to_file(output_unicharset_file.c_str());
190 }
191 
192 } // namespace tesseract
193 
void set_isupper(UNICHAR_ID unichar_id, bool value)
Definition: unicharset.h:399
int size() const
Definition: genericvector.h:72
void UTF8ToUTF32(const char *utf8_str, GenericVector< char32 > *str32)
Definition: normstrngs.cpp:31
const UNICHAR_ID unichar_to_id(const char *const unichar_repr) const
Definition: unicharset.cpp:194
static bool ReadFileToString(const string &filename, string *out)
Definition: fileio.cpp:73
bool save_to_file(const char *const filename) const
Definition: unicharset.h:306
void set_islower(UNICHAR_ID unichar_id, bool value)
Definition: unicharset.h:394
#define tprintf(...)
Definition: tprintf.h:31
void SetupBasicProperties(bool report_errors, UNICHARSET *unicharset)
bool load_from_file(const char *const filename, bool skip_fragments)
Definition: unicharset.h:346
inT32 length() const
Definition: strngs.cpp:188
int get_script_table_size() const
Definition: unicharset.h:797
static void WriteStringToFileOrDie(const string &str, const string &filename)
Definition: fileio.cpp:53
void set_normed(UNICHAR_ID unichar_id, const char *normed)
Definition: unicharset.h:440
#define ASSERT_HOST(x)
Definition: errcode.h:84
const char *const id_to_unichar(UNICHAR_ID id) const
Definition: unicharset.cpp:266
void set_ispunctuation(UNICHAR_ID unichar_id, bool value)
Definition: unicharset.h:409
void set_isdigit(UNICHAR_ID unichar_id, bool value)
Definition: unicharset.h:404
int UNICHAR_ID
Definition: unichar.h:33
UNICHAR_ID get_other_case(UNICHAR_ID unichar_id) const
Definition: unicharset.h:631
void set_other_case(UNICHAR_ID unichar_id, UNICHAR_ID other_case)
Definition: unicharset.h:425
void set_isalpha(UNICHAR_ID unichar_id, bool value)
Definition: unicharset.h:389
void set_direction(UNICHAR_ID unichar_id, UNICHARSET::Direction value)
Definition: unicharset.h:430
const char * get_script_from_script_id(int id) const
Definition: unicharset.h:802
STRING NormalizeUTF8String(const char *str8)
Definition: normstrngs.cpp:116
void UTF32ToUTF8(const GenericVector< char32 > &str32, STRING *utf8_str)
Definition: normstrngs.cpp:45
void set_script(UNICHAR_ID unichar_id, const char *value)
Definition: unicharset.h:420
bool PropertiesIncomplete(UNICHAR_ID unichar_id) const
Definition: unicharset.h:604
void SetPropertiesFromOther(const UNICHARSET &src)
Definition: unicharset.h:503
void set_mirror(UNICHAR_ID unichar_id, UNICHAR_ID mirror)
Definition: unicharset.h:435
Definition: strngs.h:44
#define NULL
Definition: host.h:144
static const char * kCustomLigatures[][2]
Definition: unicharset.h:144
int size() const
Definition: unicharset.h:297
void post_load_setup()
Definition: unicharset.cpp:867
void SetPropertiesForInputFile(const string &script_dir, const string &input_unicharset_file, const string &output_unicharset_file, const string &output_xheights_file)
const char * c_str() const
Definition: strngs.cpp:204