-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDocumentComparison.java
More file actions
308 lines (272 loc) · 7.22 KB
/
DocumentComparison.java
File metadata and controls
308 lines (272 loc) · 7.22 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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import java.io.*;
import javax.swing.*;
import java.util.*;
/** @author Gergely Kota
DcoumentComparison takes a starting point File and converts
all of them to canonical text fcrmat (lowercase alphanumeric
with no white spaces)
Extending classes need to define their canonical form and
what comparisons they wish to run.
*/
public abstract class DocumentComparison implements Serializable, Progressable
{
private File[] files;
private File localDir;
private HashMap strings;
private HashMap actualStrings;
private final String ENDING;
private int tolerance;
private DocumentComparisonResult result;
private ArrayList list, loggers;
private double progress;
/** @param the File to search for documents in
*/
public DocumentComparison(File f)
{
String s = Config.read("endtype");
if(s == null)
s = "txt";
ENDING = s;
result = null;
localDir = f;
files = getFiles();
strings = new HashMap();
actualStrings = new HashMap();
list = new ArrayList();
loggers = new ArrayList();
progress = 0;
}
public void addProgressListener(ProgressListener pl)
{
if(!list.contains(pl))
list.add(pl);
}
public double getProgress()
{
return progress; // need to make this different
}
public void notifyListeners()
{
ProgressEvent pe = new ProgressEvent(this, "Comparing Documents");
for(int i = 0; i < list.size(); i++)
((ProgressListener)list.get(i)).progressPerformed(pe);
}
public void log(String s)
{
for(int i = 0; i < loggers.size(); i++)
((Logger)loggers.get(i)).log(s);
}
public void addLogger(Logger l)
{
if(!loggers.contains(l))
loggers.add(l);
}
private File[] getFiles()
{
ArrayList temp = getFiles(localDir);
File[] f = new File[temp.size()];
for(int i = 0; i < f.length; i++)
f[i] = (File) temp.get(i);
return f;
}
private ArrayList getFiles(File start)
{
File[] f = start.listFiles(new TextFilter());
ArrayList found = new ArrayList();
for(int i = 0; i < f.length; i++)
found.add(f[i]);
File[] dirs = start.listFiles(new DirFilter());
for(int i = 0; i < dirs.length; i++)
found.addAll(getFiles(dirs[i]));
// System.out.println("Returning " + start + " " + found);
return found;
}
public int getTolerance()
{
return tolerance;
}
public boolean setTolerance(int n)
{
if(n == 0)
return false;
tolerance = n;
return true;
}
// reads in the found Files
private String readFile(File f)
{
try
{
StringBuffer sb = new StringBuffer();
BufferedReader br = new BufferedReader(new FileReader(f));
while(br.ready())
sb.append(br.readLine()).append("\n");
return sb.toString();
}
catch(Exception e) {return "";} // failure to read is an empty string
}
/** @return a description of this comparison type
*/
public static String description()
{
return "Abstract BaseClass for all comparisons";
}
/** upon read, every DocumentComparison stores all legal files'
canonical String forms for later data. The canonical form
is defined by each extending class
@return a DocumentComparison with all available files, ready to compare
*/
public DocumentComparison read()
{
log("Reading files in " + localDir.getAbsolutePath());
for(int i = 0; i < files.length; i++)
{
File f = files[i];
String s = readFile(f);
actualStrings.put(f, s);
String c = canonicalForm(f);
strings.put(f, c);
}
return this;
}
/** @return a list of read files and the working directory
*/
public String toString()
{
String s = localDir.getAbsolutePath();
for(int i = 0; i < files.length; i++)
s += "\n " + files[i].getName();
return s;
}
/** @return the canonical content of each file by its index number
*/
public String getContent(File f)
{
return (String) strings.get(f);
}
/** @return the original content of each file by its index number
*/
public String getOriginalContent(File f)
{
return (String) actualStrings.get(f);
}
/** @param f the File whose index is to be found
@return the index for the argument File
*/
/** @param i the index to get the File from
@return the File at a given index
*/
public File[] getFileList()
{
return files;
}
public DocumentComparisonResult compare()
{
log("Comparing files in " + localDir.getAbsolutePath());
boolean cheatFound = false;
progress = 0;
DocumentComparisonResult r = new DocumentComparisonResult();
r.setWorkingDirectory(localDir);
for(int i = 0; i < files.length; i++)
{
int interesting = 0;
for(int j = 0; j < files.length; j++)
{
int num = files.length;
if(i != j)
{
PairComparison pc = compare(files[j], files[i]);
r.add(pc);
if(pc.score() > 0)
interesting++;
}
progress = (i*num + j + 1) / ((double)num * num);
notifyListeners();
}
if(interesting != 0)
cheatFound = true;
log(files[i].getAbsolutePath() + " cheated from " + interesting + " other documents.");
}
if(!cheatFound)
log("None of the submitted documents had cheat occurences");
result = r;
return result;
}
public DocumentComparisonResult compare(File f)
{
log("Comparing files in " + localDir.getAbsolutePath());
progress = 0;
int interesting = 0;
DocumentComparisonResult r = new DocumentComparisonResult();
r.setWorkingDirectory(localDir);
for(int i = 0; i < files.length; i++)
if(!files[i].equals(f))
{
double num = files.length;
PairComparison pc = compare(files[i], f);
r.add(pc);
progress = (i + 1) / num;
notifyListeners();
if(pc.score() > 0)
interesting++;
log("Compared " + files[i].getName() + " and " + f.getName() + ": " + pc.score());
}
log(f.getAbsolutePath() + " cheated from " + interesting + " other documents.");
result = r;
return result;
}
public DocumentComparisonResult getResult()
{
if(result == null)
return compare();
return result;
}
/** @param f the File whose content is referred to
@param adjustedIndex the index of a char in the canonical form content of the File
@return the index of that same char in the original content of the File
*/
public abstract int indexMap(File f, int adjustedIndex);
public abstract String canonicalForm(File f);
public abstract PairComparison compare(File f1, File f2);
/* ----------------------------------------------------- */
/* private class that specifies what legal text files are */
private class TextFilter implements FileFilter
{
public boolean accept(File f)
{
if(f.getName().endsWith(ENDING))
return true;
return false;
}
}
/* ----------------------------------------------------- */
/* private class that specifies what legal directories are */
private class DirFilter implements FileFilter
{
public boolean accept(File f)
{
if(f.isDirectory())
return true;
return false;
}
}
/* ----------------------------------------------------- */
public static void main(String[] arhs)
{
try
{
StringBuffer sb = new StringBuffer();
BufferedReader br = new BufferedReader(new FileReader(new File("Downloads/Java.txt")));
while(br.ready())
sb.append(br.readLine()).append("\n");
System.out.println(sb.toString());
JFrame jf = new JFrame();
JTextArea jta = new JTextArea();
jta.setText(sb.toString());
jf.getContentPane().add(jta);
jf.setSize(400,800);
jf.show();
}
catch(Exception e) {} // failure to read is an empty string
}
}