-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPairComparison.java
More file actions
183 lines (153 loc) · 3.84 KB
/
PairComparison.java
File metadata and controls
183 lines (153 loc) · 3.84 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
import java.io.*;
import java.util.*;
/** @author Gergely Kota
PairComparison contains the results for the comparison of 2 files
*/
public class PairComparison implements Serializable, Comparable
{
private File file1, file2;
private String orig1, orig2;
// private String html1, html2;
private boolean[] used1, used2;
private ArrayList set1, set2;
private int cheats;
private double score;
private String lastColor1, lastColor2, lastResult1, lastResult2;
public PairComparison(File f1, File f2)
{
file1 = f1;
file2 = f2;
lastColor1 = lastColor2 = lastResult1 = lastResult2 = null;
set1 = new ArrayList();
set2 = new ArrayList();
cheats = 0;
score = -1;
try
{
BufferedReader br = new BufferedReader(new FileReader(file1));
StringBuffer sb = new StringBuffer();
while(br.ready())
sb.append(br.readLine()).append("\n");
orig1 = sb.toString();
br = new BufferedReader(new FileReader(file2));
sb = new StringBuffer();
while(br.ready())
sb.append(br.readLine()).append("\n");
orig2 = sb.toString();
}
catch(Exception e) {}
used1 = new boolean[orig1.length()]; // false by default
used2 = new boolean[orig2.length()];
}
public int compareTo(Object o)
{
try
{
return (int) (1000 * (((PairComparison) o).score - score));
}
catch(Exception e) {return -1;}
}
public void log(int start1, int end1, int start2, int end2)
{
cheats++;
for(int i = start1; i < end1; i++)
used1[i] = true;
for(int i = start2; i < end2; i++)
used2[i] = true;
}
public double score()
{
if(score < 0)
return ((double)cheats)/orig1.length();
return score;
}
public void setScore(double x)
{
score = x;
}
public File getFile1()
{
return file1;
}
public File getFile2()
{
return file2;
}
public String getHTML1(String colorcode)
{
if(!colorcode.equals(lastColor1))
{
lastColor1 = colorcode;
lastResult1 = getHTML(orig1, used1, colorcode);
}
return lastResult1;
}
public String getHTML2(String colorcode)
{
if(!colorcode.equals(lastColor2))
{
lastColor2 = colorcode;
lastResult2 = getHTML(orig2, used2, colorcode);
}
return lastResult2;
}
public boolean equals(Object o)
{
try
{
PairComparison other = (PairComparison) o;
if(!getFile1().equals(other.getFile1()))
return false;
if(!getFile2().equals(other.getFile2()))
return false;
return true;
}
catch(Exception e) {return false;}
}
public String toString()
{
// we will ideally use the very original names the files were
// String s = FileConverter.getOriginal(getFile1()).getName() + ", ";
// s += FileConverter.getOriginal(getFile2()).getName();
String s = getFile1().getName() + ", ";
s += getFile2().getName();
int i = (int) (1000*score());
int dec = i/10;
String cheat = "[" + ((dec<10)?" ":"") + dec + "." + (i%10) + "%]";
return cheat + " " + s;
}
public String fullString()
{
String s = getFile1().getAbsolutePath() + ", ";
s += getFile2().getAbsolutePath();
int i = (int) (1000*score());
String cheat = "[" + (i/10) + "." + (i%10) + "%]";
return cheat + " " + s;
}
private String getHTML(String original, boolean[] map, String color)
{
int max = map.length;
StringBuffer sb = new StringBuffer();
sb.append("<HTML>");
int count = 0;
while(count < max)
{
int start = count;
while(count < max && map[count] == false) {count++;}
sb.append(original.substring(start, count));
start = count;
while(count < max && map[count] == true) {count++;}
sb.append(wrap(original.substring(start, count), color));
}
sb.append("</HTML>");
return sb.toString().replaceAll("\n", "<BR>");
}
private String wrap(String content, String color)
{
return "<font color = " + color + ">" + content + "</font>";
}
public static void main(String[] args)
{
new PairComparison(new File("Downloads/Borland.txt"), new File("Downloads/tr.txt"));
}
}