-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCSV.java
More file actions
163 lines (128 loc) · 4.79 KB
/
CSV.java
File metadata and controls
163 lines (128 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import weka.classifiers.Classifier;
/**
* A class for managing the csv output for the ALC classification code.
* For each test run an individual csv will be created. Additionally there is a global CSV file
* where each testrun is appended to. There are different global csv files per document class and one for
* combined classes.
*
*/
public class CSV {
//The global csv output file for the combined documents
private final String GLOBAL_DEBUG_OUTPUT = "/home/bas-alc/test/output/GLOBAL_DEBUG";
File fcsv;
File fdebug;
PrintWriter csv;
PrintWriter debug;
//hash map to hold the global csv output files for the per doc class output files.
//hashed by document class => Printwriter
HashMap<String, PrintWriter> partitionedFiles = new HashMap<String, PrintWriter>();
boolean separated = false;
public CSV(String filename, boolean separated){
this.separated = separated;
try{
partitionedFiles.put("DP", new PrintWriter(new FileWriter(new File(GLOBAL_DEBUG_OUTPUT + "_DP.csv"), true)));
partitionedFiles.put("DQ", new PrintWriter(new FileWriter(new File(GLOBAL_DEBUG_OUTPUT + "_DQ.csv"), true)));
partitionedFiles.put("EC", new PrintWriter(new FileWriter(new File(GLOBAL_DEBUG_OUTPUT + "_EC.csv"), true)));
partitionedFiles.put("LN", new PrintWriter(new FileWriter(new File(GLOBAL_DEBUG_OUTPUT + "_LN.csv"), true)));
partitionedFiles.put("LS", new PrintWriter(new FileWriter(new File(GLOBAL_DEBUG_OUTPUT + "_LS.csv"), true)));
partitionedFiles.put("LT", new PrintWriter(new FileWriter(new File(GLOBAL_DEBUG_OUTPUT + "_LT.csv"), true)));
partitionedFiles.put("MP", new PrintWriter(new FileWriter(new File(GLOBAL_DEBUG_OUTPUT + "_MP.csv"), true)));
partitionedFiles.put("MQ", new PrintWriter(new FileWriter(new File(GLOBAL_DEBUG_OUTPUT + "_MQ.csv"), true)));
partitionedFiles.put("RA", new PrintWriter(new FileWriter(new File(GLOBAL_DEBUG_OUTPUT + "_RA.csv"), true)));
partitionedFiles.put("RR", new PrintWriter(new FileWriter(new File(GLOBAL_DEBUG_OUTPUT + "_RR.csv"), true)));
partitionedFiles.put("RT", new PrintWriter(new FileWriter(new File(GLOBAL_DEBUG_OUTPUT + "_RT.csv"), true)));
fcsv = new File(filename);
fcsv.createNewFile();
csv = new PrintWriter(new FileWriter(this.fcsv, true));
fdebug = new File(GLOBAL_DEBUG_OUTPUT + "_COMBINDED.csv");
debug = new PrintWriter(new FileWriter(fdebug, true));
}catch(IOException e){
e.printStackTrace();
}
}
public void createCSVHeader(HashMap<String, Classifier> availableClassifiers, boolean mav, String testconfig, boolean printHeader){
//write to debug which test this is
if(printHeader)
writeHeaderToDebug(testconfig + "\n");
//create an empty cell at the beginning of the table
csv.write(";");
if(printHeader){
writeHeaderToDebug(";");
}
//create a field with the name of the classifier in the middle of each 3 cells
for(Map.Entry<String, Classifier> entry : availableClassifiers.entrySet()){
csv.write(";" + entry.getKey() + ";;");
if(printHeader)
writeHeaderToDebug(";" + entry.getKey() + ";;");
}
if(mav){
csv.write(";Majority;;");
if(printHeader)
writeHeaderToDebug(";Majority;;");
}
csv.write("\n");
if(printHeader)
writeHeaderToDebug("\n");
//create the cells, containing the different measures, F-measure - Precision - Correctly Classified Instances
for(Map.Entry<String, Classifier> entry : availableClassifiers.entrySet()){
csv.write(";F;P;CCI");
if(printHeader)
writeHeaderToDebug(";UAR;P;CCI");
}
//if majority vote enabled, print once more
if(mav){
csv.write(";F;P;CCI");
if(printHeader)
writeHeaderToDebug(";UAR;P;CCI");
}
csv.write("\n");
if(printHeader)
writeHeaderToDebug("\n");
csv.flush();
debug.flush();
for(PrintWriter p : partitionedFiles.values()){
p.flush();
}
}
public void closeCSVPrinter(boolean printHeader){
if(printHeader){
writeHeaderToDebug("\n\n");
}
csv.close();
debug.close();
for(PrintWriter p : partitionedFiles.values()){
p.close();
}
}
public void write(String text, String partition){
csv.write(text);
csv.flush();
writeToDebug(text, partition);
}
private void writeToDebug(String text, String partition){
if(partition != null && !partition.contains("com")){
partitionedFiles.get(partition).write(text);
partitionedFiles.get(partition).flush();
}else{
debug.write(text);
debug.flush();
}
}
private void writeHeaderToDebug(String toWrite){
if(separated){
for(PrintWriter pw : partitionedFiles.values()){
pw.write(toWrite);
pw.flush();
}
}else{
debug.write(toWrite);
debug.flush();
}
}
}