[26] | 1 | /*************************************************************************/ |
---|
| 2 | /* */ |
---|
| 3 | /* Get case descriptions from data file */ |
---|
| 4 | /* -------------------------------------- */ |
---|
| 5 | /* */ |
---|
| 6 | /*************************************************************************/ |
---|
| 7 | |
---|
| 8 | #include "defns.i" |
---|
| 9 | #include "types.i" |
---|
| 10 | #include "extern.i" |
---|
| 11 | |
---|
| 12 | #define Inc 2048 |
---|
| 13 | |
---|
| 14 | /*************************************************************************/ |
---|
| 15 | /* */ |
---|
| 16 | /* Read raw case descriptions from file with given extension. */ |
---|
| 17 | /* */ |
---|
| 18 | /* On completion, cases are stored in array Item in the form */ |
---|
| 19 | /* of Descriptions (i.e. arrays of attribute values), and */ |
---|
| 20 | /* MaxItem is set to the number of data items. */ |
---|
| 21 | /* */ |
---|
| 22 | /*************************************************************************/ |
---|
| 23 | |
---|
[32] | 24 | GetData(Extension) |
---|
| 25 | /* -------- */ |
---|
| 26 | String Extension; { |
---|
| 27 | FILE *Df, *fopen(); |
---|
| 28 | char Fn[100]; |
---|
| 29 | ItemNo i = 0, j, ItemSpace = 0; |
---|
| 30 | Description GetDescription(); |
---|
[26] | 31 | |
---|
[32] | 32 | /* Open data file */ |
---|
[26] | 33 | |
---|
[32] | 34 | strcpy(Fn, FileName); |
---|
| 35 | strcat(Fn, Extension); |
---|
| 36 | if (!(Df = fopen(Fn, "r"))) |
---|
| 37 | Error(0, Fn, ""); |
---|
[26] | 38 | |
---|
[32] | 39 | do { |
---|
| 40 | MaxItem = i; |
---|
[26] | 41 | |
---|
[32] | 42 | /* Make sure there is room for another item */ |
---|
[26] | 43 | |
---|
[32] | 44 | if (i >= ItemSpace) { |
---|
| 45 | if (ItemSpace) { |
---|
| 46 | ItemSpace += Inc; |
---|
| 47 | Item = (Description *) realloc(Item, ItemSpace |
---|
| 48 | * sizeof(Description)); |
---|
| 49 | } else { |
---|
| 50 | Item = (Description *) malloc((ItemSpace = Inc) |
---|
| 51 | * sizeof(Description)); |
---|
| 52 | } |
---|
| 53 | } |
---|
[26] | 54 | |
---|
[32] | 55 | Item[i] = GetDescription(Df); |
---|
[26] | 56 | |
---|
[32] | 57 | } while (Item[i] != Nil && ++i); |
---|
[26] | 58 | |
---|
[32] | 59 | fclose(Df); |
---|
| 60 | MaxItem = i - 1; |
---|
[26] | 61 | } |
---|
| 62 | |
---|
| 63 | /*************************************************************************/ |
---|
| 64 | /* */ |
---|
| 65 | /* Read a raw case description from file Df. */ |
---|
| 66 | /* */ |
---|
| 67 | /* For each attribute, read the attribute value from the file. */ |
---|
| 68 | /* If it is a discrete valued attribute, find the associated no. */ |
---|
| 69 | /* of this attribute value (if the value is unknown this is 0). */ |
---|
| 70 | /* */ |
---|
| 71 | /* Returns the Description of the case (i.e. the array of */ |
---|
| 72 | /* attribute values). */ |
---|
| 73 | /* */ |
---|
| 74 | /*************************************************************************/ |
---|
| 75 | |
---|
| 76 | Description GetDescription(Df) |
---|
[32] | 77 | /* --------------- */ |
---|
| 78 | FILE *Df; { |
---|
| 79 | Attribute Att; |
---|
| 80 | char name[500], *endname, *CopyString(); |
---|
| 81 | Boolean ReadName(); |
---|
| 82 | int Dv; |
---|
| 83 | float Cv; |
---|
| 84 | Description Dvec; |
---|
| 85 | double strtod(); |
---|
[26] | 86 | |
---|
[32] | 87 | if (ReadName(Df, name)) { |
---|
| 88 | Dvec = (Description) calloc(MaxAtt + 2, sizeof(AttValue)); |
---|
[26] | 89 | |
---|
[32] | 90 | ForEach(Att, 0, MaxAtt) { |
---|
| 91 | if (SpecialStatus[Att] == IGNORE) { |
---|
| 92 | /* Skip this value */ |
---|
[26] | 93 | |
---|
[32] | 94 | DVal(Dvec, Att) = 0; |
---|
| 95 | } else if (MaxAttVal[Att] || SpecialStatus[Att] == DISCRETE) { |
---|
| 96 | /* Discrete value */ |
---|
[26] | 97 | |
---|
[32] | 98 | if (!(strcmp(name, "?"))) { |
---|
| 99 | Dv = 0; |
---|
| 100 | } else { |
---|
| 101 | Dv = Which(name, AttValName[Att], 1, MaxAttVal[Att]); |
---|
| 102 | if (!Dv) { |
---|
| 103 | if (SpecialStatus[Att] == DISCRETE) { |
---|
| 104 | /* Add value to list */ |
---|
[26] | 105 | |
---|
[32] | 106 | Dv = ++MaxAttVal[Att]; |
---|
| 107 | if (Dv > (int) AttValName[Att][0]) { |
---|
| 108 | printf("\nToo many values for %s (max %d)\n", |
---|
| 109 | AttName[Att], (int) AttValName[Att][0]); |
---|
| 110 | exit(1); |
---|
| 111 | } |
---|
[26] | 112 | |
---|
[32] | 113 | AttValName[Att][Dv] = CopyString(name); |
---|
| 114 | } else { |
---|
| 115 | Error(4, AttName[Att], name); |
---|
| 116 | } |
---|
| 117 | } |
---|
| 118 | } |
---|
| 119 | DVal(Dvec, Att) = Dv; |
---|
| 120 | } else { |
---|
| 121 | /* Continuous value */ |
---|
| 122 | |
---|
| 123 | if (!(strcmp(name, "?"))) { |
---|
| 124 | Cv = Unknown; |
---|
| 125 | } else { |
---|
| 126 | Cv = strtod(name, &endname); |
---|
| 127 | if (endname == name || *endname != '\0') |
---|
| 128 | Error(4, AttName[Att], name); |
---|
| 129 | } |
---|
| 130 | CVal(Dvec, Att) = Cv; |
---|
[26] | 131 | } |
---|
| 132 | |
---|
[32] | 133 | ReadName(Df, name); |
---|
[26] | 134 | } |
---|
[32] | 135 | |
---|
| 136 | if ((Dv = Which(name, ClassName, 0, MaxClass)) < 0) { |
---|
| 137 | Error(5, "", name); |
---|
| 138 | Dv = 0; |
---|
[26] | 139 | } |
---|
[32] | 140 | Class(Dvec) = Dv; |
---|
[26] | 141 | |
---|
[32] | 142 | return Dvec; |
---|
| 143 | } else { |
---|
| 144 | return Nil; |
---|
| 145 | } |
---|
[26] | 146 | } |
---|