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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
|
/* LanguageTool, a natural language style checker
* Copyright (C) 2006 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 de.danielnaber.languagetool.rules.patterns;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.xml.sax.Attributes;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;
import de.danielnaber.languagetool.Language;
import de.danielnaber.languagetool.rules.IncorrectExample;
import de.danielnaber.languagetool.tools.StringTools;
/**
* XML rule handler that loads rules from XML and throws
* exceptions on errors and warnings.
*
* @author Daniel Naber
*/
public class XMLRuleHandler extends DefaultHandler {
public XMLRuleHandler() {
elementList = new ArrayList<Element>();
equivalenceFeatures = new HashMap<String, List<String>>();
uTypeList = new ArrayList<String>();
}
List<PatternRule> rules = new ArrayList<PatternRule>();
protected Language language;
protected StringBuilder correctExample = new StringBuilder();
protected StringBuilder incorrectExample = new StringBuilder();
protected StringBuilder exampleCorrection = new StringBuilder();
protected StringBuilder message = new StringBuilder();
protected StringBuilder match = new StringBuilder();
protected StringBuilder elements;
protected StringBuilder exceptions;
List<String> correctExamples = new ArrayList<String>();
List<IncorrectExample> incorrectExamples = new ArrayList<IncorrectExample>();
protected boolean inPattern;
protected boolean inCorrectExample;
protected boolean inIncorrectExample;
protected boolean inMessage;
protected boolean inSuggestion;
protected boolean inMatch;
protected boolean inRuleGroup;
protected boolean inToken;
protected boolean inException;
protected boolean inPhrases;
protected boolean inAndGroup;
protected boolean tokenSpaceBefore;
protected boolean tokenSpaceBeforeSet;
protected String posToken;
protected boolean posNegation;
protected boolean posRegExp;
protected boolean caseSensitive;
protected boolean regExpression;
protected boolean tokenNegated;
protected boolean tokenInflected;
protected String exceptionPosToken;
protected boolean exceptionStringRegExp;
protected boolean exceptionStringNegation;
protected boolean exceptionStringInflected;
protected boolean exceptionPosNegation;
protected boolean exceptionPosRegExp;
protected boolean exceptionValidNext;
protected boolean exceptionValidPrev;
protected boolean exceptionSet;
protected boolean exceptionSpaceBefore;
protected boolean exceptionSpaceBeforeSet;
/** List of elements as specified by tokens. **/
protected List<Element> elementList;
/** true when phraseref is the last element in the rule. **/
protected boolean lastPhrase;
/** ID reference to the phrase. **/
protected String phraseIdRef;
/** Current phrase ID. **/
protected String phraseId;
protected int skipPos;
protected String ruleGroupId;
protected String id;
protected Element tokenElement;
protected Match tokenReference;
protected List<Match> suggestionMatches;
protected Locator pLocator;
protected int startPositionCorrection;
protected int endPositionCorrection;
protected int tokenCounter;
/** Phrase store - elementLists keyed by phraseIds. **/
protected Map<String, List<List<Element>>> phraseMap;
/**
* Logically forking element list, used for including multiple phrases in the
* current one.
**/
protected List<ArrayList<Element>> phraseElementList;
protected int andGroupCounter;
protected StringBuilder shortMessage = new StringBuilder();
protected boolean inShortMessage;
protected boolean inUnification;
protected boolean inUnificationDef;
protected boolean uniNegation;
protected String uFeature;
protected String uType = "";
protected List<String> uTypeList;
protected Map<String, List<String>> equivalenceFeatures;
/** Definitions of values in XML files. */
protected static final String YES = "yes";
protected static final String POSTAG = "postag";
protected static final String POSTAG_REGEXP = "postag_regexp";
protected static final String REGEXP = "regexp";
protected static final String NEGATE = "negate";
protected static final String INFLECTED = "inflected";
protected static final String NEGATE_POS = "negate_pos";
protected static final String MARKER = "marker";
protected static final String DEFAULT = "default";
protected static final String TYPE = "type";
protected static final String SPACEBEFORE = "spacebefore";
protected static final String EXAMPLE = "example";
protected static final String SCOPE = "scope";
protected static final String IGNORE = "ignore";
protected static final String SKIP = "skip";
protected static final String TOKEN = "token";
protected static final String FEATURE = "feature";
protected static final String UNIFY = "unify";
protected static final String AND = "and";
protected static final String EXCEPTION = "exception";
protected static final String CASE_SENSITIVE = "case_sensitive";
protected static final String PATTERN = "pattern";
protected static final String MATCH = "match";
protected static final String UNIFICATION = "unification";
protected static final String RULEGROUP = "rulegroup";
protected static final String NO = "no";
protected static final String MARK_TO = "mark_to";
protected static final String MARK_FROM = "mark_from";
protected static final String PHRASES = "phrases";
protected static final String MESSAGE = "message";
public List<PatternRule> getRules() {
return rules;
}
public void warning (final SAXParseException e) throws SAXException {
throw e;
}
public void error (final SAXParseException e) throws SAXException {
throw e;
}
@Override
public void setDocumentLocator(final Locator locator) {
pLocator = locator;
super.setDocumentLocator(locator);
}
protected void resetToken() {
posNegation = false;
posRegExp = false;
inToken = false;
tokenSpaceBefore = false;
tokenSpaceBeforeSet = false;
resetException();
exceptionSet = false;
tokenReference = null;
}
protected void resetException() {
exceptionStringNegation = false;
exceptionStringInflected = false;
exceptionPosNegation = false;
exceptionPosRegExp = false;
exceptionStringRegExp = false;
exceptionValidNext = false;
exceptionValidPrev = false;
exceptionSpaceBefore = false;
exceptionSpaceBeforeSet = false;
}
protected void phraseElementInit() {
// lazy init
if (phraseElementList == null) {
phraseElementList = new ArrayList<ArrayList<Element>>();
}
}
protected void preparePhrase(final Attributes attrs) {
phraseIdRef = attrs.getValue("idref");
if (phraseMap.containsKey(phraseIdRef)) {
for (final List<Element> curPhrEl : phraseMap.get(phraseIdRef)) {
for (final Element e : curPhrEl) {
e.setPhraseName(phraseIdRef);
}
if (elementList.isEmpty()) {
phraseElementList.add(new ArrayList<Element>(curPhrEl));
} else {
final ArrayList<Element> prevList = new ArrayList<Element>(
elementList);
prevList.addAll(curPhrEl);
phraseElementList.add(new ArrayList<Element>(prevList));
prevList.clear();
}
}
lastPhrase = true;
}
}
protected void finalizePhrase() {
// lazy init
if (phraseMap == null) {
phraseMap = new HashMap<String, List<List<Element>>>();
}
phraseElementInit();
if (phraseElementList.isEmpty()) {
phraseElementList.add(new ArrayList<Element>(elementList));
} else {
for (final ArrayList<Element> ph : phraseElementList) {
ph.addAll(new ArrayList<Element>(elementList));
}
}
phraseMap.put(phraseId, new ArrayList<List<Element>>(phraseElementList));
elementList.clear();
phraseElementList.clear();
}
protected void startPattern(final Attributes attrs) throws SAXException {
inPattern = true;
if (attrs.getValue(MARK_FROM) != null) {
startPositionCorrection = Integer.parseInt(attrs.getValue(MARK_FROM));
}
if (attrs.getValue(MARK_TO) != null) {
endPositionCorrection = Integer.parseInt(attrs.getValue(MARK_TO));
if (endPositionCorrection > 0) {
throw new SAXException("End position correction (mark_to="+ endPositionCorrection
+ ") cannot be larger than 0: " + "\n Line: "
+ pLocator.getLineNumber() + ", column: "
+ pLocator.getColumnNumber() + ".");
}
}
caseSensitive = YES.equals(attrs.getValue(CASE_SENSITIVE));
}
/**
* Calculates the offset of the match reference (if any) in case the match
* element has been used in the group.
*
* @param elList
* Element list where the match element was used. It is directly changed.
*/
protected void processElement(final List<Element> elList) {
int counter = 0;
for (final Element elTest : elList) {
if (elTest.getPhraseName() != null && counter > 0) {
if (elTest.isReferenceElement()) {
final int tokRef = elTest.getMatch().getTokenRef();
elTest.getMatch().setTokenRef(tokRef + counter - 1);
final String offsetToken = elTest.getString().replace("\\" + tokRef,
"\\" + (tokRef + counter - 1));
elTest.setStringElement(offsetToken);
}
}
counter++;
}
}
protected void setMatchElement(final Attributes attrs) throws SAXException {
inMatch = true;
match = new StringBuilder();
Match.CaseConversion caseConversion = Match.CaseConversion.NONE;
if (attrs.getValue("case_conversion") != null) {
caseConversion = Match.CaseConversion.toCase(attrs
.getValue("case_conversion").toUpperCase());
}
Match.IncludeRange includeRange = Match.IncludeRange.NONE;
if (attrs.getValue("include_skipped") != null) {
includeRange = Match.IncludeRange.toRange(attrs
.getValue("include_skipped").toUpperCase());
}
final Match mWorker = new Match(attrs.getValue(POSTAG), attrs
.getValue("postag_replace"), YES
.equals(attrs.getValue(POSTAG_REGEXP)), attrs
.getValue("regexp_match"), attrs.getValue("regexp_replace"),
caseConversion, YES.equals(attrs.getValue("setpos")),
includeRange);
mWorker.setInMessageOnly(!inSuggestion);
if (inMessage) {
if (suggestionMatches == null) {
suggestionMatches = new ArrayList<Match>();
}
suggestionMatches.add(mWorker);
//add incorrect XML character for simplicity
message.append("\u0001\\");
message.append(attrs.getValue("no"));
if (StringTools.isEmpty(attrs.getValue("no"))) {
throw new SAXException("References cannot be empty: " + "\n Line: "
+ pLocator.getLineNumber() + ", column: "
+ pLocator.getColumnNumber() + ".");
} else if (Integer.parseInt(attrs.getValue("no")) < 1) {
throw new SAXException("References must be larger than 0: "
+ attrs.getValue("no") + "\n Line: " + pLocator.getLineNumber()
+ ", column: " + pLocator.getColumnNumber() + ".");
}
} else if (inToken && attrs.getValue("no") != null) {
final int refNumber = Integer.parseInt(attrs.getValue("no"));
if (refNumber > elementList.size()) {
throw new SAXException(
"Only backward references in match elements are possible, tried to specify token "
+ refNumber
+ "\n Line: "
+ pLocator.getLineNumber()
+ ", column: " + pLocator.getColumnNumber() + ".");
}
mWorker.setTokenRef(refNumber);
tokenReference = mWorker;
elements.append('\\');
elements.append(refNumber);
}
}
protected void setExceptions(final Attributes attrs) {
inException = true;
exceptions = new StringBuilder();
resetException();
exceptionStringNegation = YES.equals(attrs.getValue(NEGATE));
exceptionValidNext = "next".equals(attrs.getValue(SCOPE));
exceptionValidPrev = "previous".equals(attrs.getValue(SCOPE));
exceptionStringInflected = YES.equals(attrs.getValue(INFLECTED));
if (attrs.getValue(POSTAG) != null) {
exceptionPosToken = attrs.getValue(POSTAG);
exceptionPosRegExp = YES.equals(attrs.getValue(POSTAG_REGEXP));
exceptionPosNegation = YES.equals(attrs.getValue(NEGATE_POS));
}
exceptionStringRegExp = YES.equals(attrs.getValue(REGEXP));
if (attrs.getValue(SPACEBEFORE) != null) {
exceptionSpaceBefore = YES.equals(attrs.getValue(SPACEBEFORE));
exceptionSpaceBeforeSet = !"ignore".equals(attrs.getValue(SPACEBEFORE));
}
}
protected void finalizeExceptions() {
inException = false;
if (!exceptionSet) {
tokenElement = new Element(StringTools.trimWhitespace(elements
.toString()), caseSensitive, regExpression, tokenInflected);
exceptionSet = true;
}
tokenElement.setNegation(tokenNegated);
if (!StringTools.isEmpty(exceptions.toString())) {
tokenElement.setStringException(StringTools.trimWhitespace(exceptions
.toString()), exceptionStringRegExp, exceptionStringInflected,
exceptionStringNegation, exceptionValidNext, exceptionValidPrev);
}
if (exceptionPosToken != null) {
tokenElement.setPosException(exceptionPosToken, exceptionPosRegExp,
exceptionPosNegation, exceptionValidNext, exceptionValidPrev);
exceptionPosToken = null;
}
if (exceptionSpaceBeforeSet) {
tokenElement.setExceptionSpaceBefore(exceptionSpaceBefore);
}
resetException();
}
protected void setToken(final Attributes attrs) {
inToken = true;
if (lastPhrase) {
elementList.clear();
}
lastPhrase = false;
tokenNegated = YES.equals(attrs.getValue(NEGATE));
tokenInflected = YES.equals(attrs.getValue(INFLECTED));
if (attrs.getValue("skip") != null) {
skipPos = Integer.parseInt(attrs.getValue("skip"));
}
elements = new StringBuilder();
// POSElement creation
if (attrs.getValue(POSTAG) != null) {
posToken = attrs.getValue(POSTAG);
posRegExp = YES.equals(attrs.getValue(POSTAG_REGEXP));
posNegation = YES.equals(attrs.getValue(NEGATE_POS));
}
regExpression = YES.equals(attrs.getValue(REGEXP));
if (attrs.getValue(SPACEBEFORE) != null) {
tokenSpaceBefore = YES.equals(attrs.getValue(SPACEBEFORE));
tokenSpaceBeforeSet = !"ignore".equals(attrs.getValue(SPACEBEFORE));
}
if (!inAndGroup) {
tokenCounter++;
}
}
protected void checkPositions(final int add) throws SAXException {
if (startPositionCorrection >= tokenCounter + add) {
throw new SAXException(
"Attempt to mark a token no. ("+ startPositionCorrection +") that is outside the pattern ("
+ tokenCounter + "). Pattern elements are numbered starting from 0!" + "\n Line: "
+ pLocator.getLineNumber() + ", column: "
+ pLocator.getColumnNumber() + ".");
}
if (tokenCounter +add - endPositionCorrection < 0) {
throw new SAXException(
"Attempt to mark a token no. ("+ endPositionCorrection +") that is outside the pattern ("
+ tokenCounter + " elements). End positions should be negative but not larger than the token count!"
+ "\n Line: "
+ pLocator.getLineNumber() + ", column: "
+ pLocator.getColumnNumber() + ".");
}
}
protected void checkMarkPositions() {
if (phraseElementList == null || phraseElementList.size() == 0) {
final int endMarker = elementList.size() + endPositionCorrection;
if (endMarker <= startPositionCorrection) {
throw new RuntimeException("Invalid combination of mark_from (" + startPositionCorrection
+ ") and mark_to (" + endPositionCorrection + ") for rule " + id
+ " with " + elementList.size()
+ " tokens: the error position created by mark_from and mark_to is less than one token");
}
}
}
/**
* Adds Match objects for all references to tokens
* (including '\1' and the like).
*/
protected List<Match> addLegacyMatches() {
if (suggestionMatches == null || suggestionMatches.isEmpty()) {
return null;
}
final List<Match> sugMatch = new ArrayList<Match>();
final String messageStr = message.toString();
int pos = 0;
int ind = 0;
int matchCounter = 0;
while (pos != -1) {
pos = messageStr.indexOf('\\', ind + 1);
if (pos != -1 && messageStr.length() > pos) {
if (Character.isDigit(messageStr.charAt(pos + 1))) {
if (pos == 1 || messageStr.charAt(pos - 1) != '\u0001') {
final Match mWorker = new Match(null, null, false, null,
null, Match.CaseConversion.NONE, false, Match.IncludeRange.NONE);
mWorker.setInMessageOnly(true);
sugMatch.add(mWorker);
} else if (messageStr.charAt(pos - 1) == '\u0001') { // real suggestion marker
sugMatch.add(suggestionMatches.get(matchCounter));
message.deleteCharAt(pos - 1 - matchCounter);
matchCounter++;
}
}
}
ind = pos;
}
if (sugMatch.isEmpty()) {
return suggestionMatches;
}
return sugMatch;
}
protected void finalizeTokens() {
if (!exceptionSet || tokenElement == null) {
tokenElement = new Element(StringTools.trimWhitespace(elements
.toString()), caseSensitive, regExpression, tokenInflected);
tokenElement.setNegation(tokenNegated);
} else {
tokenElement.setStringElement(StringTools.trimWhitespace(elements
.toString()));
}
if (skipPos != 0) {
tokenElement.setSkipNext(skipPos);
skipPos = 0;
}
if (posToken != null) {
tokenElement.setPosElement(posToken, posRegExp, posNegation);
posToken = null;
}
if (tokenReference != null) {
tokenElement.setMatch(tokenReference);
}
if (inAndGroup && andGroupCounter > 0) {
elementList.get(elementList.size() - 1)
.setAndGroupElement(tokenElement);
} else {
elementList.add(tokenElement);
}
if (inAndGroup) {
andGroupCounter++;
}
if (inUnification) {
tokenElement.setUnification(equivalenceFeatures);
if (uniNegation) {
tokenElement.setUniNegation();
}
}
if (inUnificationDef) {
language.getUnifier().setEquivalence(uFeature, uType, tokenElement);
elementList.clear();
}
if (tokenSpaceBeforeSet) {
tokenElement.setWhitespaceBefore(tokenSpaceBefore);
}
resetToken();
}
}
|