| 1 |
package pp2.prediction.evaluation; |
| 2 |
|
| 3 |
import java.io.BufferedReader; |
| 4 |
import java.io.BufferedWriter; |
| 5 |
import java.io.File; |
| 6 |
import java.io.FilenameFilter; |
| 7 |
import java.io.IOException; |
| 8 |
|
| 9 |
import pp2.tools.Tools; |
| 10 |
|
| 11 |
public class AddEvaluation { |
| 12 |
|
| 13 |
private int predictCol; |
| 14 |
private int actualCol; |
| 15 |
private int precisionCol; |
| 16 |
private int recallCol; |
| 17 |
|
| 18 |
|
| 19 |
public AddEvaluation(String inputFile, int predictCol, int actualCol) { |
| 20 |
this.predictCol = predictCol; |
| 21 |
this.actualCol = actualCol; |
| 22 |
addEvaluationToFile(new File(inputFile), inputFile+".eval"); |
| 23 |
} |
| 24 |
|
| 25 |
public AddEvaluation(String directory, FilenameFilter filter, |
| 26 |
int predictCol, int actualCol) { |
| 27 |
this.predictCol = predictCol; |
| 28 |
this.actualCol = actualCol; |
| 29 |
for(File file : new File(directory).listFiles(filter)) { |
| 30 |
addEvaluationToFile(file, file.getAbsolutePath()+".eval"); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
public void addEvaluationToFile(File inputFile, String outputFile) { |
| 36 |
try { |
| 37 |
BufferedReader input = Tools.openFile(inputFile); |
| 38 |
BufferedWriter output = Tools.writeToFile(outputFile); |
| 39 |
|
| 40 |
String line; |
| 41 |
boolean firstLine = true; |
| 42 |
|
| 43 |
while((line = input.readLine()) != null) { |
| 44 |
if(firstLine) { |
| 45 |
// output.write("#") |
| 46 |
firstLine = false; |
| 47 |
} |
| 48 |
String[] content = line.split("\t"); |
| 49 |
String[] predList = content[predictCol].split(","); |
| 50 |
String[] actList = content[actualCol].split(","); |
| 51 |
precisionCol = content.length+1; |
| 52 |
recallCol = content.length+2; |
| 53 |
GOListComparator glc = new GOListComparator(predList, actList); |
| 54 |
output.write(line+"\t"+glc.precision()+"\t"+glc.recall()+"\n"); |
| 55 |
} |
| 56 |
output.flush(); |
| 57 |
output.close(); |
| 58 |
input.close(); |
| 59 |
inputFile.delete(); |
| 60 |
} catch (IOException e) { |
| 61 |
e.printStackTrace(); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
public int getPrecisionCol() { |
| 66 |
return precisionCol; |
| 67 |
} |
| 68 |
|
| 69 |
public int getRecallCol() { |
| 70 |
return recallCol; |
| 71 |
} |
| 72 |
|
| 73 |
public static void main(String[] args) { |
| 74 |
String usage = "AddEvaluation -i inputFile"; |
| 75 |
if(args.length == 0) { |
| 76 |
System.out.println(usage); |
| 77 |
} else { |
| 78 |
String file = ""; |
| 79 |
for(int i=0; i<args.length; i+=2) { |
| 80 |
if(args[i].equals("-i")) { |
| 81 |
file = args[i+1]; |
| 82 |
} |
| 83 |
} |
| 84 |
new AddEvaluation(file, 1, 0); |
| 85 |
} |
| 86 |
} |
| 87 |
} |