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 | * TODO
|
---|
14 | * @author sherbold
|
---|
15 | *
|
---|
16 | */
|
---|
17 | class AUDIDataLoader implements SingleVersionLoader {
|
---|
18 |
|
---|
19 | /*
|
---|
20 | * (non-Javadoc)
|
---|
21 | *
|
---|
22 | * @see
|
---|
23 | * de.ugoe.cs.cpdp.loader.AbstractFolderLoader.SingleVersionLoader#load(
|
---|
24 | * java.io.File)
|
---|
25 | */
|
---|
26 | @Override
|
---|
27 | public Instances load(File file) {
|
---|
28 | final String[] lines;
|
---|
29 | try {
|
---|
30 | lines = FileTools.getLinesFromFile(file.getAbsolutePath());
|
---|
31 | } catch (IOException e) {
|
---|
32 | throw new RuntimeException(e);
|
---|
33 | }
|
---|
34 |
|
---|
35 | // information about bugs are in another file
|
---|
36 | String path = file.getAbsolutePath();
|
---|
37 | path = path.substring(0, path.length()-14) + "repro.csv";
|
---|
38 | final String[] linesBug;
|
---|
39 | try {
|
---|
40 | linesBug = FileTools.getLinesFromFile(path);
|
---|
41 | } catch (IOException e) {
|
---|
42 | throw new RuntimeException(e);
|
---|
43 | }
|
---|
44 |
|
---|
45 | // configure Instances
|
---|
46 | final ArrayList<Attribute> atts = new ArrayList<Attribute>();
|
---|
47 |
|
---|
48 | String[] lineSplit = lines[0].split(";");
|
---|
49 | // ignore first three/four and last two columns
|
---|
50 | int offset;
|
---|
51 | if( lineSplit[3].equals("project_rev") ) {
|
---|
52 | offset = 4;
|
---|
53 | } else {
|
---|
54 | offset = 3;
|
---|
55 | }
|
---|
56 | for (int j = 0; j < lineSplit.length - (offset+2); j++) {
|
---|
57 | atts.add(new Attribute(lineSplit[j + offset]));
|
---|
58 | }
|
---|
59 | final ArrayList<String> classAttVals = new ArrayList<String>();
|
---|
60 | classAttVals.add("0");
|
---|
61 | classAttVals.add("1");
|
---|
62 | final Attribute classAtt = new Attribute("bug", classAttVals);
|
---|
63 | atts.add(classAtt);
|
---|
64 |
|
---|
65 | final Instances data = new Instances(file.getName(), atts, 0);
|
---|
66 | data.setClass(classAtt);
|
---|
67 |
|
---|
68 | // fetch data
|
---|
69 | for (int i = 1; i < lines.length; i++) {
|
---|
70 | boolean validInstance = true;
|
---|
71 | lineSplit = lines[i].split(";");
|
---|
72 | String[] lineSplitBug = linesBug[i].split(";");
|
---|
73 | double[] values = new double[data.numAttributes()];
|
---|
74 | for (int j = 0; validInstance && j < values.length-1; j++) {
|
---|
75 | if( lineSplit[j + offset].trim().isEmpty() ) {
|
---|
76 | validInstance = false;
|
---|
77 | } else {
|
---|
78 | values[j] = Double.parseDouble(lineSplit[j + offset].trim());
|
---|
79 | }
|
---|
80 | }
|
---|
81 | if( offset==3 ) {
|
---|
82 | values[values.length - 1] = lineSplitBug[7].equals("0") ? 0 : 1;
|
---|
83 | } else {
|
---|
84 | values[values.length - 1] = lineSplitBug[8].equals("0") ? 0 : 1;
|
---|
85 | }
|
---|
86 |
|
---|
87 | if( validInstance ) {
|
---|
88 | data.add(new DenseInstance(1.0, values));
|
---|
89 | } else {
|
---|
90 | System.out.println("instance " + i + " is invalid");
|
---|
91 | }
|
---|
92 | }
|
---|
93 | return data;
|
---|
94 | }
|
---|
95 |
|
---|
96 | /*
|
---|
97 | * (non-Javadoc)
|
---|
98 | *
|
---|
99 | * @see de.ugoe.cs.cpdp.loader.AbstractFolderLoader.SingleVersionLoader#
|
---|
100 | * filenameFilter(java.lang.String)
|
---|
101 | */
|
---|
102 | @Override
|
---|
103 | public boolean filenameFilter(String filename) {
|
---|
104 | return filename.endsWith("src.csv");
|
---|
105 | }
|
---|
106 |
|
---|
107 | }
|
---|