Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test whether a word is in singular form or not in python?

I am trying to get whether a word is in singular form or in plural form by using nltk pos_tag. But the results are not accurate.

So, I need a way to find how can get whether a word is in singular form or in plural form? moreover I need it without using any python package.

like image 793
user2605977 Avatar asked Sep 20 '13 07:09

user2605977


People also ask

How do you know if a word is singular?

The easiest way to tell if a noun is a singular noun or a plural noun is to look at how much of something it is referring to. If it is only referring to one person or thing, it is a singular noun. If it is referring to more than one person or thing, it is a plural noun.

How do you determine if a word is plural?

A plural noun is the form of a noun used to show there are more than one. Most nouns simply add –s or –es to the end to become plural.

How do you determine singular and plural?

The difference between singular and plural nouns is easy to spot. When a noun indicates one only, it is a singular noun. When a noun indicates more than one, it is plural.


2 Answers

For English, every word should somehow have a root lemma where the default plurality is singular.

Assuming that you have only nouns in your list, you can try this:

from nltk.stem import WordNetLemmatizer

wnl = WordNetLemmatizer()

def isplural(word):
    lemma = wnl.lemmatize(word, 'n')
    plural = True if word is not lemma else False
    return plural, lemma

nounls = ['geese', 'mice', 'bars', 'foos', 'foo', 
                'families', 'family', 'dog', 'dogs']

for nn in nounls:
    isp, lemma = isplural(nn)
    print nn, lemma, isp

You will have a problem when word is out of wordnet, then you have to use more sophiscated classifier or finite state machines out of NLTK.

like image 149
alvas Avatar answered Jan 02 '23 21:01

alvas


Assuming you want an English solution, you can do something similar to 2er0's solution a bit more directly with pattern-en:

from pattern.en import singularize

def isplural(pluralForm):
     singularForm = singularize(pluralForm)
     plural = True if pluralForm is not singularForm else False
     return plural, singularForm

nounls = ['geese', 'mice', 'bars', 'foos', 'foo', 
            'families', 'family', 'dog', 'dogs']

for pluralForm in nounls:
    isp, singularForm = isplural(pluralForm)
    print pluralForm, singularForm, isp

which outputs

geese goose True
mice mouse True
bars bar True
foos foo True
foo foo False
families family True
family family False
dog dog False
dogs dog True

the only difference in output between 2er0's solution and this is

foos foo True

since his solution outputs False, as he pointed out since foos is not in wordnet (and not an English word at all).

like image 29
arturomp Avatar answered Jan 02 '23 21:01

arturomp