Reputation: 1032
Question like in topic - I'm trying to do that in python for app in Google App Engine. I know PyEnchant library is used for natural language recognition but I don't see if I can use it for my problem and how.
Upvotes: 9
Views: 23111
Reputation: 598
Ashwini referred to the useful inflect library, but did not explain how to check if a given word is in the plural or singular form.
If you know that the word is either a singular or a plural you can use:
singular_noun(word)
This will return False
if the word is not a plural, therefore your word should in theory be singular.
Do note the shortcomings displayed in my examples when it comes to classical plurals, forms that can be singular or plural, and the fact that it will return False for unrecognised forms in general.
import inflect
inflect = inflect.engine()
english_words = ["hat", "hats",
"hero", "heroes",
"cherry", "cherries",
"dish", "dishes",
"stadium", "stadia", "stadiums",
"mitochondrion", "mitochondria",
"sheep", "a sheep", "the sheep",
"whjkjhkjh", "msipelling"]
for en in english_words:
if not inflect.singular_noun(en):
print (en, "is singular")
else:
print (en, "is plural")
>>>
hat is singular
hats is plural
hero is singular
heroes is plural
cherry is singular
cherries is plural
dish is singular
dishes is plural
stadium is singular
stadia is singular
stadiums is plural
mitochondrion is singular
mitochondria is singular
sheep is plural
a sheep is plural
the sheep is plural
whjkjhkjh is singular
Upvotes: 21
Reputation: 250871
Checkout the inflect 0.2.4 library.
inflect 0.2.4
Correctly generate plurals, singular nouns, ordinals, indefinite articles; convert numbers to words
Upvotes: 12
Reputation: 38033
You do not say if your problem is isolated words or words in the context of English language sentences.
For example "the sheep" could be either single or plural. However:
The sheep was in the field
is singular and
The sheep were in the field
is plural.
For the latter you need a part-of-speech tagger, which will identify the roles of nouns in the sentence. There are many free and commercial ones and Wikipedia has an excellent list. NLTK is probably the natural choice for Python.
If you have only isolated words the best you can do is to refer to the many dictionaries (such as Wordnet which will indicate the singular and plural forms of nouns).
Upvotes: 3