Monday 10 June 2013

Pluralize/Singularize Words with the .NET Framework

You may sometimes come across the need to pluralize or singularize words automatically within your applications. As the English language is a fairly complicated one this is not as straight forward as adding or removing an "S" on the end of a word. There are various different implementation out there that perform with a varying degree of success.

Those of you who have used the Entity Framework may have noticed the designer attempts to singularize/pluralize words for us, and in my opinion does as good a job if not better than other algorithms I have tested out.

Thankfully those kind guys at Microsoft have opened up this functionality to the rest of via the PluralizationService and it couldn't be simpler to use. For this you will need to add a reference to System.Data.Entity.Design and a using statement for the System.Data.Entity.Design.PluralizationServices namespace.

PluralizationService is an abstract class so we need to use the static CreateService method to instantiate the service. The CreateService method takes one parameter, System.Globalization.CultureInfo, so I'll pass through my current thread's CurrentCulture.

The PluralizationService object exposes four methods of interest, IsPlural, IsSingular, Pluralize and Singularize. All four methods are self-explanitory and do exactly what the name suggests.

Here is the code I wrote to test this service, along with the output, to illustrate how to use this service.

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data.Entity.Design.PluralizationServices;
  4. using System.Threading;
  5.  
  6. namespace PluralizationServiceTest
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             PluralizationService service = PluralizationService.CreateService(Thread.CurrentThread.CurrentCulture);
  13.  
  14.             List<string> words = new List<string>
  15.             {
  16.                 "babies",
  17.                 "desks",
  18.                 "lady",
  19.                 "child",
  20.                 "data",
  21.                 "bacterium",
  22.                 "elves",
  23.                 "geese",
  24.                 "lice",
  25.                 "mouse",
  26.                 "people",
  27.                 "women",
  28.                 "tooth"
  29.             };
  30.  
  31.             foreach (string word in words)
  32.             {
  33.                 string singular = null;
  34.                 string plural = null;
  35.  
  36.                 if (service.IsSingular(word))
  37.                 {
  38.                     singular = word;
  39.                     plural = service.Pluralize(word);
  40.                 }
  41.                 else
  42.                 {
  43.                     singular = service.Singularize(word);
  44.                     plural = service.Pluralize(word);
  45.                 }
  46.  
  47.                 Console.WriteLine(String.Format("Input: {0}, Singular: {1}, Plural: {2}", word, singular, plural));
  48.             }
  49.         }
  50.     }
  51. }

Output

Input: babies, Singular: baby, Plural: babies
Input: desks, Singular: desk, Plural: desks
Input: lady, Singular: lady, Plural: ladies
Input: child, Singular: child, Plural: children
Input: data, Singular: datum, Plural: data
Input: bacterium, Singular: bacterium, Plural: bacteria
Input: elves, Singular: elf, Plural: elves
Input: geese, Singular: goose, Plural: geese
Input: lice, Singular: louse, Plural: lice
Input: mouse, Singular: mouse, Plural: mice
Input: people, Singular: person, Plural: people
Input: women, Singular: woman, Plural: women
Input: tooth, Singular: tooth, Plural: teeth

No comments:

Post a Comment