forked from languagetool-org/languagetool
/
AbstractPatternRule.java
404 lines (350 loc) · 11.1 KB
/
AbstractPatternRule.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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/* LanguageTool, a natural language style checker
* Copyright (C) 2008 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.rules.patterns;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.languagetool.AnalyzedSentence;
import org.languagetool.Experimental;
import org.languagetool.Language;
import org.languagetool.rules.ITSIssueType;
import org.languagetool.rules.Rule;
import org.languagetool.rules.RuleMatch;
import org.languagetool.tagging.disambiguation.rules.DisambiguationPatternRule;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
/**
* An Abstract Pattern Rule that describes a pattern of words or part-of-speech tags
* used for PatternRule and DisambiguationPatternRule.
* Introduced to minimize code duplication between those classes.
*
* @author Marcin Miłkowski
*/
public abstract class AbstractPatternRule extends Rule {
protected final Language language;
protected final List<PatternToken> patternTokens;
protected final boolean testUnification;
protected final boolean sentStart;
protected List<Match> suggestionMatches;
private boolean adjustSuggestionCase = true;
protected List<Match> suggestionMatchesOutMsg;
protected List<DisambiguationPatternRule> antiPatterns;
protected String subId; // because there can be more than one rule in a rule group
protected int startPositionCorrection;
protected int endPositionCorrection;
protected String suggestionsOutMsg; // extra suggestions outside message
protected RuleFilter filter;
protected String filterArgs;
protected String message;
protected String sourceFile = null;
protected RuleMatch.Type type = null; // allow setting custom match types without relying on IssueType
private int lineNumber = -1;
private final String id;
private final String description;
private final boolean getUnified;
private final boolean groupsOrUnification;
protected AbstractPatternRule(String id, String description, Language language) {
this(id, description, language, null, false);
}
public AbstractPatternRule(String id, String description, Language language, List<PatternToken> patternTokens, boolean getUnified, String message) {
this(id, description, language, patternTokens, getUnified);
this.message = message;
}
public AbstractPatternRule(String id, String description, Language language, List<PatternToken> patternTokens, boolean getUnified) {
this.id = Objects.requireNonNull(id, "id cannot be null");
this.description = Objects.requireNonNull(description, "description ('name' in XML) cannot be null ruleID: " + id);
this.language = Objects.requireNonNull(language, "language cannot be null");
this.getUnified = getUnified;
if (patternTokens != null) {
this.patternTokens = new ArrayList<>(patternTokens);
testUnification = initUnifier();
sentStart = this.patternTokens.size() > 0 && this.patternTokens.get(0).isSentenceStart();
if (!testUnification) {
boolean found = false;
for (PatternToken elem : this.patternTokens) {
if (elem.hasAndGroup()) {
found = true;
break;
}
}
groupsOrUnification = found;
} else {
groupsOrUnification = true;
}
} else {
this.patternTokens = null;
groupsOrUnification = false;
sentStart = false;
testUnification = false;
}
}
@Override
public boolean supportsLanguage(Language language) {
return language.equalsConsiderVariantsIfSpecified(this.language);
}
private boolean initUnifier() {
for (PatternToken pToken : patternTokens) {
if (pToken.isUnified()) {
return true;
}
}
return false;
}
@Override
public String toString() {
return id + "[" + subId + "]" + (sourceFile != null ? "@" + sourceFile : "" ) + ":" + patternTokens + ":" + description;
}
@Override
public String getDescription() {
return description;
}
@Nullable
public String getSourceFile() {
return sourceFile;
}
void setSourceFile(String sourceFile) {
this.sourceFile = sourceFile;
}
/**
* @see #getFullId()
*/
@Override
public String getId() {
return id;
}
@Override
public RuleMatch[] match(AnalyzedSentence sentence) throws IOException {
return null;
}
/**
* @since 2.3
*/
public final Language getLanguage() {
return language;
}
public final void setStartPositionCorrection(int startPositionCorrection) {
this.startPositionCorrection = startPositionCorrection;
}
public final int getStartPositionCorrection() {
return startPositionCorrection;
}
public final void setEndPositionCorrection(int endPositionCorrection) {
this.endPositionCorrection = endPositionCorrection;
}
public final int getEndPositionCorrection() {
return endPositionCorrection;
}
/**
* The rule id and its sub id, if any. The format is like {@code RULE_ID[SUB_ID]}, e.g. {@code WANT_TO[2]}.
* @since 3.2
* @see #getId()
*/
@Override
public String getFullId() {
if (subId != null) {
return id + "[" + subId + "]";
} else {
return id;
}
}
/**
* The rule id and its sub id, if any.
* @since 3.2
*/
public PatternRuleId getPatternRuleId() {
if (subId != null) {
return new PatternRuleId(id, subId);
} else {
return new PatternRuleId(id);
}
}
public final String getSubId() {
return subId;
}
public final void setSubId(String subId) {
this.subId = subId;
}
/**
* @since 2.3
*/
public boolean isGroupsOrUnification() {
return groupsOrUnification;
}
/**
* @since 2.3
*/
public boolean isGetUnified() {
return getUnified;
}
/**
* @since 2.3
*/
public boolean isSentStart() {
return sentStart;
}
/**
* @since 2.3
*/
public boolean isTestUnification() {
return testUnification;
}
/**
* @since 2.3
*/
public List<PatternToken> getPatternTokens() {
return patternTokens;
}
/** Add formatted suggestion elements. */
public final void addSuggestionMatch(Match m) {
if (suggestionMatches == null) {
suggestionMatches = new ArrayList<>(0);
}
suggestionMatches.add(m);
}
/** Add formatted suggestion elements outside message. */
public final void addSuggestionMatchOutMsg(Match m) {
if (suggestionMatchesOutMsg == null) {
suggestionMatchesOutMsg = new ArrayList<>(0);
}
suggestionMatchesOutMsg.add(m);
}
List<Match> getSuggestionMatches() {
return suggestionMatches == null ? Collections.emptyList() : suggestionMatches;
}
List<Match> getSuggestionMatchesOutMsg() {
return suggestionMatchesOutMsg == null ? Collections.emptyList() : suggestionMatchesOutMsg;
}
@NotNull
public final String getSuggestionsOutMsg() {
return suggestionsOutMsg;
}
/**
* Get the message shown to the user if this rule matches.
*/
public final String getMessage() {
return message;
}
/**
* Set the message shown to the user if this rule matches.
*/
public final void setMessage(String message) {
this.message = message;
}
/** @since 2.7 (public since 3.2) */
public void setFilter(RuleFilter filter) {
this.filter = filter;
}
/** @since 2.7 (public since 3.2) */
@Nullable
public RuleFilter getFilter() {
return filter;
}
/** @since 2.7 (public since 3.2) */
public void setFilterArguments(String filterArgs) {
this.filterArgs = filterArgs;
}
/** @since 2.7 (public since 3.2) */
@Nullable
public String getFilterArguments() {
return filterArgs;
}
/**
* Set up the list of antipatterns used to immunize tokens, i.e., make them
* non-matchable by the current rule. Useful for multi-word complex exceptions,
* such as multi-word idiomatic expressions.
* @param antiPatterns A list of antiPatterns, implemented as {@code DisambiguationPatternRule}.
* @since 2.5
*/
public void setAntiPatterns(List<DisambiguationPatternRule> antiPatterns) {
if (this.antiPatterns == null) {
this.antiPatterns = new ArrayList<>(0);
}
this.antiPatterns.addAll(antiPatterns);
}
/**
* @since 3.1
*/
@Override
public final List<DisambiguationPatternRule> getAntiPatterns() {
return antiPatterns == null ? Collections.emptyList() : Collections.unmodifiableList(antiPatterns);
}
/**
* @return String of short message as specified in <short>...</short>
* @since 4.4
*/
String getShortMessage() {
return StringUtils.EMPTY;
}
/**
* Determines the match type, based on the type variable if set (to allow overriding) or {@link org.languagetool.rules.Rule#getLocQualityIssueType()}
* @since 5.7
* @return The match type for the matches created by this rule
*/
@Nullable
@Experimental
public RuleMatch.Type getType() {
if (type == null) {
ITSIssueType issueType = getLocQualityIssueType();
if (issueType == ITSIssueType.Style || issueType == ITSIssueType.LocaleViolation || issueType == ITSIssueType.Register) {
// interpret the issue type - this is what the clients have done so far before there was RuleMatch.Type
return RuleMatch.Type.Hint;
} else {
// default type as defined in RuleMatch
return RuleMatch.Type.Other;
}
}
return type;
}
/**
* Allows overriding the match type, otherwise determined by {@link org.languagetool.rules.Rule#getLocQualityIssueType()}
* @since 5.7
* @param type the desired match type
*/
@Experimental
public void setType(RuleMatch.Type type) {
this.type = type;
}
/**
* Allows adjusting the behavior of uppercasing suggestions when the matched text started with an upper-case letter
*/
@Experimental
public boolean isAdjustSuggestionCase() {
return adjustSuggestionCase;
}
/**
* Allows adjusting the behavior of uppercasing suggestions when the matched text started with an upper-case letter
*/
@Experimental
public void setAdjustSuggestionCase(boolean adjustSuggestionCase) {
this.adjustSuggestionCase = adjustSuggestionCase;
}
public void setXmlLineNumber(int lineNumber) {
this.lineNumber = lineNumber;
}
/**
* Returns the line in the XML file where this rule is located. Will return <code>-1</code>
* if the line isn't known.
*/
public int getXmlLineNumber() {
return this.lineNumber;
}
}