Click here to Skip to main content
15,885,955 members
Articles / Database Development / NoSQL

RavenDB - An Introduction

,
Rate me:
Please Sign up or sign in to vote.
4.87/5 (38 votes)
28 Apr 2010CPOL7 min read 262.6K   2.7K   112  
An introduction to RavenDB - a new open source .NET document database using .NET 4.0 and VS 2010
// Copyright 2004-2010 Castle Project - http://www.castleproject.org/
// 
// 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.

using System.Collections;
using System.Text.RegularExpressions;

namespace Raven.Client.Util
{
	/// <summary>
	/// The Inflector class transforms words from one 
	/// form to another. For example, from singular to plural.
	/// </summary>
	public class Inflector
	{
		private static readonly ArrayList plurals = new ArrayList();
		private static readonly ArrayList singulars = new ArrayList();
		private static readonly ArrayList uncountables = new ArrayList();

		private Inflector()
		{
		}

		#region Default Rules

		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$", "");
			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");
		}

		#endregion

		#region Rule inner class

		private class Rule
		{
			private readonly Regex regex;
			private readonly string replacement;

			public Rule(string pattern, string replacement)
			{
				regex = new Regex(pattern, RegexOptions.IgnoreCase);
				this.replacement = replacement;
			}

			public string Apply(string word)
			{
				if (!regex.IsMatch(word))
				{
					return null;
				}

				return regex.Replace(word, replacement);
			}
		}

		#endregion

		/// <summary>
		/// Return the plural of a word.
		/// </summary>
		/// <param name="word">The singular form</param>
		/// <returns>The plural form of <paramref name="word"/></returns>
		public static string Pluralize(string word)
		{
			return ApplyRules(plurals, word);
		}

		/// <summary>
		/// Return the singular of a word.
		/// </summary>
		/// <param name="word">The plural form</param>
		/// <returns>The singular form of <paramref name="word"/></returns>
		public static string Singularize(string word)
		{
			return ApplyRules(singulars, word);
		}

		/// <summary>
		/// Capitalizes a word.
		/// </summary>
		/// <param name="word">The word to be capitalized.</param>
		/// <returns><paramref name="word"/> capitalized.</returns>
		public static string Capitalize(string word)
		{
			return word.Substring(0, 1).ToUpper() + word.Substring(1).ToLower();
		}

		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));
		}

		private static void AddUncountable(string word)
		{
			uncountables.Add(word.ToLower());
		}

		private static void AddPlural(string rule, string replacement)
		{
			plurals.Add(new Rule(rule, replacement));
		}

		private static void AddSingular(string rule, string replacement)
		{
			singulars.Add(new Rule(rule, replacement));
		}

		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--)
				{
					Rule rule = (Rule)rules[i];

					if ((result = rule.Apply(word)) != null)
					{
						break;
					}
				}
			}

			return result;
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
I've been a software developer since 1996 and have enjoyed C# since 2003. I have a Bachelor's degree in Computer Science and for some reason, a Master's degree in Business Administration. I currently do software development contracting/consulting.

Written By
Israel Israel
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions