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 | |
---|
32 | /* Open data file */ |
---|
33 | |
---|
34 | strcpy(Fn, FileName); |
---|
35 | strcat(Fn, Extension); |
---|
36 | if (!(Df = fopen(Fn, "r"))) |
---|
37 | Error(0, Fn, ""); |
---|
38 | |
---|
39 | do { |
---|
40 | MaxItem = i; |
---|
41 | |
---|
42 | /* Make sure there is room for another item */ |
---|
43 | |
---|
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 | } |
---|
54 | |
---|
55 | Item[i] = GetDescription(Df); |
---|
56 | |
---|
57 | } while (Item[i] != Nil && ++i); |
---|
58 | |
---|
59 | fclose(Df); |
---|
60 | MaxItem = i - 1; |
---|
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) |
---|
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(); |
---|
86 | |
---|
87 | if (ReadName(Df, name)) { |
---|
88 | Dvec = (Description) calloc(MaxAtt + 2, sizeof(AttValue)); |
---|
89 | |
---|
90 | ForEach(Att, 0, MaxAtt) { |
---|
91 | if (SpecialStatus[Att] == IGNORE) { |
---|
92 | /* Skip this value */ |
---|
93 | |
---|
94 | DVal(Dvec, Att) = 0; |
---|
95 | } else if (MaxAttVal[Att] || SpecialStatus[Att] == DISCRETE) { |
---|
96 | /* Discrete value */ |
---|
97 | |
---|
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 */ |
---|
105 | |
---|
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 | } |
---|
112 | |
---|
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; |
---|
131 | } |
---|
132 | |
---|
133 | ReadName(Df, name); |
---|
134 | } |
---|
135 | |
---|
136 | if ((Dv = Which(name, ClassName, 0, MaxClass)) < 0) { |
---|
137 | Error(5, "", name); |
---|
138 | Dv = 0; |
---|
139 | } |
---|
140 | Class(Dvec) = Dv; |
---|
141 | |
---|
142 | return Dvec; |
---|
143 | } else { |
---|
144 | return Nil; |
---|
145 | } |
---|
146 | } |
---|