-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBenchmark.java
182 lines (151 loc) · 6.28 KB
/
Benchmark.java
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
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.util.*;
import javax.management.timer.Timer;
import java.io.FileReader;
import java.io.IOException;
import java.util.stream.Stream;
import java.nio.file.Paths;
import java.nio.file.Files;
import java.nio.file.Path;
class Benchmark {
public static final String TARGET_DIR = "LeePincombeWelsh";
public static final String ENCODING = "UTF-8";
public static final String DELIMTER = " ";
private static final String BOLD_TEXT = "\033[0;1m";
private static final String NORMAL_TEXT = "\033[0m";
private static final long MS_TO_NS = 1000000;
static class TestResult {
double x;
double y;
long elalpsedTime;
public TestResult(double rating, double humanRating, long elapsedTime){
this.x = rating;
this.y = humanRating;
this.elalpsedTime = elapsedTime;
}
}
private static TestResult benchmark(String filePath1, String filePath2, double humanRating, TestMethod rodent) {
// FILE READING
Path path1 = Paths.get(filePath1);
Path path2 = Paths.get(filePath2);
List<String> words1, words2, tmp;
try {
words1 = Arrays.asList(new String(Files.readAllBytes(path1), ENCODING).split(DELIMTER));
words2 = Arrays.asList(new String(Files.readAllBytes(path2), ENCODING).split(DELIMTER));
if(words1.size() < words2.size()){
// words1 should be the longer text
tmp = words1;
words1 = words2;
words2 = tmp;
}
}
catch(Exception ex){
System.out.println("[ERROR] " + ex.getMessage());
return null;
}
long t1, t2;
t1 = System.nanoTime();
double rating = rodent.rate(words1, words2);
t2 = System.nanoTime();
long msElapsed = (t2 - t1) / MS_TO_NS;
return new TestResult(rating, humanRating, msElapsed);
}
private static double roundUp(double number){
return Math.round(number * 100.0) / 100.0;
}
private static double getPearsonCorrelation(List<TestResult> sample) {
double meanX = 0.0, meanY = 0.0;
double xySum = 0.0, xSum = 0.0, ySum = 0.0;
for(TestResult point : sample){
meanX += point.x;
meanY += point.y;
}
meanX /= sample.size();
meanY /= sample.size();
for(TestResult point : sample){
xySum += (point.x - meanX) * (point.y - meanY);
xSum += Math.pow(point.x - meanX, 2);
ySum += Math.pow(point.y - meanY, 2);
}
return xySum / (Math.sqrt(xSum) * Math.sqrt(ySum));
}
public static void main(String[] args) {
Optional<Path> csvPath = Arrays.stream(args).filter(s -> !s.startsWith("-")).findFirst().map(str -> Paths.get(str));
if(!csvPath.isPresent()){
System.out.println("[ERROR] The location of the csv-file is required as an command-line-argument!");
return;
}
TestMethod rodent = new SSR(Paths.get("SSR/index"), Arrays.stream(args).anyMatch("--verbose"::equals));
List<TestResult> results = new ArrayList<TestResult>();
// ARGUMENTS
List<List<String>> lines = new ArrayList<>();
try (BufferedReader br = new BufferedReader(new FileReader(csvPath.get().toFile()))) {
String line;
while ((line = br.readLine()) != null) {
String[] cellValues = line.split(",");
lines.add(Arrays.asList(cellValues));
}
}
catch(Exception e) {
System.out.println("[ERROR] Could not read csv-file!");
}
Object[][] table = new Object[lines.size() + 1][4];
table[0] = new Object[] {"DOCUMENT PERMUTATION", "ALGORITHMIC OPINION", "HUMAN OPINION", "TIME"};
int row = 0;
for(List<String> cells : lines) {
String filePath1 = csvPath.get().getParent() + "/LeePincombeWelshDocuments_" + cells.get(1) + ".txt";
String filePath2 = csvPath.get().getParent() + "/LeePincombeWelshDocuments_" + cells.get(2) + ".txt";
double humanRating;
try {
humanRating = Double.parseDouble(cells.get(3)) / 5.0;
}
catch(NumberFormatException e) {
continue;
}
TestResult result = benchmark(filePath1, filePath2, humanRating, rodent);
results.add(result);
table[++row] = new Object[] {
String.format("%67s", Paths.get(filePath1).getFileName() + " | " + Paths.get(filePath2).getFileName()),
roundUp(result.x),
roundUp(result.y),
result.elalpsedTime + "ms"
};
}
if (Arrays.stream(args).anyMatch("--sort"::equals)){
Arrays.sort(table, new Comparator<Object[]>() {
@Override
public int compare(Object[] a, Object[] b){
double x, y;
try {
x = Math.abs((double)a[1] - (double)a[2]);
}
catch(Exception e){
return -1;
}
try {
y = Math.abs((double)b[1] - (double)b[2]);
}
catch(Exception e){
return 1;
}
return Double.compare(x, y);
}
});
}
final int tableCellSize = 50;
String tableFormat = "%" + tableCellSize + "s%" + tableCellSize + "s%" + tableCellSize + "s%" + tableCellSize + "s\n";
System.out.print(BOLD_TEXT);
System.out.format(tableFormat, table[0]);
System.out.print(NORMAL_TEXT);
System.out.println("");
for(int i = 1; i < lines.size(); i++){
System.out.format(tableFormat, table[i]);
}
System.out.print(BOLD_TEXT);
System.out.println("\nPEARSON CORRELATION = " + roundUp(getPearsonCorrelation(results)));
System.out.print(NORMAL_TEXT);
System.out.print("\033[0m");
System.out.println("\n[INFO] " + lines.size() + " tests completed!\n");
}
}