Core API¶
Core pluralization and singularization engine.
This module implements the main algorithm that transforms a word between its singular and plural forms. The algorithm follows a three-step priority chain for each word:
Uncountable check — if the word is in the language’s
uncountableset, it is returned unchanged.Irregular lookup — if the word appears in the irregular mapping (
irregular_pluralsorirregular_singles), the mapped form is returned.Regex rules — the first matching regex rule in the ordered list is applied. If no rule matches, the word is returned unchanged.
Additional features handled here:
Case preservation: the output mirrors the casing of the input (title case, all caps, lowercase).
Hyphenated words: only the first segment is transformed (e.g.
"mother-in-law"→"mothers-in-law").Count-aware pluralization: when
count == 1the word is returned in singular form regardless of other rules.Input validation: non-string inputs raise
TypeError; empty or whitespace-only strings are returned as-is.
- pluralio.core.pluralize(word: str, lang: str = 'en', count: int | None = None) str[source]¶
Convert a word to its plural form.
The function follows the three-step priority chain (uncountable → irregular → regex) and preserves the input casing in the output.
- Parameters:
word – The singular word to pluralize.
lang – ISO 639-1 language code. Defaults to
"en".count – Optional integer count. When
count == 1the word is returned unchanged (singular form). Any other value (including0, negative numbers, andNone) produces the plural form.
- Returns:
The plural form of
word, with original casing preserved.- Raises:
TypeError – If
wordis not a string.ValueError – If
langis not a registered language.
Example
>>> pluralize("cat") 'cats' >>> pluralize("box") 'boxes' >>> pluralize("child") 'children' >>> pluralize("gato", lang="es") 'gatos' >>> pluralize("item", count=1) 'item' >>> pluralize("Library") 'Libraries' >>> pluralize("mother-in-law") 'mothers-in-law'
- pluralio.core.singularize(word: str, lang: str = 'en') str[source]¶
Convert a word to its singular form.
The function follows the three-step priority chain (uncountable → irregular → regex) and preserves the input casing in the output.
- Parameters:
word – The plural word to singularize.
lang – ISO 639-1 language code. Defaults to
"en".
- Returns:
The singular form of
word, with original casing preserved.- Raises:
TypeError – If
wordis not a string.ValueError – If
langis not a registered language.
Example
>>> singularize("cities") 'city' >>> singularize("mice") 'mouse' >>> singularize("lápices", lang="es") 'lápiz' >>> singularize("Libraries") 'Library' >>> singularize("mothers-in-law") 'mother-in-law'
Internal functions¶
- pluralio.core._match_case(source: str, target: str) str[source]¶
Apply the casing pattern of
sourcetotarget.Four modes are supported:
All caps: if
sourceis entirely uppercase,targetis returned in uppercase.Title case: if only the first character of
sourceis uppercase and the rest are lowercase,targetis capitalized on its first character.Mixed case: if
sourcehas uppercase letters beyond the first character but is not all caps, each character position intargetmirrors the case of the corresponding position insource. Extra characters intargetdefault to lowercase.Lowercase: otherwise
targetis returned unchanged.
- Parameters:
source – The original word whose casing pattern to replicate.
target – The transformed word to re-case.
- Returns:
targetwith the casing ofsourceapplied.
Example
>>> _match_case("Library", "libraries") 'Libraries' >>> _match_case("LIBRARY", "libraries") 'LIBRARIES' >>> _match_case("library", "libraries") 'libraries' >>> _match_case("McDonald", "mcdonalds") 'McDonalds' >>> _match_case("iPhone", "iphones") 'iPhones'
- pluralio.core._apply_rules(word: str, lower: str, rules: LanguageRules, is_plural: bool) str[source]¶
Apply the three-step transformation chain to a single word.
This is the shared inner logic used by both
pluralize()andsingularize(). It applies regex rules and preserves the input casing in the result. Uncountable and irregular checks are performed by the caller before reaching this function.- Parameters:
word – The original word (with original casing).
lower – The lowercase version of
word.rules – The
LanguageRulesfor the target language.is_plural –
Truefor pluralization rules,Falsefor singularization rules.
- Returns:
The transformed word with original casing preserved.