jaraco / inflect

Correctly generate plurals, ordinals, indefinite articles; convert numbers to words
https://pypi.org/project/inflect
MIT License
957 stars 107 forks source link

"Entity" gets pluralized to "Entitys" #171

Closed fezzik1620 closed 1 year ago

fezzik1620 commented 1 year ago
docker run -it python:3.11 bash
root@4966b54eda43:/# pip install inflect==6.0.2
Collecting inflect==6.0.2
  Downloading inflect-6.0.2-py3-none-any.whl (34 kB)
Collecting pydantic>=1.9.1
  Downloading pydantic-1.10.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (14.1 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 14.1/14.1 MB 9.7 MB/s eta 0:00:00
Collecting typing-extensions>=4.1.0
  Downloading typing_extensions-4.4.0-py3-none-any.whl (26 kB)
Installing collected packages: typing-extensions, pydantic, inflect
Successfully installed inflect-6.0.2 pydantic-1.10.2 typing-extensions-4.4.0
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
root@4966b54eda43:/# python
Python 3.11.1 (main, Dec 21 2022, 18:32:57) [GCC 10.2.1 20210110] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import inflect
>>> word="Entity"
>>> print(f'The plural of "{word}" is "{inflect.engine().plural(word)}".')
The plural of "Entity" is "Entitys".
>>> print(f'The plural of "{word}" is "{inflect.engine().plural_noun(word)}".')
The plural of "Entity" is "Entitys".
fezzik1620 commented 1 year ago

Found this old bug that was purportedly fixed (#5). But this does seem to be the same issue. As, if I run the same code above, but change the word from "Entity" to "entity", it works.

>>> import inflect
>>> word="entity"
>>> print(f'The plural of "{word}" is "{inflect.engine().plural(word)}".')
The plural of "entity" is "entities".
>>> print(f'The plural of "{word}" is "{inflect.engine().plural_noun(word)}".')
The plural of "entity" is "entities".

So, it is specifically a problem when the first letter is capitalized.

fezzik1620 commented 1 year ago

Dug through and found more closed issues.

5, #71, #78, #104, #106

So, since the owner doesn't think this is a problem (seems like this could be handled with a flag), but I have a case where I do commonly have a capitalized inputs but, in my case, they would never be proper nouns, I created this wrapper function.

import inflect
inflect_engine = inflect.engine()

def get_plural_of(singular):
    """
    Given the singular form of a word, returns the plural form. This function is a wrapper of the inflect plugin. Any
     word that begins with a capital letter and ends with a 'y', it assumes is a proper noun, and thus just throws an
     's' on the end. "Entity" becomes "Entitys". The developer doesn't see this as a problem. So this function tries to
     deal with those cases by converting it to lowercase, running it through the plural method and then converting the
     case back.
    @param singular: singular form of a noun or noun phrase to pluralize
    @return: Plural form of the input in the same capitalization as the input.
    """
    lowercase = singular.lower()
    plural = inflect_engine.plural(lowercase)
    if singular == singular.upper():
        plural = plural.upper()
    elif singular == singular.capitalize():
        plural = plural.capitalize()
    return plural

It's not fool proof, by a long shot, but it fits my needs; thought it might help someone else too.

jaraco commented 1 year ago

This is a duplicate of #5.

jaraco commented 1 year ago

71 indicates a possible workaround also.

78 invites someone to generalize the problem and come up with a solution, but also there's a comment there that I don't understand that may be another workaround.