-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrainTestSets.java
More file actions
150 lines (137 loc) · 3.16 KB
/
Copy pathTrainTestSets.java
File metadata and controls
150 lines (137 loc) · 3.16 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
import java.io.File;
import java.io.FileInputStream;
import java.util.Scanner;
public class TrainTestSets implements OptionHandler{
protected DataSet test;
protected DataSet train;
protected String[] options;
public boolean hasTrain = false;
public boolean hasTest = false;
TrainTestSets() {}
TrainTestSets(DataSet train, DataSet test)
{
this.test = test;
this.train = train;
hasTrain = true;
hasTest = true;
}
TrainTestSets(java.lang.String[] options) throws Exception
{
boolean flag=false;
if(options.length==2)
{
if(options[0].compareTo("-t")==0)
{
train = buildSet(options[1]);
//System.out.println(train.toString());
hasTrain = true;
flag=true;
}
if(options[0].compareTo("-T")==0)
{
test = buildSet(options[1]);
//System.out.println(test.toString());
hasTest = true;
flag=true;
}
}
else if(options.length==4)
{
for(int i =0;i<options.length;i=i+2)
{
if(options[i].compareTo("-t")==0)
{
train = buildSet(options[i+1]);
//System.out.println(train.toString());
hasTrain = true;
flag=true;
}
else if(options[i].compareTo("-T")==0)
{
test = buildSet(options[i+1]);
//System.out.println(test.toString());
hasTest = true;
flag=true;
}
}
}
if(!flag)
System.out.println("Invalid Arguments(Options)!");
if((!hasTest)&&!(hasTrain)) System.out.println("Invalid file format!");
}
public DataSet buildSet(String filename) throws Exception
{
File file = new File(filename);
Attributes attributeset = new Attributes();
Scanner scan = new Scanner(new FileInputStream(file));
String datasetname="";
Examples exampleset = new Examples(attributeset);
String s="";
scan.useDelimiter("\n");
while(scan.hasNext())
{
s=scan.next();
if(s.contains("@dataset"))
{
String[] q =s.split(" ");
datasetname = q[1];
}
if(s.contains("@attribute"))
{
String parsing = "";
while(s.contains("@attribute"))
{
parsing+=s+"\n";
s=scan.next();
}
// System.out.println(parsing);
Scanner temp = new Scanner(parsing);
attributeset = new Attributes(temp);
}
else if(s.contains("@examples"))
{
String parsing="";
while(scan.hasNext())
{
parsing+=scan.next()+"\n";
}
Scanner temp = new Scanner(parsing);
exampleset = new Examples(attributeset,temp);
}
//System.out.println(s);
}
//dataset
DataSet trainningset = new DataSet(attributeset, datasetname);
trainningset.addExamples(exampleset);
return trainningset;
}
DataSet getTestingSet()
{
return test;
}
DataSet getTrainingSet()
{
return train;
}
public void setOptions(String[] options)
{
this.options = options;
}
void setTestingSet(DataSet test)
{
this.test = test;
}
void setTrainingSet(DataSet train)
{
this.train = train;
}
public java.lang.String toString()
{
String output ="";
if(hasTrain)
output+=this.train.toString()+"\n\n";
if(hasTest)
output+=this.test.toString()+"\n";
return output;
}
}