-
Notifications
You must be signed in to change notification settings - Fork 0
/
Comparer.cs
255 lines (234 loc) · 10.2 KB
/
Comparer.cs
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using static StatsGenerator.ConstInfo;
using static StatsGenerator.ErrorInfo;
namespace StatsGenerator
{
class Comparer
{
public static ErrorMsg Compare(StreamReader classifierTrue,
StreamReader classifierFalse,
StreamReader groundTrue,
StreamReader groundFalse,
Genre outputGenre,
string storagePath)
{
bool isPages = outputGenre == Genre.pages;
Dictionary<string, byte> classifierTrueList = new Dictionary<string, byte>();
Dictionary<string, byte> groundTrueList = new Dictionary<string, byte>();
Dictionary<string, byte> groundFalseList = new Dictionary<string, byte>();
while (!classifierTrue.EndOfStream)
{
var line = classifierTrue.ReadLine();
if (line != "")
{
classifierTrueList.Add(line, 1);
}
}
while (!groundTrue.EndOfStream)
{
var line = groundTrue.ReadLine();
if (line != "")
{
groundTrueList.Add(line, 1);
}
}
while (!groundFalse.EndOfStream)
{
var line = groundFalse.ReadLine();
if(line != "")
{
groundFalseList.Add(line, 1);
}
}
string TPrslt = outputGenre.ToString() + "_TP.csv";
FileStream TPfs = new FileStream(storagePath + TPrslt, FileMode.Create, FileAccess.Write);
StreamWriter TPsw = new StreamWriter(TPfs, Encoding.UTF8);
string FPrslt = outputGenre.ToString() + "_FP.csv";
FileStream FPfs = new FileStream(storagePath + FPrslt, FileMode.Create, FileAccess.Write);
StreamWriter FPsw = new StreamWriter(FPfs, Encoding.UTF8);
string FNrslt = outputGenre.ToString() + "_FN.csv";
FileStream FNfs = new FileStream(storagePath + FNrslt, FileMode.Create, FileAccess.Write);
StreamWriter FNsw = new StreamWriter(FNfs, Encoding.UTF8);
string TNrslt = outputGenre.ToString() + "_TN.csv";
FileStream TNfs = new FileStream(storagePath + TNrslt, FileMode.Create, FileAccess.Write);
StreamWriter TNsw = new StreamWriter(TNfs, Encoding.UTF8);
string statRslt = outputGenre.ToString() + "_stat.csv";
FileStream statfs = new FileStream(storagePath + statRslt, FileMode.Create, FileAccess.Write);
StreamWriter statsw = new StreamWriter(statfs, Encoding.UTF8);
string errorStat = "errorStat.csv";
FileStream errorfs = new FileStream(storagePath + errorStat, FileMode.Append, FileAccess.Write);
StreamWriter errsw = new StreamWriter(errorfs, Encoding.UTF8);
TPsw.WriteLine("num,classifier true,ground truth true");
FPsw.WriteLine("num,classifier true,ground truth false");
FNsw.WriteLine("num,classifier false,ground truth true");
TNsw.WriteLine("num,classifier false,ground truth false");
statsw.WriteLine("num of TP,num of FP,num of FN,num of TN,precision,recall");
long TPcount = 0;
long FPcount = 0;
long FNcount = 0;
long TNcount = 0;
foreach (var item in classifierTrueList)
{
int count = 0;
if (isPages)
{
string tmp = item.Key + "_true.jpg";
if (groundTrueList.ContainsKey(tmp))
{
TPcount++;
count++;
var line = StringFormator(TPcount, item.Key, tmp);
TPsw.WriteLine(line);
}
tmp = item.Key + "_false.jpg";
if (groundFalseList.ContainsKey(tmp))
{
FPcount++;
count++;
var line = StringFormator(FPcount, item.Key, tmp);
FPsw.WriteLine(line);
}
}
else
{
if (groundTrueList.ContainsKey(item.Key))
{
TPcount++;
count++;
var line = StringFormator(TPcount, item.Key, item.Key);
TPsw.WriteLine(line);
}
if (groundFalseList.ContainsKey(item.Key))
{
FPcount++;
count++;
var line = StringFormator(FPcount, item.Key, item.Key);
FPsw.WriteLine(line);
}
}
if (count == 0)
{
var errorLine = StringFormator(outputGenre.ToString(),
outputGenre == Genre.snippet ? "classifier-snippets-true.txt"
: "classifier-pages-true.txt",
item.Key, ErrorMsg.item_not_found.ToString());
errsw.WriteLine(errorLine);
//return ErrorMsg.item_not_found;
}
if (count > 1)
{
var errorLine = StringFormator(outputGenre.ToString(),
outputGenre == Genre.snippet ? "classifier-snippets-true.txt"
: "classifier-pages-true.txt",
item.Key, ErrorMsg.dup_item_groundT_groudF.ToString());
errsw.WriteLine(errorLine);
//return ErrorMsg.dup_item_groundT_groudF;
}
}
classifierTrueList.Clear();
Dictionary<string, byte> classifierFalseList = new Dictionary<string, byte>();
while (!classifierFalse.EndOfStream)
{
var line = classifierFalse.ReadLine();
if (line != "")
{
classifierFalseList.Add(line, 1);
}
}
foreach (var item in classifierFalseList)
{
int count = 0;
if (isPages)
{
string tmp = item.Key + "_true.jpg";
if (groundTrueList.ContainsKey(tmp))
{
FNcount++;
count++;
var line = StringFormator(FNcount, item.Key, tmp);
FNsw.WriteLine(line);
}
tmp = item.Key + "_false.jpg";
if (groundFalseList.ContainsKey(tmp))
{
TNcount++;
count++;
var line = StringFormator(TNcount, item.Key, tmp);
TNsw.WriteLine(line);
}
}
else
{
if (groundTrueList.ContainsKey(item.Key))
{
FNcount++;
count++;
var line = StringFormator(FNcount, item.Key, item.Key);
FNsw.WriteLine(line);
}
if (groundFalseList.ContainsKey(item.Key))
{
TNcount++;
count++;
var line = StringFormator(TNcount, item.Key, item.Key);
TNsw.WriteLine(line);
}
}
if (count == 0)
{
var errorLine = StringFormator(outputGenre.ToString(),
outputGenre == Genre.snippet ? "classifier-snippets-false.txt"
: "classifier-pages-false.txt",
item.Key, ErrorMsg.item_not_found.ToString());
errsw.WriteLine(errorLine);
//return ErrorMsg.item_not_found;
}
if (count > 1)
{
var errorLine = StringFormator(outputGenre.ToString(),
outputGenre == Genre.snippet ? "classifier-snippets-false.txt"
: "classifier-pages-false.txt",
item.Key, ErrorMsg.dup_item_groundT_groudF.ToString());
errsw.WriteLine(errorLine);
//return ErrorMsg.dup_item_groundT_groudF;
}
}
double precision = (double)TPcount / ((double)TPcount + (double)FPcount);
double recall = (double)TPcount / ((double)TPcount + (double)FNcount);
string statLine = StringFormator(TPcount, FPcount, FNcount, TNcount, precision, recall);
statsw.WriteLine(statLine);
TPsw.Close();
TPfs.Close();
FPsw.Close();
FPfs.Close();
FNsw.Close();
FNfs.Close();
TNsw.Close();
TNfs.Close();
statsw.Close();
statfs.Close();
errsw.Close();
errorfs.Close();
return ErrorMsg.success;
}
private static string StringFormator(params object[] args)
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < args.Length; i++)
{
if(i == args.Length - 1)
{
sb.Append(args[i]);
}
else
{
sb.Append(args[i]).Append(",");
}
}
return sb.ToString();
}
}
}