I wanna check if a string is singular or plural in PHP.
-
you need a dictionary for that if you want to do it reliably – Gordon Jan 27 '12 at 10:42
-
Think for yourself. What are the characteristics of a plural word? The last letter "s" in many cases. But there are exceptions, too. Another and innovative way would be to get ressources from a online dictionary and use that. – danijar Jan 27 '12 at 10:43
3 Answers
Not easy to do, even just in one language like English. First, you need to identify your nouns.
Using a rule such as: word ends in "s" is very simplistic,
SHEEP singular SHEEP plural
LADY singular LADIES plural but what about LADY'S
So you need to get a lot cleverer, and test for an apostrophe immediately before the S
Try using something like a Brill Parser, this can identify nouns and could probably be adapted to identify singular/plural with a reasonable degree of accuracy, but even that isn't perfect.
Then again, you could want to do this in French, or German... you're incredibly broad question doesn't identify a language
EDIT
Examples of the complexities of singular/plural in English are described in this wikipedia article
A technique like porter stemming can identify the "root" of a word, and comparison with the actual word could help check if the extension was a typical singluar or plural. A PHP implementation of a Porter Stemmer is available here.

- 209,507
- 32
- 346
- 385
-
Then you get *rhinoceros*, a non-plural noun ending in an s with no sign of an apostrophe. – Quentin Jan 27 '12 at 10:50
-
Given the dificulty already shown about the incertainty of plural words in different languages, and in English (and others) not allways ending in "s" for plural, and also sometimes words ending in "s" not being plural, I'll try to provide a possible resolution.
You could create a big array for words that plurals allways end with "s", and another for irregular plural, and validate each word of the sentence towards it.
I'll complete this post with some working PHP example later.
I've found some java example here

- 1,442
- 17
- 20
Well that depends on your language in is not easy, it might be even undecidable.
How do you for example distinguish "man" and "men"?
You could use a heuristic, like everything, that ends on "s" is a plural and provide additional exception cases, like the mentioned "man". This also leads into false answers because for example "kiss" ends on "s" but is no plural.

- 31,591
- 21
- 89
- 127