Repository navigation
Expand file tree
/
Copy pathInputProcessor.java
More file actions
768 lines (464 loc) · 17 KB
/
Copy pathInputProcessor.java
File metadata and controls
768 lines (464 loc) · 17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
/**
* <code>InputProcessor</code> provides the key functionality to process the
* input data files and populate the Node structures.
*
* @author pandit
*
* @version 1.0
*
* Revision Log
*
* Date Version Description
******************************************************************************
* 17th Nov 1.0 First cut at InputProcessor - reading training set
*
* 18th Nov 1.1 Added functionality to read the names file separately
*
* 25th Nov 1.2 Added the code to discretize the linear / nominal data
******************************************************************************
*/
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.Vector;
public class InputProcessor {
private String [] attributeNames;
private Vector [] domains;
private TreeNode trainingRoot = new TreeNode ();
private TreeNode testingRoot = new TreeNode ();
private int numAttributes;
private int skipCount = -1;
private double [] minLinearArray;
private double [] maxLinearArray;
public static final int MAX_POSS = +9999;
public static final int MIN_POSS = -9999;
/*
* The number of discrete classes for linear and nominal data
*/
public static final int NUM_OF_CLASSES = 4;
public static final String TINY = "TINY";
public static final String SMALL = "SMALL";
public static final String MEDIUM = "MEDIUM";
public static final String LARGE = "LARGE";
public static final String HUGE = "HUGE";
/*
* Flag-field to indicate whether we've at least one linear or nominal attribute.
*/
private boolean caseOfLinearOrNominal = false;
public InputProcessor () {
}
public InputProcessor (DecisionTree instance) throws Exception {
// read the names file
if (readAttributes (instance.getNamesFile ()) < 0) {
System.out.println ("Problem reading the names file");
throw new IOException ();
}
// check whether the names-file had any nominal or linear attributes;
// if yes, then create new training / testing files after pre-processing
// the train / test data into discreet bins - tiny / small / medium / large / huge
if (caseOfLinearOrNominal) {
computeMinMaxInTrainingSet (instance.getTrainingDataFile ());
// populate training data set - moreover, based on whether a test set is provided or not, split training data into
// 2/3-1/3 for train-test.
if (discretize (instance.getTrainingDataFile (), this.trainingRoot, instance.getTestingDataFile ()) < 0) {
System.out.println ("Problem reading the training file");
throw new IOException ();
}
// if the testing file is given, then load the testing data set from it,
// otherwise skip, since testingRoot would be already populated in the above 'if'.
if (instance.getTestingDataFile ().equals ("SPLIT") == false) {
if (discretize (instance.getTestingDataFile (), this.testingRoot, "DONT_SPLIT") < 0) {
System.out.println ("Problem reading the testing file");
throw new IOException ();
}
}
}
else {
// populate training data set - moreover, based on whether a test set is provided or not, split training data into
// 2/3-1/3 for train-test.
if (readDataSets (instance.getTrainingDataFile (), this.trainingRoot, instance.getTestingDataFile ()) < 0) {
System.out.println ("Problem reading the training file");
throw new IOException ();
}
// if the testing file is given, then load the testing data set from it,
// otherwise skip, since testingRoot would be already populated in the above 'if'.
if (instance.getTestingDataFile ().equals ("SPLIT") == false) {
if (readDataSets (instance.getTestingDataFile (), this.testingRoot, "DONT_SPLIT") < 0) {
System.out.println ("Problem reading the testing file");
throw new IOException ();
}
}
}
// set all the values back in the decision tree class.
instance.setTrainingRoot (this.trainingRoot);
instance.setTestingRoot (this.testingRoot);
instance.setAttributeNames (this.attributeNames);
instance.setDomains (this.domains);
instance.setNumAttributes (this.numAttributes);
}
public int readAttributes (final String namesFile) throws Exception {
FileInputStream in = null;
/********************************************
* Reading the Names File *
********************************************/
try {
File inputFile = new File (namesFile);
in = new FileInputStream (inputFile);
} catch (Exception e) {
System.err.println ("Unable to open the names file: " + namesFile + "\n" + e);
return -1;
}
BufferedReader bin = new BufferedReader (new InputStreamReader (in));
String input = bin.readLine ();
if (input == null) {
System.err.println ("No data found in the names file: " + namesFile + "\n");
return 0;
}
String classNames = input.substring (0, input.indexOf ("."));
Vector tempSpanVector = new Vector ();
int featureCount = 0;
while ((input = bin.readLine ()) != null) {
if (input.startsWith ("|")) {
continue;
} else if (input.contains ("|")) {
input = input.substring (0, input.indexOf ("|"));
}
if (input.equals ("")) {
continue;
}
// Strip the domains of the attributes as we'll be anyways reading those from
// the actual data sets - needn't store them here
try {
if (input.substring (input.indexOf (":")).contains ("linear") || input.substring (input.indexOf (":")).contains ("nominal")) {
caseOfLinearOrNominal = true;
}
input = input.substring (0, input.indexOf (":"));
} catch (IndexOutOfBoundsException excp) {
if (input.contains ("label")) {
skipCount = featureCount;
continue;
// Done - change code for test data to skip label values
} else {
System.out.print ("Bad formatting in names file - missing ':' for some field");
System.exit (1);
}
}
// skip the class names - store at the end of the attributeNames array
if (input.equals (classNames)) {
continue;
}
tempSpanVector.addElement (input);
featureCount = featureCount + 1;
}
tempSpanVector.addElement (classNames);
numAttributes = tempSpanVector.size ();
if (numAttributes <= 1) {
return -1;
}
domains = new Vector [numAttributes];
for (int i = 0; i < numAttributes; i++) {
domains[i] = new Vector ();
}
attributeNames = new String [numAttributes];
Iterator spanIterator = tempSpanVector.iterator ();
int index = 0;
while (spanIterator.hasNext ()) {
attributeNames[index] = (String) spanIterator.next ();
index = index + 1;
}
in.close ();
bin.close ();
return 1;
}
public int readDataSets (final String fileName, final TreeNode root, final String toSplit) throws Exception {
FileInputStream in = null;
/************************************************
* Reading the Training / Testing data set File *
************************************************/
try {
File inputFile = new File (fileName);
in = new FileInputStream (inputFile);
} catch (Exception e) {
System.err.println ("Unable to open file: " + fileName + "\n" + e);
return -1;
}
BufferedReader bin = new BufferedReader (new InputStreamReader (in));
String input;
int index = 1;
while (true) {
input = bin.readLine ();
if (input == null) {
break;
} else if (input.startsWith ("|")) {
continue;
} else if (input.contains ("|")) {
input = input.substring (0, input.indexOf ("|"));
}
if (input.equals ("")) {
continue;
}
StringTokenizer tokenizer = new StringTokenizer (input, ",");
int numtokens = tokenizer.countTokens ();
if (skipCount > -1) {
if (numtokens != numAttributes + 1) {
return -1;
}
} else if (numtokens != numAttributes) {
return -1;
}
DataPoint point = new DataPoint (numAttributes);
// if there is no label to skip
if (skipCount == -1) {
point.label = "Example#" + index;
for (int i = 0; i < numAttributes; i++) {
point.attributes[i] = getSymbolValue (i, tokenizer.nextToken ());
}
} else if (skipCount > -1) {
int attributeIndex = 0;
for (int panditIndex = 0; panditIndex < numAttributes + 1; panditIndex++) {
// assign label to the data point and skip it as an attribute field
if (panditIndex == skipCount) {
point.label = tokenizer.nextToken ();
continue;
}
point.attributes[attributeIndex] = getSymbolValue (attributeIndex, tokenizer.nextToken ());
attributeIndex = attributeIndex + 1;
}
}
/************************************************
* Required 2/3-1/3 random data split follows *
************************************************/
if (toSplit.equals ("SPLIT")) {
double randomNumber = 3 * Math.random ();
if (randomNumber > 2) {
testingRoot.data.addElement (point);
} else {
root.data.addElement (point);
}
} else {
root.data.addElement (point);
}
index = index + 1;
}
in.close ();
bin.close ();
return 1;
}
public int getSymbolValue (final int attribute, final String symbol) {
int index = domains[attribute].indexOf (symbol);
if (index < 0) {
domains[attribute].addElement (symbol);
return domains[attribute].size () - 1;
}
return index;
}
public int discretize (final String fileName, final TreeNode root, final String toSplit) throws Exception {
FileInputStream in = null;
/*****************************************************************
* Reading and discretizing the Training / Testing data set File *
*****************************************************************/
try {
File inputFile = new File (fileName);
in = new FileInputStream (inputFile);
} catch (Exception e) {
System.err.println ("Unable to open file: " + fileName + "\n" + e);
return -1;
}
BufferedReader bin = new BufferedReader (new InputStreamReader (in));
String input;
int index = 1;
while (true) {
input = bin.readLine ();
if (input == null) {
break;
} else if (input.startsWith ("|")) {
continue;
} else if (input.contains ("|")) {
input = input.substring (0, input.indexOf ("|"));
}
if (input.equals ("")) {
continue;
}
StringTokenizer tokenizer = new StringTokenizer (input, ",");
int numtokens = tokenizer.countTokens ();
if (skipCount > -1) {
if (numtokens != numAttributes + 1) {
return -1;
}
} else if (numtokens != numAttributes) {
return -1;
}
DataPoint point = new DataPoint (numAttributes);
// if there is no label to skip
if (skipCount == -1) {
point.label = "Example#" + index;
String newString;
for (int attributeIndex = 0; attributeIndex < numAttributes; attributeIndex++) {
// don't discretize the class attribute
if (attributeIndex == numAttributes-1) {
point.attributes[attributeIndex] = getSymbolValue (attributeIndex, tokenizer.nextToken ());
continue;
}
try {
double nextToken = Double.valueOf (tokenizer.nextToken ());
// Based on the computed bin string, form the new string to be written
// to the new training / testing file
newString = computeBin (nextToken, attributeIndex);
} catch (NumberFormatException excp) {
// case when a '?' is encountered - assign the 'HUGE' class by default
newString = HUGE;
}
point.attributes[attributeIndex] = getSymbolValue (attributeIndex, newString);
}
} else if (skipCount > -1) {
int attributeIndex = 0;
String newString;
for (int panditIndex = 0; panditIndex < numAttributes + 1; panditIndex++) {
// assign label to the data point and skip it as an attribute field
if (panditIndex == skipCount) {
point.label = tokenizer.nextToken ();
continue;
}
// don't discretize the class attribute
if (panditIndex == numAttributes) {
point.attributes[attributeIndex] = getSymbolValue (attributeIndex, tokenizer.nextToken ());
attributeIndex = attributeIndex + 1;
continue;
}
try {
double nextToken = Double.valueOf (tokenizer.nextToken ());
// Based on the computed bin string, form the new string to be written
// to the new training / testing file
newString = computeBin (nextToken, index);
} catch (NumberFormatException excp) {
// case when a '?' is encountered - assign the 'HUGE' class by default
newString = HUGE;
}
point.attributes[attributeIndex] = getSymbolValue (attributeIndex, newString);
attributeIndex = attributeIndex + 1;
}
}
/************************************************
* Required 2/3-1/3 random data split follows *
************************************************/
if (toSplit.equals ("SPLIT")) {
double randomNumber = 3 * Math.random ();
if (randomNumber > 2) {
testingRoot.data.addElement (point);
} else {
root.data.addElement (point);
}
} else {
root.data.addElement (point);
}
index = index + 1;
}
in.close ();
bin.close ();
return 1;
}
/**
* Computes the Bin for the passed token based on the min / max
* values of the corresponding nominal / linear attribute
*
* @param token
* @param whichAttribute
*
* @return a String representing a discrete bin
*/
public String computeBin (final double token, final int whichAttribute) {
int bin = -1;
for (int index = 1; index <= NUM_OF_CLASSES; index++) {
double classComparisonValue = 1. * (index * (this.maxLinearArray[whichAttribute] - this.minLinearArray[whichAttribute]) / NUM_OF_CLASSES) + this.minLinearArray[whichAttribute];
if (token <= classComparisonValue) {
bin = index;
break;
}
}
// The encountered token extends beyond the defined limits as per the training set;
// thus, assign it to the highest class (unbounded)
if (bin == -1) {
bin = NUM_OF_CLASSES + 1;
}
switch (bin) {
case 1: return TINY;
case 2: return SMALL;
case 3: return MEDIUM;
case 4: return LARGE;
case 5: return HUGE;
default: return HUGE;
}
}
/**
* This function computes the min / max bounds for a each
* nominal / linear attribute in the training data set *
*
* @param trainingFileName
*
* @throws IOException
*/
public void computeMinMaxInTrainingSet (final String trainingFileName) throws IOException {
FileInputStream in = null;
/*********************************************************
* Computing min / max of the linear and nominal data in *
* the training data set. *
*********************************************************/
try {
File inputFile = new File (trainingFileName);
in = new FileInputStream (inputFile);
} catch (Exception e) {
System.err.println ("Unable to open file during discretization process: " + trainingFileName + "\n" + e);
System.exit (-1);
}
BufferedReader bin = new BufferedReader (new InputStreamReader (in));
String input = bin.readLine ();
if (input == null) {
System.out.println ("File empty: " + trainingFileName);
System.exit (1);
}
StringTokenizer tokenizer = new StringTokenizer (input, ",");
int numTokens = tokenizer.countTokens ();
double [] minArray = new double [numTokens];
double [] maxArray = new double [numTokens];
for (int index = 0; index < numTokens; index++) {
minArray[index] = MAX_POSS;
maxArray[index] = MIN_POSS;
}
while (true) {
if (input == null) {
break;
} else if (input.startsWith ("|")) {
continue;
} else if (input.contains ("|")) {
input = input.substring (0, input.indexOf ("|"));
}
if (input.equals ("")) {
continue;
}
tokenizer = new StringTokenizer (input, ",");
numTokens = tokenizer.countTokens ();
for (int index = 0; index < numTokens; index++) {
try {
double nextToken = Double.valueOf (tokenizer.nextToken ());
if (nextToken < minArray[index]) {
minArray[index] = nextToken;
}
if (nextToken > maxArray[index]) {
maxArray[index] = nextToken;
}
} catch (NumberFormatException excp) {
// case when a '?' is encountered - do nothing
}
}
input = bin.readLine ();
}
in.close ();
bin.close ();
this.minLinearArray = minArray;
this.maxLinearArray = maxArray;
}
}