/*---------------------------------------------------------------------------*/
void ReadTrainingSamples (
     FILE	*File,
	 LIST* TrainingSamples)

/*
**	Parameters:
**		File		open text file to read samples from
**	Globals: none
**	Operation:
**		This routine reads training samples from a file and
**		places them into a data structure which organizes the
**		samples by FontName and CharName.  It then returns this
**		data structure.
**	Return: none
**	Exceptions: none
**	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 = CharDesc->FeatureSets[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 < CharDesc->NumFeatureSets; i++)
            if (Type != i)
              FreeFeatureSet(CharDesc->FeatureSets[i]);
          free (CharDesc);
        }
}	// ReadTrainingSamples
/*---------------------------------------------------------------------------*/
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);
}
/*---------------------------------------------------------------------------*/
LIST ReadTrainingSamples (
     FILE	*File)

/*
**	Parameters:
**		File		open text file to read samples from
**	Globals: none
**	Operation:
**		This routine reads training samples from a file and
**		places them into a data structure which organizes the
**		samples by FontName and CharName.  It then returns this
**		data structure.
**	Return: none
**	Exceptions: none
**	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;
	LIST			TrainingSamples = NIL;
	CHAR_DESC		CharDesc;
	int			Type, i;

	while (fscanf (File, "%s %s", CTFontName, unichar) == 2) {
          if (!unicharset_training.contains_unichar(unichar)) {
            unicharset_training.unichar_insert(unichar);
            if (unicharset_training.size() > MAX_NUM_CLASSES) {
              cprintf("Error: Size of unicharset of mftraining is "
                      "greater than MAX_NUM_CLASSES\n");
              exit(1);
            }
          }
		CharSample = FindList (TrainingSamples, unichar);
		if (CharSample == NULL) {
			CharSample = NewLabeledList (unichar);
			TrainingSamples = push (TrainingSamples, CharSample);
		}
		CharDesc = ReadCharDescription (File);
		Type = ShortNameToFeatureType(PROGRAM_FEATURE_TYPE);
		FeatureSamples = CharDesc->FeatureSets[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] += dim == MFDirection ?
                                    UniformRandomNumber(-MINSD_ANGLE, MINSD_ANGLE) :
                                    UniformRandomNumber(-MINSD, MINSD);
                }
		CharSample->List = push (CharSample->List, FeatureSamples);
        CharSample->SampleCount++;
		for (i = 0; i < CharDesc->NumFeatureSets; i++)
                  if (Type != i)
                    FreeFeatureSet(CharDesc->FeatureSets[i]);
		free (CharDesc);
        }
	return (TrainingSamples);

}	/* ReadTrainingSamples */