42 static const int kNumTopNErrs = 10;
43 static const int kNumTop2Errs = kNumTopNErrs + 20;
44 static const int kNumTop1Errs = kNumTop2Errs + 30;
45 static const int kNumTopTopErrs = kNumTop1Errs + 25;
46 static const int kNumNonReject = 1000;
47 static const int kNumCorrect = kNumNonReject - kNumTop1Errs;
50 static const int kNumAnswers = kNumNonReject + 2 * (kNumTop2Errs - kNumTopNErrs) +
51 (kNumTop1Errs - kNumTop2Errs) + (kNumTopTopErrs - kNumTop1Errs);
53 #ifndef DISABLED_LEGACY_ENGINE
54 static bool safe_strto32(
const std::string &str,
int *pResult) {
55 long n = strtol(str.c_str(),
nullptr, 0);
66 : shape_table_(shape_table), num_done_(0), done_bad_font_(false) {
70 false_unichar_id_ = 67;
71 false_shape_ = shape_table_->AddShape(false_unichar_id_, 25);
82 std::vector<ShapeRating> *results)
override {
85 if (++num_done_ > kNumNonReject) {
91 int shape_id = shape_table_->FindShape(class_id, font_id);
93 int wrong_id1 = shape_id > 10 ? shape_id - 1 : shape_id + 1;
94 int wrong_id2 = shape_id > 10 ? shape_id - 2 : shape_id + 2;
95 if (num_done_ <= kNumTopNErrs) {
98 }
else if (num_done_ <= kNumTop2Errs) {
101 results->push_back(
ShapeRating(wrong_id2, 0.875f));
103 }
else if (num_done_ <= kNumTop1Errs) {
107 }
else if (num_done_ <= kNumTopTopErrs) {
113 }
else if (!done_bad_font_ && class_id == false_unichar_id_) {
115 results->push_back(
ShapeRating(false_shape_, 1.0f));
116 done_bad_font_ =
true;
121 return results->size();
132 int false_unichar_id_;
145 #ifndef DISABLED_LEGACY_ENGINE
148 std::locale::global(std::locale(
""));
160 shape_table_ =
nullptr;
161 master_trainer_ =
nullptr;
171 FLAGS_output_trainer = TmpNameToPath(
"tmp_trainer").c_str();
172 FLAGS_F =
file::JoinPath(LANGDATA_DIR,
"font_properties").c_str();
176 const char *filelist[] = {tr_file_name.c_str(),
nullptr};
177 std::string file_prefix;
179 shape_table_ =
nullptr;
180 master_trainer_ =
LoadTrainingData(filelist,
false, &shape_table_, file_prefix);
181 EXPECT_TRUE(master_trainer_ !=
nullptr);
182 EXPECT_TRUE(shape_table_ !=
nullptr);
189 int font_id = master_trainer_->GetFontInfoId(
"Arial");
190 EXPECT_GE(font_id, 0);
192 int unichar_I = master_trainer_->unicharset().unichar_to_id(
"I");
193 EXPECT_GT(unichar_I, 0);
194 int unichar_l = master_trainer_->unicharset().unichar_to_id(
"l");
195 EXPECT_GT(unichar_l, 0);
196 int unichar_1 = master_trainer_->unicharset().unichar_to_id(
"1");
197 EXPECT_GT(unichar_1, 0);
199 int shape_I = shape_table_->FindShape(unichar_I, font_id);
200 EXPECT_GE(shape_I, 0);
201 int shape_l = shape_table_->FindShape(unichar_l, font_id);
202 EXPECT_GE(shape_l, 0);
203 int shape_1 = shape_table_->FindShape(unichar_1, font_id);
204 EXPECT_GE(shape_1, 0);
206 float dist_I_l = master_trainer_->ShapeDistance(*shape_table_, shape_I, shape_l);
208 EXPECT_EQ(0.0f, dist_I_l);
209 float dist_l_I = master_trainer_->ShapeDistance(*shape_table_, shape_l, shape_I);
211 EXPECT_EQ(0.0f, dist_l_I);
214 float dist_l_1 = master_trainer_->ShapeDistance(*shape_table_, shape_l, shape_1);
216 float dist_1_l = master_trainer_->ShapeDistance(*shape_table_, shape_1, shape_l);
220 float dist_I_1 = master_trainer_->ShapeDistance(*shape_table_, shape_I, shape_1);
222 float dist_1_I = master_trainer_->ShapeDistance(*shape_table_, shape_1, shape_I);
235 #ifdef DISABLED_LEGACY_ENGINE
248 #ifdef DISABLED_LEGACY_ENGINE
256 if (shape_table_->FindShape(0, -1) < 0) {
257 shape_table_->AddShape(0, 0);
260 auto shape_classifier = std::make_unique<MockClassifier>(shape_table_);
262 std::string accuracy_report;
264 shape_classifier.get(), &accuracy_report);
265 LOG(
INFO) << accuracy_report.c_str();
266 std::string result_string = accuracy_report.c_str();
267 std::vector<std::string> results =
split(result_string,
'\t');
271 EXPECT_TRUE(safe_strto32(results[i + 1], &result_values[i]));
275 int num_samples = master_trainer_->GetSamples()->num_raw_samples();
const double kMin1lDistance
const std::vector< std::string > split(const std::string &s, char c)
std::string TestDataNameToPath(const std::string &name)
std::unique_ptr< MasterTrainer > LoadTrainingData(const char *const *filelist, bool replication, ShapeTable **shape_table, std::string &file_prefix)
TEST_F(EuroText, FastLatinOCR)
UNICHAR_ID class_id() const
static std::string JoinPath(const std::string &s1, const std::string &s2)
const ShapeTable * GetShapeTable() const override
~MockClassifier() override=default
MockClassifier(ShapeTable *shape_table)
int ClassifySample(const TrainingSample &sample, Image page_pix, int debug, UNICHAR_ID keep_this, std::vector< ShapeRating > *results) override
~MasterTrainerTest() override
ShapeTable * shape_table_
std::string TestDataNameToPath(const std::string &name)
std::string TmpNameToPath(const std::string &name)
std::unique_ptr< MasterTrainer > master_trainer_