16 #ifdef DISABLED_LEGACY_ENGINE 54 usage +=
" -v | --version | ";
57 usage +=
" [.tr files ...]";
66 #include "allheaders.h" 110 "Min number of samples per proto as % of total");
112 "Max percentage of samples in a cluster which have more" 113 " than 1 feature in that cluster");
115 "Desired independence between dimensions");
117 "Desired confidence in prototypes created");
134 usage +=
" -v | --version | ";
137 usage +=
" [.tr files ...]";
144 std::max(0.0, std::min(1.0,
double(FLAGS_clusterconfig_min_samples_fraction)));
146 std::max(0.0, std::min(1.0,
double(FLAGS_clusterconfig_max_illegal)));
148 std::max(0.0, std::min(1.0,
double(FLAGS_clusterconfig_independence)));
150 std::max(0.0, std::min(1.0,
double(FLAGS_clusterconfig_confidence)));
152 if (!FLAGS_configfile.empty()) {
154 FLAGS_configfile.c_str(),
164 STRING shape_table_file = file_prefix;
165 shape_table_file += kShapeTableFileSuffix;
167 if (shape_fp.
Open(shape_table_file.
string(),
nullptr)) {
171 shape_table =
nullptr;
172 tprintf(
"Error: Failed to read shape table %s\n",
173 shape_table_file.
string());
175 int num_shapes = shape_table->
NumShapes();
176 tprintf(
"Read shape table %s of %d shapes\n",
177 shape_table_file.
string(), num_shapes);
180 tprintf(
"Warning: No shape table file present: %s\n",
181 shape_table_file.
string());
188 STRING shape_table_file = file_prefix;
189 shape_table_file += kShapeTableFileSuffix;
190 FILE* fp = fopen(shape_table_file.
string(),
"wb");
193 fprintf(stderr,
"Error writing shape table: %s\n",
194 shape_table_file.
string());
198 fprintf(stderr,
"Error creating shape table: %s\n",
199 shape_table_file.
string());
226 if (!FLAGS_D.empty()) {
227 *file_prefix += FLAGS_D.
c_str();
234 bool shape_analysis =
false;
235 if (shape_table !=
nullptr) {
237 if (*shape_table !=
nullptr) shape_analysis =
true;
239 shape_analysis =
true;
249 if (!FLAGS_F.empty()) {
255 if (!FLAGS_X.empty()) {
262 const char* page_name;
265 tprintf(
"Reading %s ...\n", page_name);
270 int pagename_len = strlen(page_name);
271 char* fontinfo_file_name =
new char[pagename_len + 7];
272 strncpy(fontinfo_file_name, page_name, pagename_len - 2);
273 strcpy(fontinfo_file_name + pagename_len - 2,
"fontinfo");
275 delete[] fontinfo_file_name;
278 if (FLAGS_load_images) {
279 STRING image_name = page_name;
288 if (!FLAGS_output_trainer.empty()) {
289 FILE* fp = fopen(FLAGS_output_trainer.c_str(),
"wb");
291 tprintf(
"Can't create saved trainer data!\n");
298 if (!FLAGS_O.empty() &&
300 fprintf(stderr,
"Failed to save unicharset to file %s\n", FLAGS_O.c_str());
304 if (shape_table !=
nullptr) {
307 if (*shape_table ==
nullptr) {
310 tprintf(
"Flat shape table summary: %s\n",
311 (*shape_table)->SummaryStr().string());
313 (*shape_table)->set_unicharset(trainer->
unicharset());
354 if (strcmp (LabeledList->
Label, Label) == 0)
355 return (LabeledList);
374 strcpy (LabeledList->
Label, Label);
378 return (LabeledList);
400 const char *feature_name,
int max_samples,
402 FILE* file,
LIST* training_samples) {
408 uint32_t feature_type =
412 LIST it = *training_samples;
418 while (fgets(buffer, 2048, file) !=
nullptr) {
419 if (buffer[0] ==
'\n')
422 sscanf(buffer,
"%*s %s", unichar);
426 tprintf(
"Error: Size of unicharset in training is " 427 "greater than MAX_NUM_CLASSES\n");
431 char_sample =
FindList(*training_samples, unichar);
432 if (char_sample ==
nullptr) {
434 *training_samples =
push(*training_samples, char_sample);
437 feature_samples = char_desc->
FeatureSets[feature_type];
439 char_sample->
List =
push(char_sample->
List, feature_samples);
446 if (feature_type != i)
467 LIST nodes = CharList;
470 FeatureList = char_sample->
List;
491 free(LabeledList->
Label);
509 const char* program_feature_type) {
512 float* Sample =
nullptr;
515 LIST FeatureList =
nullptr;
523 FeatureList = char_sample->
List;
528 if (Sample ==
nullptr) Sample = (
float*)
Emalloc(N *
sizeof(
float));
529 for (j = 0; j < N; j++)
545 bool debug = strcmp(FLAGS_test_ch.c_str(), label) == 0;
547 LIST pProtoList = ProtoList;
552 float best_dist = 0.125;
555 LIST list_it = ProtoList;
558 if (test_p != Prototype && !test_p->
Merged) {
562 if (dist < best_dist) {
568 if (best_match !=
nullptr && !best_match->
Significant) {
570 tprintf(
"Merging red clusters (%d+%d) at %g,%g and %g,%g\n",
572 best_match->
Mean[0], best_match->
Mean[1],
573 Prototype->
Mean[0], Prototype->
Mean[1]);
582 }
else if (best_match !=
nullptr) {
584 tprintf(
"Red proto at %g,%g matched a green one at %g,%g\n",
585 Prototype->
Mean[0], Prototype->
Mean[1],
586 best_match->
Mean[0], best_match->
Mean[1]);
593 pProtoList = ProtoList;
600 tprintf(
"Red proto at %g,%g becoming green\n",
601 Prototype->
Mean[0], Prototype->
Mean[1]);
629 bool KeepInsigProtos,
639 pProtoList = ProtoList;
648 NewProto->
Mean = (
float *)
Emalloc(N *
sizeof(
float));
655 for (i=0; i < N; i++)
659 for (i=0; i < N; i++)
667 for (i=0; i < N; i++)
675 for (i=0; i < N; i++)
683 NewProtoList =
push_last(NewProtoList, NewProto);
687 return (NewProtoList);
697 if (strcmp (MergeClass->
Label, Label) == 0)
710 strcpy (MergeClass->
Label, Label);
727 LIST nodes = ClassList;
731 free (MergeClass->
Label);
741 LIST LabeledClassList) {
768 for(i=0; i < NumProtos; i++)
772 Values[0] = OldProto->
X;
773 Values[1] = OldProto->
Y;
774 Values[2] = OldProto->
Angle;
776 NewProto->
X = OldProto->
X;
777 NewProto->
Y = OldProto->
Y;
780 NewProto->
A = Values[0];
781 NewProto->
B = Values[1];
782 NewProto->
C = Values[2];
790 for(i=0; i < NumConfigs; i++)
794 for(j=0; j < NumWords; j++)
795 NewConfig[j] = OldConfig[j];
799 return float_classes;
810 Slope = tan(Values [2] * 2 * M_PI);
811 Intercept = Values [1] - Slope * Values [0];
812 Normalizer = 1 / sqrt (Slope * Slope + 1.0);
814 Values [0] = Slope * Normalizer;
815 Values [1] = - Normalizer;
816 Values [2] = Intercept * Normalizer;
825 LIST nodes = CharList;
848 LabeledProtoList->
List =
push(LabeledProtoList->
List, Proto);
850 *NormProtoList =
push(*NormProtoList, LabeledProtoList);
855 bool CountInsigProtos) {
867 #endif // def DISABLED_LEGACY_ENGINE LIST RemoveInsignificantProtos(LIST ProtoList, bool KeepSigProtos, bool KeepInsigProtos, int N)
bool LoadXHeights(const char *filename)
bool Serialize(FILE *fp) const
void move(UnicityTable< T > *from)
void FreeLabeledList(LABELEDLIST LabeledList)
const UNICHARSET & unicharset() const
DOUBLE_PARAM_FLAG(clusterconfig_min_samples_fraction, Config.MinSamples, "Min number of samples per proto as % of total")
const int kBoostXYBuckets
#define WordsInVectorOfSize(NumBits)
void Init(uint8_t xbuckets, uint8_t ybuckets, uint8_t thetabuckets)
bool save_to_file(const char *const filename) const
bool AddSpacingInfo(const char *filename)
void LoadUnicharset(const char *filename)
CHAR_DESC ReadCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs, FILE *File)
const char * string() const
void ReadTrainingSamples(const FEATURE_DEFS_STRUCT &feature_definitions, const char *feature_name, int max_samples, UNICHARSET *unicharset, FILE *file, LIST *training_samples)
LABELEDLIST NewLabeledList(const char *Label)
void ParseArguments(int *argc, char ***argv)
LIST push(LIST list, void *element)
UNICHAR_ID unichar_to_id(const char *const unichar_repr) const
bool DeSerialize(TFile *fp)
const int kBoostDirBuckets
SAMPLE * MakeSample(CLUSTERER *Clusterer, const float *Feature, int32_t CharID)
void MergeInsignificantProtos(LIST ProtoList, const char *label, CLUSTERER *Clusterer, CLUSTERCONFIG *clusterconfig)
struct LABELEDLISTNODE * LABELEDLIST
UnicityTableEqEq< int > font_set
#define ProtoIn(Class, Pid)
void LoadPageImages(const char *filename)
FEATURE_DEFS_STRUCT feature_defs
void unichar_insert(const char *const unichar_repr, OldUncleanUnichars old_style)
const PARAM_DESC * ParamDesc
const char * c_str() const
void FreeClass(CLASS_TYPE Class)
MERGE_CLASS FindClass(LIST List, const char *Label)
void ParseCommandLineFlags(const char *usage, int *argc, char ***argv, const bool remove_flags)
void FreeProtoList(LIST *ProtoList)
INT_PARAM_FLAG(debug_level, 0, "Level of Trainer debugging")
BIT_VECTOR NewBitVector(int NumBits)
void WriteShapeTable(const STRING &file_prefix, const ShapeTable &shape_table)
MERGE_CLASS NewLabeledClass(const char *Label)
bool contains_unichar(const char *const unichar_repr) const
void FreeNormProtoList(LIST CharList)
void SetFeatureSpace(const IntFeatureSpace &fs)
void InitFeatureDefs(FEATURE_DEFS_STRUCT *featuredefs)
bool LoadFontInfo(const char *filename)
static bool ReadParamsFile(const char *file, SetParamConstraint constraint, ParamsVectors *member_params)
LIST push_last(LIST list, void *item)
FEATURE_SET FeatureSets[NUM_FEATURE_TYPES]
int32_t MergeClusters(int16_t N, PARAM_DESC ParamDesc[], int32_t n1, int32_t n2, float m[], float m1[], float m2[])
void AddToNormProtosList(LIST *NormProtoList, LIST ProtoList, char *CharName)
DLLSYM void tprintf(const char *format,...)
FEATURE_SET_STRUCT * FEATURE_SET
void truncate_at(int32_t index)
STRING_PARAM_FLAG(configfile, "", "File to load more configs from")
void FreeTrainingSamples(LIST CharList)
void FreeLabeledClassList(LIST ClassList)
bool Open(const STRING &filename, FileReader reader)
LABELEDLIST FindList(LIST List, char *Label)
int NumberOfProtos(LIST ProtoList, bool CountSigProtos, bool CountInsigProtos)
void FreeFeatureSet(FEATURE_SET FeatureSet)
bool Serialize(FILE *fp) const
const FEATURE_DESC_STRUCT * FeatureDesc[NUM_FEATURE_TYPES]
void CleanUpUnusedData(LIST ProtoList)
const char * GetNextFilename(int argc, const char *const *argv)
void ReadTrainingSamples(const char *page_name, const FEATURE_DEFS_STRUCT &feature_defs, bool verification)
uint32_t ShortNameToFeatureType(const FEATURE_DEFS_STRUCT &FeatureDefs, const char *ShortName)
MERGE_CLASS_NODE * MERGE_CLASS
CLASS_TYPE NewClass(int NumProtos, int NumConfigs)
void SetupFlatShapeTable(ShapeTable *shape_table)
void Normalize(float *Values)
CLASS_STRUCT * SetUpForFloat2Int(const UNICHARSET &unicharset, LIST LabeledClassList)
CLUSTERER * SetUpForClustering(const FEATURE_DEFS_STRUCT &FeatureDefs, LABELEDLIST char_sample, const char *program_feature_type)
MasterTrainer * LoadTrainingData(int argc, const char *const *argv, bool replication, ShapeTable **shape_table, STRING *file_prefix)
float ComputeDistance(int k, PARAM_DESC *dim, float p1[], float p2[])
CLUSTERER * MakeClusterer(int16_t SampleSize, const PARAM_DESC ParamDesc[])
ShapeTable * LoadShapeTable(const STRING &file_prefix)