28 #include "absl/strings/numbers.h"
29 #include "absl/strings/str_split.h"
45 static const int kNumTopNErrs = 10;
46 static const int kNumTop2Errs = kNumTopNErrs + 20;
47 static const int kNumTop1Errs = kNumTop2Errs + 30;
48 static const int kNumTopTopErrs = kNumTop1Errs + 25;
49 static const int kNumNonReject = 1000;
50 static const int kNumCorrect = kNumNonReject - kNumTop1Errs;
53 static const int kNumAnswers = kNumNonReject + 2 * (kNumTop2Errs - kNumTopNErrs) +
54 (kNumTop1Errs - kNumTop2Errs) +
55 (kNumTopTopErrs - kNumTop1Errs);
57 #ifndef DISABLED_LEGACY_ENGINE
58 static bool safe_strto32(
const std::string& str,
int* pResult)
60 long n = strtol(str.c_str(),
nullptr, 0);
73 : shape_table_(shape_table), num_done_(0), done_bad_font_(false) {
77 false_unichar_id_ = 67;
78 false_shape_ = shape_table_->
AddShape(false_unichar_id_, 25);
93 if (++num_done_ > kNumNonReject)
return 0;
95 int class_id =
sample.class_id();
96 int font_id =
sample.font_id();
97 int shape_id = shape_table_->
FindShape(class_id, font_id);
99 int wrong_id1 = shape_id > 10 ? shape_id - 1 : shape_id + 1;
100 int wrong_id2 = shape_id > 10 ? shape_id - 2 : shape_id + 2;
101 if (num_done_ <= kNumTopNErrs) {
104 }
else if (num_done_ <= kNumTop2Errs) {
109 }
else if (num_done_ <= kNumTop1Errs) {
113 }
else if (num_done_ <= kNumTopTopErrs) {
119 }
else if (!done_bad_font_ && class_id == false_unichar_id_) {
122 done_bad_font_ =
true;
127 return results->
size();
136 int false_unichar_id_;
154 const double kMin1lDistance = 0.25;
157 class MasterTrainerTest :
public testing::Test {
158 #ifndef DISABLED_LEGACY_ENGINE
161 std::locale::global(std::locale(
""));
171 MasterTrainerTest() {
172 shape_table_ =
nullptr;
173 master_trainer_ =
nullptr;
175 ~MasterTrainerTest() {
176 delete master_trainer_;
183 void LoadMasterTrainer() {
184 FLAGS_output_trainer = TmpNameToPath(
"tmp_trainer").c_str();
185 FLAGS_F =
file::JoinPath(LANGDATA_DIR,
"font_properties").c_str();
186 FLAGS_X = TestDataNameToPath(
"eng.xheights").c_str();
187 FLAGS_U = TestDataNameToPath(
"eng.unicharset").c_str();
188 std::string tr_file_name(TestDataNameToPath(
"eng.Arial.exp0.tr"));
189 const char* argv[] = {tr_file_name.c_str()};
192 delete master_trainer_;
194 shape_table_ =
nullptr;
198 EXPECT_TRUE(master_trainer_ !=
nullptr);
199 EXPECT_TRUE(shape_table_ !=
nullptr);
206 int font_id = master_trainer_->GetFontInfoId(
"Arial");
207 EXPECT_GE(font_id, 0);
209 int unichar_I = master_trainer_->unicharset().unichar_to_id(
"I");
210 EXPECT_GT(unichar_I, 0);
211 int unichar_l = master_trainer_->unicharset().unichar_to_id(
"l");
212 EXPECT_GT(unichar_l, 0);
213 int unichar_1 = master_trainer_->unicharset().unichar_to_id(
"1");
214 EXPECT_GT(unichar_1, 0);
216 int shape_I = shape_table_->FindShape(unichar_I, font_id);
217 EXPECT_GE(shape_I, 0);
218 int shape_l = shape_table_->FindShape(unichar_l, font_id);
219 EXPECT_GE(shape_l, 0);
220 int shape_1 = shape_table_->FindShape(unichar_1, font_id);
221 EXPECT_GE(shape_1, 0);
224 master_trainer_->ShapeDistance(*shape_table_, shape_I, shape_l);
226 EXPECT_EQ(0.0f, dist_I_l);
228 master_trainer_->ShapeDistance(*shape_table_, shape_l, shape_I);
230 EXPECT_EQ(0.0f, dist_l_I);
234 master_trainer_->ShapeDistance(*shape_table_, shape_l, shape_1);
235 EXPECT_GT(dist_l_1, kMin1lDistance);
237 master_trainer_->ShapeDistance(*shape_table_, shape_1, shape_l);
238 EXPECT_GT(dist_1_l, kMin1lDistance);
242 master_trainer_->ShapeDistance(*shape_table_, shape_I, shape_1);
243 EXPECT_GT(dist_I_1, kMin1lDistance);
245 master_trainer_->ShapeDistance(*shape_table_, shape_1, shape_I);
246 EXPECT_GT(dist_1_I, kMin1lDistance);
250 ShapeTable* shape_table_;
251 MasterTrainer* master_trainer_;
257 TEST_F(MasterTrainerTest, Il1Test) {
258 #ifdef DISABLED_LEGACY_ENGINE
270 TEST_F(MasterTrainerTest, ErrorCounterTest) {
271 #ifdef DISABLED_LEGACY_ENGINE
279 if (shape_table_->FindShape(0, -1) < 0) shape_table_->AddShape(0, 0);
286 false, shape_classifier,
290 std::vector<std::string> results =
291 absl::StrSplit(result_string,
'\t', absl::SkipEmpty());
295 EXPECT_TRUE(safe_strto32(results[i + 1], &result_values[i]));
299 int num_samples = master_trainer_->GetSamples()->num_raw_samples();
307 EXPECT_EQ(kNumTopTopErrs - kNumTop1Errs,
312 delete shape_classifier;