using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Tanshu.Accounts.SqlDAO
{
using System.Collections;
using System.Collections.Generic;
using System.Text.RegularExpressions;
///
/// The Inflector class transforms words from one
/// form to another. For example, from singular to plural.
///
public class Inflector
{
private static readonly List plurals = new List();
private static readonly List singulars = new List();
private static readonly List uncountables = new List();
///
/// Initializes static members of the class.
///
static Inflector()
{
AddPlural("$", "s");
AddPlural("s$", "s");
AddPlural("(ax|test)is$", "$1es");
AddPlural("(octop|vir)us$", "$1i");
AddPlural("(alias|status)$", "$1es");
AddPlural("(bu)s$", "$1ses");
AddPlural("(buffal|tomat)o$", "$1oes");
AddPlural("([ti])um$", "$1a");
AddPlural("sis$", "ses");
AddPlural("(?:([^f])fe|([lr])f)$", "$1$2ves");
AddPlural("(hive)$", "$1s");
AddPlural("([^aeiouy]|qu)y$", "$1ies");
AddPlural("(x|ch|ss|sh)$", "$1es");
AddPlural("(matr|vert|ind)ix|ex$", "$1ices");
AddPlural("([m|l])ouse$", "$1ice");
AddPlural("^(ox)$", "$1en");
AddPlural("(quiz)$", "$1zes");
AddSingular("s$", string.Empty);
AddSingular("(n)ews$", "$1ews");
AddSingular("([ti])a$", "$1um");
AddSingular("((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$", "$1$2sis");
AddSingular("(^analy)ses$", "$1sis");
AddSingular("([^f])ves$", "$1fe");
AddSingular("(hive)s$", "$1");
AddSingular("(tive)s$", "$1");
AddSingular("([lr])ves$", "$1f");
AddSingular("([^aeiouy]|qu)ies$", "$1y");
AddSingular("(s)eries$", "$1eries");
AddSingular("(m)ovies$", "$1ovie");
AddSingular("(x|ch|ss|sh)es$", "$1");
AddSingular("([m|l])ice$", "$1ouse");
AddSingular("(bus)es$", "$1");
AddSingular("(o)es$", "$1");
AddSingular("(shoe)s$", "$1");
AddSingular("(cris|ax|test)es$", "$1is");
AddSingular("(octop|vir)i$", "$1us");
AddSingular("(alias|status)es$", "$1");
AddSingular("^(ox)en", "$1");
AddSingular("(vert|ind)ices$", "$1ex");
AddSingular("(matr)ices$", "$1ix");
AddSingular("(quiz)zes$", "$1");
AddIrregular("person", "people");
AddIrregular("man", "men");
AddIrregular("child", "children");
AddIrregular("sex", "sexes");
AddIrregular("move", "moves");
AddUncountable("equipment");
AddUncountable("information");
AddUncountable("rice");
AddUncountable("money");
AddUncountable("species");
AddUncountable("series");
AddUncountable("fish");
AddUncountable("sheep");
}
///
/// Prevents a default instance of the class from being created.
///
private Inflector()
{
}
///
/// Capitalizes a word.
///
///
/// The word to be capitalized.
///
///
/// capitalized.
///
public static string Capitalize(string word)
{
return word.Substring(0, 1).ToUpper() + word.Substring(1).ToLower();
}
///
/// Return the plural of a word.
///
///
/// The singular form
///
///
/// The plural form of
///
public static string Pluralize(string word)
{
return ApplyRules(plurals, word);
}
///
/// Return the singular of a word.
///
///
/// The plural form
///
///
/// The singular form of
///
public static string Singularize(string word)
{
return ApplyRules(singulars, word);
}
///
/// Adds the irregular.
///
/// The singular.
/// The plural.
private static void AddIrregular(string singular, string plural)
{
AddPlural("(" + singular[0] + ")" + singular.Substring(1) + "$", "$1" + plural.Substring(1));
AddSingular("(" + plural[0] + ")" + plural.Substring(1) + "$", "$1" + singular.Substring(1));
}
///
/// Adds the plural.
///
/// The replacement rule.
/// The replacement.
private static void AddPlural(string rule, string replacement)
{
plurals.Add(new Rule(rule, replacement));
}
///
/// Adds the singular.
///
/// The replacement rule.
/// The replacement.
private static void AddSingular(string rule, string replacement)
{
singulars.Add(new Rule(rule, replacement));
}
///
/// Adds the uncountable.
///
/// The uncountable word.
private static void AddUncountable(string word)
{
uncountables.Add(word.ToLower());
}
///
/// Applies the rules.
///
/// The replacement rules.
/// The word to be replaced.
/// String resulting from rules being applied.
private static string ApplyRules(IList rules, string word)
{
string result = word;
if (!uncountables.Contains(word.ToLower()))
{
for (int i = rules.Count - 1; i >= 0; i--)
{
var rule = (Rule)rules[i];
if ((result = rule.Apply(word)) != null)
{
break;
}
}
}
return result;
}
///
/// Replacement rule.
///
private class Rule
{
private readonly Regex regex;
private readonly string replacement;
///
/// Initializes a new instance of the class.
///
///
/// The pattern.
///
///
/// The replacement.
///
public Rule(string pattern, string replacement)
{
this.regex = new Regex(pattern, RegexOptions.IgnoreCase);
this.replacement = replacement;
}
///
/// Applies the specified word.
///
/// The word to be replaced.
/// Regex replaced word.
public string Apply(string word)
{
if (!this.regex.IsMatch(word))
{
return null;
}
return this.regex.Replace(word, this.replacement);
}
}
}
}