Make Plural

As part of “class module week”, I need a function that takes a class module name and makes it plural. I’d like to catch more than 95% of standard nouns and throw in a few non-standard ones for good measure. Here’s what I have so far.

Function MakePlural(sWord As String) As String
   
    Select Case sWord
        Case "CPerson"
            MakePlural = "CPeople"
        Case "CChild"
            MakePlural = "CChildren"
        Case "CDatum"
            MakePlural = "CData"
        Case "CAlumnus"
            MakePlural = "CAlumni"
        Case "CCriterion"
            MakePlural = "CCriteria"
        Case "CMedium"
            MakePlural = "CMedia"
        Case Else
            If Right$(sWord, 2) Like "[a,e,i,o,u]y" Then
                MakePlural = sWord & "s"
            ElseIf Right$(sWord, 2) = "is" Then
                MakePlural = Left$(sWord, Len(sWord) - 2) & "es"
            ElseIf Right$(sWord, 1) = "y" Then
                MakePlural = Left$(sWord, Len(sWord) - 1) & "ies"
            ElseIf Right$(sWord, 1) = "f" Then
                MakePlural = Left$(sWord, Len(sWord) - 1) & "ves"
            ElseIf Right$(sWord, 2) = "ss" Or Right$(sWord, 1) = "x" Then
                MakePlural = sWord & "es"
            Else
                MakePlural = sWord & "s"
            End If
    End Select
   
End Function

Thoughts?

10 Comments

  1. Claudio I. says:

    Radius vs. Radii

    And what about words that don’t change when plural?
    “Moose” “Deer” “Aircraft” “Water”

  2. Scott says:

    You missed some that do a quirky change, like: mouse -> mice, goose -> geese, man -> men, woman -> women, tooth -> teeth.

    Then there are the ones that don’t change at all, like: deer, fish, sheep.

    But c’mon. How likely are you to have a class module called “CSheep”? :-)

  3. Shawn Wheatley says:

    Ruby on Rails has been doing something similar for years with helpers associated with ActiveRecord. I don’t know how well you can read Ruby, but you might want to take a look at a pretty battle-tested piece of code to do pluralization.

    http://api.rubyonrails.org/classes/ActionView/Helpers/TextHelper.html#M002280

  4. Ron says:

    And what happens if an already plural word is passed in. There is no check for that. Fun exercise.

  5. Scott: I definitely left out some irregulars that would *never* be classes – at least in any apps I’m going to write.

    Shawn: I guess I can’t read Ruby, because I could not make heads or tails of it. I didn’t see any real string manipulation, just further calls to pluralize.

    Ron: If you pass in a plural, e.g. Jobs, you get Jobss. I don’t think I would change that behavior. Should I?

  6. Ron says:

    I would think an error should be returned or return the original word since you can’t pluralize a plural. But I may be over-complicating the exercise.

  7. Jon Peltier says:

    For words that are the same in singular and plural, you could do what MS has done:

    Singular: Series
    Plural: SeriesCollection

  8. Rob van Gelder says:

    You could capture most cases, then leave it as an exercise for the end-user for the remainder, storing singular/plural matches as you go, so they only need to do it once.

    Also: found some examples googled ‘code to pluralize’

  9. Rick Williams says:

    Here’s one in PHP, see the source code link below the form.

    It seems to have a lengthy list of specific cases, and makes good use of regular expressions.

Leave a Reply