001: package org.andromda.utils.inflector;
002:
003: import java.util.Map;
004: import java.util.HashMap;
005: import java.util.Iterator;
006:
007: /**
008: * Language utility for transforming English words.
009: * See also <a href="http://www.csse.monash.edu.au/~damian/papers/HTML/Plurals.html">
010: * http://www.csse.monash.edu.au/~damian/papers/HTML/Plurals.html</a>
011: *
012: * @author maetl@coretxt.net.nz
013: * @author wouter@andromda.org
014: */
015: public class EnglishInflector {
016: /**
017: * Converts an English word to plural form
018: *
019: * @param word an English word
020: * @return the pluralization of the argument English word, or the argument in case it is <code>null</code>
021: */
022: public static String pluralize(String word) {
023: if (word == null)
024: return null;
025:
026: final Map rules = EnglishInflector.getPluralRules();
027: for (Iterator ruleIterator = rules.entrySet().iterator(); ruleIterator
028: .hasNext();) {
029: final Map.Entry rule = (Map.Entry) ruleIterator.next();
030: final String pattern = rule.getKey().toString();
031: final String replace = rule.getValue().toString();
032: if (word.matches(pattern)) {
033: return word.replaceFirst(pattern, replace);
034: }
035: }
036: return word.replaceFirst("([\\w]+)([^s])$", "$1$2s");
037: }
038:
039: /**
040: * Returns map of plural patterns
041: */
042: private static Map getPluralRules() {
043: final Map rules = new HashMap();
044: rules.put("(\\w+)(x|ch|ss|sh)$", "$1$2es");
045: rules.put("(\\w+)([^aeiou])y$", "$1$2ies");
046: rules.put("(\\w*)(f)$", "$1ves");
047: rules.put("(\\w*)(fe)$", "$1ves");
048: rules.put("(\\w+)(sis)$", "$1ses");
049: rules.put("(\\w*)person$", "$1people");
050: rules.put("(\\w*)child$", "$1children");
051: rules.put("(\\w*)series$", "$1series");
052: rules.put("(\\w*)foot$", "$1feet");
053: rules.put("(\\w*)tooth$", "$1teeth");
054: rules.put("(\\w*)bus$", "$1buses");
055: rules.put("(\\w*)man$", "$1men");
056: return rules;
057: }
058:
059: /**
060: * Converts an English word to singular form
061: *
062: * @param word an English word
063: * @return the singularization of the argument English word, or the argument in case it is <code>null</code>
064: */
065: public static String singularize(String word) {
066: if (word == null)
067: return null;
068:
069: final Map rules = EnglishInflector.getSingularRules();
070: for (Iterator ruleIterator = rules.entrySet().iterator(); ruleIterator
071: .hasNext();) {
072: final Map.Entry rule = (Map.Entry) ruleIterator.next();
073: final String pattern = rule.getKey().toString();
074: final String replace = rule.getValue().toString();
075: if (word.matches(pattern)) {
076: return word.replaceFirst(pattern, replace);
077: }
078: }
079: return word.replaceFirst("([\\w]+)s$", "$1");
080: }
081:
082: /**
083: * Returns map of singular patterns
084: */
085: private static Map getSingularRules() {
086: final Map rules = new HashMap();
087: rules.put("(\\w+)(x|ch|ss)es$", "$1$2");
088: rules.put("(\\w+)([^aeiou])ies", "$1$2y");
089: rules.put("(\\w+)([^l])ves", "$1$2fe");
090: rules.put("(\\w+)([ll])ves", "$1$2f");
091: rules.put("(\\w+)(ses)$", "$1sis");
092: rules.put("(\\w*)people$", "$1person");
093: rules.put("(\\w*)children$", "$1child");
094: rules.put("(\\w*)series$", "$1series");
095: rules.put("(\\w*)feet$", "$1foot");
096: rules.put("(\\w*)teeth$", "$1tooth");
097: rules.put("(\\w*)buses$", "$1bus");
098: rules.put("(\\w*)men$", "$1man");
099: return rules;
100: }
101:
102: }
|