#include "normmatch.h"
#include "clusttool.h"
#include "normfeat.h"
#include "debug.h"
#include "const.h"
#include "efio.h"
#include "emalloc.h"
#include "globals.h"
#include "scanutils.h"
#include <stdio.h>
#include <math.h>
Go to the source code of this file.
#define NORM_PROTO_FILE "tessdata/normproto" |
** (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.
Definition at line 36 of file normmatch.cpp.
Referenced by InitNormProtoVars().
Compares Features against each character normalization proto for ClassId and returns the match rating of the best match.
ClassId | Id of class to match against | |
Feature | Character normalization feature | |
DebugMatch | Controls dump of debug info |
Definition at line 96 of file normmatch.cpp.
References CharNormLength, CharNormRx, CharNormRy, CharNormY, cprintf(), FLOATUNION::Elliptical, first, iterate, MAX_FLOAT32, proto::Mean, NO_CLASS, NormEvidenceOf(), NormProtos, NORM_PROTOS::NumParams, ParamOf, PrintNormMatch(), NORM_PROTOS::Protos, proto::Variance, proto::Weight, WriteFeature(), and WriteNFloats().
Referenced by ComputeIntCharNormArray().
00096 { 00097 LIST Protos; 00098 FLOAT32 BestMatch; 00099 FLOAT32 Match; 00100 FLOAT32 Delta; 00101 PROTOTYPE *Proto; 00102 int ProtoId; 00103 00104 /* handle requests for classification as noise */ 00105 if (ClassId == NO_CLASS) { 00106 /* kludge - clean up constants and make into control knobs later */ 00107 Match = (ParamOf (Feature, CharNormLength) * 00108 ParamOf (Feature, CharNormLength) * 500.0 + 00109 ParamOf (Feature, CharNormRx) * 00110 ParamOf (Feature, CharNormRx) * 8000.0 + 00111 ParamOf (Feature, CharNormRy) * 00112 ParamOf (Feature, CharNormRy) * 8000.0); 00113 return (1.0 - NormEvidenceOf (Match)); 00114 } 00115 00116 BestMatch = MAX_FLOAT32; 00117 Protos = NormProtos->Protos[ClassId]; 00118 00119 if (DebugMatch) { 00120 cprintf ("\nFeature = "); 00121 WriteFeature(stdout, Feature); 00122 } 00123 00124 ProtoId = 0; 00125 iterate(Protos) { 00126 Proto = (PROTOTYPE *) first (Protos); 00127 Delta = ParamOf (Feature, CharNormY) - Proto->Mean[CharNormY]; 00128 Match = Delta * Delta * Proto->Weight.Elliptical[CharNormY]; 00129 Delta = ParamOf (Feature, CharNormRx) - Proto->Mean[CharNormRx]; 00130 Match += Delta * Delta * Proto->Weight.Elliptical[CharNormRx]; 00131 00132 if (Match < BestMatch) 00133 BestMatch = Match; 00134 00135 if (DebugMatch) { 00136 cprintf ("Proto %1d = ", ProtoId); 00137 WriteNFloats (stdout, NormProtos->NumParams, Proto->Mean); 00138 cprintf (" var = "); 00139 WriteNFloats (stdout, NormProtos->NumParams, 00140 Proto->Variance.Elliptical); 00141 cprintf (" match = "); 00142 PrintNormMatch (stdout, NormProtos->NumParams, Proto, Feature); 00143 } 00144 ProtoId++; 00145 } 00146 return (1.0 - NormEvidenceOf (BestMatch)); 00147 } /* ComputeNormMatch */
void FreeNormProtos | ( | ) |
Definition at line 175 of file normmatch.cpp.
References Efree(), FreeProtoList(), MAX_CLASS_ID, NormProtos, NULL, NORM_PROTOS::ParamDesc, and NORM_PROTOS::Protos.
Referenced by EndAdaptiveClassifier().
00175 { 00176 if (NormProtos != NULL) { 00177 for (int i = 0; i <= MAX_CLASS_ID; i++) 00178 FreeProtoList(&NormProtos->Protos[i]); 00179 Efree(NormProtos->ParamDesc); 00180 Efree(NormProtos); 00181 NormProtos = NULL; 00182 } 00183 }
void GetNormProtos | ( | ) |
Reads in a set of character normalization protos from NormProtoFile and places them into NormProtos.
none |
Definition at line 163 of file normmatch.cpp.
References demodir, Efopen(), NormProtoFile, NormProtos, and ReadNormProtos().
Referenced by InitAdaptiveClassifier().
00163 { 00164 FILE *File; 00165 char name[1024]; 00166 00167 strcpy(name, demodir); 00168 strcat(name, NormProtoFile); 00169 File = Efopen (name, "r"); 00170 NormProtos = ReadNormProtos (File); 00171 fclose(File); 00172 00173 } /* GetNormProtos */
void InitNormProtoVars | ( | ) |
Initialize the control variables for the normalization matcher.
none |
Definition at line 195 of file normmatch.cpp.
References dummy, NORM_PROTO_FILE, NormProtoFile, and string_variable.
Referenced by init_dj_debug().
00195 { 00196 VALUE dummy; 00197 00198 string_variable (NormProtoFile, "NormProtoFile", NORM_PROTO_FILE); 00199 00200 MakeNormAdjMidpoint(); 00201 MakeNormAdjCurl(); 00202 00203 } /* InitNormProtoVars */
Return the new type of evidence number corresponding to a normalization adjustment.
The equation that represents the transform is:
1 / (1 + (NormAdj / midpoint) ^ curl)
Definition at line 218 of file normmatch.cpp.
Referenced by ComputeNormMatch(), and PrintNormMatch().
00218 { 00219 NormAdj /= NormAdjMidpoint; 00220 00221 if (NormAdjCurl == 3) 00222 NormAdj = NormAdj * NormAdj * NormAdj; 00223 else if (NormAdjCurl == 2) 00224 NormAdj = NormAdj * NormAdj; 00225 else 00226 NormAdj = pow (NormAdj, NormAdjCurl); 00227 return (1.0 / (1.0 + NormAdj)); 00228 }
Dumps out detailed normalization match info.
File | Open text file to dump match debug info to | |
NumParams | Number of parameters in proto and feature | |
Proto | Array of prototype parameters | |
Feature | Array of feature parameters |
Definition at line 243 of file normmatch.cpp.
References CharNormRx, CharNormY, Mean(), NormEvidenceOf(), ParamOf, and StandardDeviation().
Referenced by ComputeNormMatch().
00246 { 00247 int i; 00248 FLOAT32 ParamMatch; 00249 FLOAT32 TotalMatch; 00250 00251 for (i = 0, TotalMatch = 0.0; i < NumParams; i++) { 00252 ParamMatch = ((ParamOf (Feature, i) - Mean (Proto, i)) / 00253 StandardDeviation (Proto, i)); 00254 00255 fprintf (File, " %6.1f", ParamMatch); 00256 00257 if (i == CharNormY || i == CharNormRx) 00258 TotalMatch += ParamMatch * ParamMatch; 00259 } 00260 fprintf (File, " --> %6.1f (%4.2f)\n", 00261 TotalMatch, NormEvidenceOf (TotalMatch)); 00262 00263 } /* PrintNormMatch */
NORM_PROTOS * ReadNormProtos | ( | FILE * | File | ) |
Allocates a new data structure to hold a set of character normalization protos & fills in the data structure by reading from the specified File.
File | Open text file to read normalization protos from |
Definition at line 277 of file normmatch.cpp.
References Emalloc(), fscanf(), MAX_CLASS_ID, NIL, NormProtos, NORM_PROTOS::NumParams, NORM_PROTOS::ParamDesc, NORM_PROTOS::Protos, push_last(), ReadParamDesc(), ReadPrototype(), and ReadSampleSize().
Referenced by GetNormProtos().
00277 { 00278 NORM_PROTOS *NormProtos; 00279 int i; 00280 char ClassId[2]; 00281 LIST Protos; 00282 int NumProtos; 00283 00284 /* allocate and initialization data structure */ 00285 NormProtos = (NORM_PROTOS *) Emalloc (sizeof (NORM_PROTOS)); 00286 for (i = 0; i <= MAX_CLASS_ID; i++) 00287 NormProtos->Protos[i] = NIL; 00288 00289 /* read file header and save in data structure */ 00290 NormProtos->NumParams = ReadSampleSize (File); 00291 NormProtos->ParamDesc = ReadParamDesc (File, NormProtos->NumParams); 00292 00293 /* read protos for each class into a separate list */ 00294 while (fscanf (File, "%1s %d", ClassId, &NumProtos) == 2) { 00295 Protos = NormProtos->Protos[ClassId[0]]; 00296 for (i = 0; i < NumProtos; i++) 00297 Protos = 00298 push_last (Protos, ReadPrototype (File, NormProtos->NumParams)); 00299 NormProtos->Protos[ClassId[0]] = Protos; 00300 } 00301 00302 return (NormProtos); 00303 00304 } /* ReadNormProtos */
const char* NormProtoFile = NORM_PROTO_FILE [static] |
Name of file containing char normalization protos
Definition at line 68 of file normmatch.cpp.
Referenced by GetNormProtos(), and InitNormProtoVars().
NORM_PROTOS* NormProtos [static] |
Global data structure to hold char normalization protos
Definition at line 65 of file normmatch.cpp.
Referenced by ComputeNormMatch(), FreeNormProtos(), GetNormProtos(), and ReadNormProtos().