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 | |
---|
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(); |
---|
31 | Description* Itemaux; |
---|
32 | |
---|
33 | /* Open data file */ |
---|
34 | |
---|
35 | strcpy(Fn, FileName); |
---|
36 | strcat(Fn, Extension); |
---|
37 | if (!(Df = fopen(Fn, "r"))) |
---|
38 | Error(0, Fn, ""); |
---|
39 | |
---|
40 | do { |
---|
41 | MaxItem = i; |
---|
42 | |
---|
43 | /* Make sure there is room for another item */ |
---|
44 | |
---|
45 | if (i >= ItemSpace) { |
---|
46 | if (ItemSpace) { |
---|
47 | ItemSpace += Inc; |
---|
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 | */ |
---|
59 | } else { |
---|
60 | Item = (Description *) malloc((ItemSpace = Inc) |
---|
61 | * sizeof(Description)); |
---|
62 | } |
---|
63 | } |
---|
64 | |
---|
65 | Item[i] = GetDescription(Df); |
---|
66 | |
---|
67 | } while (Item[i] != Nil && ++i); |
---|
68 | |
---|
69 | fclose(Df); |
---|
70 | MaxItem = i - 1; |
---|
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) |
---|
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(); |
---|
96 | |
---|
97 | if (ReadName(Df, name)) { |
---|
98 | Dvec = (Description) calloc(MaxAtt + 2, sizeof(AttValue)); |
---|
99 | |
---|
100 | ForEach(Att, 0, MaxAtt) { |
---|
101 | if (SpecialStatus[Att] == IGNORE) { |
---|
102 | /* Skip this value */ |
---|
103 | |
---|
104 | DVal(Dvec, Att) = 0; |
---|
105 | } else if (MaxAttVal[Att] || SpecialStatus[Att] == DISCRETE) { |
---|
106 | /* Discrete value */ |
---|
107 | |
---|
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 */ |
---|
115 | |
---|
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 | } |
---|
122 | |
---|
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; |
---|
141 | } |
---|
142 | |
---|
143 | ReadName(Df, name); |
---|
144 | } |
---|
145 | |
---|
146 | if ((Dv = Which(name, ClassName, 0, MaxClass)) < 0) { |
---|
147 | Error(5, "", name); |
---|
148 | Dv = 0; |
---|
149 | } |
---|
150 | Class(Dvec) = Dv; |
---|
151 | |
---|
152 | return Dvec; |
---|
153 | } else { |
---|
154 | return Nil; |
---|
155 | } |
---|
156 | } |
---|