13

Is there any function (VB.NET or C#) that can transform an English word into its singular or to its plural form?

I was thinking of having a database that contains all words in English as well as their plural form but I also think that it is stupid since that will be huge and besides there are rules in English on how to translate a word to its plural form, so why not create a function that does the transformation?

hawbsl
  • 15,313
  • 25
  • 73
  • 114
kazinix
  • 28,987
  • 33
  • 107
  • 157
  • Maybe storing only the non-deducible exceptions and calculating the rest by some formula(s)? – Uwe Keim Oct 19 '11 at 05:02
  • possible duplicate of [How to turn plural words singular?](http://stackoverflow.com/questions/796412/how-to-turn-plural-words-singular) – paxdiablo Oct 19 '11 at 05:05
  • @paxdiablo, thanks, but it only deals with plural to singular, not vice versa. – kazinix Oct 19 '11 at 05:06
  • Have a look at the top-voted answer, it works both ways. But you're probably right, since the questions are subtly different, they're not a dupe. – paxdiablo Oct 19 '11 at 05:07
  • It does however seem to be a duplicate of the one @CouncilScribe found: http://stackoverflow.com/questions/475705/pluralize-singularize (although I'm perfectly happy to be told otherwise). – paxdiablo Oct 19 '11 at 05:14

4 Answers4

36

In the System.Data.Entity.Design dll there is a namespace called PluralizationServices.

System.Data.Entity.Design.PluralizationServices.PluralizationService.CreateService(CultureInfo.GetCultureInfo("en-us")) will give you an object that exposes the self explanatory Pluralize, Singularize, IsPlural and IsSingular methods.

EF uses it to pluralize and singularize table names.

Greatran
  • 180
  • 3
  • 18
Iain
  • 2,259
  • 1
  • 16
  • 18
  • Awesome. I was gonna do it the hard way with a collection of words and their plurals, but this PluralizationServices should cover 95% of the cases, and that's good enough for me. (There are certain words it gets wrong, like "Christmas". It thinks it's plural and creates a singular of "Christma". I'm sure there are many other flubs, but like I said, 95%.) – Doug S Aug 11 '16 at 10:24
  • awesome. are there such equivalents for Turkish? – Furkan Gözükara Mar 03 '17 at 15:03
11

You can use Humanizer micro library. It's capable of this and a lot more.

Disclaimer: I am the creator of the library.

Mehdi Khalili
  • 927
  • 1
  • 11
  • 18
1

I would just write a pluralise function that you could fill with the generic and specific rules that create plural words. This question has been mentioned before here.

Community
  • 1
  • 1
CouncilScribe
  • 691
  • 7
  • 19
1

I answered a very similar question but not close enough to be a dupe, so I'll paraphrase here.


My own preference would be to have a transformation engine along with a set of transformations (surprisingly enough) for doing the actual work.

You would run through the transformations (from specific to general) and, when a match was found, apply the transformation to the word.

Regular expressions would be an ideal approach to this due to their expressiveness. An example rule set for converting plural to singular could be:

 1. If the word is fish, return fish.
 2. If the word is sheep, return sheep.
 3. If the word is "radii", return "radius".
 4. If the word is "types", return "type".
 5. If the word ends in "ii", replace that "ii" with "us" (octopii,virii).
    : : : : :
97. If a word ends with -ies, I replace the ending with -y
98. If a word ends with -es, I remove this ending.
99. Otherwise, I just remove the trailing -s.

Remember that this is an example, so don't give me a hard time if they're not entirely perfect English. In any case, that just means you should add more rules, which is the whole essence of the answer.

Note that an earlier version of the rules may not have had entry number 4. However, when we found the problem with "types" being transformed to "typ" at rule 98, we then created a higher-priority transformation at 4 to cater for this.

You'll basically need to keep this transformation table updated as you find all those wondrous exceptions (and there are many) that English has spawned.

A similar scheme could be put together for the other direction, singular to plural.

The major problem with this is that you'll have to catch all those edge cases that English is famous for but, short of convincing the entire planet to speak Esperanto, that may be your best shot :-)

Community
  • 1
  • 1
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • Note that is exactly what http://msdn.microsoft.com/en-us/library/system.data.entity.design.pluralizationservices.pluralizationservice.aspx does (the internal class `System.Data.Entity.Design.PluralizationServices.EnglishPluralizationService` provides it). – Jeroen Wiert Pluimers Oct 26 '13 at 16:47
  • @JeroenWiertPluimers There is no `System.Data.Entity.Design.PluralizationServices.EnglishPluralizationService` class. Perhaps you meant `System.Data.Entity.Infrastructure.Pluralization.EnglishPluralizationService`. In either case, the accepted answer provides a class that has a couple more useful methods, namely `IsPlural()` and `IsSingular()`. – Doug S Aug 11 '16 at 10:20