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