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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
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?
Radius vs. Radii
And what about words that don’t change when plural?
“Moose” “Deer” “Aircraft” “Water”
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”? :-)
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
And what happens if an already plural word is passed in. There is no check for that. Fun exercise.
I’d like to see you try that with Welsh ;-)
http://uk.answers.yahoo.com/question/index?qid=20090110141552AAcJv8p
Rob
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?
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.
For words that are the same in singular and plural, you could do what MS has done:
Singular: Series
Plural: SeriesCollection
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’
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.
=IF(RIGHT(B16,2)="is",LEFT(B16,LEN(B16)-2)&"es",IF(RIGHT(B16,2)="us",LEFT(B16,LEN(B16)-2)&"i",IF(OR(RIGHT(B16,2)="ss",RIGHT(B16,2)="ch",RIGHT(B16,2)="sh"),B16&"es",IF(RIGHT(B16,2)="fe",LEFT(B16,LEN(B16)-2)&"ves",IF(OR(RIGHT(B16,1)="s",RIGHT(B16,1)="o",RIGHT(B16,1)="x",RIGHT(B16,1)="z"),B16&"es",IF(RIGHT(B16,1)="y",LEFT(B16,LEN(B16)-1)&"ies",IF(RIGHT(B16,1)="f",LEFT(B16,LEN(B16)-1)&"ves",IF(RIGHT(B16,2)="on",LEFT(B16,LEN(B16)-2)&"a",B16&"s"))))))))
abc formula didn’t work until I replaced the quotation marks. It works great now except for words that are plural already. It also does not wok great with irregular nouns.
WordPress like to make quote marks fancy. I put <code> tags around it to fix the quote marks