From 71dab5c40a46632b11ef111ceb9d8beaa6281c22 Mon Sep 17 00:00:00 2001 From: student Date: Mon, 3 Apr 2017 11:46:49 -0400 Subject: Start on noun inflection --- src/main/java/bjc/inflexion/InflectionUtils.java | 48 +++++++++--- src/main/java/bjc/inflexion/NounInflector.java | 93 ++++++++++++++++++++++++ 2 files changed, 130 insertions(+), 11 deletions(-) create mode 100644 src/main/java/bjc/inflexion/NounInflector.java (limited to 'src/main/java/bjc') diff --git a/src/main/java/bjc/inflexion/InflectionUtils.java b/src/main/java/bjc/inflexion/InflectionUtils.java index 6ea0ac3..69e359d 100644 --- a/src/main/java/bjc/inflexion/InflectionUtils.java +++ b/src/main/java/bjc/inflexion/InflectionUtils.java @@ -29,10 +29,10 @@ public class InflectionUtils { * Returns true if the given word ends in the given suffix. * * @param word - * The word to check. + * The word to check. * * @param suffix - * The suffix to check for. + * The suffix to check for. * * @return Whether or not the provided word ends in the provided suffix. */ @@ -44,20 +44,19 @@ public class InflectionUtils { } /** - * Check if the given word inflects from the given singular suffix to - * the given plural suffix. + * Check if the given word inflects from the given singular suffix to the + * given plural suffix. * * @param word - * The word to check. + * The word to check. * * @param singular - * The singular suffix. + * The singular suffix. * * @param plural - * The plural suffix. + * The plural suffix. * - * @return Whether or not the provided word is in that inflection - * category. + * @return Whether or not the provided word is in that inflection category. */ public static boolean category(String word, String singular, String plural) { /* @@ -67,11 +66,20 @@ public class InflectionUtils { } /** + * Replace the provided singular suffix in the word with the given plural + * suffix. * * @param word + * The word to inflect. + * * @param singular + * The singular suffix to remove. + * * @param plural - * @return + * The plural suffix to add. Unlike normal suffixes, this must be + * a '-' followed by a string. + * + * @return The word with the suffix replaced. */ public static String inflect(String word, String singular, String plural) { /* @@ -79,4 +87,22 @@ public class InflectionUtils { */ return null; } -} + + /** + * Remove the specified suffix from the word. + * + * @param word + * The word to stem. + * + * @param suffix + * The suffix to remove from the word. + * + * @return The word with the suffix removed. + */ + public static String stem(String word, String suffix) { + /* + * TODO implement me. + */ + return null; + } +} \ No newline at end of file diff --git a/src/main/java/bjc/inflexion/NounInflector.java b/src/main/java/bjc/inflexion/NounInflector.java new file mode 100644 index 0000000..8f839f4 --- /dev/null +++ b/src/main/java/bjc/inflexion/NounInflector.java @@ -0,0 +1,93 @@ +/** + * (C) Copyright 2017 Benjamin Culkin. + * + * 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; + +import static bjc.inflexion.InflectionUtils.*; + +/** + * Inflect English nouns. + * + * @author student + * + */ +public class NounInflector { + /** + * + */ + public static String inflectNoun(String noun) { + if (isUserPlural(noun)) { + return userPlural(noun); + } + + if (dontPluralize(noun)) + return noun; + + return ""; + } + + /** + * Check if a given noun shouldn't be pluralized. + * + * @param noun + * The noun to check. + * + * @return Whether or not the noun has no plural. + */ + private static boolean dontPluralize(String noun) { + String[] suffixes = new String[] { "-fish", "-ois", "-sheep", "-deer", "-pox", "-[A-Z].*ese", "-itis", }; + + for (String sfx : suffixes) { + if (suffix(noun, sfx)) + return true; + } + + if (category(noun, "", "")) + return true; + + return false; + } + + /** + * Inflect a noun according to a user-defined plural. + * + * @param noun + * The noun to inflect. + * + * @return The noun, inflected by the user form. + */ + private static String userPlural(String noun) { + /* + * TODO Auto-generated method stub + */ + return null; + } + + /** + * Check if there is a user defined plural form for the given noun. + * + * @param noun + * The noun to check. + * + * @return Whether or not a user-defined plural exists for a particular + * noun. + */ + private static boolean isUserPlural(String noun) { + /* + * TODO Auto-generated method stub + */ + return false; + } +} -- cgit v1.2.3