inflection icon indicating copy to clipboard operation
inflection copied to clipboard

[Feature Request] Could `pluralize` be dynamic based on a count argument?

Open bastienboutonnet opened this issue 4 years ago • 7 comments

I was wondering if there would be a case for having a version of pluralize() that could automatically decide on whether to pluralise things based on a count similar to what the inflect package does. Their API looks like this:

import inflect
p = inflect.engine()
number_of_cats = 3
print(f"I have {number_of_cats} {p.plural('cat',number_of_cats)}")

Looking at the code, I think it'd be an easy change or addition if you would rather keep pluralize() to only do plural and we could find a more generic name which could be number_agreement() or whaetever we can come up with.

I'd be more than happy to contribute to the feature if this change was welcome.

I really like your package better than inflect and would rather not add a new dependency into my tools or implement this decision every time from "scratch" in my code.

Let me know what you think.

bastienboutonnet avatar Dec 23 '20 18:12 bastienboutonnet

I like this idea, but since this code references Rails as an inspiration it might be best to swap your proposed arguments to match Rails' API which would be pluralize(count, singular), which would be:

error_count = 1
pluralize(error_count, "error")

I ended up implementing my own local copy of pluralize like the above with:

from inflection import pluralize as _pluralize


def pluralize(count, singular):
    if count == 1:
        return f"1 {singular}"

    return f"{count} {_pluralize(singular)}"

What do you think about adding something like that in @jpvanhal?

nickjj avatar Apr 05 '21 19:04 nickjj

Yeah that's kind of exactly what I was thinking about. It'd be nice to have this ability IMO :) If there is anything I can help with let me know. If not looks like there's already the begining of a patch here.

bastienboutonnet avatar Apr 07 '21 16:04 bastienboutonnet

I'm not sure if it should be patched in directly as is because it breaks older implementations by changing the function signature and return value. For example, what if you might want the pluralized word returned without the count? This use case isn't an issue with Ruby because Ruby has a "foo.pluralize" string method to use if you don't want the count.

What do you think about maybe introducing a pluralize_count function which does the above, this way the original pluralize works as it does before and it can still be called in the new pluralize_count function.

Personally I always want the count in the use cases I've been using it for so I'm ok with either solution. Really comes down to what the maintainer of this library wants to do!

nickjj avatar Apr 07 '21 16:04 nickjj

Good point. And yeah that was the proposal I was making with a probably badly named function that I called number_agreement.

bastienboutonnet avatar Apr 07 '21 17:04 bastienboutonnet

If you wanted to start a PR with my code snippet and pluralize_count function name I don't mind btw.

nickjj avatar Apr 07 '21 17:04 nickjj

@nickjj I really don't mind either! If you don't have time I could look into it this weekend. But also, I think it might be a good idea to see if there is buy in from the maintainer. It's always annoying for them to say no after someone did some work and it's wasted time on the contributor.

What do you think?

bastienboutonnet avatar Apr 08 '21 06:04 bastienboutonnet

Yeah we could wait for a reply here before taking any type of action.

nickjj avatar Apr 08 '21 10:04 nickjj