Ruby on Rails
InflectorInCSharp
using System;
using System.Collections;
using System.Text.RegularExpressions;
/// <summary>
/// This is for pluralising and de-pluralising strings
/// <a href="http://dev.rubyonrails.org/browser/trunk/activerecord/lib/active_record/support/inflector.rb?rev=4">http://dev.rubyonrails.org/browser/trunk/activerecord/lib/active_record/support/inflector.rb?rev=4</a>
/// <a href="http://www.knowdotnet.com/articles/regereplacementstrings.html">http://www.knowdotnet.com/articles/regereplacementstrings.html</a>
/// </summary>
public class Inflector
{
// To ensure this is a singleton
public static readonly Inflector Instance = new Inflector();
private Inflector() {}
static Inflector() { }
private static Inflector_Rule[] Plural_Rules = new Inflector_Rule[] {
new Inflector_Rule("(x|ch|ss)$","$1es"), // search, switch, fix, box, process, address
new Inflector_Rule("([^aeiouy]|qu)y$","$1ies"), // query, ability, agency
new Inflector_Rule("(?:([^f])fe|([lr])f)$","$1$2ves"), // half, safe, wife
new Inflector_Rule("sis$","ses"), // basis, diagnosis
new Inflector_Rule("([ti])um$","$1a"), // datum, medium
new Inflector_Rule("person$", "people"), // person, salesperson
new Inflector_Rule("man$","men"), // man, woman, spokesman
new Inflector_Rule("child$","children"), // child
new Inflector_Rule("s$","s"), // no change (compatibility)
new Inflector_Rule("$","s")
};
public string Pluralise(string word)
{
foreach (Inflector_Rule rule in Plural_Rules)
{
if (Regex.IsMatch(word,rule.rule))
{
return Regex.Replace(word,rule.rule,rule.replacement);
}
}
return word + 's';
}
}
class Inflector_Rule
{
public string rule;
public string replacement;
public Inflector_Rule(string Rule,string Replacement)
{
rule=Rule;
replacement=Replacement;
}
}