Jump to content

Forgive my lack of terminology, I'm new to python and programming in general. I'm writing a code to generate simple phrases from a defined input. I need a way to detect if a word(s) are plular. I'm using inflect(inflect.py) to turn numerals into words already and have seem the pluralize function. But I need a simple true/false output to set the parameters to create proper verb agreement. Help?

Link to comment
https://linustechtips.com/topic/482017-python-detecting-plurals/
Share on other sites

Link to post
Share on other sites

If you have way of generating plurals, like inflect, you could do something like:

isPlural = word == inflect.plural(word)

You may get strange results if the word cannot possibly be pluralized, like "hello". I'm not sure how inflect handles that.

Edit: I just read the Inflect readme on github. Calling the plural function on a plural may give undefined results, therefore what I said above won't work.

Link to comment
https://linustechtips.com/topic/482017-python-detecting-plurals/#findComment-6466671
Share on other sites

Link to post
Share on other sites

The words I need to detect will be team names. I need to distinguish between names of the teams cities and their mascot(so that "new York beats" can be written and "giants beat" won't be "giants beats". By detecting the words singularity or plural I'll be adding an if/else function to adjust the verb.

Link to comment
https://linustechtips.com/topic/482017-python-detecting-plurals/#findComment-6466792
Share on other sites

Link to post
Share on other sites

Playing around with the inflect library a bit, there's the singular_noun() function.  It returns a boolean False if the argument is already singular, and the singular form if the argument is plural:

import inflectp = inflect.engine()# Returns boolean Falsep.singular_noun('test')# Returns the singular form, 'test'p.singular_noun('tests')

So you should be able to do something like this:

# Make sure verb is in singular formverb_initial = 'wins'noun = 'New York Giants'# Check if singular_noun is plural, use plural verb form if so.if p.singular_noun(noun) != False:    verb_final = p.plural_verb(verb_initial)else:    verb_final = verb_initial# Prints "New York Giants win"print(noun + ' ' + verb)

You would just have to make sure that all your verbs are in the singular form by default.  Otherwise, p.plural_verb() will do nothing, and you'll always be using the plural forms with the above code.

Link to comment
https://linustechtips.com/topic/482017-python-detecting-plurals/#findComment-6467261
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×