Inflect
Inflect.py is a Python library designed to correctly generate plurals, singular nouns, ordinals, indefinite articles (a/an), and word-based representations of numbers. Its functionality is primarily focused on the English language. As of version 7.5.0, it maintains an active development and release cadence, with regular updates addressing features, bug fixes, and compatibility.
Warnings
- breaking Version 7.0.0 removed methods that were renamed in an earlier 0.2.0 release. If your code uses very old aliases, they will cease to function.
- gotcha When using methods like `plural_noun()`, ensure you pass the *singular* form of the word. Similarly, `singular_noun()` expects the *plural* form. Passing the incorrect base form can lead to undefined or incorrect inflections.
- gotcha Older versions (pre-7.2.0, particularly in the v6.x series) experienced compatibility issues with Pydantic, leading to `ValueError` (e.g., `ValueError: Field default cannot be set in Annotated`). This was often due to Pydantic 1 vs. 2 conflicts. While v7.2.0 replaced Pydantic with Typeguard, users with existing Pydantic installations in their environment might still encounter conflicts if older `inflect` versions are used or if a new conflict arises.
- gotcha For combining multiple inflections within a single string, the `p.inflect()` string-interpolating method is generally more readable and efficient than concatenating multiple method calls.
Install
-
pip install inflect
Imports
- engine
import inflect p = inflect.engine()
Quickstart
import inflect
p = inflect.engine()
# Pluralization
word = 'cat'
count = 5
print(f"The plural of '{word}' is '{p.plural(word)}'")
print(f"I saw {count} {p.plural_noun(word, count)}.")
# Ordinal numbers
number = 21
print(f"The ordinal of {number} is {p.ordinal(number)}")
# Numbers to words
big_number = 1234567
print(f"'{big_number}' in words is '{p.number_to_words(big_number)}'")
# Indefinite articles
print(f"An apple and {p.an('historical object')}")