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.cpdp.loader.AbstractFolderLoader.SingleVersionLoader;
|
---|
11 | import de.ugoe.cs.util.FileTools;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * Loads the instances for a software version from a CSV file of the // TODO dataset citation
|
---|
15 | * data set.
|
---|
16 | * @author Steffen Herbold
|
---|
17 | */
|
---|
18 | class CSVDataLoader implements SingleVersionLoader {
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Loads the instances.
|
---|
22 | * @param file handle to the file of the instances
|
---|
23 | * @return the instances
|
---|
24 | */
|
---|
25 | @Override
|
---|
26 | public Instances load(File file) {
|
---|
27 | final String[] lines;
|
---|
28 | try {
|
---|
29 | lines = FileTools.getLinesFromFile(file.getAbsolutePath());
|
---|
30 | } catch (IOException e) {
|
---|
31 | throw new RuntimeException(e);
|
---|
32 | }
|
---|
33 |
|
---|
34 | // configure Instances
|
---|
35 | final ArrayList<Attribute> atts = new ArrayList<Attribute>();
|
---|
36 |
|
---|
37 | String[] lineSplit = lines[0].split(",");
|
---|
38 | for( int j=0 ; j<lineSplit.length-4 ; j++ ) {
|
---|
39 | atts.add(new Attribute(lineSplit[j+3]));
|
---|
40 | }
|
---|
41 | final ArrayList<String> classAttVals = new ArrayList<String>();
|
---|
42 | classAttVals.add("0");
|
---|
43 | classAttVals.add("1");
|
---|
44 | final Attribute classAtt = new Attribute("bug", classAttVals);
|
---|
45 | atts.add(classAtt);
|
---|
46 |
|
---|
47 | final Instances data = new Instances(file.getName(), atts, 0);
|
---|
48 | data.setClass(classAtt);
|
---|
49 |
|
---|
50 | // fetch data
|
---|
51 | for( int i=1 ; i<lines.length ; i++ ) {
|
---|
52 | lineSplit = lines[i].split(",");
|
---|
53 | double[] values = new double[lineSplit.length-3];
|
---|
54 | for( int j=0 ; j<values.length-1 ; j++ ) {
|
---|
55 | values[j] = Double.parseDouble(lineSplit[j+3].trim());
|
---|
56 | }
|
---|
57 | values[values.length-1] = lineSplit[lineSplit.length-1].trim().equals("0") ? 0 : 1;
|
---|
58 | data.add(new DenseInstance(1.0, values));
|
---|
59 | }
|
---|
60 |
|
---|
61 | return data;
|
---|
62 | }
|
---|
63 |
|
---|
64 | @Override
|
---|
65 | public boolean filenameFilter(String filename) {
|
---|
66 | return filename.endsWith(".csv");
|
---|
67 | }
|
---|
68 |
|
---|
69 | }
|
---|