adaptmatch.cpp

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

CPP
1,846
字号
          }          else {            #ifndef SECURE_NAMES            if (LearningDebugLevel >= 1)              cprintf ("Adapting to char = %s, thr= %g\n",                       unicharset.id_to_unichar(                           unicharset.unichar_to_id(BestChoice_string,                                                    *BestChoice_lengths)),                           *Threshold);            #endif            AdaptToChar(Blob, &LineStats,                        unicharset.unichar_to_id(BestChoice_string,                                                 *BestChoice_lengths),                        *Threshold);          }//        }//        else//          AdaptToPunc(Blob, &LineStats,//                      unicharset.unichar_to_id(BestChoice_string,//                                               *BestChoice_lengths),//                      *Threshold);      }    }    if (LearningDebugLevel >= 1)      cprintf ("\n");  }}                                /* AdaptToWord *//*---------------------------------------------------------------------------*/void EndAdaptiveClassifier() {/* **                         Parameters: none **                         Globals: **                         AdaptedTemplates              current set of adapted templates**                          SaveAdaptedTemplates              TRUE if templates should be saved**                          EnableAdaptiveMatcher              TRUE if adaptive matcher is enabled**                          Operation: This routine performs cleanup operations on the**                          adaptive classifier.  It should be called before the**                          program is terminated.  Its main function is to save**                          the adapted templates to a file.**                          Return: none**                          Exceptions: none**                          History: Tue Mar 19 14:37:06 1991, DSJ, Created.*/  char Filename[256];  FILE *File;  #ifndef SECURE_NAMES  if (EnableAdaptiveMatcher && SaveAdaptedTemplates) {    strcpy(Filename, imagefile);    strcat(Filename, ADAPT_TEMPLATE_SUFFIX);    File = fopen (Filename, "wb");    if (File == NULL)      cprintf ("Unable to save adapted templates to %s!\n", Filename);    else {      cprintf ("\nSaving adapted templates to %s ...", Filename);      fflush(stdout);      WriteAdaptedTemplates(File, AdaptedTemplates);      cprintf ("\n");      fclose(File);    }  }  #endif  if (PreTrainedTemplates == NULL)    return;  // This function isn't safe to run twice.  EndDangerousAmbigs();  FreeNormProtos();  free_int_templates(PreTrainedTemplates);  PreTrainedTemplates = NULL;  FreeBitVector(AllProtosOn);  FreeBitVector(PrunedProtos);  FreeBitVector(AllConfigsOn);  FreeBitVector(AllProtosOff);  FreeBitVector(AllConfigsOff);  FreeBitVector(TempProtoMask);  AllProtosOn = NULL;  PrunedProtos = NULL;  AllConfigsOn = NULL;  AllProtosOff = NULL;  AllConfigsOff = NULL;  TempProtoMask = NULL;}                                /* EndAdaptiveClassifier *//*---------------------------------------------------------------------------*/void InitAdaptiveClassifier() {/* **                         Parameters: none **                         Globals: **                         BuiltInTemplatesFile              file to get built-in temps from**                          BuiltInCutoffsFile              file to get avg. feat per class from**                          PreTrainedTemplates              pre-trained configs and protos**                          AdaptedTemplates              templates adapted to current page**                          CharNormCutoffs              avg # of features per class**                          AllProtosOn              dummy proto mask with all bits 1**                          AllConfigsOn              dummy config mask with all bits 1**                          UsePreAdaptedTemplates              enables use of pre-adapted templates**                          Operation: This routine reads in the training information needed**                          by the adaptive classifier and saves it into global**                          variables.**                          Return: none**                          Exceptions: none**                          History: Mon Mar 11 12:49:34 1991, DSJ, Created.*/  int i;  FILE *File;  STRING Filename;  if (!EnableAdaptiveMatcher)    return;  if (PreTrainedTemplates != NULL)    EndAdaptiveClassifier();  // Don't leak with multiple inits.  Filename = language_data_path_prefix;  Filename += BuiltInTemplatesFile;  #ifndef SECURE_NAMES  //      cprintf( "\nReading built-in templates from %s ...",  //              Filename);  fflush(stdout);  #endif  #ifdef __UNIX__  File = Efopen (Filename.string(), "r");  #else  File = Efopen (Filename.string(), "rb");  #endif  PreTrainedTemplates = ReadIntTemplates (File, TRUE);  fclose(File);  Filename = language_data_path_prefix;  Filename += BuiltInCutoffsFile;  #ifndef SECURE_NAMES  //      cprintf( "\nReading built-in pico-feature cutoffs from %s ...",  //              Filename);  fflush(stdout);  #endif  ReadNewCutoffs (Filename.string(), PreTrainedTemplates->IndexFor,                  CharNormCutoffs);  GetNormProtos();  InitIntegerMatcher();  InitIntegerFX();  AllProtosOn = NewBitVector (MAX_NUM_PROTOS);  PrunedProtos = NewBitVector (MAX_NUM_PROTOS);  AllConfigsOn = NewBitVector (MAX_NUM_CONFIGS);  AllProtosOff = NewBitVector (MAX_NUM_PROTOS);  AllConfigsOff = NewBitVector (MAX_NUM_CONFIGS);  TempProtoMask = NewBitVector (MAX_NUM_PROTOS);  set_all_bits (AllProtosOn, WordsInVectorOfSize (MAX_NUM_PROTOS));  set_all_bits (PrunedProtos, WordsInVectorOfSize (MAX_NUM_PROTOS));  set_all_bits (AllConfigsOn, WordsInVectorOfSize (MAX_NUM_CONFIGS));  zero_all_bits (AllProtosOff, WordsInVectorOfSize (MAX_NUM_PROTOS));  zero_all_bits (AllConfigsOff, WordsInVectorOfSize (MAX_NUM_CONFIGS));  if (UsePreAdaptedTemplates) {    Filename = imagefile;    Filename += ADAPT_TEMPLATE_SUFFIX;    File = fopen (Filename.string(), "rb");    if (File == NULL)      AdaptedTemplates = NewAdaptedTemplates ();    else {      #ifndef SECURE_NAMES      cprintf ("\nReading pre-adapted templates from %s ...", Filename.string());      fflush(stdout);      #endif      AdaptedTemplates = ReadAdaptedTemplates (File);      cprintf ("\n");      fclose(File);      PrintAdaptedTemplates(stdout, AdaptedTemplates);      for (i = 0; i < NumClassesIn (AdaptedTemplates->Templates); i++) {        BaselineCutoffs[i] =          CharNormCutoffs[IndexForClassId (PreTrainedTemplates,          ClassIdForIndex          (AdaptedTemplates->Templates,          i))];      }    }  } else {    if (AdaptedTemplates != NULL)      free_adapted_templates(AdaptedTemplates);    AdaptedTemplates = NewAdaptedTemplates ();  }  old_enable_learning = EnableLearning;}                                /* InitAdaptiveClassifier */void ResetAdaptiveClassifier() {  free_adapted_templates(AdaptedTemplates);  AdaptedTemplates = NULL;}/*---------------------------------------------------------------------------*/void InitAdaptiveClassifierVars() {/* **                         Parameters: none **                         Globals: none **                         Operation: This routine installs the control knobs used by the **                         adaptive matcher. **                         Return: none **                         Exceptions: none **                         History: Mon Mar 11 12:49:34 1991, DSJ, Created. */  VALUE dummy;  string_variable (BuiltInTemplatesFile, "BuiltInTemplatesFile",    BUILT_IN_TEMPLATES_FILE);  string_variable (BuiltInCutoffsFile, "BuiltInCutoffsFile",    BUILT_IN_CUTOFFS_FILE);  MakeEnableAdaptiveMatcher();  MakeUsePreAdaptedTemplates();  MakeSaveAdaptedTemplates();  MakeEnableLearning();  MakeEnableAdaptiveDebugger();  MakeBadMatchPad();  MakeGoodAdaptiveMatch();  MakeGreatAdaptiveMatch();  MakeNoiseBlobLength();  MakeMinNumPermClasses();  MakeReliableConfigThreshold();  MakeMaxAngleDelta();  MakeLearningDebugLevel();  MakeMatcherDebugLevel();  MakeMatchDebugFlags();  MakeRatingMargin();  MakePerfectRating();  MakeEnableIntFX();  MakeEnableNewAdaptRules();  MakeRatingScale();  MakeCertaintyScale();  MakeFailedAdaptionsBeforeReset();  InitPicoFXVars();  InitOutlineFXVars();  //?}                                /* InitAdaptiveClassifierVars *//*---------------------------------------------------------------------------*/void PrintAdaptiveStatistics(FILE *File) {/* **                         Parameters: **                         File              open text file to print adaptive statistics to**                          Globals: none**                          Operation: Print to File the statistics which have been gathered**                          for the adaptive matcher.**                          Return: none**                          Exceptions: none**                          History: Thu Apr 18 14:37:37 1991, DSJ, Created.*/  #ifndef SECURE_NAMES  fprintf (File, "\nADAPTIVE MATCHER STATISTICS:\n");  fprintf (File, "\tNum blobs classified = %d\n", AdaptiveMatcherCalls);  fprintf (File, "\tNum classes output   = %d (Avg = %4.2f)\n",    NumClassesOutput,    ((AdaptiveMatcherCalls == 0) ? (0.0) :  ((float) NumClassesOutput / AdaptiveMatcherCalls)));  fprintf (File, "\t\tBaseline Classifier: %4d calls (%4.2f classes/call)\n",    BaselineClassifierCalls,    ((BaselineClassifierCalls == 0) ? (0.0) :  ((float) NumBaselineClassesTried / BaselineClassifierCalls)));  fprintf (File, "\t\tCharNorm Classifier: %4d calls (%4.2f classes/call)\n",    CharNormClassifierCalls,    ((CharNormClassifierCalls == 0) ? (0.0) :  ((float) NumCharNormClassesTried / CharNormClassifierCalls)));  fprintf (File, "\t\tAmbig    Classifier: %4d calls (%4.2f classes/call)\n",    AmbigClassifierCalls,    ((AmbigClassifierCalls == 0) ? (0.0) :  ((float) NumAmbigClassesTried / AmbigClassifierCalls)));  fprintf (File, "\nADAPTIVE LEARNER STATISTICS:\n");  fprintf (File, "\tNumber of words adapted to: %d\n", NumWordsAdaptedTo);  fprintf (File, "\tNumber of chars adapted to: %d\n", NumCharsAdaptedTo);  PrintAdaptedTemplates(File, AdaptedTemplates);  #endif}                                /* PrintAdaptiveStatistics *//*---------------------------------------------------------------------------*/void SettupPass1() {/* **                         Parameters: none **                         Globals: **                         EnableLearning              set to TRUE by this routine**                          Operation: This routine prepares the adaptive matcher for the start**                          of the first pass.  Learning is enabled (unless it is**                          disabled for the whole program).**                          Return: none**                          Exceptions: none**                          History: Mon Apr 15 16:39:29 1991, DSJ, Created.*/  /* Note: this is somewhat redundant, it simply says that if learning is  enabled then it will remain enabled on the first pass.  If it is  disabled, then it will remain disabled.  This is only put here to  make it very clear that learning is controlled directly by the global    setting of EnableLearning. */  EnableLearning = old_enable_learning;  SettupStopperPass1();}                                /* SettupPass1 *//*---------------------------------------------------------------------------*/void SettupPass2() {/* **                         Parameters: none **                         Globals: **                         EnableLearning              set to FALSE by this routine**                          Operation: This routine prepares the adaptive matcher for the start**                          of the second pass.  Further learning is disabled.**                          Return: none**                          Exceptions: none**                          History: Mon Apr 15 16:39:29 1991, DSJ, Created.*/  EnableLearning = FALSE;  SettupStopperPass2();}                                /* SettupPass2 *//*---------------------------------------------------------------------------*/void MakeNewAdaptedClass(TBLOB *Blob,                         LINE_STATS *LineStats,                         CLASS_ID ClassId,                         ADAPT_TEMPLATES Templates) {/* **                         Parameters: **                         Blob              blob to model new class after**                          LineStats              statistics for text row blob is in**                          ClassId              id of new class to be created**                          Templates              adapted templates to add new class to**                          Globals:**                          AllProtosOn              dummy mask with all 1's**                          BaselineCutoffs              kludge needed to get cutoffs**                          PreTrainedTemplates              kludge needed to get cutoffs**                          Operation: This routine creates a new adapted class and uses Blob**                          as the model for the first config in that class.**                          Return: none**                          Exceptions: none**                          History: Thu Mar 14 12:49:39 1991, DSJ, Created.

⌨️ 快捷键说明

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