diff options
| author | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2017-10-11 21:34:29 -0300 |
|---|---|---|
| committer | Benjamin J. Culkin <bjculkin@mix.wvu.edu> | 2017-10-11 21:34:29 -0300 |
| commit | 235208946ceb2bf0f422956a3ebc0ebb88ba28b6 (patch) | |
| tree | e50faa94428972c6c23a605ffb4ec88d28f73cba /src/main/java/bjc | |
| parent | 46cb1f6c030991d314d0ef1fafa53e53ef3e03c9 (diff) | |
Cleanup
Diffstat (limited to 'src/main/java/bjc')
14 files changed, 464 insertions, 356 deletions
diff --git a/src/main/java/bjc/inflexion/EnglishUtils.java b/src/main/java/bjc/inflexion/EnglishUtils.java index 3feb55b..28fc6c6 100644 --- a/src/main/java/bjc/inflexion/EnglishUtils.java +++ b/src/main/java/bjc/inflexion/EnglishUtils.java @@ -16,37 +16,27 @@ package bjc.inflexion; /** - * @author student + * General utils for dealing with english. * + * @author student */ public class EnglishUtils { - private static String[] smallNums = new String[] { "zero", "one", "two", "three", "four", "five", "six", - "seven", "eight", "nine", "ten" - }; + private static String[] smallNums = new String[] { + "zero", "one", "two", "three", "four", "five", "six", "seven", + "eight", "nine", "ten" }; - private static String[] summaryNums = new String[] { "no", "one", "a couple of", "a few", "several" }; - private static String[] endSummaryNums = new String[] { "no", "one", "a couple of", "a few", "several" }; + private static String[] summaryNums = new String[] { "no", "one", "a couple of", "a few", "several" }; private static int[] summaryMap = new int[] { - /* - * no - */ + /* no */ 0, - /* - * one - */ + /* one */ 1, - /* - * a couple of - */ + /* a couple of */ 2, - /* - * a few - */ + /* a few */ 3, 3, 3, - /* - * several - */ + /* several */ 4, 4, 4, 4 }; @@ -54,9 +44,10 @@ public class EnglishUtils { * Convert small integers to words. * * @param num - * The number to convert. + * The number to convert. * - * @return The word for the number, if it's less than ten. + * @return + * The word for the number, if it's less than ten. */ public static String smallIntToWord(final int num) { if (num >= 0 && num <= 10) return smallNums[num]; @@ -68,18 +59,17 @@ public class EnglishUtils { * Summarize an integer. * * @param num - * The number to summarize. + * The number to summarize. * * @param atEnd - * Whether or not the integer is at the end of a string. + * Whether or not the integer is at the end of a string. * - * @return A string summarizing the integer. + * @return + * A string summarizing the integer. */ public static String intSummarize(final int num, final boolean atEnd) { - final String[] nums = atEnd ? endSummaryNums : summaryNums; - - if (num >= 0 && num < 10) return nums[summaryMap[num]]; + if (num >= 0 && num < 10) return summaryNums[summaryMap[num]]; return "many"; } -}
\ No newline at end of file +} diff --git a/src/main/java/bjc/inflexion/InflectionML.java b/src/main/java/bjc/inflexion/InflectionML.java index e12263b..939e96e 100644 --- a/src/main/java/bjc/inflexion/InflectionML.java +++ b/src/main/java/bjc/inflexion/InflectionML.java @@ -27,17 +27,28 @@ import bjc.inflexion.nouns.Noun; import bjc.inflexion.nouns.Nouns; import bjc.inflexion.nouns.Prepositions; +/* + * @TODO 10/11/17 Ben Culkin :InflectionML + * Complete the implementation of this from the documentation for + * Lingua::EN:Inflexion + */ /** - * @author student + * Implementation of a simple format language for inflections. * + * @author student */ public class InflectionML { - private static final List<String> ESUB_OPT = Arrays.asList("a", "s", "w"); - private static Pattern FORM_MARKER = Pattern - .compile("<(?<command>[#N])(?<options>[^:]*):(?<text>[^>]*)>"); + /* The options implied by the E option. */ + private static final List<String> ESUB_OPT = Arrays.asList("a", "s", "w"); + /* The regex that marks an inflection form. */ + private static Pattern FORM_MARKER = + Pattern.compile("<(?<command>[#N])(?<options>[^:]*):(?<text>[^>]*)>"); + + /* The database of nouns. */ private static Nouns nounDB; + /* Load DBs from files. */ static { final Prepositions prepositionDB = new Prepositions(); prepositionDB.loadFromStream(InflectionML.class.getResourceAsStream("/prepositions.txt")); @@ -50,9 +61,10 @@ public class InflectionML { * Apply inflection to marked forms in the string. * * @param form - * The string to inflect. + * The string to inflect. * - * @return The inflected string. + * @return + * The inflected string. */ public static String inflect(final String form) { final Matcher formMatcher = FORM_MARKER.matcher(form); @@ -65,7 +77,7 @@ public class InflectionML { while (formMatcher.find()) { final String command = formMatcher.group("command"); final String options = formMatcher.group("options"); - final String text = formMatcher.group("text"); + final String text = formMatcher.group("text"); final Set<String> optionSet = new HashSet<>(); @@ -75,6 +87,11 @@ public class InflectionML { switch (command) { case "#": + /* @NOTE + * These should maybe be moved into their + * own function. This will also allow the + * use of custom inflection forms. + */ try { if (optionSet.contains("e")) { optionSet.remove("e"); @@ -97,7 +114,6 @@ public class InflectionML { inflectSingular = true; } - String rep = text; if (optionSet.contains("n")) { @@ -113,21 +129,21 @@ public class InflectionML { } if (optionSet.contains("a")) { - /* - * @TODO implement a/an for nouns + /* :InflectionML + * Implement a/an for nouns. */ } - /* - * Break out of switch. - */ + /* Break out of switch. */ if (optionSet.contains("d")) { formMatcher.appendReplacement(formBuffer, rep); break; } - final boolean shouldOverride = !(rep.equals("no") || rep.equals("a") - || rep.equals("an")); + final boolean shouldOverride = + !(rep.equals("no") || + rep.equals("a") || + rep.equals("an") ); if (optionSet.contains("w") && shouldOverride) { rep = EnglishUtils.smallIntToWord(curCount); @@ -142,9 +158,7 @@ public class InflectionML { throw new InflectionException("Count setter must take a number as a parameter", nfex); } - break; - case "N": final Noun noun = nounDB.getNoun(text); @@ -157,9 +171,7 @@ public class InflectionML { } else { formMatcher.appendReplacement(formBuffer, noun.singular()); } - break; - default: final String msg = String.format("Unknown command '%s'", command); @@ -176,12 +188,13 @@ public class InflectionML { * Alias method to format a string, then inflect it. * * @param format - * The combined format/inflection string. + * The combined format/inflection string. * * @param objects - * The parameters for the format string. + * The parameters for the format string. * - * @return The string, formatted & inflected. + * @return + * The string, formatted & inflected. */ public static String iprintf(final String format, final Object... objects) { return inflect(String.format(format, objects)); diff --git a/src/main/java/bjc/inflexion/nouns/CategoricalNounInflection.java b/src/main/java/bjc/inflexion/nouns/CategoricalNounInflection.java index 2651cc5..1371ab3 100644 --- a/src/main/java/bjc/inflexion/nouns/CategoricalNounInflection.java +++ b/src/main/java/bjc/inflexion/nouns/CategoricalNounInflection.java @@ -23,14 +23,17 @@ package bjc.inflexion.nouns; * */ public class CategoricalNounInflection implements NounInflection { + /* The toString format. */ private static final String TOSTRING_FMT = - "CategoricalNounInflection [singular=%s, modernPlural=%s," - + " classicalPlural=%s]"; + "CategoricalNounInflection [singular=%s, modernPlural=%s, classicalPlural=%s]"; + /* The affix for a singular noun. */ private final InflectionAffix singular; - private final InflectionAffix modernPlural; - private final InflectionAffix classicalPlural; + /* The affix for a modern plural. */ + private final InflectionAffix modernPlural; + /* The affix for a classical plural. */ + private final InflectionAffix classicalPlural; /** * Create a new categorical inflection. @@ -47,10 +50,11 @@ public class CategoricalNounInflection implements NounInflection { public CategoricalNounInflection(final InflectionAffix singlar, final InflectionAffix modrnPlural, final InflectionAffix classiclPlural) { - if (singlar == null) + if (singlar == null) { throw new NullPointerException("Singular form must not be null"); - else if (modrnPlural == null && classiclPlural == null) + } else if (modrnPlural == null && classiclPlural == null) { throw new NullPointerException("One of modern/classical plural forms must not be null"); + } singular = singlar; modernPlural = modrnPlural; @@ -59,24 +63,20 @@ public class CategoricalNounInflection implements NounInflection { @Override public boolean matches(final String noun) { - if (singular.hasAffix(noun)) - return true; - else if (modernPlural != null && modernPlural.hasAffix(noun)) - return true; - else if (classicalPlural != null && classicalPlural.hasAffix(noun)) - return true; - else return false; + if (singular.hasAffix(noun)) return true; + else if (modernPlural != null && modernPlural.hasAffix(noun)) return true; + else if (classicalPlural != null && classicalPlural.hasAffix(noun)) return true; + else return false; } @Override public boolean isSingular(final String noun) { - if (singular.hasAffix(noun)) + if (singular.hasAffix(noun)) { return true; - else if (matchesPlural(noun)) + } else if (matchesPlural(noun)) { return false; - else { - final String msg = String.format("Noun '%s' doesn't belong to this inflection", noun, - this); + } else { + final String msg = String.format("Noun '%s' doesn't belong to this inflection", noun); throw new InflectionException(msg); } @@ -84,13 +84,12 @@ public class CategoricalNounInflection implements NounInflection { @Override public boolean isPlural(final String noun) { - if (singular.hasAffix(noun)) + if (singular.hasAffix(noun)) { return false; - else if (matchesPlural(noun)) + } else if (matchesPlural(noun)) { return true; - else { - final String msg = String.format("Noun '%s' doesn't belong to this inflection", noun, - this); + } else { + final String msg = String.format("Noun '%s' doesn't belong to this inflection", noun); throw new InflectionException(msg); } @@ -98,13 +97,13 @@ public class CategoricalNounInflection implements NounInflection { @Override public String singularize(final String plural) { - if (singular.hasAffix(plural)) + if (singular.hasAffix(plural)) { return plural; - else if (modernPlural != null && modernPlural.hasAffix(plural)) + } else if (modernPlural != null && modernPlural.hasAffix(plural)) { return singular.affix(modernPlural.deaffix(plural)); - else if (classicalPlural != null && classicalPlural.hasAffix(plural)) + } else if (classicalPlural != null && classicalPlural.hasAffix(plural)) { return singular.affix(classicalPlural.deaffix(plural)); - else { + } else { final String msg = String.format("Noun '%s' doesn't belong to this inflection", plural, this); @@ -118,9 +117,9 @@ public class CategoricalNounInflection implements NounInflection { if (modernPlural == null) return classicalPlural.affix(singular.deaffix(singlar)); return modernPlural.affix(singular.deaffix(singlar)); - } else if (matchesPlural(singlar)) + } else if (matchesPlural(singlar)) { return singlar; - else { + } else { final String msg = String.format("Noun '%s' doesn't belong to this inflection", singlar, this); @@ -128,6 +127,7 @@ public class CategoricalNounInflection implements NounInflection { } } + /* Check if a string matches a plural form. */ private boolean matchesPlural(final String noun) { final boolean hasModernPlural = modernPlural != null && modernPlural.hasAffix(noun); @@ -196,4 +196,4 @@ public class CategoricalNounInflection implements NounInflection { return classicalPlural.affix(singular.deaffix(actSinglar)); } -}
\ No newline at end of file +} diff --git a/src/main/java/bjc/inflexion/nouns/CompoundNounInflection.java b/src/main/java/bjc/inflexion/nouns/CompoundNounInflection.java index e3fc574..6edcb54 100644 --- a/src/main/java/bjc/inflexion/nouns/CompoundNounInflection.java +++ b/src/main/java/bjc/inflexion/nouns/CompoundNounInflection.java @@ -26,57 +26,69 @@ import java.util.regex.Pattern; * */ public class CompoundNounInflection implements NounInflection { + /* Format for toString. */ private static final String TOSTRING_FMT = - "CompoundNounInflection [compoundMatcher=%s, singularPattern=%s," - + " modernPluralPattern=%s, classicalPluralPattern=%s, hasPreposition=%s]"; - /* - * Data stores for use. - */ + "CompoundNounInflection [compoundMatcher=%s, singularPattern=%s, modernPluralPattern=%s, classicalPluralPattern=%s, hasPreposition=%s]"; + /* Data stores for use. */ private final Nouns nunDB; private final Prepositions pepositionDB; + /* The pattern for compound matching. */ private final Pattern cmpoundMatcher; + /* The pattern for singular matching. */ private final String sigularPattern; - private final String mdernPluralPattern; - private final String clasicalPluralPattern; + /* The patterns for plural matching. */ + private final String mdernPluralPattern; + private final String clasicalPluralPattern; - /* - * Whether or not this inflection takes a preposition. - */ + /* Whether or not this inflection takes a preposition. */ private final boolean haPreposition; - /* - * Whether or not there is a scratch word in place. - */ + /* Whether or not there is a scratch word in place. */ private final boolean hasScratch; /** - * TODO fill in documentation. + * Create a new compound noun inflection. * * @param nounDB + * The database of nouns to lookup. + * * @param prepositionDB + * The database of prepositions to lookup. + * * @param compoundMatcher + * The matcher for the compound noun. + * * @param singularPattern + * The pattern for a singular form. + * * @param modernPluralPattern + * The pattern for a modern plural form. + * * @param classicalPluralPattern + * The pattern for a classical plural form. + * * @param hasPreposition + * Whether or not this inflection uses a preposition. + * * @param hasScrtch + * Whether or not this inflection has a scratch word. */ public CompoundNounInflection(final Nouns nounDB, final Prepositions prepositionDB, final Pattern compoundMatcher, final String singularPattern, final String modernPluralPattern, final String classicalPluralPattern, final boolean hasPreposition, final boolean hasScrtch) { - nunDB = nounDB; - pepositionDB = prepositionDB; - cmpoundMatcher = compoundMatcher; - sigularPattern = singularPattern; - mdernPluralPattern = modernPluralPattern; + nunDB = nounDB; + pepositionDB = prepositionDB; + cmpoundMatcher = compoundMatcher; + sigularPattern = singularPattern; + mdernPluralPattern = modernPluralPattern; clasicalPluralPattern = classicalPluralPattern; - haPreposition = hasPreposition; - hasScratch = hasScrtch; + haPreposition = hasPreposition; + hasScratch = hasScrtch; } @Override @@ -99,7 +111,7 @@ public class CompoundNounInflection implements NounInflection { @Override public boolean isSingular(final String noun) { final Matcher matcher = cmpoundMatcher.matcher(noun); - final Noun actNoun = nunDB.getNoun(matcher.group("noun")); + final Noun actNoun = nunDB.getNoun(matcher.group("noun")); return actNoun.isSingular(); } @@ -107,7 +119,7 @@ public class CompoundNounInflection implements NounInflection { @Override public boolean isPlural(final String noun) { final Matcher matcher = cmpoundMatcher.matcher(noun); - final Noun actNoun = nunDB.getNoun(matcher.group("noun")); + final Noun actNoun = nunDB.getNoun(matcher.group("noun")); return actNoun.isPlural(); } @@ -115,34 +127,38 @@ public class CompoundNounInflection implements NounInflection { @Override public String singularize(final String plural) { final Matcher matcher = cmpoundMatcher.matcher(plural); - final Noun actNoun = getNoun(matcher); + final Noun actNoun = getNoun(matcher); - if (haPreposition && hasScratch) + if (haPreposition && hasScratch) { return String.format(sigularPattern, actNoun.singular(), matcher.group("preposition"), matcher.group("scratch")); - else if (hasScratch) + } else if (hasScratch) { return String.format(sigularPattern, actNoun.singular(), matcher.group("scratch")); - else if (haPreposition) + } else if (haPreposition) { return String.format(sigularPattern, actNoun.singular(), matcher.group("preposition")); - else return String.format(sigularPattern, actNoun.singular()); + } else { + return String.format(sigularPattern, actNoun.singular()); + } } @Override public String pluralize(final String singular) { final Matcher matcher = cmpoundMatcher.matcher(singular); - final Noun actNoun = getNoun(matcher); + final Noun actNoun = getNoun(matcher); final String patt = mdernPluralPattern == null ? clasicalPluralPattern : mdernPluralPattern; - if (haPreposition && hasScratch) + if (haPreposition && hasScratch) { return String.format(patt, actNoun.plural(), matcher.group("preposition"), matcher.group("scratch")); - else if (hasScratch) + } else if (hasScratch) { return String.format(patt, actNoun.plural(), matcher.group("scratch")); - else if (haPreposition) + } else if (haPreposition) { return String.format(patt, actNoun.plural(), matcher.group("preposition")); - else return String.format(patt, actNoun.plural()); + } else { + return String.format(patt, actNoun.plural()); + } } @Override @@ -150,19 +166,21 @@ public class CompoundNounInflection implements NounInflection { if (mdernPluralPattern == null) return pluralizeClassical(singular); final Matcher matcher = cmpoundMatcher.matcher(singular); - final Noun actNoun = getNoun(matcher); + final Noun actNoun = getNoun(matcher); - if (haPreposition && hasScratch) + if (haPreposition && hasScratch) { return String.format(mdernPluralPattern, actNoun.modernPlural(), matcher.group("preposition"), matcher.group("scratch")); - else if (hasScratch) + } else if (hasScratch) { return String.format(mdernPluralPattern, actNoun.modernPlural(), matcher.group("scratch")); - else if (haPreposition) + } else if (haPreposition) { return String.format(mdernPluralPattern, actNoun.modernPlural(), matcher.group("preposition")); - else return String.format(mdernPluralPattern, actNoun.modernPlural()); + } else { + return String.format(mdernPluralPattern, actNoun.modernPlural()); + } } @Override @@ -170,20 +188,23 @@ public class CompoundNounInflection implements NounInflection { if (clasicalPluralPattern == null) return pluralizeModern(singular); final Matcher matcher = cmpoundMatcher.matcher(singular); - final Noun actNoun = getNoun(matcher); + final Noun actNoun = getNoun(matcher); - if (haPreposition && hasScratch) + if (haPreposition && hasScratch) { return String.format(clasicalPluralPattern, actNoun.classicalPlural(), matcher.group("preposition"), matcher.group("scratch")); - else if (hasScratch) + } else if (hasScratch) { return String.format(clasicalPluralPattern, actNoun.classicalPlural(), matcher.group("scratch")); - else if (haPreposition) + } else if (haPreposition) { return String.format(clasicalPluralPattern, actNoun.classicalPlural(), matcher.group("preposition")); - else return String.format(clasicalPluralPattern, actNoun.classicalPlural()); + } else { + return String.format(clasicalPluralPattern, actNoun.classicalPlural()); + } } + /* Get the noun for a matcher. */ private Noun getNoun(final Matcher matcher) { matcher.matches(); @@ -243,4 +264,4 @@ public class CompoundNounInflection implements NounInflection { return String.format(TOSTRING_FMT, cmpoundMatcher, sigularPattern, mdernPluralPattern, clasicalPluralPattern, haPreposition); } -}
\ No newline at end of file +} diff --git a/src/main/java/bjc/inflexion/nouns/DefaultNounInflection.java b/src/main/java/bjc/inflexion/nouns/DefaultNounInflection.java index 71adba3..e982bc9 100644 --- a/src/main/java/bjc/inflexion/nouns/DefaultNounInflection.java +++ b/src/main/java/bjc/inflexion/nouns/DefaultNounInflection.java @@ -19,7 +19,6 @@ package bjc.inflexion.nouns; * Default noun inflection for english nouns. * * @author EVE - * */ public class DefaultNounInflection implements NounInflection { @Override @@ -39,11 +38,13 @@ public class DefaultNounInflection implements NounInflection { @Override public String singularize(final String plural) { - if (plural.endsWith("ses")) + if (plural.endsWith("ses")) { return plural.substring(0, plural.length() - 3); - else if (plural.endsWith("s")) + } else if (plural.endsWith("s")) { return plural.substring(0, plural.length() - 1); - else return plural; + } else { + return plural; + } } @Override @@ -62,4 +63,4 @@ public class DefaultNounInflection implements NounInflection { public String pluralizeClassical(final String singular) { return pluralize(singular); } -}
\ No newline at end of file +} diff --git a/src/main/java/bjc/inflexion/nouns/InflectionAffix.java b/src/main/java/bjc/inflexion/nouns/InflectionAffix.java index 54588a2..d224340 100644 --- a/src/main/java/bjc/inflexion/nouns/InflectionAffix.java +++ b/src/main/java/bjc/inflexion/nouns/InflectionAffix.java @@ -19,17 +19,16 @@ package bjc.inflexion.nouns; * An affix attached to a word and used for inflection. * * @author EVE - * */ public interface InflectionAffix { - /** * Check if a word has this affix. * * @param word - * The word to check. + * The word to check. * - * @return Whether or not the word has the affix. + * @return + * Whether or not the word has the affix. */ boolean hasAffix(String word); @@ -37,9 +36,10 @@ public interface InflectionAffix { * Remove the affix from a word. * * @param word - * The word to remove the affix from. + * The word to remove the affix from. * - * @return The word with the affix removed. + * @return + * The word with the affix removed. */ String deaffix(String word); @@ -47,10 +47,10 @@ public interface InflectionAffix { * Apply this affix to a word. * * @param word - * The word to apply the affix to. + * The word to apply the affix to. * - * @return The word with the affix applied. + * @return + * The word with the affix applied. */ String affix(String word); - -}
\ No newline at end of file +} diff --git a/src/main/java/bjc/inflexion/nouns/InflectionAffixes.java b/src/main/java/bjc/inflexion/nouns/InflectionAffixes.java index 4b1d2cf..facf9d0 100644 --- a/src/main/java/bjc/inflexion/nouns/InflectionAffixes.java +++ b/src/main/java/bjc/inflexion/nouns/InflectionAffixes.java @@ -21,7 +21,6 @@ import java.util.regex.Pattern; * Utility methods for constructing inflection affixes. * * @author EVE - * */ public class InflectionAffixes { /* @@ -48,9 +47,10 @@ public class InflectionAffixes { * Create an affix that's a word by itself. * * @param suffix - * The suffix to use. + * The suffix to use. * - * @return A affix that represents the suffix. + * @return + * A affix that represents the suffix. */ public static InflectionAffix complete(final String suffix) { final Pattern patt = Pattern.compile(String.format(COMPLETE_PATT_FMT, suffix)); @@ -62,9 +62,10 @@ public class InflectionAffixes { * Create an affix that's not a word by itself. * * @param suffix - * The suffix to use. + * The suffix to use. * - * @return An affix that represents the suffix. + * @return + * An affix that represents the suffix. */ public static InflectionAffix incomplete(final String suffix) { final Pattern patt = Pattern.compile(String.format(INCOMPLETE_PATT_FMT, suffix)); diff --git a/src/main/java/bjc/inflexion/nouns/InflectionException.java b/src/main/java/bjc/inflexion/nouns/InflectionException.java index 59ec61b..56715ff 100644 --- a/src/main/java/bjc/inflexion/nouns/InflectionException.java +++ b/src/main/java/bjc/inflexion/nouns/InflectionException.java @@ -19,19 +19,19 @@ package bjc.inflexion.nouns; * Exception thrown when something goes wrong with inflection. * * @author EVE - * */ public class InflectionException extends RuntimeException { + /* Version ID for exception. */ private static final long serialVersionUID = 5680541587449153748L; /** * Create a new inflection exception with the given message and cause. * * @param message - * The message of the exception. + * The message of the exception. * * @param cause - * The cause of the exception. + * The cause of the exception. */ public InflectionException(final String message, final Throwable cause) { super(message, cause); @@ -41,7 +41,7 @@ public class InflectionException extends RuntimeException { * Create a new inflection exception with the given message. * * @param message - * The message of the exception. + * The message of the exception. */ public InflectionException(final String message) { super(message); diff --git a/src/main/java/bjc/inflexion/nouns/IrregularNounInflection.java b/src/main/java/bjc/inflexion/nouns/IrregularNounInflection.java index f38a138..471a99e 100644 --- a/src/main/java/bjc/inflexion/nouns/IrregularNounInflection.java +++ b/src/main/java/bjc/inflexion/nouns/IrregularNounInflection.java @@ -19,35 +19,39 @@ package bjc.inflexion.nouns; * Implementation of {@link NounInflection} for irregular nouns. * * @author EVE - * */ public class IrregularNounInflection implements NounInflection { + /* Format string for toString. */ private static final String TOSTRING_FMT = "IrregularNounInflection [singular=%s, modernPlural=%s," + " classicalPlural=%s, preferClassical=%s]"; + /* The singular form. */ private final String singular; - private final String modernPlural; - private final String classicalPlural; + /* The modern plural form. */ + private final String modernPlural; + /* The classical plural form. */ + private final String classicalPlural; + /* Whether to prefer the classical form or not. */ private final boolean preferClassical; /** * Create a new irregular noun inflection. * * @param singlar - * The singular form of the noun. + * The singular form of the noun. * * @param modrnPlural - * The modern plural of the noun. + * The modern plural of the noun. * * @param classiclPlural - * The classical plural of the noun. + * The classical plural of the noun. * * @param prefrClassical - * Whether the classical form should be preferred if it - * is available. + * Whether the classical form should be preferred if it is + * available. */ public IrregularNounInflection(final String singlar, final String modrnPlural, final String classiclPlural, @@ -65,22 +69,24 @@ public class IrregularNounInflection implements NounInflection { @Override public boolean matches(final String noun) { - if (noun.equalsIgnoreCase(singular)) + if (noun.equalsIgnoreCase(singular)) { return true; - else if (noun.equalsIgnoreCase(modernPlural)) + } else if (noun.equalsIgnoreCase(modernPlural)) { return true; - else if (noun.equalsIgnoreCase(classicalPlural)) + } else if (noun.equalsIgnoreCase(classicalPlural)) { return true; - else return false; + } else { + return false; + } } @Override public boolean isSingular(final String noun) { - if (noun.equalsIgnoreCase(singular)) + if (noun.equalsIgnoreCase(singular)) { return true; - else if (matchesPlural(noun)) + } else if (matchesPlural(noun)) { return false; - else { + } else { final String msg = String.format("Noun '%s' doesn't belong to this inflection '%s'", noun, this); @@ -90,11 +96,11 @@ public class IrregularNounInflection implements NounInflection { @Override public boolean isPlural(final String noun) { - if (noun.equalsIgnoreCase(singular)) + if (noun.equalsIgnoreCase(singular)) { return false; - else if (matchesPlural(noun)) + } else if (matchesPlural(noun)) { return true; - else { + } else { final String msg = String.format("Noun '%s' doesn't belong to this inflection '%s'", noun, this); @@ -104,11 +110,11 @@ public class IrregularNounInflection implements NounInflection { @Override public String singularize(final String plural) { - if (plural.equalsIgnoreCase(singular)) + if (plural.equalsIgnoreCase(singular)) { return singular; - else if (matchesPlural(plural)) + } else if (matchesPlural(plural)) { return singular; - else { + } else { final String msg = String.format("Noun '%s' doesn't belong to this inflection '%s'", plural, this); @@ -119,11 +125,11 @@ public class IrregularNounInflection implements NounInflection { @Override public String pluralize(final String singlar) { - if (singlar.equalsIgnoreCase(singlar)) + if (singlar.equalsIgnoreCase(singlar)) { return getPlural(); - else if (matchesPlural(singlar)) + } else if (matchesPlural(singlar)) { return getPlural(); - else { + } else { final String msg = String.format("Noun '%s' doesn't belong to this inflection '%s'", singlar, this); @@ -132,16 +138,20 @@ public class IrregularNounInflection implements NounInflection { } } + /* Get the plural form. */ private String getPlural() { if (preferClassical) { if (classicalPlural == null) return modernPlural; return classicalPlural; - } else if (modernPlural == null) + } else if (modernPlural == null) { return classicalPlural; - else return modernPlural; + } else { + return modernPlural; + } } + /* Check if something matches the plural forms. */ private boolean matchesPlural(final String noun) { return noun.equalsIgnoreCase(modernPlural) || noun.equalsIgnoreCase(classicalPlural); } @@ -202,4 +212,4 @@ public class IrregularNounInflection implements NounInflection { return classicalPlural; } -}
\ No newline at end of file +} diff --git a/src/main/java/bjc/inflexion/nouns/Noun.java b/src/main/java/bjc/inflexion/nouns/Noun.java index 5ab5009..f94e0bc 100644 --- a/src/main/java/bjc/inflexion/nouns/Noun.java +++ b/src/main/java/bjc/inflexion/nouns/Noun.java @@ -19,23 +19,23 @@ package bjc.inflexion.nouns; * A noun attached to an inflection. * * @author EVE - * */ public class Noun { + /* Format string for toString. */ private static final String TOSTRING_FMT = "Noun [word=%s, inflection=%s]"; - + /* The word itself. */ private final String word; - + /* Its inflection. */ private final NounInflection inflection; /** * Create a new noun from a word and inflection. * * @param wrd - * The word for the noun. + * The word for the noun. * * @param inflction - * The inflection for the word. + * The inflection for the word. */ public Noun(final String wrd, final NounInflection inflction) { word = wrd; @@ -45,7 +45,8 @@ public class Noun { /** * Get the input noun. * - * @return The noun, as input. + * @return + * The noun, as input. */ public String getWord() { return word; @@ -54,7 +55,8 @@ public class Noun { /** * Get the inflection for this noun. * - * @return The inflection for this noun. + * @return + * The inflection for this noun. */ public NounInflection getInflection() { return inflection; @@ -63,7 +65,8 @@ public class Noun { /** * Check if this noun is singular. * - * @return Whether or not the noun is singular. + * @return + * Whether or not the noun is singular. */ public boolean isSingular() { return inflection.isSingular(word); @@ -72,7 +75,8 @@ public class Noun { /** * Check if this noun is plural. * - * @return Whether or not this noun is plural. + * @return + * Whether or not this noun is plural. */ public boolean isPlural() { return inflection.isPlural(word); @@ -81,7 +85,8 @@ public class Noun { /** * Get the singular form of this noun. * - * @return The singular form of this noun. + * @return + * The singular form of this noun. */ public String singular() { return inflection.singularize(word); @@ -90,7 +95,8 @@ public class Noun { /** * Get the plural form of this noun. * - * @return The plural form of this noun. + * @return + * The plural form of this noun. */ public String plural() { return inflection.pluralize(word); @@ -104,7 +110,8 @@ public class Noun { /** * Get the modern plural form of this noun. * - * @return The modern plural form of this noun. + * @return + * The modern plural form of this noun. */ public String modernPlural() { return inflection.pluralizeModern(word); @@ -113,9 +120,10 @@ public class Noun { /** * Get the classical plural form of this noun. * - * @return The classical plural form of this noun. + * @return + * The classical plural form of this noun. */ public String classicalPlural() { return inflection.pluralizeClassical(word); } -}
\ No newline at end of file +} diff --git a/src/main/java/bjc/inflexion/nouns/NounInflection.java b/src/main/java/bjc/inflexion/nouns/NounInflection.java index 142968f..978efdb 100644 --- a/src/main/java/bjc/inflexion/nouns/NounInflection.java +++ b/src/main/java/bjc/inflexion/nouns/NounInflection.java @@ -19,16 +19,16 @@ package bjc.inflexion.nouns; * Interface for inflecting nouns. * * @author EVE - * */ public interface NounInflection { /** * Check if a noun matches this inflection. * * @param noun - * The noun to check on this inflection. + * The noun to check on this inflection. * - * @return Whether or not the noun belongs to the inflection. + * @return + * Whether or not the noun belongs to the inflection. */ public boolean matches(String noun); @@ -36,12 +36,13 @@ public interface NounInflection { * Check if a noun for this inflection is singular or not. * * @param noun - * The noun to check for singularity. + * The noun to check for singularity. * - * @return Whether or not the noun is singular. + * @return + * Whether or not the noun is singular. * * @throws InflectionException - * If the noun isn't part of this inflection. + * If the noun isn't part of this inflection. */ public boolean isSingular(String noun); @@ -49,12 +50,13 @@ public interface NounInflection { * Check if a noun for this inflection is plural or not. * * @param noun - * The noun to check for plurality. + * The noun to check for plurality. * - * @return Whether or not the noun is plural. + * @return + * Whether or not the noun is plural. * * @throws InflectionException - * If the noun isn't part of this inflection. + * If the noun isn't part of this inflection. */ public boolean isPlural(String noun); @@ -62,12 +64,13 @@ public interface NounInflection { * Convert a singular noun to a plural noun. * * @param plural - * The plural noun to inflect to a singular form. + * The plural noun to inflect to a singular form. * - * @return The singular form of the noun. + * @return + * The singular form of the noun. * * @throws InflectionException - * If the noun isn't part of the inflection. + * If the noun isn't part of the inflection. */ public String singularize(String plural); @@ -75,12 +78,13 @@ public interface NounInflection { * Convert a singular noun to a plural noun. * * @param singular - * The singular noun to inflect to a plural form. + * The singular noun to inflect to a plural form. * - * @return The plural form of the noun. + * @return + * The plural form of the noun. * * @throws InflectionException - * If the noun isn't part of the inflection. + * If the noun isn't part of the inflection. */ public String pluralize(String singular); @@ -88,12 +92,13 @@ public interface NounInflection { * Convert a singular noun to a modern plural noun. * * @param singular - * The singular noun to inflect to a modern plural form. + * The singular noun to inflect to a modern plural form. * - * @return The modern plural form of the noun. + * @return + * The modern plural form of the noun. * * @throws InflectionException - * If the noun isn't part of the inflection. + * If the noun isn't part of the inflection. */ public String pluralizeModern(String singular); @@ -101,13 +106,13 @@ public interface NounInflection { * Convert a singular noun to a classical plural noun. * * @param singular - * The singular noun to inflect to a classical plural - * form. + * The singular noun to inflect to a classical plural form. * - * @return The classical plural form of the noun. + * @return + * The classical plural form of the noun. * * @throws InflectionException - * If the noun isn't part of the inflection. + * If the noun isn't part of the inflection. */ public String pluralizeClassical(String singular); -}
\ No newline at end of file +} diff --git a/src/main/java/bjc/inflexion/nouns/Nouns.java b/src/main/java/bjc/inflexion/nouns/Nouns.java index d63b704..aeb2f2f 100644 --- a/src/main/java/bjc/inflexion/nouns/Nouns.java +++ b/src/main/java/bjc/inflexion/nouns/Nouns.java @@ -28,34 +28,41 @@ import java.util.Scanner; import java.util.regex.Pattern; /** - * @author EVE + * Utilities for doing things with nouns. * + * @author EVE */ public class Nouns { + /* The default inflection. */ private static final DefaultNounInflection DEFAULT_INFLECTION = new - DefaultNounInflection(); + 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. + * The source for prepositions. */ public Nouns(final Prepositions prepDB) { prepositionDB = prepDB; - userIrregulars = new HashMap<>(); + userIrregulars = new HashMap<>(); userInflections = new LinkedList<>(); - predefinedIrregulars = new HashMap<>(); + predefinedIrregulars = new HashMap<>(); predefinedInflections = new LinkedList<>(); } @@ -63,12 +70,13 @@ public class Nouns { * Retrieve a noun with its inflection from the database of inflections. * * @param noun - * The noun to retrieve. + * The noun to retrieve. * - * @return The noun with its inflection. + * @return + * The noun with its inflection. * * @throws InflectionException - * If the noun matched no inflection. + * If the noun matched no inflection. */ public Noun getNoun(final String noun) { if (userIrregulars.containsKey(noun)) return new Noun(noun, userIrregulars.get(noun)); @@ -77,8 +85,9 @@ public class Nouns { if (inflect.matches(noun)) return new Noun(noun, inflect); } - if (predefinedIrregulars.containsKey(noun)) return new Noun(noun, - predefinedIrregulars.get(noun)); + 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); @@ -91,24 +100,19 @@ public class Nouns { * Load the contents of the stream into this DB. * * @param stream - * The stream to load from. + * 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("#")) { - continue; - } - - if (ln.equals("")) { + /* 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('-', ' ')); @@ -119,6 +123,7 @@ public class Nouns { } } + /* Handle a line from a noun database. */ private void handleLine(final String ln) { final String[] parts = ln.split(Pattern.quote("=>")); @@ -129,9 +134,9 @@ public class Nouns { } final String singular = parts[0].trim(); - final String plural = parts[1].trim(); + final String plural = parts[1].trim(); - String modernPlural = ""; + String modernPlural = ""; String classicalPlural = ""; if (plural.contains("|")) { @@ -140,7 +145,7 @@ public class Nouns { if (plurals.length == 1) { modernPlural = plurals[0].trim(); } else { - modernPlural = plurals[0].trim(); + modernPlural = plurals[0].trim(); classicalPlural = plurals[1].trim(); } @@ -152,7 +157,7 @@ public class Nouns { classicalPlural = null; } } else { - modernPlural = plural; + modernPlural = plural; classicalPlural = null; } @@ -167,31 +172,41 @@ public class Nouns { } } + /* + * 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 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+)"); + 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"); + 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); + handleCompoundPreposition(actSingular, actModern, + actClassical, singularPatt, modernPatt, + classicalPatt, inflections); } else { - handleCompound(actSingular, actModern, actClassical, singularPatt, modernPatt, - classicalPatt, - inflections); + handleCompound(actSingular, actModern, actClassical, + singularPatt, modernPatt, classicalPatt, + inflections); } for (final NounInflection inf : inflections) { @@ -199,129 +214,173 @@ public class Nouns { } } - 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) { + /* + * 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 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); + handleNonpluralCompound(actSingular, actModern, + actClassical, singularPatt, modernPatt, + classicalPatt, inflections, true); } else { - handleNonpluralCompound(actSinglar, actModrn, actClasscal, singularPtt, modernPtt, - classicalPtt, - inflections, false); + handleNonpluralCompound(actSinglar, actModrn, + actClasscal, singularPtt, modernPtt, + classicalPtt, inflections, false); } } - 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; + /* 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 CompoundNounInflection singularInflection = new CompoundNounInflection(this, - prepositionDB, - Pattern.compile(singularPatt), actSinglar, actModern, actClassical, false, hasScratch); + 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 CompoundNounInflection modernInflection = new CompoundNounInflection(this, - prepositionDB, - Pattern.compile(modernPatt), actSinglar, actModern, actClassical, false, - hasScratch); + 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 CompoundNounInflection classicalInflection = new CompoundNounInflection(this, - prepositionDB, Pattern.compile(classicalPatt), actSinglar, actModern, - actClassical, false, hasScratch); + final Pattern clasPt = Pattern.compile(classicalPatt); + + final CompoundNounInflection classicalInflection = new + CompoundNounInflection(this, prepositionDB, + clasPt, actSinglar, actModern, + actClassical, false, + hasScratch); inflections.add(classicalInflection); } } - 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"); + /* + * 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+)"); + 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"); + 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; + actModern = actModern.equals("") ? null : actModern; actClassical = actClassical.equals("") ? null : actClassical; - addCompoundInflections(actSingular, actModern, actClassical, singularPatt, modernPatt, - classicalPatt, inflections, true); + addCompoundInflections(actSingular, actModern, + actClassical, singularPatt, modernPatt, + classicalPatt, inflections, true); } else { - addCompoundInflections(actSingular, actModern, actClassical, singularPatt, modernPatt, - classicalPatt, inflections, false); + addCompoundInflections(actSingular, actModern, + actClassical, singularPatt, modernPatt, + classicalPatt, inflections, false); } } - 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) { - final CompoundNounInflection singularInflection = new CompoundNounInflection(this, - prepositionDB, - Pattern.compile(singularPatt), actSingular, actModern, actClassical, true, hasScratch); + /* 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("")) { - final CompoundNounInflection modernInflection = new CompoundNounInflection(this, - prepositionDB, - Pattern.compile(modernPatt), actSingular, actModern, actClassical, true, - hasScratch); + Pattern modPt = Pattern.compile(modernPatt); + + final CompoundNounInflection modernInflection = new + CompoundNounInflection(this, prepositionDB, + modPt, actSingular, actModern, + actClassical, true, hasScratch); inflections.add(modernInflection); } if (!classicalPatt.equals("")) { - final CompoundNounInflection classicalInflection = new CompoundNounInflection(this, - prepositionDB, Pattern.compile(classicalPatt), actSingular, actModern, - actClassical, true, hasScratch); + Pattern clasPt = Pattern.compile(classicalPatt); + + final CompoundNounInflection classicalInflection = new + CompoundNounInflection(this, prepositionDB, + clasPt, actSingular, actModern, + actClassical, true, hasScratch); inflections.add(classicalInflection); } } - private void handleIncompletePlural(final String singular, final String modernPlural, - final String classicalPlural) { + /* 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 modernAffix = null; InflectionAffix classicalAffix = null; if (modernPlural != null) { @@ -332,18 +391,19 @@ public class Nouns { classicalAffix = incomplete(classicalPlural.substring(1)); } - final CategoricalNounInflection inflection = new CategoricalNounInflection(singularAffix, - modernAffix, - classicalAffix); + final CategoricalNounInflection inflection = new + CategoricalNounInflection(singularAffix, modernAffix, + classicalAffix); predefinedInflections.add(inflection); } - private void handleCompletePlural(final String singular, final String modernPlural, - final String classicalPlural) { + /* 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 modernAffix = null; InflectionAffix classicalAffix = null; if (modernPlural != null) { @@ -354,18 +414,19 @@ public class Nouns { classicalAffix = complete(classicalPlural.substring(1)); } - final CategoricalNounInflection inflection = new CategoricalNounInflection(singularAffix, - modernAffix, - classicalAffix); + final CategoricalNounInflection inflection = new + CategoricalNounInflection(singularAffix, modernAffix, + classicalAffix); predefinedInflections.add(inflection); } - private void handleIrregularPlural(final String singular, final String modernPlural, - final String classicalPlural) { - final IrregularNounInflection inflection = new IrregularNounInflection(singular, - modernPlural, - classicalPlural, false); + /* 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); diff --git a/src/main/java/bjc/inflexion/nouns/Prepositions.java b/src/main/java/bjc/inflexion/nouns/Prepositions.java index 9f8424e..0d36c7e 100644 --- a/src/main/java/bjc/inflexion/nouns/Prepositions.java +++ b/src/main/java/bjc/inflexion/nouns/Prepositions.java @@ -24,14 +24,12 @@ import java.util.Set; * List of prepositions. * * @author EVE - * */ public class Prepositions { + /* Our set of prepositions. */ private final Set<String> prepositions; - /** - * Create an empty preposition DB. - */ + /** Create an empty preposition DB. */ public Prepositions() { prepositions = new HashSet<>(); } @@ -40,9 +38,10 @@ public class Prepositions { * Check if a word is a preposition. * * @param word - * The word as a preposition. + * The word as a preposition. * - * @return Whether or not the word is a preposition. + * @return + * Whether or not the word is a preposition. */ public boolean isPreposition(final String word) { return prepositions.contains(word); @@ -52,16 +51,14 @@ public class Prepositions { * Load the contents of the stream into this DB. * * @param stream - * The stream to load from. + * 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 - */ + /* Ignore comments */ if (ln.startsWith("#")) { continue; } @@ -70,4 +67,4 @@ public class Prepositions { } } } -}
\ No newline at end of file +} diff --git a/src/main/java/bjc/inflexion/nouns/SimpleInflectionAffix.java b/src/main/java/bjc/inflexion/nouns/SimpleInflectionAffix.java index e50fe5c..87991b5 100644 --- a/src/main/java/bjc/inflexion/nouns/SimpleInflectionAffix.java +++ b/src/main/java/bjc/inflexion/nouns/SimpleInflectionAffix.java @@ -22,27 +22,28 @@ import java.util.regex.Pattern; * Simple implementation of {@link InflectionAffix} * * @author EVE - * */ public class SimpleInflectionAffix implements InflectionAffix { + /* Format string for toString. */ private static final String TOSTRING_FMT = "SimpleInflectionAffix [affixTemplate=%s, affixMatcher=%s]"; + /* Affix template. */ private final String affixTmplate; - + /* Affix matching. */ private final Pattern affixMtcher; /** * Create a new inflection affix. * * @param affixTemplate - * The template for applying the affix, Should be a - * printf-style format string with a single string blank. + * The template for applying the affix, Should be a printf-style + * format string with a single string blank. * * @param affixMatcher - * The regular expression that matches the affix on - * strings. The 'stem' or word should be placed in a - * named capturing group named 'stem'. + * The regular expression that matches the affix on strings. The + * 'stem' or word should be placed in a named capturing group named + * 'stem'. */ public SimpleInflectionAffix(final String affixTemplate, final Pattern affixMatcher) { affixTmplate = affixTemplate; @@ -103,4 +104,4 @@ public class SimpleInflectionAffix implements InflectionAffix { return true; } -}
\ No newline at end of file +} |
