C# Plurals

Dictionary

Programs sometimes need to handle plural words. A layer of abstraction helps manage plural and singular nouns. A lookup table of known values can provide better performance, smaller memory footprint, and fewer errors.

Dictionary

Example

This program provides a simple abstraction on the procedure of obtaining a plural word from a singular word based on the count. This means it can be used to get the phrase "2 results" from the values (2) and "result".

Note

Note:The implementation is not advanced but offers the benefit of simplicity and can improve the overall structure of certain programs.

And:Because the problem of parsing English text is extremely difficult, this class simply uses hard-coded values and does a lookup.

Program that resolves plural words: C#

using System;
using System.Collections.Generic;

public static class Plurals
{
    static Dictionary<string, string> _pluralTable =
	new Dictionary<string, string>
    {
	{"entry", "entries"},
	{"image", "images"},
	{"view", "views"},
	{"file", "files"},
	{"result", "results"},
	{"word", "words"},
	{"definition", "definitions"},
	{"item", "items"},
	{"megabyte", "megabytes"},
	{"game", "games"}
    };

    public static string Plural(string word, int count)
    {
	// Special-case count of one.
	// ... Otherwise, return the pluralized word.
	if (count == 1)
	{
	    return word;
	}
	return _pluralTable[word];
    }

    public static string PluralPhrase(string word, int count)
    {
	// Returns phrase of complete pluralized phrase.
	// .. Such as "3 files".
	string properPlural = Plural(word, count);
	return count.ToString() + " " + properPlural;
    }
}

class Program
{
    static void Main()
    {
	Console.WriteLine(Plurals.Plural("word", 0));
	Console.WriteLine(Plurals.Plural("word", 1));
	Console.WriteLine(Plurals.Plural("word", 2));
	Console.WriteLine(Plurals.PluralPhrase("view", 0));
	Console.WriteLine(Plurals.PluralPhrase("view", 1));
	Console.WriteLine(Plurals.PluralPhrase("view", 2));
    }
}

Output

words
word
words
0 views
1 view
2 views
Note

Plural receives the word and the count of the word in the sentence. It returns a version of the word that it is correctly pluralized if located in the Dictionary. PluralPhrase returns a string with the original number in it.

Static Dictionary storage. This program shows the static Dictionary as well. You can use a static Dictionary and use a field initializer to populate it with values before it is used.

Static ModifierStatic FieldStatic

Info:The indexer on the Dictionary is used. This will throw an exception if the string is not found.

And:The methods provide fast lookup even with many strings in the Dictionary. You can populate the Dictionary through a text file.

Discussion

Programming tip

Declarative, expression-based programming styles can benefit certain program requirements. In some simple programs, a ternary expression or if-statement is used to handle the English plural rules for a count of one.

However:If this pattern is repeated often, it can become a burden on the overall structure.

Thus:Using a static and immutable data structure can simplify programs. This provides another level of indirection.

Summary

Here we looked at a simple class that demonstrates a private and static Dictionary when used as a data structure for providing plural words. The program receives singular words and looks these words up to form singular words.


C#: Method