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