cntraining.cpp

来自「一个google的OCR源码」· C++ 代码 · 共 856 行 · 第 1/2 页

CPP
856
字号
**	History: Fri Aug 18 13:11:39 1989, DSJ, Created.**			 Tue May 17 1998 simplifications to structure, illiminated**				font, and feature specification levels of structure.*/{	char		unichar[UNICHAR_LEN + 1];	LABELEDLIST	CharSample;	FEATURE_SET	FeatureSamples;	CHAR_DESC	CharDesc;	int		Type, i;	while (fscanf (File, "%s %s", FontName, unichar) == 2) {          CharSample = FindList (*TrainingSamples, unichar);          if (CharSample == NULL) {            CharSample = NewLabeledList (unichar);            *TrainingSamples = push (*TrainingSamples, CharSample);          }          CharDesc = ReadCharDescription (File);          Type = ShortNameToFeatureType(PROGRAM_FEATURE_TYPE);          FeatureSamples = FeaturesOfType(CharDesc, Type);          for (int feature = 0; feature < FeatureSamples->NumFeatures; ++feature) {            FEATURE f = FeatureSamples->Features[feature];            for (int dim =0; dim < f->Type->NumParams; ++dim)              f->Params[dim] += UniformRandomNumber(-MINSD, MINSD);          }          CharSample->List = push (CharSample->List, FeatureSamples);          CharSample->SampleCount++;          for (i = 0; i < NumFeatureSetsIn (CharDesc); i++)            if (Type != i)              FreeFeatureSet (FeaturesOfType (CharDesc, i));          free (CharDesc);        }}	// ReadTrainingSamples/*---------------------------------------------------------------------------*/LABELEDLIST FindList (     LIST	List,     char	*Label)/***	Parameters:**		List		list to search**		Label		label to search for**	Globals: none**	Operation:**		This routine searches thru a list of labeled lists to find**		a list with the specified label.  If a matching labeled list**		cannot be found, NULL is returned.**	Return: Labeled list with the specified Label or NULL.**	Exceptions: none**	History: Fri Aug 18 15:57:41 1989, DSJ, Created.*/{	LABELEDLIST	LabeledList;	iterate (List)    {		LabeledList = (LABELEDLIST) first_node (List);		if (strcmp (LabeledList->Label, Label) == 0)			return (LabeledList);    }	return (NULL);}	/* FindList *//*---------------------------------------------------------------------------*/LABELEDLIST NewLabeledList (     char	*Label)/***	Parameters:**		Label	label for new list**	Globals: none**	Operation:**		This routine allocates a new, empty labeled list and gives**		it the specified label.**	Return: New, empty labeled list.**	Exceptions: none**	History: Fri Aug 18 16:08:46 1989, DSJ, Created.*/{	LABELEDLIST	LabeledList;	LabeledList = (LABELEDLIST) (char*)Emalloc (sizeof (LABELEDLISTNODE));	LabeledList->Label = (char*)Emalloc (strlen (Label)+1);	strcpy (LabeledList->Label, Label);	LabeledList->List = NIL;    LabeledList->SampleCount = 0;	return (LabeledList);}	/* NewLabeledList *//*---------------------------------------------------------------------------*/void WriteTrainingSamples (     char	*Directory,     LIST	CharList)/***	Parameters:**		Directory	directory to place sample files into**		FontList	list of fonts used in the training samples**	Globals:**		MaxNumSamples	max number of samples per class to write**	Operation:**		This routine writes the specified samples into files which**		are organized according to the font name and character name**		of the samples.**	Return: none**	Exceptions: none**	History: Fri Aug 18 16:17:06 1989, DSJ, Created.*/{	LABELEDLIST	CharSample;	FEATURE_SET	FeatureSet;	LIST		FeatureList;	FILE		*File;	char		Filename[MAXNAMESIZE];	int		NumSamples;	iterate (CharList)		// iterate thru all of the fonts	{		CharSample = (LABELEDLIST) first_node (CharList);		// construct the full pathname for the current samples file		strcpy (Filename, "");		if (Directory != NULL)		{			strcat (Filename, Directory);			strcat (Filename, "/");		}		strcat (Filename, "Merged");		strcat (Filename, "/");		strcat (Filename, CharSample->Label);		strcat (Filename, ".");		strcat (Filename, PROGRAM_FEATURE_TYPE);		printf ("\nWriting %s ...", Filename);		/* if file does not exist, create a new one with an appropriate		header; otherwise append samples to the existing file */		File = fopen (Filename, "r");		if (File == NULL)		{			File = Efopen (Filename, "w");			WriteOldParamDesc				(File, DefinitionOf (ShortNameToFeatureType (PROGRAM_FEATURE_TYPE)));		}		else		{			fclose (File);			File = Efopen (Filename, "a");		}		// append samples onto the file		FeatureList = CharSample->List;		NumSamples = 0;		iterate (FeatureList)		{			//if (NumSamples >= MaxNumSamples) break;			FeatureSet = (FEATURE_SET) first_node (FeatureList);			WriteFeatureSet (File, FeatureSet);			NumSamples++;		}		fclose (File);	}}	/* WriteTrainingSamples *//*----------------------------------------------------------------------------*/void WriteNormProtos (     char	*Directory,     LIST	LabeledProtoList,	 CLUSTERER *Clusterer)/***	Parameters:**		Directory	directory to place sample files into**	Globals:**		MaxNumSamples	max number of samples per class to write**	Operation:**		This routine writes the specified samples into files which**		are organized according to the font name and character name**		of the samples.**	Return: none**	Exceptions: none**	History: Fri Aug 18 16:17:06 1989, DSJ, Created.*/{	FILE		*File;	char		Filename[MAXNAMESIZE];	LABELEDLIST LabeledProto;	int N;	strcpy (Filename, "");	if (Directory != NULL)	{		strcat (Filename, Directory);		strcat (Filename, "/");	}	strcat (Filename, "normproto");	printf ("\nWriting %s ...", Filename);	File = Efopen (Filename, "w");	fprintf(File,"%0d\n",Clusterer->SampleSize);	WriteParamDesc(File,Clusterer->SampleSize,Clusterer->ParamDesc);	iterate(LabeledProtoList)	{		LabeledProto = (LABELEDLIST) first_node (LabeledProtoList);		N = NumberOfProtos(LabeledProto->List,		ShowSignificantProtos, ShowInsignificantProtos);                if (N < 1) {                  printf ("\nError! Not enough protos for %s: %d protos"                          " (%d significant protos"                          ", %d insignificant protos)\n",                          LabeledProto->Label, N,                          NumberOfProtos(LabeledProto->List, 1, 0),                          NumberOfProtos(LabeledProto->List, 0, 1));                  exit(1);                }		fprintf(File, "\n%s %d\n", LabeledProto->Label, N);		WriteProtos(File, Clusterer->SampleSize, LabeledProto->List,			ShowSignificantProtos, ShowInsignificantProtos);	}	fclose (File);}	// WriteNormProtos/*---------------------------------------------------------------------------*/void FreeTrainingSamples (     LIST	CharList)/***	Parameters:**		FontList	list of all fonts in document**	Globals: none**	Operation:**		This routine deallocates all of the space allocated to**		the specified list of training samples.**	Return: none**	Exceptions: none**	History: Fri Aug 18 17:44:27 1989, DSJ, Created.*/{	LABELEDLIST	CharSample;	FEATURE_SET	FeatureSet;	LIST		FeatureList;	printf ("\nFreeTrainingSamples...");	iterate (CharList) 		/* iterate thru all of the fonts */	{		CharSample = (LABELEDLIST) first_node (CharList);		FeatureList = CharSample->List;		iterate (FeatureList)	/* iterate thru all of the classes */		{			FeatureSet = (FEATURE_SET) first_node (FeatureList);			FreeFeatureSet (FeatureSet);		}		FreeLabeledList (CharSample);	}	destroy (CharList);}	/* FreeTrainingSamples *//*-------------------------------------------------------------------------*/void FreeNormProtoList (     LIST	CharList){	LABELEDLIST	CharSample;	iterate (CharList) 		/* iterate thru all of the fonts */	{		CharSample = (LABELEDLIST) first_node (CharList);		FreeLabeledList (CharSample);	}	destroy (CharList);}	// FreeNormProtoList/*---------------------------------------------------------------------------*/void FreeLabeledList (     LABELEDLIST	LabeledList)/***	Parameters:**		LabeledList	labeled list to be freed**	Globals: none**	Operation:**		This routine deallocates all of the memory consumed by**		a labeled list.  It does not free any memory which may be**		consumed by the items in the list.**	Return: none**	Exceptions: none**	History: Fri Aug 18 17:52:45 1989, DSJ, Created.*/{	destroy (LabeledList->List);	free (LabeledList->Label);	free (LabeledList);}	/* FreeLabeledList *//*---------------------------------------------------------------------------*/CLUSTERER *SetUpForClustering(     LABELEDLIST	CharSample)/***	Parameters:**		CharSample: LABELEDLIST that holds all the feature information for a**		given character.**	Globals:**		None**	Operation:**		This routine reads samples from a LABELEDLIST and enters**		those samples into a clusterer data structure.  This**		data structure is then returned to the caller.**	Return:**		Pointer to new clusterer data structure.**	Exceptions:**		None**	History:**		8/16/89, DSJ, Created.*/{	uinT16	N;	int		i, j;	FLOAT32	*Sample = NULL;	CLUSTERER	*Clusterer;	inT32		CharID;	LIST FeatureList = NULL;	FEATURE_SET FeatureSet = NULL;	FEATURE_DESC FeatureDesc = NULL;//	PARAM_DESC* ParamDesc;	FeatureDesc = DefinitionOf(ShortNameToFeatureType(PROGRAM_FEATURE_TYPE));	N = FeatureDesc->NumParams;	//ParamDesc = ConvertToPARAMDESC(FeatureDesc->ParamDesc, N);	Clusterer = MakeClusterer(N,FeatureDesc->ParamDesc);//	free(ParamDesc);	FeatureList = CharSample->List;	CharID = 0;	iterate(FeatureList)	{		FeatureSet = (FEATURE_SET) first_node (FeatureList);		for (i=0; i < FeatureSet->MaxNumFeatures; i++)		{			if (Sample == NULL)				Sample = (FLOAT32 *)Emalloc(N * sizeof(FLOAT32));			for (j=0; j < N; j++)				if (RoundingAccuracy != 0.0)					Sample[j] = round(FeatureSet->Features[i]->Params[j], RoundingAccuracy);				else					Sample[j] = FeatureSet->Features[i]->Params[j];				MakeSample (Clusterer, Sample, CharID);		}		CharID++;	}	if ( Sample != NULL ) free( Sample );	return( Clusterer );}	/* SetUpForClustering *//*---------------------------------------------------------------------------*/void AddToNormProtosList(	LIST* NormProtoList,	LIST ProtoList,	char* CharName){	PROTOTYPE* Proto;	LABELEDLIST LabeledProtoList;	LabeledProtoList = NewLabeledList(CharName);	iterate(ProtoList)	{		Proto = (PROTOTYPE *) first_node (ProtoList);		LabeledProtoList->List = push(LabeledProtoList->List, Proto);	}	*NormProtoList = push(*NormProtoList, LabeledProtoList);}/*-------------------------------------------------------------------------*/void WriteProtos(     FILE	*File,     uinT16	N,     LIST	ProtoList,     BOOL8	WriteSigProtos,     BOOL8	WriteInsigProtos){	PROTOTYPE	*Proto;	// write prototypes	iterate(ProtoList)	{		Proto = (PROTOTYPE *) first_node ( ProtoList );		if (( Proto->Significant && WriteSigProtos )	||			( ! Proto->Significant && WriteInsigProtos ) )			WritePrototype( File, N, Proto );	}}	// WriteProtos/*---------------------------------------------------------------------------*/int NumberOfProtos(	LIST ProtoList,    BOOL8	CountSigProtos,    BOOL8	CountInsigProtos){	int N = 0;	PROTOTYPE	*Proto;	iterate(ProtoList)	{		Proto = (PROTOTYPE *) first_node ( ProtoList );		if (( Proto->Significant && CountSigProtos )	||			( ! Proto->Significant && CountInsigProtos ) )			N++;	}	return(N);}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?