-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
German.java
300 lines (273 loc) · 10.2 KB
/
German.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
/* LanguageTool, a natural language style checker
* Copyright (C) 2007 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.language;
import org.jetbrains.annotations.NotNull;
import org.languagetool.Language;
import org.languagetool.LanguageMaintainedState;
import org.languagetool.UserConfig;
import org.languagetool.chunking.Chunker;
import org.languagetool.chunking.GermanChunker;
import org.languagetool.languagemodel.LanguageModel;
import org.languagetool.languagemodel.LuceneLanguageModel;
import org.languagetool.rules.*;
import org.languagetool.rules.de.*;
import org.languagetool.rules.de.LongSentenceRule;
import org.languagetool.rules.de.SentenceWhitespaceRule;
import org.languagetool.rules.neuralnetwork.NeuralNetworkRuleCreator;
import org.languagetool.rules.neuralnetwork.Word2VecModel;
import org.languagetool.synthesis.GermanSynthesizer;
import org.languagetool.synthesis.Synthesizer;
import org.languagetool.tagging.Tagger;
import org.languagetool.tagging.de.GermanTagger;
import org.languagetool.tagging.disambiguation.Disambiguator;
import org.languagetool.tagging.disambiguation.rules.de.GermanRuleDisambiguator;
import org.languagetool.tokenizers.CompoundWordTokenizer;
import org.languagetool.tokenizers.SRXSentenceTokenizer;
import org.languagetool.tokenizers.SentenceTokenizer;
import org.languagetool.tokenizers.de.GermanCompoundTokenizer;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.ResourceBundle;
/**
* Support for German - use the sub classes {@link GermanyGerman}, {@link SwissGerman}, or {@link AustrianGerman}
* if you need spell checking.
*/
public class German extends Language implements AutoCloseable {
private static final Language GERMANY_GERMAN = new GermanyGerman();
private Tagger tagger;
private Synthesizer synthesizer;
private SentenceTokenizer sentenceTokenizer;
private Disambiguator disambiguator;
private GermanChunker chunker;
private CompoundWordTokenizer compoundTokenizer;
private GermanCompoundTokenizer strictCompoundTokenizer;
private LanguageModel languageModel;
private List<Rule> nnRules;
private Word2VecModel word2VecModel;
/**
* @deprecated use {@link GermanyGerman}, {@link AustrianGerman}, or {@link SwissGerman} instead -
* they have rules for spell checking, this class doesn't (deprecated since 3.2)
*/
@Deprecated
public German() {
}
@Override
public Language getDefaultLanguageVariant() {
return GERMANY_GERMAN;
}
@Override
public Disambiguator getDisambiguator() {
if (disambiguator == null) {
disambiguator = new GermanRuleDisambiguator();
}
return disambiguator;
}
/**
* @since 2.9
*/
@Override
public Chunker getPostDisambiguationChunker() {
if (chunker == null) {
chunker = new GermanChunker();
}
return chunker;
}
@Override
public String getName() {
return "German";
}
@Override
public String getShortCode() {
return "de";
}
@Override
public String[] getCountries() {
return new String[]{"LU", "LI", "BE"};
}
@Override
public Tagger getTagger() {
Tagger t = tagger;
if (t == null) {
synchronized (this) {
t = tagger;
if (t == null) {
tagger = t = new GermanTagger();
}
}
}
return t;
}
@Override
@NotNull
public Synthesizer getSynthesizer() {
if (synthesizer == null) {
synthesizer = new GermanSynthesizer();
}
return synthesizer;
}
@Override
public SentenceTokenizer getSentenceTokenizer() {
if (sentenceTokenizer == null) {
sentenceTokenizer = new SRXSentenceTokenizer(this);
}
return sentenceTokenizer;
}
@Override
public Contributor[] getMaintainers() {
return new Contributor[] {
new Contributor("Jan Schreiber"),
Contributors.DANIEL_NABER,
};
}
@Override
public List<Rule> getRelevantRules(ResourceBundle messages, UserConfig userConfig) throws IOException {
return Arrays.asList(
new CommaWhitespaceRule(messages,
Example.wrong("Die Partei<marker> ,</marker> die die letzte Wahl gewann."),
Example.fixed("Die Partei<marker>,</marker> die die letzte Wahl gewann.")),
new GenericUnpairedBracketsRule(messages,
Arrays.asList("[", "(", "{", "„", "»", "«", "\""),
Arrays.asList("]", ")", "}", "“", "«", "»", "\"")),
new UppercaseSentenceStartRule(messages, this,
Example.wrong("Das Haus ist alt. <marker>es</marker> wurde 1950 gebaut."),
Example.fixed("Das Haus ist alt. <marker>Es</marker> wurde 1950 gebaut.")),
new MultipleWhitespaceRule(messages, this),
// specific to German:
new OldSpellingRule(messages),
new SentenceWhitespaceRule(messages),
new GermanDoublePunctuationRule(messages),
new MissingVerbRule(messages, this),
new GermanWordRepeatRule(messages, this),
new GermanWordRepeatBeginningRule(messages, this),
new GermanWrongWordInContextRule(messages),
new AgreementRule(messages, this),
new CaseRule(messages, this),
new CompoundRule(messages),
new DashRule(messages),
new VerbAgreementRule(messages, this),
new SubjectVerbAgreementRule(messages, this),
new WordCoherencyRule(messages),
new SimilarNameRule(messages),
new WiederVsWiderRule(messages),
new WhiteSpaceBeforeParagraphEnd(messages, this),
new WhiteSpaceAtBeginOfParagraph(messages),
new EmptyLineRule(messages, this),
new GermanStyleRepeatedWordRule(messages, userConfig),
new CompoundCoherencyRule(messages),
new LongSentenceRule(messages, userConfig),
new LongParagraphRule(messages, this, userConfig),
new GermanFillerWordsRule(messages, this, userConfig),
new GermanParagraphRepeatBeginningRule(messages, this),
new PunctuationMarkAtParagraphEnd(messages, this),
new DuUpperLowerCaseRule(messages),
new UnitConversionRule(messages)
);
}
/**
* @since 2.7
*/
public CompoundWordTokenizer getNonStrictCompoundSplitter() {
if (compoundTokenizer == null) {
try {
GermanCompoundTokenizer tokenizer = new GermanCompoundTokenizer(false); // there's a spelling mistake in (at least) one part, so strict mode wouldn't split the word
compoundTokenizer = word -> new ArrayList<>(tokenizer.tokenize(word));
} catch (IOException e) {
throw new RuntimeException("Could not set up German compound splitter", e);
}
}
return compoundTokenizer;
}
/**
* @since 2.7
*/
public GermanCompoundTokenizer getStrictCompoundTokenizer() {
if (strictCompoundTokenizer == null) {
try {
strictCompoundTokenizer = new GermanCompoundTokenizer();
} catch (IOException e) {
throw new RuntimeException("Could not set up strict German compound splitter", e);
}
}
return strictCompoundTokenizer;
}
@Override
public synchronized LanguageModel getLanguageModel(File indexDir) throws IOException {
if (languageModel == null) {
languageModel = new LuceneLanguageModel(new File(indexDir, getShortCode()));
// for testing:
//languageModel = new BerkeleyRawLanguageModel(new File("/media/Data/berkeleylm/google_books_binaries/ger.blm.gz"));
//languageModel = new BerkeleyLanguageModel(new File("/media/Data/berkeleylm/google_books_binaries/ger.blm.gz"));
}
return languageModel;
}
/** @since 4.0 */
@Override
public synchronized Word2VecModel getWord2VecModel(File indexDir) throws IOException {
if (word2VecModel == null) {
word2VecModel = new Word2VecModel(indexDir + File.separator + getShortCode());
}
return word2VecModel;
}
/** @since 3.1 */
@Override
public List<Rule> getRelevantLanguageModelRules(ResourceBundle messages, LanguageModel languageModel) throws IOException {
return Arrays.asList(
new GermanConfusionProbabilityRule(messages, languageModel, this),
new ProhibitedCompoundRule(messages, languageModel)
);
}
/** @since 4.0 */
@Override
public List<Rule> getRelevantWord2VecModelRules(ResourceBundle messages, Word2VecModel word2vecModel) throws IOException {
if (nnRules == null) {
nnRules = NeuralNetworkRuleCreator.createRules(messages, this, word2vecModel);
}
return nnRules;
}
/**
* Closes the language model, if any.
* @since 3.1
*/
@Override
public void close() throws Exception {
if (languageModel != null) {
languageModel.close();
}
}
@Override
public LanguageMaintainedState getMaintainedState() {
return LanguageMaintainedState.ActivelyMaintained;
}
@Override
public int getPriorityForId(String id) {
switch (id) {
case "OLD_SPELLING_INTERNAL": return 10;
case "DE_PROHIBITED_COMPOUNDS": return 1; // a more detailed error message than from spell checker
case "ANS_OHNE_APOSTROPH": return 1;
case "CONFUSION_RULE": return -1; // probably less specific than the rules from grammar.xml
case "AKZENT_STATT_APOSTROPH": return -1; // lower prio than PLURAL_APOSTROPH
case "PUNKT_ENDE_ABSATZ": return -10; // should never hide other errors, as chance for a false alarm is quite high
case "KOMMA_ZWISCHEN_HAUPT_UND_NEBENSATZ": return -10;
}
return 0;
}
}