-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
SimpleCorpusEvaluator.java
243 lines (215 loc) · 9.61 KB
/
SimpleCorpusEvaluator.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
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
/* LanguageTool, a natural language style checker
* Copyright (C) 2015 Daniel Naber (http://www.danielnaber.de)
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
* USA
*/
package org.languagetool.dev.eval;
import org.apache.commons.lang.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.languagetool.JLanguageTool;
import org.languagetool.dev.errorcorpus.ErrorCorpus;
import org.languagetool.dev.errorcorpus.ErrorSentence;
import org.languagetool.dev.errorcorpus.SimpleCorpus;
import org.languagetool.language.English;
import org.languagetool.languagemodel.LuceneLanguageModel;
import org.languagetool.markup.AnnotatedText;
import org.languagetool.rules.Rule;
import org.languagetool.rules.RuleMatch;
import org.languagetool.rules.en.EnglishNgramProbabilityRule;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
/**
* Evaluates the ngram rule with a simple corpus, see {@link SimpleCorpus}.
* @since 3.2
*/
public class SimpleCorpusEvaluator {
private static final double START_THRESHOLD = 0.000001;
private static final double END_THRESHOLD = 0.00000000000000001;
private static final double STEP_FACTOR = 0.1;
private static EnglishNgramProbabilityRule probabilityRule;
private final Evaluator evaluator;
private final List<String> badConfusionMatchWords = new ArrayList<>();
private int sentenceCount;
private int errorsInCorpusCount;
private int perfectMatches;
private int goodMatches;
private int matchCount;
private int perfectConfusionMatches;
private int goodConfusionMatches;
private int badConfusionMatches;
public SimpleCorpusEvaluator(File indexTopDir) throws IOException {
evaluator = getEvaluator(indexTopDir);
}
@NotNull
private Evaluator getEvaluator(File indexTopDir) throws IOException {
return new NgramLanguageToolEvaluator(indexTopDir);
}
@NotNull
private ErrorCorpus getCorpus(File file) throws IOException {
return new SimpleCorpus(file);
}
void close() {
evaluator.close();
}
public PrecisionRecall run(File file, double threshold) throws IOException {
probabilityRule.setMinProbability(threshold);
System.out.println("Output explanation:");
System.out.println(" [ ] = this is not an expected error");
System.out.println(" [+ ] = this is an expected error");
System.out.println(" [++] = this is an expected error and the first suggestion is correct");
System.out.println(" [//] = not counted because already matches by a different rule");
System.out.println("");
checkLines(getCorpus(file));
return printAndResetResults();
}
private void checkLines(ErrorCorpus corpus) throws IOException {
for (ErrorSentence sentence : corpus) {
List<RuleMatch> matches = evaluator.check(sentence.getAnnotatedText());
sentenceCount++;
errorsInCorpusCount += sentence.getErrors().size();
System.out.println(sentence.getMarkupText() + " => " + matches.size());
for (RuleMatch match : matches) {
int length = match.getToPos() - match.getFromPos();
System.out.println(StringUtils.repeat(" ", match.getFromPos()) + StringUtils.repeat("^", length));
}
List<Span> detectedErrorPositions = new ArrayList<>();
for (RuleMatch match : matches) {
boolean alreadyCounted = errorAlreadyCounted(match, detectedErrorPositions);
if (!alreadyCounted && sentence.hasErrorCoveredByMatchAndGoodFirstSuggestion(match)) {
//TODO: it depends on the order of matches whether [++] comes before [ +] (it should!)
goodMatches++;
perfectMatches++;
matchCount++;
if (isConfusionRule(match)) {
perfectConfusionMatches++;
}
System.out.println(" [++] " + match + ": " + match.getSuggestedReplacements());
} else if (!alreadyCounted && sentence.hasErrorCoveredByMatch(match)) {
//} else if (!alreadyCounted && sentence.hasErrorOverlappingWithMatch(match)) {
goodMatches++;
matchCount++;
if (isConfusionRule(match)) {
goodConfusionMatches++;
}
System.out.println(" [+ ] " + match + ": " + match.getSuggestedReplacements());
} else if (alreadyCounted) {
System.out.println(" [//] " + match + ": " + match.getSuggestedReplacements());
} else {
System.out.println(" [ ] " + match + ": " + match.getSuggestedReplacements());
matchCount++;
if (isConfusionRule(match)) {
badConfusionMatches++;
badConfusionMatchWords.add(sentence.getMarkupText().substring(match.getFromPos(), match.getToPos()));
}
}
detectedErrorPositions.add(new Span(match.getFromPos(), match.getToPos()));
}
}
}
private boolean isConfusionRule(RuleMatch match) {
return match.getRule().getId().equals("CONFUSION_RULE");
}
private PrecisionRecall printAndResetResults() {
System.out.println("");
System.out.println(sentenceCount + " lines checked with " + errorsInCorpusCount + " errors.");
System.out.println("Confusion rule matches: " + perfectConfusionMatches+ " perfect, "
+ goodConfusionMatches + " good, " + badConfusionMatches + " bad (" + badConfusionMatchWords + ")");
System.out.println("\nCounting matches, no matter whether the first suggestion is correct:");
System.out.print(" " + goodMatches + " out of " + matchCount + " matches are real errors");
float precision = (float)goodMatches / matchCount;
float recall = (float)goodMatches / errorsInCorpusCount;
double fMeasure = FMeasure.getFMeasure(precision, recall, 1.0f);
System.out.printf(" => %.3f precision, %.3f recall, %.5f f-measure\n", precision, recall, fMeasure);
sentenceCount = 0;
errorsInCorpusCount = 0;
perfectMatches = 0;
goodMatches = 0;
matchCount = 0;
perfectConfusionMatches = 0;
goodConfusionMatches = 0;
badConfusionMatches = 0;
return new PrecisionRecall(precision, recall);
}
private boolean errorAlreadyCounted(RuleMatch match, List<Span> detectedErrorPositions) {
for (Span span : detectedErrorPositions) {
Span matchSpan = new Span(match.getFromPos(), match.getToPos());
if (span.covers(matchSpan) || matchSpan.covers(span)) {
return true;
}
}
return false;
}
public static void main(String[] args) throws IOException {
if (args.length != 2) {
System.out.println("Usage: " + SimpleCorpusEvaluator.class.getSimpleName() + " <corpusFile> <languageModelDir>");
System.out.println(" [languageModel] is a Lucene index directory with ngram frequency information");
System.exit(1);
}
File inputFile = new File(args[0]);
File languageModelTopDir = new File(args[1]);
System.out.println("Running with language model from " + languageModelTopDir);
SimpleCorpusEvaluator evaluator = new SimpleCorpusEvaluator(languageModelTopDir);
List<String> results = new ArrayList<>();
double threshold = START_THRESHOLD;
while (threshold >= END_THRESHOLD) {
PrecisionRecall res = evaluator.run(inputFile, threshold);
//String thresholdStr = String.format(Locale.ENGLISH, "%.20f", threshold);
String thresholdStr = StringUtils.rightPad(String.valueOf(threshold), 22);
double fMeasure = FMeasure.getFMeasure(res.getPrecision(), res.getRecall(), 1.0f);
String fMeasureStr = String.format(Locale.ENGLISH, "%.3f", fMeasure);
String precision = String.format(Locale.ENGLISH, "%.3f", res.getPrecision());
String recall = String.format(Locale.ENGLISH, "%.3f", res.getRecall());
results.add(thresholdStr + ": f=" + fMeasureStr + ", precision=" + precision + ", recall=" + recall);
threshold = threshold * STEP_FACTOR;
}
System.out.println("=== Results: ==================================");
for (String result : results) {
System.out.println(result);
}
evaluator.close();
}
static class NgramLanguageToolEvaluator implements Evaluator {
private final JLanguageTool langTool;
private final LuceneLanguageModel languageModel;
NgramLanguageToolEvaluator(File indexTopDir) throws IOException {
langTool = new JLanguageTool(new English());
disableAllRules();
languageModel = new LuceneLanguageModel(indexTopDir);
LuceneLanguageModel.clearCaches();
System.out.println("Using Lucene language model from " + languageModel);
probabilityRule = new EnglishNgramProbabilityRule(JLanguageTool.getMessageBundle(), languageModel, new English());
langTool.addRule(probabilityRule);
}
@Override
public void close() {
if (languageModel != null) {
languageModel.close();
}
}
private void disableAllRules() {
for (Rule rule : langTool.getAllActiveRules()) {
langTool.disableRule(rule.getId());
}
}
@Override
public List<RuleMatch> check(AnnotatedText annotatedText) throws IOException {
return langTool.check(annotatedText);
}
}
}