📄 adaptmatch.cpp
字号:
/****************************************************************************** ** 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 "tessdata/inttemp"#define BUILT_IN_CUTOFFS_FILE "tessdata/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{ FLOAT32 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;}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, char *Ambiguities, ADAPT_RESULTS *Results);char *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); //CLASS_ID *Class1,int CompareCurrentRatings(const void *arg1, const void *arg2); //CLASS_ID *Class2);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 char *BestChoice,const char *BestRawChoice, FLOAT32 Thresholds[]);char *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, FLOAT32 *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, FLOAT32 *BlobLength);int GetIntBaselineFeatures(TBLOB *Blob, LINE_STATS *LineStats, INT_TEMPLATES Templates, INT_FEATURE_ARRAY IntFeatures, CLASS_NORMALIZATION_ARRAY CharNormArray, FLOAT32 *BlobLength);int GetIntCharNormFeatures(TBLOB *Blob, LINE_STATS *LineStats, INT_TEMPLATES Templates, INT_FEATURE_ARRAY IntFeatures, CLASS_NORMALIZATION_ARRAY CharNormArray, FLOAT32 *BlobLength);void InitMatcherRatings(register FLOAT32 *Rating);void 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, CLASS_ID ClassId, ADAPT_TEMPLATES Templates)); void MakeNewTemporaryConfig _ARGS((ADAPT_TEMPLATES Templates, CLASS_ID ClassId, int NumFeatures, INT_FEATURE_ARRAY Features, FEATURE_SET FloatFeatures)); PROTO_ID MakeNewTempProtos _ARGS((FEATURE_SET Features, int NumBadFeat, FEATURE_ID BadFeat [], INT_CLASS IClass, ADAPT_CLASS Class, BIT_VECTOR TempProtoMask)); void MakePermanent _ARGS((ADAPT_TEMPLATES Templates, CLASS_ID ClassId, int ConfigId, BLOB *Blob, LINE_STATS *LineStats)); int MakeTempProtoPerm _ARGS((TEMP_PROTO TempProto, PROTO_KEY *ProtoKey)); int NumBlobsIn _ARGS((TWERD *Word)); int NumOutlinesInBlob _ARGS((BLOB *Blob)); void PrintAdaptiveMatchResults _ARGS((FILE *File, ADAPT_RESULTS *Results)); void RemoveBadMatches
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -