[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(); |
---|
[107] | 31 | Description* Itemaux; |
---|
[26] | 32 | |
---|
[32] | 33 | /* Open data file */ |
---|
[26] | 34 | |
---|
[32] | 35 | strcpy(Fn, FileName); |
---|
| 36 | strcat(Fn, Extension); |
---|
| 37 | if (!(Df = fopen(Fn, "r"))) |
---|
| 38 | Error(0, Fn, ""); |
---|
[26] | 39 | |
---|
[32] | 40 | do { |
---|
| 41 | MaxItem = i; |
---|
[26] | 42 | |
---|
[32] | 43 | /* Make sure there is room for another item */ |
---|
[26] | 44 | |
---|
[32] | 45 | if (i >= ItemSpace) { |
---|
| 46 | if (ItemSpace) { |
---|
| 47 | ItemSpace += Inc; |
---|
[107] | 48 | |
---|
| 49 | Itemaux = (Description *) malloc(ItemSpace * sizeof(Description)); |
---|
| 50 | for(j = 0; j < MaxItem; j++){ |
---|
| 51 | Itemaux[j] = Item[j]; |
---|
| 52 | } |
---|
| 53 | free(Item); |
---|
| 54 | Item = Itemaux; |
---|
| 55 | |
---|
| 56 | /* |
---|
| 57 | Item = (Description *) realloc(Item, ItemSpace * sizeof(Description)); |
---|
| 58 | */ |
---|
[32] | 59 | } else { |
---|
| 60 | Item = (Description *) malloc((ItemSpace = Inc) |
---|
| 61 | * sizeof(Description)); |
---|
| 62 | } |
---|
| 63 | } |
---|
[26] | 64 | |
---|
[32] | 65 | Item[i] = GetDescription(Df); |
---|
[26] | 66 | |
---|
[32] | 67 | } while (Item[i] != Nil && ++i); |
---|
[26] | 68 | |
---|
[32] | 69 | fclose(Df); |
---|
| 70 | MaxItem = i - 1; |
---|
[26] | 71 | } |
---|
| 72 | |
---|
| 73 | /*************************************************************************/ |
---|
| 74 | /* */ |
---|
| 75 | /* Read a raw case description from file Df. */ |
---|
| 76 | /* */ |
---|
| 77 | /* For each attribute, read the attribute value from the file. */ |
---|
| 78 | /* If it is a discrete valued attribute, find the associated no. */ |
---|
| 79 | /* of this attribute value (if the value is unknown this is 0). */ |
---|
| 80 | /* */ |
---|
| 81 | /* Returns the Description of the case (i.e. the array of */ |
---|
| 82 | /* attribute values). */ |
---|
| 83 | /* */ |
---|
| 84 | /*************************************************************************/ |
---|
| 85 | |
---|
| 86 | Description GetDescription(Df) |
---|
[32] | 87 | /* --------------- */ |
---|
| 88 | FILE *Df; { |
---|
| 89 | Attribute Att; |
---|
| 90 | char name[500], *endname, *CopyString(); |
---|
| 91 | Boolean ReadName(); |
---|
| 92 | int Dv; |
---|
| 93 | float Cv; |
---|
| 94 | Description Dvec; |
---|
| 95 | double strtod(); |
---|
[26] | 96 | |
---|
[32] | 97 | if (ReadName(Df, name)) { |
---|
| 98 | Dvec = (Description) calloc(MaxAtt + 2, sizeof(AttValue)); |
---|
[26] | 99 | |
---|
[32] | 100 | ForEach(Att, 0, MaxAtt) { |
---|
| 101 | if (SpecialStatus[Att] == IGNORE) { |
---|
| 102 | /* Skip this value */ |
---|
[26] | 103 | |
---|
[32] | 104 | DVal(Dvec, Att) = 0; |
---|
| 105 | } else if (MaxAttVal[Att] || SpecialStatus[Att] == DISCRETE) { |
---|
| 106 | /* Discrete value */ |
---|
[26] | 107 | |
---|
[32] | 108 | if (!(strcmp(name, "?"))) { |
---|
| 109 | Dv = 0; |
---|
| 110 | } else { |
---|
| 111 | Dv = Which(name, AttValName[Att], 1, MaxAttVal[Att]); |
---|
| 112 | if (!Dv) { |
---|
| 113 | if (SpecialStatus[Att] == DISCRETE) { |
---|
| 114 | /* Add value to list */ |
---|
[26] | 115 | |
---|
[32] | 116 | Dv = ++MaxAttVal[Att]; |
---|
| 117 | if (Dv > (int) AttValName[Att][0]) { |
---|
| 118 | printf("\nToo many values for %s (max %d)\n", |
---|
| 119 | AttName[Att], (int) AttValName[Att][0]); |
---|
| 120 | exit(1); |
---|
| 121 | } |
---|
[26] | 122 | |
---|
[32] | 123 | AttValName[Att][Dv] = CopyString(name); |
---|
| 124 | } else { |
---|
| 125 | Error(4, AttName[Att], name); |
---|
| 126 | } |
---|
| 127 | } |
---|
| 128 | } |
---|
| 129 | DVal(Dvec, Att) = Dv; |
---|
| 130 | } else { |
---|
| 131 | /* Continuous value */ |
---|
| 132 | |
---|
| 133 | if (!(strcmp(name, "?"))) { |
---|
| 134 | Cv = Unknown; |
---|
| 135 | } else { |
---|
| 136 | Cv = strtod(name, &endname); |
---|
| 137 | if (endname == name || *endname != '\0') |
---|
| 138 | Error(4, AttName[Att], name); |
---|
| 139 | } |
---|
| 140 | CVal(Dvec, Att) = Cv; |
---|
[26] | 141 | } |
---|
| 142 | |
---|
[32] | 143 | ReadName(Df, name); |
---|
[26] | 144 | } |
---|
[32] | 145 | |
---|
| 146 | if ((Dv = Which(name, ClassName, 0, MaxClass)) < 0) { |
---|
| 147 | Error(5, "", name); |
---|
| 148 | Dv = 0; |
---|
[26] | 149 | } |
---|
[32] | 150 | Class(Dvec) = Dv; |
---|
[26] | 151 | |
---|
[32] | 152 | return Dvec; |
---|
| 153 | } else { |
---|
| 154 | return Nil; |
---|
| 155 | } |
---|
[26] | 156 | } |
---|