16 #include "allheaders.h"
67 "Min number of samples per proto as % of total");
69 "Max percentage of samples in a cluster which have more"
70 " than 1 feature in that cluster");
72 "Desired independence between dimensions");
74 "Desired confidence in prototypes created");
93 usage +=
" [.tr files ...]";
100 MAX(0.0,
MIN(1.0,
double(FLAGS_clusterconfig_min_samples_fraction)));
102 MAX(0.0,
MIN(1.0,
double(FLAGS_clusterconfig_max_illegal)));
104 MAX(0.0,
MIN(1.0,
double(FLAGS_clusterconfig_independence)));
106 MAX(0.0,
MIN(1.0,
double(FLAGS_clusterconfig_confidence)));
108 if (!FLAGS_configfile.empty()) {
110 FLAGS_configfile.c_str(),
120 STRING shape_table_file = file_prefix;
121 shape_table_file += kShapeTableFileSuffix;
122 FILE* shape_fp = fopen(shape_table_file.
string(),
"rb");
123 if (shape_fp !=
NULL) {
128 tprintf(
"Error: Failed to read shape table %s\n",
129 shape_table_file.
string());
131 int num_shapes = shape_table->
NumShapes();
132 tprintf(
"Read shape table %s of %d shapes\n",
133 shape_table_file.
string(), num_shapes);
137 tprintf(
"Warning: No shape table file present: %s\n",
138 shape_table_file.
string());
145 STRING shape_table_file = file_prefix;
146 shape_table_file += kShapeTableFileSuffix;
147 FILE* fp = fopen(shape_table_file.
string(),
"wb");
150 fprintf(stderr,
"Error writing shape table: %s\n",
151 shape_table_file.
string());
155 fprintf(stderr,
"Error creating shape table: %s\n",
156 shape_table_file.
string());
182 if (!FLAGS_D.empty()) {
183 *file_prefix += FLAGS_D.
c_str();
190 bool shape_analysis =
false;
191 if (shape_table !=
NULL) {
193 if (*shape_table !=
NULL)
194 shape_analysis =
true;
196 shape_analysis =
true;
204 if (FLAGS_T.empty()) {
207 if (!FLAGS_F.empty()) {
213 if (!FLAGS_X.empty()) {
220 const char* page_name;
223 tprintf(
"Reading %s ...\n", page_name);
228 int pagename_len = strlen(page_name);
229 char *fontinfo_file_name =
new char[pagename_len + 7];
230 strncpy(fontinfo_file_name, page_name, pagename_len - 2);
231 strcpy(fontinfo_file_name + pagename_len - 2,
"fontinfo");
233 delete[] fontinfo_file_name;
236 if (FLAGS_load_images) {
237 STRING image_name = page_name;
246 if (!FLAGS_output_trainer.empty()) {
247 FILE* fp = fopen(FLAGS_output_trainer.c_str(),
"wb");
249 tprintf(
"Can't create saved trainer data!\n");
256 bool success =
false;
257 tprintf(
"Loading master trainer from file:%s\n",
259 FILE* fp = fopen(FLAGS_T.c_str(),
"rb");
261 tprintf(
"Can't read file %s to initialize master trainer\n",
268 tprintf(
"Deserialize of master trainer failed!\n");
275 if (!FLAGS_O.empty() &&
277 fprintf(stderr,
"Failed to save unicharset to file %s\n", FLAGS_O.c_str());
281 if (shape_table !=
NULL) {
284 if (*shape_table ==
NULL) {
287 tprintf(
"Flat shape table summary: %s\n",
288 (*shape_table)->SummaryStr().string());
290 (*shape_table)->set_unicharset(trainer->
unicharset());
340 if (strcmp (LabeledList->
Label, Label) == 0)
341 return (LabeledList);
364 strcpy (LabeledList->
Label, Label);
368 return (LabeledList);
395 const char *feature_name,
int max_samples,
397 FILE* file,
LIST* training_samples) {
406 LIST it = *training_samples;
412 while (fgets(buffer, 2048, file) !=
NULL) {
413 if (buffer[0] ==
'\n')
416 sscanf(buffer,
"%*s %s", unichar);
420 tprintf(
"Error: Size of unicharset in training is "
421 "greater than MAX_NUM_CLASSES\n");
425 char_sample =
FindList(*training_samples, unichar);
426 if (char_sample ==
NULL) {
428 *training_samples =
push(*training_samples, char_sample);
431 feature_samples = char_desc->
FeatureSets[feature_type];
433 char_sample->
List =
push(char_sample->
List, feature_samples);
440 if (feature_type != i)
466 FeatureList = char_sample->
List;
489 free(LabeledList->
Label);
509 const char* program_feature_type) {
522 FeatureList = char_sample->
List;
529 for (j = 0; j < N; j++)
535 if ( Sample !=
NULL ) free( Sample );
544 bool debug = strcmp(FLAGS_test_ch.c_str(), label) == 0;
546 LIST pProtoList = ProtoList;
554 LIST list_it = ProtoList;
557 if (test_p != Prototype && !test_p->
Merged) {
561 if (dist < best_dist) {
569 tprintf(
"Merging red clusters (%d+%d) at %g,%g and %g,%g\n",
571 best_match->
Mean[0], best_match->
Mean[1],
572 Prototype->
Mean[0], Prototype->
Mean[1]);
581 }
else if (best_match !=
NULL) {
583 tprintf(
"Red proto at %g,%g matched a green one at %g,%g\n",
584 Prototype->
Mean[0], Prototype->
Mean[1],
585 best_match->
Mean[0], best_match->
Mean[1]);
591 pProtoList = ProtoList;
598 tprintf(
"Red proto at %g,%g becoming green\n",
599 Prototype->
Mean[0], Prototype->
Mean[1]);
636 BOOL8 KeepInsigProtos,
646 pProtoList = ProtoList;
662 for (i=0; i < N; i++)
667 for (i=0; i < N; i++)
676 for (i=0; i < N; i++)
685 for (i=0; i < N; i++)
693 NewProtoList =
push_last(NewProtoList, NewProto);
697 return (NewProtoList);
710 if (strcmp (MergeClass->
Label, Label) == 0)
725 strcpy (MergeClass->
Label, Label);
749 free (MergeClass->
Label);
759 LIST LabeledClassList) {
786 for(i=0; i < NumProtos; i++)
790 Values[0] = OldProto->
X;
791 Values[1] = OldProto->
Y;
792 Values[2] = OldProto->
Angle;
794 NewProto->
X = OldProto->
X;
795 NewProto->
Y = OldProto->
Y;
798 NewProto->
A = Values[0];
799 NewProto->
B = Values[1];
800 NewProto->
C = Values[2];
808 for(i=0; i < NumConfigs; i++)
812 for(j=0; j < NumWords; j++)
813 NewConfig[j] = OldConfig[j];
817 return float_classes;
824 register float Slope;
825 register float Intercept;
826 register float Normalizer;
828 Slope = tan (Values [2] * 2 *
PI);
829 Intercept = Values [1] - Slope * Values [0];
830 Normalizer = 1 / sqrt (Slope * Slope + 1.0);
832 Values [0] = Slope * Normalizer;
833 Values [1] = - Normalizer;
834 Values [2] = Intercept * Normalizer;
866 LabeledProtoList->
List =
push(LabeledProtoList->
List, Proto);
868 *NormProtoList =
push(*NormProtoList, LabeledProtoList);
874 BOOL8 CountSigProtos,
875 BOOL8 CountInsigProtos)
MERGE_CLASS NewLabeledClass(const char *Label)
void memfree(void *element)
CHAR_DESC ReadCharDescription(const FEATURE_DEFS_STRUCT &FeatureDefs, FILE *File)
bool LoadFontInfo(const char *filename)
void LoadPageImages(const char *filename)
const UNICHAR_ID unichar_to_id(const char *const unichar_repr) const
#define WordsInVectorOfSize(NumBits)
bool save_to_file(const char *const filename) const
DOUBLE_PARAM_FLAG(clusterconfig_min_samples_fraction, Config.MinSamples,"Min number of samples per proto as % of total")
void Init(uinT8 xbuckets, uinT8 ybuckets, uinT8 thetabuckets)
void FreeNormProtoList(LIST CharList)
#define ProtoIn(Class, Pid)
void FreeLabeledList(LABELEDLIST LabeledList)
STRING_PARAM_FLAG(configfile,"","File to load more configs from")
void InitFeatureDefs(FEATURE_DEFS_STRUCT *featuredefs)
BIT_VECTOR NewBitVector(int NumBits)
const int kBoostXYBuckets
const int kBoostDirBuckets
void ParseCommandLineFlags(const char *usage, int *argc, char ***argv, const bool remove_flags)
LIST RemoveInsignificantProtos(LIST ProtoList, BOOL8 KeepSigProtos, BOOL8 KeepInsigProtos, int N)
const FEATURE_DESC_STRUCT * FeatureDesc[NUM_FEATURE_TYPES]
FEATURE_SET FeatureSets[NUM_FEATURE_TYPES]
void SetupFlatShapeTable(ShapeTable *shape_table)
bool Serialize(FILE *fp) const
CLUSTERER * MakeClusterer(inT16 SampleSize, const PARAM_DESC ParamDesc[])
SAMPLE * MakeSample(CLUSTERER *Clusterer, const FLOAT32 *Feature, inT32 CharID)
bool DeSerialize(bool swap, FILE *fp)
UnicityTableEqEq< int > font_set
bool Serialize(FILE *fp) const
void FreeLabeledClassList(LIST ClassList)
ShapeTable * LoadShapeTable(const STRING &file_prefix)
MERGE_CLASS FindClass(LIST List, const char *Label)
struct LABELEDLISTNODE * LABELEDLIST
void CleanUpUnusedData(LIST ProtoList)
const char * GetNextFilename(int argc, const char *const *argv)
CLASS_TYPE NewClass(int NumProtos, int NumConfigs)
FEATURE_DEFS_STRUCT feature_defs
void Normalize(float *Values)
CLUSTERER * SetUpForClustering(const FEATURE_DEFS_STRUCT &FeatureDefs, LABELEDLIST char_sample, const char *program_feature_type)
int NumberOfProtos(LIST ProtoList, BOOL8 CountSigProtos, BOOL8 CountInsigProtos)
void truncate_at(inT32 index)
void MergeInsignificantProtos(LIST ProtoList, const char *label, CLUSTERER *Clusterer, CLUSTERCONFIG *Config)
void FreeProtoList(LIST *ProtoList)
LIST push_last(LIST list, void *item)
bool LoadXHeights(const char *filename)
void ParseArguments(int *argc, char ***argv)
void FreeTrainingSamples(LIST CharList)
const UNICHARSET & unicharset() const
void unichar_insert(const char *const unichar_repr)
void LoadUnicharset(const char *filename)
FLOAT32 ComputeDistance(int k, PARAM_DESC *dim, FLOAT32 p1[], FLOAT32 p2[])
void FreeClass(CLASS_TYPE Class)
FEATURE_SET_STRUCT * FEATURE_SET
bool DeSerialize(bool swap, FILE *fp)
inT32 MergeClusters(inT16 N, register PARAM_DESC ParamDesc[], register inT32 n1, register inT32 n2, register FLOAT32 m[], register FLOAT32 m1[], register FLOAT32 m2[])
MasterTrainer * LoadTrainingData(int argc, const char *const *argv, bool replication, ShapeTable **shape_table, STRING *file_prefix)
void move(UnicityTable< T > *from)
CLASS_STRUCT * SetUpForFloat2Int(const UNICHARSET &unicharset, LIST LabeledClassList)
int ShortNameToFeatureType(const FEATURE_DEFS_STRUCT &FeatureDefs, const char *ShortName)
bool contains_unichar(const char *const unichar_repr) const
static bool ReadParamsFile(const char *file, SetParamConstraint constraint, ParamsVectors *member_params)
void ReadTrainingSamples(const char *page_name, const FEATURE_DEFS_STRUCT &feature_defs, bool verification)
LABELEDLIST FindList(LIST List, char *Label)
const PARAM_DESC * ParamDesc
void SetFeatureSpace(const IntFeatureSpace &fs)
const char * string() const
void ReadTrainingSamples(const FEATURE_DEFS_STRUCT &feature_defs, const char *feature_name, int max_samples, UNICHARSET *unicharset, FILE *file, LIST *training_samples)
void FreeFeatureSet(FEATURE_SET FeatureSet)
void WriteShapeTable(const STRING &file_prefix, const ShapeTable &shape_table)
MERGE_CLASS_NODE * MERGE_CLASS
INT_PARAM_FLAG(debug_level, 0,"Level of Trainer debugging")
LABELEDLIST NewLabeledList(const char *Label)
LIST push(LIST list, void *element)
void AddToNormProtosList(LIST *NormProtoList, LIST ProtoList, char *CharName)
bool AddSpacingInfo(const char *filename)
const char * c_str() const