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
|
/**
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package bjc.inflexion.nouns;
import static bjc.inflexion.nouns.InflectionAffixes.complete;
import static bjc.inflexion.nouns.InflectionAffixes.incomplete;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.regex.Pattern;
/**
* Utilities for doing things with nouns.
*
* @author EVE
*/
public class Nouns {
/* The default inflection. */
private static final DefaultNounInflection DEFAULT_INFLECTION
= new DefaultNounInflection();
/* Database of prepositions. */
private final Prepositions prepositionDB;
/* User defined irregular inflections. */
private final Map<String, NounInflection> userIrregulars;
/* User defined categorical inflections. */
private final List<NounInflection> userInflections;
/* Predefined irregular inflections. */
private final Map<String, NounInflection> predefinedIrregulars;
/* Predefined categorical inflections. */
private final List<NounInflection> predefinedInflections;
/**
* Create a new empty noun DB.
*
* @param prepDB
* The source for prepositions.
*/
public Nouns(final Prepositions prepDB) {
prepositionDB = prepDB;
userIrregulars = new HashMap<>();
userInflections = new LinkedList<>();
predefinedIrregulars = new HashMap<>();
predefinedInflections = new LinkedList<>();
}
/**
* Retrieve a noun with its inflection from the database of inflections.
*
* @param noun
* The noun to retrieve.
*
* @return The noun with its inflection.
*
* @throws InflectionException
* If the noun matched no inflection.
*/
public Noun getNoun(final String noun) {
if (userIrregulars.containsKey(noun))
return new Noun(noun, userIrregulars.get(noun));
for (final NounInflection inflect : userInflections) {
if (inflect.matches(noun))
return new Noun(noun, inflect);
}
if (predefinedIrregulars.containsKey(noun)) {
return new Noun(noun, predefinedIrregulars.get(noun));
}
for (final NounInflection inflect : predefinedInflections) {
if (inflect.matches(noun))
return new Noun(noun, inflect);
}
return new Noun(noun, DEFAULT_INFLECTION);
}
/**
* Load the contents of the stream into this DB.
*
* @param stream
* The stream to load from.
*/
public void loadFromStream(final InputStream stream) {
try (Scanner scn = new Scanner(stream)) {
while (scn.hasNextLine()) {
final String ln = scn.nextLine().trim();
/* Ignore comments and blank lines. */
if (ln.startsWith("#") || ln.equals("")) {
continue;
}
/* Handle being able to replace -'s with spaces. */
if (ln.contains("-")) {
handleLine(ln);
handleLine(ln.replace('-', ' '));
} else {
handleLine(ln);
}
}
}
}
/* Handle a line from a noun database. */
private void handleLine(final String ln) {
final String[] parts = ln.split(Pattern.quote("=>"));
if (parts.length != 2) {
final String msg = String.format("Improperly formatted noun defn '%s'", ln);
throw new InflectionException(msg);
}
final String singular = parts[0].trim();
final String plural = parts[1].trim();
String modernPlural = "";
String classicalPlural = "";
if (plural.contains("|")) {
final String[] plurals = plural.split(Pattern.quote("|"));
if (plurals.length == 1) {
modernPlural = plurals[0].trim();
} else {
modernPlural = plurals[0].trim();
classicalPlural = plurals[1].trim();
}
if (modernPlural.equals("")) {
modernPlural = null;
}
if (classicalPlural.equals("")) {
classicalPlural = null;
}
} else {
modernPlural = plural;
classicalPlural = null;
}
if (singular.contains("(SING)")) {
handleCompoundPlural(singular, modernPlural, classicalPlural);
} else if (singular.startsWith("*")) {
handleCompletePlural(singular, modernPlural, classicalPlural);
} else if (singular.startsWith("-")) {
handleIncompletePlural(singular, modernPlural, classicalPlural);
} else {
handleIrregularPlural(singular, modernPlural, classicalPlural);
}
}
/*
* Handle a compound inflection.
*/
private void handleCompoundPlural(final String singular, final String modernPlural,
final String classicalPlural) {
String actSingular = singular;
String actModern = modernPlural == null ? "" : modernPlural;
String actClassical = classicalPlural == null ? "" : classicalPlural;
final String singularPatt
= actSingular.replaceAll(Pattern.quote("(SING)"), "(?<noun>\\\\w+)");
final String modernPatt
= actModern.replaceAll(Pattern.quote("(PL)"), "(?<noun>\\\\w+)");
final String classicalPatt
= actClassical.replaceAll(Pattern.quote("(PL)"), "(?<noun>\\\\w+)");
actSingular = actSingular.replaceAll(Pattern.quote("(SING)"), "%1\\$s");
actModern = actModern.replaceAll(Pattern.quote("(PL)"), "%1\\$s");
actClassical = actClassical.replaceAll(Pattern.quote("(PL)"), "%1\\$s");
final List<CompoundNounInflection> inflections = new ArrayList<>(3);
if (singular.contains("(PREP)")) {
handleCompoundPreposition(actSingular, actModern, actClassical, singularPatt,
modernPatt, classicalPatt, inflections);
} else {
handleCompound(actSingular, actModern, actClassical, singularPatt, modernPatt,
classicalPatt, inflections);
}
for (final NounInflection inf : inflections) {
predefinedInflections.add(inf);
}
}
/*
* Handle a compound inflection.
*/
private void handleCompound(final String actSinglar, final String actModrn,
final String actClasscal, final String singularPtt, final String modernPtt,
final String classicalPtt, final List<CompoundNounInflection> inflections) {
if (singularPtt.contains("*")) {
final String singularPatt
= singularPtt.replaceAll(Pattern.quote("*"), "(?<scratch>\\\\w+)");
final String modernPatt
= modernPtt.replaceAll(Pattern.quote("*"), "(?<scratch>\\\\w+)");
final String classicalPatt
= classicalPtt.replaceAll(Pattern.quote("*"), "(?<scratch>\\\\w+)");
final String actSingular
= actSinglar.replaceAll(Pattern.quote("*"), "%2\\$s");
final String actModern = actModrn.replaceAll(Pattern.quote("*"), "%2\\$s");
final String actClassical
= actClasscal.replaceAll(Pattern.quote("*"), "%2\\$s");
handleNonpluralCompound(actSingular, actModern, actClassical, singularPatt,
modernPatt, classicalPatt, inflections, true);
} else {
handleNonpluralCompound(actSinglar, actModrn, actClasscal, singularPtt,
modernPtt, classicalPtt, inflections, false);
}
}
/* Handle a non-plural compound inflection. */
private void handleNonpluralCompound(final String actSinglar, final String actModrn,
final String actClasscal, final String singularPatt, final String modernPatt,
final String classicalPatt, final List<CompoundNounInflection> inflections,
final boolean hasScratch) {
final String actModern = actModrn.equals("") ? null : actModrn;
final String actClassical = actClasscal.equals("") ? null : actClasscal;
final Pattern singPt = Pattern.compile(singularPatt);
final CompoundNounInflection singularInflection
= new CompoundNounInflection(this, prepositionDB, singPt, actSinglar,
actModern, actClassical, false, hasScratch);
inflections.add(singularInflection);
if (!modernPatt.equals("")) {
final Pattern modPt = Pattern.compile(modernPatt);
final CompoundNounInflection modernInflection
= new CompoundNounInflection(this, prepositionDB, modPt, actSinglar,
actModern, actClassical, false, hasScratch);
inflections.add(modernInflection);
}
if (!classicalPatt.equals("")) {
final Pattern clasPt = Pattern.compile(classicalPatt);
final CompoundNounInflection classicalInflection
= new CompoundNounInflection(this, prepositionDB, clasPt, actSinglar,
actModern, actClassical, false, hasScratch);
inflections.add(classicalInflection);
}
}
/*
* Handle a compound plural with a preposition.
*/
private void handleCompoundPreposition(final String actSinglar, final String actModrn,
final String actClasscal, final String singularPtt, final String modernPtt,
final String classicalPtt, final List<CompoundNounInflection> inflections) {
String singularPatt = singularPtt.replaceAll(Pattern.quote("(PREP)"),
"(?<preposition>\\\\w+)");
String modernPatt
= modernPtt.replaceAll(Pattern.quote("(PREP)"), "(?<preposition>\\\\w+)");
String classicalPatt = classicalPtt.replaceAll(Pattern.quote("(PREP)"),
"(?<preposition>\\\\w+)");
String actSingular = actSinglar.replaceAll(Pattern.quote("(PREP)"), "%2\\$s");
String actModern = actModrn.replaceAll(Pattern.quote("(PREP)"), "%2\\$s");
String actClassical = actClasscal.replaceAll(Pattern.quote("(PREP)"), "%2\\$s");
if (singularPatt.contains("*")) {
singularPatt
= singularPatt.replaceAll(Pattern.quote("*"), "(?<scratch>\\\\w+)");
modernPatt = modernPatt.replaceAll(Pattern.quote("*"), "(?<scratch>\\\\w+)");
classicalPatt
= classicalPatt.replaceAll(Pattern.quote("*"), "(?<scratch>\\\\w+)");
actSingular = actSingular.replaceAll(Pattern.quote("*"), "%3\\$s");
actModern = actModern.replaceAll(Pattern.quote("*"), "%3\\$s");
actClassical = actClassical.replaceAll(Pattern.quote("*"), "%3\\$s");
actModern = actModern.equals("") ? null : actModern;
actClassical = actClassical.equals("") ? null : actClassical;
addCompoundInflections(actSingular, actModern, actClassical, singularPatt,
modernPatt, classicalPatt, inflections, true);
} else {
addCompoundInflections(actSingular, actModern, actClassical, singularPatt,
modernPatt, classicalPatt, inflections, false);
}
}
/* Do adding a compound inflection. */
private void addCompoundInflections(final String actSingular, final String actModern,
final String actClassical, final String singularPatt, final String modernPatt,
final String classicalPatt, final List<CompoundNounInflection> inflections,
final boolean hasScratch) {
Pattern singPt = Pattern.compile(singularPatt);
final CompoundNounInflection singularInflection
= new CompoundNounInflection(this, prepositionDB, singPt, actSingular,
actModern, actClassical, true, hasScratch);
inflections.add(singularInflection);
if (!modernPatt.equals("")) {
Pattern modPt = Pattern.compile(modernPatt);
final CompoundNounInflection modernInflection
= new CompoundNounInflection(this, prepositionDB, modPt, actSingular,
actModern, actClassical, true, hasScratch);
inflections.add(modernInflection);
}
if (!classicalPatt.equals("")) {
Pattern clasPt = Pattern.compile(classicalPatt);
final CompoundNounInflection classicalInflection
= new CompoundNounInflection(this, prepositionDB, clasPt, actSingular,
actModern, actClassical, true, hasScratch);
inflections.add(classicalInflection);
}
}
/* Handle an incomplete plural. */
private void handleIncompletePlural(final String singular, final String modernPlural,
final String classicalPlural) {
final InflectionAffix singularAffix = incomplete(singular.substring(1));
InflectionAffix modernAffix = null;
InflectionAffix classicalAffix = null;
if (modernPlural != null) {
modernAffix = incomplete(modernPlural.substring(1));
}
if (classicalPlural != null) {
classicalAffix = incomplete(classicalPlural.substring(1));
}
final CategoricalNounInflection inflection = new CategoricalNounInflection(
singularAffix, modernAffix, classicalAffix);
predefinedInflections.add(inflection);
}
/* Handle a complete plural. */
private void handleCompletePlural(final String singular, final String modernPlural,
final String classicalPlural) {
final InflectionAffix singularAffix = complete(singular.substring(1));
InflectionAffix modernAffix = null;
InflectionAffix classicalAffix = null;
if (modernPlural != null) {
modernAffix = complete(modernPlural.substring(1));
}
if (classicalPlural != null) {
classicalAffix = complete(classicalPlural.substring(1));
}
final CategoricalNounInflection inflection = new CategoricalNounInflection(
singularAffix, modernAffix, classicalAffix);
predefinedInflections.add(inflection);
}
/* Handle an irregular plural. */
private void handleIrregularPlural(final String singular, final String modernPlural,
final String classicalPlural) {
final IrregularNounInflection inflection = new IrregularNounInflection(singular,
modernPlural, classicalPlural, false);
if (!predefinedIrregulars.containsKey(singular)) {
predefinedIrregulars.put(singular, inflection);
}
if (modernPlural != null && !predefinedIrregulars.containsKey(modernPlural)) {
predefinedIrregulars.put(modernPlural, inflection);
}
if (classicalPlural != null
&& !predefinedIrregulars.containsKey(classicalPlural)) {
predefinedIrregulars.put(classicalPlural, inflection);
}
}
}
|