diff options
Diffstat (limited to 'src/main/java/bjc/inflexion/InflectionML.java')
| -rw-r--r-- | src/main/java/bjc/inflexion/InflectionML.java | 57 |
1 files changed, 35 insertions, 22 deletions
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)); |
