1

Resolved

ToPlural

description

I noticed a bug in string.ToPlural(). It will pluralize any word ending in "y" with "ies". But that only the case if the letter before the "Y" is a consonants. If the 2nd to last letter is a vowel, just "s" is used (e.g. the plural of "boy" is "boys" not "boies")
 
//-ies rule
if (singular.EndsWith("y") && "aeiou".IndexOf(singular.Substring(singular.Length-2,1)) == -1)
    return singular.Remove(singular.Length - 1, 1) + "ies";
 
Next, I'm not exactly sure what you want the result of "A of Bs" to be (either "As" or "As of Bs"), but it doesn't do either. It gave "A of Bss". This should correct that (giving "As of Bs"):
 
// Multiple words in the form A of B : Apply the plural to the first word only (A)
int index = singular.LastIndexOf(" of ");
if (index > 0) return (singular.Substring(0, index).ToPlural()) + singular.Substring(index);
 
Finally, you forgot the words ending in "x" should be pluralized with "es":
 
if (singular.EndsWith("x")) return singular + "es";
 
Previously, "box of balls".ToPlural() gave "box of ballss". With these changes, it gives "boxes of balls"

comments

PatrickLorenz wrote Nov 7, 2011 at 12:12 PM

Hi James,

sounds good. Would you like to implement the change directly?

Regards,

Patrick

akronite wrote Nov 8, 2011 at 12:20 AM

Hi James,

Thank you for your suggestions.

I've been holding this one off for a while,
because there isn't a easy way to cover all the "bases" when it comes to English.

Here are the tests i'm looking at doing to the "ToPlural()" method:

Assert.AreEqual("test".ToPlural(), "tests"); // standard
Assert.AreEqual("goose".ToPlural(), "geese"); // special nouns
Assert.AreEqual("box".ToPlural(), "boxes"); // -ch, x, s to -es
Assert.AreEqual("boy".ToPlural(), "boys"); // -y to -ies
Assert.AreEqual("box of ball".ToPlural(), "box of balls"); // of
Assert.AreEqual("kiss".ToPlural(), "kisses"); // -s to -es
Assert.AreEqual("phenomenon".ToPlural(), "phenomena"); // nouns that maintain their Latin or Greek form in the plural

Assert.AreEqual("potato".ToPlural(), "potatoes"); // -o to -oes
Assert.AreEqual("memo".ToPlural(), "memos"); // -o to -oes (exceptions)
Assert.AreEqual("stereo".ToPlural(), "stereos"); // -o to -oes (exceptions)

Assert.AreEqual("knife".ToPlural(), "knives"); // -f, fe to -es
// special -f exceptions (complex)
// Assert.AreEqual("dwarf".ToPlural(), "dwarfs"); // -f, fe to -es (exceptions)
// Assert.AreEqual("roof".ToPlural(), "roofs"); // -f, fe to -es (exceptions)

As you've guested, these tests will fail on the current extensions.
This is why i've implemented a new set of "overload" method to fix this problem.
Due to the limitation of the current extension being .NET 3.5. I've created a new .NET 4.0 extension project ("PGK.Extensions4")

Here's what the method look like:
    /// <summary>
    /// Returns the plural form of the specified word.
    /// </summary>
    /// <param name="singular">The singular string value</param>
    /// <param name="count">How many of the specified word there are. 
    A count equal to 1 will not pluralize the specified word.</param>
    /// <param name="cultureInfo">Provide a culture info to pluralize (default to en-US)</param>
    /// <returns>A string that is the plural form of the input parameter.</returns>
    public static string ToPlural(this string singular, int count = 0, CultureInfo cultureInfo = null)
    {
        return count == 1 ? singular : PluralizationService.CreateService(cultureInfo ?? new CultureInfo("en-US")).Pluralize(singular);
    }
I also had name the method to "ToPlural2()" because the first has already been taken (long story).
Anyway, short answer is that this is tested (with the test above), checked in and ready for using.

NOTE for Patrick: Could you please generate the key (.snk file) for this new project? Thank you

Let me know if you have any questions or feedbacks.

wrote Nov 8, 2011 at 12:30 AM

wrote Nov 8, 2011 at 12:30 AM

wrote Nov 8, 2011 at 12:30 AM

wrote Feb 22, 2013 at 12:19 AM

wrote May 16, 2013 at 11:51 AM

wrote May 16, 2013 at 11:51 AM

wrote Jun 14, 2013 at 8:26 AM