1 | package de.ugoe.cs.cpdp.loader; |
---|
2 | |
---|
3 | import java.io.File; |
---|
4 | import java.io.IOException; |
---|
5 | import java.util.ArrayList; |
---|
6 | |
---|
7 | import weka.core.Attribute; |
---|
8 | import weka.core.DenseInstance; |
---|
9 | import weka.core.Instances; |
---|
10 | import de.ugoe.cs.util.FileTools; |
---|
11 | |
---|
12 | |
---|
13 | class CSVMockusDataLoader implements SingleVersionLoader { |
---|
14 | |
---|
15 | @Override |
---|
16 | public Instances load(File file) { |
---|
17 | final String[] lines; |
---|
18 | try { |
---|
19 | |
---|
20 | lines = FileTools.getLinesFromFile(file.getAbsolutePath()); |
---|
21 | } catch (IOException e) { |
---|
22 | throw new RuntimeException(e); |
---|
23 | } |
---|
24 | |
---|
25 | |
---|
26 | // configure Instances |
---|
27 | final ArrayList<Attribute> atts = new ArrayList<Attribute>(); |
---|
28 | |
---|
29 | String[] lineSplit = lines[0].split(","); |
---|
30 | for (int j = 0; j < lineSplit.length - 3; j++) { |
---|
31 | atts.add(new Attribute(lineSplit[j + 2])); |
---|
32 | } |
---|
33 | |
---|
34 | final ArrayList<String> classAttVals = new ArrayList<String>(); |
---|
35 | classAttVals.add("0"); |
---|
36 | classAttVals.add("1"); |
---|
37 | final Attribute classAtt = new Attribute("bug", classAttVals); |
---|
38 | atts.add(classAtt); |
---|
39 | |
---|
40 | final Instances data = new Instances(file.getName(), atts, 0); |
---|
41 | data.setClass(classAtt); |
---|
42 | |
---|
43 | // fetch data |
---|
44 | for (int i = 1; i < lines.length; i++) { |
---|
45 | lineSplit = lines[i].split(","); |
---|
46 | double[] values = new double[lineSplit.length - 2]; |
---|
47 | for (int j = 0; j < values.length - 1; j++) { |
---|
48 | values[j] = Double.parseDouble(lineSplit[j + 2].trim()); |
---|
49 | } |
---|
50 | values[values.length - 1] = lineSplit[lineSplit.length - 1].trim() |
---|
51 | .equals("0") ? 0 : 1; |
---|
52 | data.add(new DenseInstance(1.0, values)); |
---|
53 | } |
---|
54 | |
---|
55 | return data; |
---|
56 | } |
---|
57 | |
---|
58 | @Override |
---|
59 | public boolean filenameFilter(String filename) { |
---|
60 | return filename.endsWith(".csv"); |
---|
61 | } |
---|
62 | |
---|
63 | } |
---|
64 | |
---|