/
ReplaceOperationNamesRule.java
205 lines (173 loc) · 6.5 KB
/
ReplaceOperationNamesRule.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
/* LanguageTool, a natural language style checker
* Copyright (C) 2005 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.ca;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.languagetool.AnalyzedSentence;
import org.languagetool.AnalyzedToken;
import org.languagetool.AnalyzedTokenReadings;
import org.languagetool.Language;
import org.languagetool.rules.*;
import org.languagetool.synthesis.ca.CatalanSynthesizer;
/**
* A rule that suggests better names for technical operation names
*
* Loads the relevant words from <code>/ca/replace_operationnames.txt</code>.
*
* @author Jaume Ortolà
*/
public class ReplaceOperationNamesRule extends AbstractSimpleReplaceRule {
private static final Map<String, List<String>> wrongWords = loadFromPath("/ca/replace_operationnames.txt");
private static final Locale CA_LOCALE = new Locale("CA");
@Override
public Map<String, List<String>> getWrongWords() {
return wrongWords;
}
private CatalanSynthesizer synth;
private static final Pattern PrevToken_POS = Pattern.compile("D[^R].*|PX.*|SPS00|SENT_START");
private static final Pattern PrevToken_POS_Excep = Pattern.compile("RG_anteposat|N.*|CC|_PUNCT.*|_loc_unavegada|RN");
private static final Pattern NextToken_POS_Excep = Pattern.compile("N.*");
private static final Pattern PUNTUACIO = Pattern.compile("PUNCT.*|SENT_START");
private static final Pattern DETERMINANT = Pattern.compile("D[^R].M.*");
public ReplaceOperationNamesRule(final ResourceBundle messages, Language language) throws IOException {
super(messages, language);
super.setLocQualityIssueType(ITSIssueType.Style);
super.setCategory(new Category(new CategoryId("FORMES_SECUNDARIES"), "C8) Formes secundàries"));
synth = (CatalanSynthesizer) language.getSynthesizer();
super.useSubRuleSpecificIds();
}
@Override
public final String getId() {
return "NOMS_OPERACIONS";
}
@Override
public String getDescription() {
return "S'ha d'evitar com a nom d'operació tècnica: $match";
}
@Override
public String getShort() {
return "Forma preferible";
}
@Override
public String getMessage(String tokenStr, List<String> replacements) {
return "Si és el nom d'una operació tècnica, val més usar una altra forma.";
}
@Override
public Locale getLocale() {
return CA_LOCALE;
}
@Override
public boolean isCaseSensitive() {
return false;
}
@Override
public final RuleMatch[] match(final AnalyzedSentence sentence) {
List<RuleMatch> ruleMatches = new ArrayList<>();
AnalyzedTokenReadings[] tokens = sentence.getTokensWithoutWhitespace();
loop: for (int i=1; i<tokens.length; i++) {
List<String> replacementLemmas = null;
String token = tokens[i].getToken().toLowerCase();
if (token.length()>3 && token.endsWith("s")) {
token = token.substring(0, token.length() - 1);
}
if (wrongWords.containsKey(token)) {
replacementLemmas = wrongWords.get(token);
} else {
continue loop;
}
// exceptions
if (token.equals("duplicat") && tokens[i-1].getToken().equalsIgnoreCase("per")) {
continue loop;
}
// Assecat el braç del riu
if (i + 1 < tokens.length &&
matchPostagRegexp(tokens[i - 1], PUNTUACIO) &&
matchPostagRegexp(tokens[i + 1], DETERMINANT)) {
continue loop;
}
// relevant token
if (tokens[i].hasPosTag("_GV_")) {
continue loop;
}
// next token
if (i + 1 < tokens.length
&& (tokens[i + 1].hasLemma("per") || tokens[i + 1].hasLemma("com")
|| tokens[i + 1].hasLemma("des") || tokens[i + 1].hasLemma("amb")
|| matchPostagRegexp(tokens[i + 1], NextToken_POS_Excep))) {
continue loop;
}
// prev token
if (!matchPostagRegexp(tokens[i - 1], PrevToken_POS)
|| matchPostagRegexp(tokens[i - 1], PrevToken_POS_Excep)) {
continue loop;
}
// The rule matches!
//synthesize replacements
if (replacementLemmas != null) {
List<String> possibleReplacements = new ArrayList<>();
String[] synthesized = null;
if (!tokens[i].getToken().toLowerCase().endsWith("s")) {
possibleReplacements.addAll(replacementLemmas);
} else {
//synthesize plural
for (String replacementLemma : replacementLemmas) {
try {
synthesized = synth.synthesize(new AnalyzedToken(
replacementLemma, "NCMS000", replacementLemma), "NC.P.*");
} catch (IOException e) {
throw new RuntimeException("Could not synthesize: "
+ replacementLemma + " with tag NC.P.*.", e);
}
possibleReplacements.addAll(Arrays.asList(synthesized));
}
}
if (possibleReplacements.size() > 0) {
RuleMatch potentialRuleMatch = createRuleMatch(tokens[i],possibleReplacements, sentence, token);
ruleMatches.add(potentialRuleMatch);
}
}
}
return toRuleMatchArray(ruleMatches);
}
/**
* Match POS tag with regular expression
*/
private boolean matchPostagRegexp(AnalyzedTokenReadings aToken, Pattern pattern) {
boolean matches = false;
for (AnalyzedToken analyzedToken : aToken) {
String posTag = analyzedToken.getPOSTag();
if (posTag == null) {
posTag = "UNKNOWN";
}
final Matcher m = pattern.matcher(posTag);
if (m.matches()) {
matches = true;
break;
}
}
return matches;
}
}