📄 hooks.c
字号:
} } else if ( DateVal(Att) ) { Cv = DateToDay(Name); if ( Cv < 1 ) { XError(BADDATE, AttName[Att], Name); Cv = UNKNOWN; } } else if ( TimeVal(Att) ) { Cv = TimeToSecs(Name); if ( Cv < 0 ) { XError(BADTIME, AttName[Att], Name); Cv = UNKNOWN; } } else { Cv = strtod(Name, &EndVal); if ( EndVal == Name || *EndVal != '\0' ) { XError(BADATTVAL, AttName[Att], Name); Cv = UNKNOWN; } } CVal(DVec, Att) = Cv; CheckValue(DVec, Att); } } Class(DVec) = CVal(DVec, ClassAtt); return DVec; } else { return Nil; }}/*************************************************************************//* *//* Store a label or ignored value in IValStore *//* *//*************************************************************************/int StoreIVal(String S)/* --------- */{ int StartIx, Length; if ( (Length=strlen(S) + 1) + IValsOffset > IValsSize ) { if ( IgnoredVals ) { Realloc(IgnoredVals, IValsSize += 32768, char); } else { IValsSize = 32768; IValsOffset = 0; IgnoredVals = Alloc(IValsSize, char); } } StartIx = IValsOffset; strcpy(IgnoredVals + StartIx, S); IValsOffset += Length; return StartIx;}/*************************************************************************//* *//* Check for bad continuous value *//* *//*************************************************************************/void CheckValue(Description DVec, Attribute Att)/* ---------- */{ ContValue Cv; Cv = CVal(DVec, Att); if ( ! finite(Cv) ) { Error(BADNUMBER, AttName[Att], ""); CVal(DVec, Att) = UNKNOWN; DVal(DVec, Att) = 0; }}/*************************************************************************//* *//* Free case description space *//* *//*************************************************************************/void FreeCases(Description *Case, ItemNo MaxCase)/* -------- */{ ItemNo i; /* Release any strings holding ignored attribute values */ ForEach(i, 0, MaxCase) { FreeCase(Case[i]); } free(Case);}void FreeCase(Description DVec)/* -------- */{ free(DVec); IValsOffset = 0;}/*************************************************************************//* *//* Routines for reading model files *//* -------------------------------- *//* *//*************************************************************************/Boolean BINARY=false;int Entry;char* Prop[]={"null", "id", "att", "elts", "prec", "globalmean", "floor", "ceiling", "sample", "init", "mean", "sd", "mode", "entries", "rules", "cover", "loval", "hival", "extrap", "insts", "nn", "maxd", "esterr", "conds", "type", "cut", "result", "val", "coeff" };char PropName[20], *PropVal=Nil, *Unquoted;int PropValSize=0;#define PROPS 28#define IDP 1#define ATTP 2#define ELTSP 3#define PRECP 4#define GLOBALMEANP 5#define FLOORP 6#define CEILINGP 7#define SAMPLEP 8#define INITP 9#define MEANP 10#define SDP 11#define MODEP 12#define ENTRIESP 13#define RULESP 14#define COVERP 15#define LOVALP 16#define HIVALP 17#define EXTRAPP 18#define INSTSP 19#define NNP 20#define MAXDP 21#define ESTERRP 22#define CONDSP 23#define TYPEP 24#define CUTP 25#define RESULTP 26#define VALP 27#define COEFFP 28/*************************************************************************//* *//* Check whether file is open. If it is not, open it and *//* read/write sampling information and discrete names *//* *//*************************************************************************/void CheckFile(String Extension, Boolean Write)/* --------- */{ static char *LastExt=""; if ( ! Mf || strcmp(LastExt, Extension) ) { LastExt = Extension; if ( Mf ) { fclose(Mf); Mf = Nil; } ReadFilePrefix(Extension); }}/*************************************************************************//* *//* Read header information and decide whether model files are *//* in ASCII or binary format *//* *//*************************************************************************/void ReadFilePrefix(String Extension)/* -------------- */{ int Head;#if defined WIN32 || defined _CONSOLE if ( ! (Mf = GetFile(Extension, "rb")) ) Error(NOFILE, Fn, "");#else if ( ! (Mf = GetFile(Extension, "r")) ) Error(NOFILE, Fn, "");#endif /* Read first integer to find model file type */ StreamIn((char *) &Head, sizeof(int)); rewind(Mf); if ( memcmp((char *) &Head, "id=", 3) != 0 ) { BINARY = true; MEMBERS = 1; BinRecoverDiscreteNames(); } else { BINARY = false; ReadHeader(); }}/*************************************************************************//* *//* Read the header information (id, saved names, models) *//* *//*************************************************************************/void ReadHeader()/* --------- */{ Attribute Att; DiscrValue v; char *p, Dummy; double Xd; int Year, Month, Day; /* First allocate storage for various globals */ AttMean = Alloc(MaxAtt+1, ContValue); AttSD = Alloc(MaxAtt+1, ContValue); Modal = Alloc(MaxAtt+1, DiscrValue); while ( true ) { switch ( ReadProp(&Dummy) ) { case IDP: /* Recover year run and set base date for timestamps */ if ( sscanf(PropVal + strlen(PropVal) - 11, "%d-%d-%d\"", &Year, &Month, &Day) == 3 ) { SetTSBase(Year); } break; case ATTP: Unquoted = RemoveQuotes(PropVal); Att = Which(Unquoted, AttName, 1, MaxAtt); if ( ! Att || Exclude(Att) ) { Error(MODELFILE, E_MFATT, Unquoted); } break; case ELTSP: MaxAttVal[Att] = 1; AttValName[Att][1] = strdup("N/A"); for ( p = PropVal ; *p ; ) { p = RemoveQuotes(p); v = ++MaxAttVal[Att]; AttValName[Att][v] = Alloc(strlen(p)+1, char); strcpy(AttValName[Att][v], p); for ( p += strlen(p) ; *p != '"' ; p++ ) ; p++; if ( *p == ',' ) p++; } AttValName[Att][MaxAttVal[Att]+1] = "<other>"; MaxDiscrVal = Max(MaxDiscrVal, MaxAttVal[Att]+1); break; case PRECP: sscanf(PropVal, "\"%d\"", &Precision); break; case GLOBALMEANP: sscanf(PropVal, "\"%f\"", &GlobalMean); break; case EXTRAPP: sscanf(PropVal, "\"%f\"", &EXTRAP); break; case INSTSP: USEINSTANCES = PropVal[1] - '0'; if ( USEINSTANCES ) { /* Set legacy values */ NN = 5; MAXD = 50; } break; case NNP: sscanf(PropVal, "\"%d\"", &NN); break; case MAXDP: sscanf(PropVal, "\"%f\"", &MAXD); break; case CEILINGP: sscanf(PropVal, "\"%lf\"", &Xd); Ceiling = Xd; break; case FLOORP: sscanf(PropVal, "\"%lf\"", &Xd); Floor = Xd; break; case MEANP: sscanf(PropVal, "\"%lf\"", &Xd); AttMean[Att] = Xd; break; case SDP: sscanf(PropVal, "\"%lf\"", &Xd); AttSD[Att] = Xd; break; case MODEP: Unquoted = RemoveQuotes(PropVal); Modal[Att] = Which(Unquoted, AttValName[Att], 1, MaxAttVal[Att]); if ( ! Modal[Att] ) { /* An unknown modal value is an error! */ Error(MODELFILE, E_MFATTVAL, Unquoted); } else if ( Modal[Att] == 1 ) { /* This means that all training cases had value N/A. For consistency with instance distances, this attribute is ignored */ SpecialStatus[Att] |= SKIP; } break; case SAMPLEP: sscanf(PropVal, "\"%f\"", &SAMPLE); break; case INITP: sscanf(PropVal, "\"%d\"", &KRInit); break; case ENTRIESP: sscanf(PropVal, "\"%d\"", &MEMBERS); Entry = 0; return; } }}/*************************************************************************//* *//* Retrieve ruleset with extension Extension *//* (Separate functions for ruleset, single rule, single condition) *//* *//*************************************************************************/RRuleSet *GetCommittee(String Extension)/* ------------- */{ RRuleSet *Cttee; int m; ErrMsgs = 0; CheckFile(Extension, false); if ( ErrMsgs ) { if ( Mf ) { fclose(Mf); Mf = Nil; } return Nil; } Cttee = Alloc(MEMBERS, RRuleSet); if ( BINARY ) { Cttee[0] = BinInRules(); } else { ForEach(m, 0, MEMBERS-1) { Cttee[m] = InRules(); } } fclose(Mf); Mf = Nil; return ( ErrMsgs ? Nil : Cttee );}RRuleSet InRules()/* ------- */{ RRuleSet RS; RuleNo r; char Delim; RS = Alloc(1, RuleSetRec); do { switch ( ReadProp(&Delim) ) { case RULESP: sscanf(PropVal, "\"%d\"", &RS->SNRules); break; } } while ( Delim == ' ' ); /* Read each rule */ RS->SRule = Alloc(RS->SNRules+1, CRule); ForEach(r, 1, RS->SNRules) { RS->SRule[r] = InRule(); RS->SRule[r]->RNo = r; RS->SRule[r]->MNo = Entry; } Entry++; return RS;}CRule InRule()/* ------ */{ CRule R; int d; char Delim; Attribute Att=0; float V, Range; R = Alloc(1, RuleRec); /* General rule information */ do { switch ( ReadProp(&Delim) ) { case CONDSP: sscanf(PropVal, "\"%d\"", &R->Size); break; case COVERP: sscanf(PropVal, "\"%d\"", &R->Cover); break; case MEANP: sscanf(PropVal, "\"%f\"", &R->Mean); break; case LOVALP: sscanf(PropVal, "\"%f\"", &R->LoVal); break; case HIVALP: sscanf(PropVal, "\"%f\"", &R->HiVal); break; case ESTERRP: sscanf(PropVal, "\"%f\"", &R->EstErr); break; } } while ( Delim == ' ' ); Range = R->HiVal - R->LoVal; R->LoLim = ( (V = R->LoVal - EXTRAP * Range) < 0 && R->LoVal >= 0 ? 0 : V ); R->HiLim = ( (V = R->HiVal + EXTRAP * Range) > 0 && R->HiVal <= 0 ? 0 : V ); /* Conditions making up rule's left-hand side */ R->Lhs = Alloc(R->Size+1, Condition); ForEach(d, 1, R->Size) { R->Lhs[d] = InCondition(); } /* Linear model associated with rule */ R->Rhs = AllocZero(MaxAtt+1, double); do { switch ( ReadProp(&Delim) ) { case ATTP: Unquoted = RemoveQuotes(PropVal); Att = Which(Unquoted, AttName, 1, MaxAtt); if ( ! Att || Exclude(Att) ) { Error(MODELFILE, E_MFATT, Unquoted); } break; case COEFFP: sscanf(PropVal, "\"%lf\"", &R->Rhs[Att]); break; } } while ( Delim == ' ' ); return R;}Condition InCondition()/* ----------- */{ Condition C; char Delim; int X; double XD; C = Alloc(1, CondRec); do { switch ( ReadProp(&Delim) ) { case TYPEP: sscanf(PropVal, "\"%d\"", &X); C->NodeType = X; break; case ATTP: Unquoted = RemoveQuotes(PropVal); C->Tested = Which(Unquoted, AttName, 1, MaxAtt); if ( ! C->Tested || Exclude(C->Tested) ) { Error(MODELFILE, E_MFATT, Unquoted); } break; case CUTP: sscanf(PropVal, "\"%lf\"", &XD); C->Cut = XD; break; case RESULTP: C->TestValue = ( PropVal[1] == '<' ? 2 : 3 ); break; case VALP: if ( Continuous(C->Tested) ) { C->TestValue = 1; } else { Unquoted = RemoveQuotes(PropVal); C->TestValue = Which(Unquoted, AttValName[C->Tested], 1, MaxAttVal[C->Tested]); if ( ! C->TestValue ) { Error(MODELFILE, E_MFATTVAL, Unquoted); } } break;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -