adaptmatch.cpp
来自「一个google的OCR源码」· C++ 代码 · 共 1,846 行 · 第 1/5 页
CPP
1,846 行
/****************************************************************************** ** Filename: adaptmatch.c ** Purpose: High level adaptive matcher. ** Author: Dan Johnson ** History: Mon Mar 11 10:00:10 1991, DSJ, Created. ** ** (c) Copyright Hewlett-Packard Company, 1988. ** Licensed under the Apache License, Version 2.0 (the "License"); ** you may not use this file except in compliance with the License. ** You may obtain a copy of the License at ** http://www.apache.org/licenses/LICENSE-2.0 ** Unless required by applicable law or agreed to in writing, software ** distributed under the License is distributed on an "AS IS" BASIS, ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. ** See the License for the specific language governing permissions and ** limitations under the License. ******************************************************************************//**---------------------------------------------------------------------------- Include Files and Type Defines----------------------------------------------------------------------------**/#include <ctype.h>#include "adaptmatch.h"#include "normfeat.h"#include "mfoutline.h"#include "picofeat.h"#include "float2int.h"#include "outfeat.h"#include "emalloc.h"#include "intfx.h"#include "permnum.h"#include "speckle.h"#include "efio.h"#include "normmatch.h"#include "stopper.h"#include "permute.h"#include "context.h"#include "ndminx.h"#include "intproto.h"#include "const.h"#include "globals.h"#include "werd.h"#include "callcpp.h"#include "tordvars.h"#include <stdio.h>#include <string.h>#include <ctype.h>#include <stdlib.h>#include <math.h>#ifdef __UNIX__#include <assert.h>#endif#define ADAPT_TEMPLATE_SUFFIX ".a"#define BUILT_IN_TEMPLATES_FILE "inttemp"#define BUILT_IN_CUTOFFS_FILE "pffmtable"#define MAX_MATCHES 10#define UNLIKELY_NUM_FEAT 200#define NO_DEBUG 0#define MAX_ADAPTABLE_WERD_SIZE 40#define ADAPTABLE_WERD (GOOD_NUMBER + 0.05)#define Y_DIM_OFFSET (Y_SHIFT - BASELINE_Y_SHIFT)#define WORST_POSSIBLE_RATING (1.0)typedef struct{ inT32 BlobLength; int NumMatches; CLASS_ID Classes[MAX_NUM_CLASSES]; FLOAT32 Ratings[MAX_CLASS_ID + 1]; uinT8 Configs[MAX_CLASS_ID + 1]; FLOAT32 BestRating; CLASS_ID BestClass; uinT8 BestConfig; CLASS_PRUNER_RESULTS CPResults;}ADAPT_RESULTS;typedef struct{ ADAPT_TEMPLATES Templates; CLASS_ID ClassId; int ConfigId;}PROTO_KEY;/**---------------------------------------------------------------------------- Private Macros----------------------------------------------------------------------------**/#define MarginalMatch(Rating) \((Rating) > GreatAdaptiveMatch)#define TempConfigReliable(Config) \((Config)->NumTimesSeen >= ReliableConfigThreshold)#define InitIntFX() (FeaturesHaveBeenExtracted = FALSE)/**---------------------------------------------------------------------------- Private Function Prototypes----------------------------------------------------------------------------**/void AdaptToChar(TBLOB *Blob, LINE_STATS *LineStats, CLASS_ID ClassId, FLOAT32 Threshold);void AdaptToPunc(TBLOB *Blob, LINE_STATS *LineStats, CLASS_ID ClassId, FLOAT32 Threshold);void AddNewResult(ADAPT_RESULTS *Results, CLASS_ID ClassId, FLOAT32 Rating, int ConfigId);void AmbigClassifier(TBLOB *Blob, LINE_STATS *LineStats, INT_TEMPLATES Templates, UNICHAR_ID *Ambiguities, ADAPT_RESULTS *Results);UNICHAR_ID *BaselineClassifier(TBLOB *Blob, LINE_STATS *LineStats, ADAPT_TEMPLATES Templates, ADAPT_RESULTS *Results);void make_config_pruner(INT_TEMPLATES templates, CONFIG_PRUNER *config_pruner);void CharNormClassifier(TBLOB *Blob, LINE_STATS *LineStats, INT_TEMPLATES Templates, ADAPT_RESULTS *Results);void ClassifyAsNoise(TBLOB *Blob, LINE_STATS *LineStats, ADAPT_RESULTS *Results);int CompareCurrentRatings(const void *arg1, const void *arg2);LIST ConvertMatchesToChoices(ADAPT_RESULTS *Results);void DebugAdaptiveClassifier(TBLOB *Blob, LINE_STATS *LineStats, ADAPT_RESULTS *Results);void DoAdaptiveMatch(TBLOB *Blob, LINE_STATS *LineStats, ADAPT_RESULTS *Results);void GetAdaptThresholds(TWERD * Word,LINE_STATS * LineStats,const WERD_CHOICE& BestChoice,const WERD_CHOICE& BestRawChoice, FLOAT32 Thresholds[]);UNICHAR_ID *GetAmbiguities(TBLOB *Blob, LINE_STATS *LineStats, CLASS_ID CorrectClass);int GetBaselineFeatures(TBLOB *Blob, LINE_STATS *LineStats, INT_TEMPLATES Templates, INT_FEATURE_ARRAY IntFeatures, CLASS_NORMALIZATION_ARRAY CharNormArray, inT32 *BlobLength);FLOAT32 GetBestRatingFor(TBLOB *Blob, LINE_STATS *LineStats, CLASS_ID ClassId);int GetCharNormFeatures(TBLOB *Blob, LINE_STATS *LineStats, INT_TEMPLATES Templates, INT_FEATURE_ARRAY IntFeatures, CLASS_NORMALIZATION_ARRAY CharNormArray, inT32 *BlobLength);int GetIntBaselineFeatures(TBLOB *Blob, LINE_STATS *LineStats, INT_TEMPLATES Templates, INT_FEATURE_ARRAY IntFeatures, CLASS_NORMALIZATION_ARRAY CharNormArray, inT32 *BlobLength);int GetIntCharNormFeatures(TBLOB *Blob, LINE_STATS *LineStats, INT_TEMPLATES Templates, INT_FEATURE_ARRAY IntFeatures, CLASS_NORMALIZATION_ARRAY CharNormArray, inT32 *BlobLength);void InitMatcherRatings(register FLOAT32 *Rating);int MakeNewTemporaryConfig(ADAPT_TEMPLATES Templates, CLASS_ID ClassId, int NumFeatures, INT_FEATURE_ARRAY Features, FEATURE_SET FloatFeatures);PROTO_ID MakeNewTempProtos(FEATURE_SET Features,int NumBadFeat,FEATURE_ID BadFeat[],INT_CLASS IClass,ADAPT_CLASS Class, BIT_VECTOR TempProtoMask);void MakePermanent(ADAPT_TEMPLATES Templates, CLASS_ID ClassId, int ConfigId, TBLOB *Blob, LINE_STATS *LineStats);int MakeTempProtoPerm(void *item1, void *item2);int NumBlobsIn(TWERD *Word);int NumOutlinesInBlob(TBLOB *Blob);void PrintAdaptiveMatchResults(FILE *File, ADAPT_RESULTS *Results);void RemoveBadMatches(ADAPT_RESULTS *Results);void RemoveExtraPuncs(ADAPT_RESULTS *Results);void SetAdaptiveThreshold(FLOAT32 Threshold);void ShowBestMatchFor(TBLOB *Blob, LINE_STATS *LineStats, CLASS_ID ClassId, BOOL8 AdaptiveOn, BOOL8 PreTrainedOn);/*#if defined(__STDC__) || defined(__cplusplus)# define _ARGS(s) s#else# define _ARGS(s) ()#endif*//* /users/danj/wiseowl/src/danj/microfeatures/adaptmatch.cint AdaptableWord _ARGS((TWERD *Word, char *BestChoice, char *BestRawChoice)); void AdaptToChar _ARGS((BLOB *Blob, LINE_STATS *LineStats, CLASS_ID ClassId, FLOAT32 Threshold)); void AdaptToPunc _ARGS((BLOB *Blob, LINE_STATS *LineStats, CLASS_ID ClassId, FLOAT32 Threshold)); void AddNewResult _ARGS((ADAPT_RESULTS *Results, CLASS_ID ClassId, FLOAT32 Rating, int ConfigId)); void AmbigClassifier _ARGS((BLOB *Blob, LINE_STATS *LineStats, INT_TEMPLATES Templates, char *Ambiguities, ADAPT_RESULTS *Results)); char *BaselineClassifier _ARGS((BLOB *Blob, LINE_STATS *LineStats, ADAPT_TEMPLATES Templates, ADAPT_RESULTS *Results)); void CharNormClassifier _ARGS((BLOB *Blob, LINE_STATS *LineStats, INT_TEMPLATES Templates, ADAPT_RESULTS *Results)); void ClassifyAsNoise _ARGS((BLOB *Blob, LINE_STATS *LineStats, ADAPT_RESULTS *Results)); int CompareCurrentRatings _ARGS((CLASS_ID *Class1, CLASS_ID *Class2)); LIST ConvertMatchesToChoices _ARGS((ADAPT_RESULTS *Results)); void DebugAdaptiveClassifier _ARGS((BLOB *Blob, LINE_STATS *LineStats, ADAPT_RESULTS *Results)); void DoAdaptiveMatch _ARGS((BLOB *Blob, LINE_STATS *LineStats, ADAPT_RESULTS *Results)); void GetAdaptThresholds _ARGS((TWERD *Word, LINE_STATS *LineStats, char *BestChoice, char *BestRawChoice, FLOAT32 Thresholds []));int GetAdaptiveFeatures _ARGS((BLOB *Blob, LINE_STATS *LineStats, INT_FEATURE_ARRAY IntFeatures, CHAR_DESC *FloatFeatures)); char *GetAmbiguities _ARGS((BLOB *Blob, LINE_STATS *LineStats, CLASS_ID CorrectClass)); int GetBaselineFeatures _ARGS((BLOB *Blob, LINE_STATS *LineStats, INT_TEMPLATES Templates, INT_FEATURE_ARRAY IntFeatures, CLASS_NORMALIZATION_ARRAY CharNormArray, FLOAT32 *BlobLength)); FLOAT32 GetBestRatingFor _ARGS((BLOB *Blob, LINE_STATS *LineStats, CLASS_ID ClassId)); int GetCharNormFeatures _ARGS((BLOB *Blob, LINE_STATS *LineStats, INT_TEMPLATES Templates, INT_FEATURE_ARRAY IntFeatures, CLASS_NORMALIZATION_ARRAY CharNormArray, FLOAT32 *BlobLength)); int GetIntBaselineFeatures _ARGS((BLOB *Blob, LINE_STATS *LineStats, INT_TEMPLATES Templates, INT_FEATURE_ARRAY IntFeatures, CLASS_NORMALIZATION_ARRAY CharNormArray, FLOAT32 *BlobLength)); int GetIntCharNormFeatures _ARGS((BLOB *Blob, LINE_STATS *LineStats, INT_TEMPLATES Templates, INT_FEATURE_ARRAY IntFeatures, CLASS_NORMALIZATION_ARRAY CharNormArray, FLOAT32 *BlobLength)); void InitMatcherRatings _ARGS((FLOAT32 *Rating));void MakeNewAdaptedClass _ARGS((BLOB *Blob, LINE_STATS *LineStats,
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?