| 1 |
Metadata-Version: 2.1 |
| 2 |
Name: inflect |
| 3 |
Version: 7.5.0 |
| 4 |
Summary: Correctly generate plurals, singular nouns, ordinals, indefinite articles |
| 5 |
Author-email: Paul Dyson <[email protected]> |
| 6 |
Maintainer-email: "Jason R. Coombs" <[email protected]> |
| 7 |
Project-URL: Source, https://github.com/jaraco/inflect |
| 8 |
Keywords: plural,inflect,participle |
| 9 |
Classifier: Development Status :: 5 - Production/Stable |
| 10 |
Classifier: Intended Audience :: Developers |
| 11 |
Classifier: License :: OSI Approved :: MIT License |
| 12 |
Classifier: Programming Language :: Python :: 3 |
| 13 |
Classifier: Programming Language :: Python :: 3 :: Only |
| 14 |
Classifier: Natural Language :: English |
| 15 |
Classifier: Operating System :: OS Independent |
| 16 |
Classifier: Topic :: Software Development :: Libraries :: Python Modules |
| 17 |
Classifier: Topic :: Text Processing :: Linguistic |
| 18 |
Requires-Python: >=3.9 |
| 19 |
Description-Content-Type: text/x-rst |
| 20 |
License-File: LICENSE |
| 21 |
Requires-Dist: more_itertools>=8.5.0 |
| 22 |
Requires-Dist: typeguard>=4.0.1 |
| 23 |
Requires-Dist: typing_extensions; python_version < "3.9" |
| 24 |
Provides-Extra: test |
| 25 |
Requires-Dist: pytest!=8.1.*,>=6; extra == "test" |
| 26 |
Requires-Dist: pygments; extra == "test" |
| 27 |
Provides-Extra: doc |
| 28 |
Requires-Dist: sphinx>=3.5; extra == "doc" |
| 29 |
Requires-Dist: jaraco.packaging>=9.3; extra == "doc" |
| 30 |
Requires-Dist: rst.linker>=1.9; extra == "doc" |
| 31 |
Requires-Dist: furo; extra == "doc" |
| 32 |
Requires-Dist: sphinx-lint; extra == "doc" |
| 33 |
Requires-Dist: jaraco.tidelift>=1.4; extra == "doc" |
| 34 |
Provides-Extra: check |
| 35 |
Requires-Dist: pytest-checkdocs>=2.4; extra == "check" |
| 36 |
Requires-Dist: pytest-ruff>=0.2.1; sys_platform != "cygwin" and extra == "check" |
| 37 |
Provides-Extra: cover |
| 38 |
Requires-Dist: pytest-cov; extra == "cover" |
| 39 |
Provides-Extra: enabler |
| 40 |
Requires-Dist: pytest-enabler>=2.2; extra == "enabler" |
| 41 |
Provides-Extra: type |
| 42 |
Requires-Dist: pytest-mypy; extra == "type" |
| 43 |
|
| 44 |
.. image:: https://img.shields.io/pypi/v/inflect.svg |
| 45 |
:target: https://pypi.org/project/inflect |
| 46 |
|
| 47 |
.. image:: https://img.shields.io/pypi/pyversions/inflect.svg |
| 48 |
|
| 49 |
.. image:: https://github.com/jaraco/inflect/actions/workflows/main.yml/badge.svg |
| 50 |
:target: https://github.com/jaraco/inflect/actions?query=workflow%3A%22tests%22 |
| 51 |
:alt: tests |
| 52 |
|
| 53 |
.. image:: https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/charliermarsh/ruff/main/assets/badge/v2.json |
| 54 |
:target: https://github.com/astral-sh/ruff |
| 55 |
:alt: Ruff |
| 56 |
|
| 57 |
.. image:: https://readthedocs.org/projects/inflect/badge/?version=latest |
| 58 |
:target: https://inflect.readthedocs.io/en/latest/?badge=latest |
| 59 |
|
| 60 |
.. image:: https://img.shields.io/badge/skeleton-2024-informational |
| 61 |
:target: https://blog.jaraco.com/skeleton |
| 62 |
|
| 63 |
.. image:: https://tidelift.com/badges/package/pypi/inflect |
| 64 |
:target: https://tidelift.com/subscription/pkg/pypi-inflect?utm_source=pypi-inflect&utm_medium=readme |
| 65 |
|
| 66 |
NAME |
| 67 |
==== |
| 68 |
|
| 69 |
inflect.py - Accurately generate plurals, singular nouns, ordinals, indefinite articles, and word-based representations of numbers. This functionality is limited to English. |
| 70 |
|
| 71 |
SYNOPSIS |
| 72 |
======== |
| 73 |
|
| 74 |
.. code-block:: python |
| 75 |
|
| 76 |
>>> import inflect |
| 77 |
>>> p = inflect.engine() |
| 78 |
|
| 79 |
Simple example with pluralization and word-representation of numbers: |
| 80 |
|
| 81 |
.. code-block:: python |
| 82 |
|
| 83 |
>>> count=1 |
| 84 |
>>> print('There', p.plural_verb('was', count), p.number_to_words(count), p.plural_noun('person', count), 'by the door.') |
| 85 |
There was one person by the door. |
| 86 |
|
| 87 |
When ``count=243``, the same code will generate: |
| 88 |
|
| 89 |
.. code-block:: python |
| 90 |
|
| 91 |
There were two hundred and forty-three people by the door. |
| 92 |
|
| 93 |
|
| 94 |
Methods |
| 95 |
======= |
| 96 |
|
| 97 |
- ``plural``, ``plural_noun``, ``plural_verb``, ``plural_adj``, ``singular_noun``, ``no``, ``num`` |
| 98 |
- ``compare``, ``compare_nouns``, ``compare_nouns``, ``compare_adjs`` |
| 99 |
- ``a``, ``an`` |
| 100 |
- ``present_participle`` |
| 101 |
- ``ordinal``, ``number_to_words`` |
| 102 |
- ``join`` |
| 103 |
- ``inflect``, ``classical``, ``gender`` |
| 104 |
- ``defnoun``, ``defverb``, ``defadj``, ``defa``, ``defan`` |
| 105 |
|
| 106 |
Plurality/Singularity |
| 107 |
--------------------- |
| 108 |
Unconditionally Form the Plural |
| 109 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 110 |
|
| 111 |
.. code-block:: python |
| 112 |
|
| 113 |
>>> "the plural of person is " + p.plural("person") |
| 114 |
'the plural of person is people' |
| 115 |
|
| 116 |
Conditionally Form the Plural |
| 117 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 118 |
|
| 119 |
.. code-block:: python |
| 120 |
|
| 121 |
>>> "the plural of 1 person is " + p.plural("person", 1) |
| 122 |
'the plural of 1 person is person' |
| 123 |
|
| 124 |
Form Plurals for Specific Parts of Speech |
| 125 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 126 |
|
| 127 |
.. code-block:: python |
| 128 |
|
| 129 |
>>> p.plural_noun("I", 2) |
| 130 |
'we' |
| 131 |
>>> p.plural_verb("saw", 1) |
| 132 |
'saw' |
| 133 |
>>> p.plural_adj("my", 2) |
| 134 |
'our' |
| 135 |
>>> p.plural_noun("saw", 2) |
| 136 |
'saws' |
| 137 |
|
| 138 |
Form the Singular of Plural Nouns |
| 139 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 140 |
|
| 141 |
.. code-block:: python |
| 142 |
|
| 143 |
>>> "The singular of people is " + p.singular_noun("people") |
| 144 |
'The singular of people is person' |
| 145 |
|
| 146 |
Select the Gender of Singular Pronouns |
| 147 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 148 |
|
| 149 |
.. code-block:: python |
| 150 |
|
| 151 |
>>> p.singular_noun("they") |
| 152 |
'it' |
| 153 |
>>> p.gender("feminine") |
| 154 |
>>> p.singular_noun("they") |
| 155 |
'she' |
| 156 |
|
| 157 |
Deal with "0/1/N" -> "no/1/N" Translation |
| 158 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 159 |
|
| 160 |
.. code-block:: python |
| 161 |
|
| 162 |
>>> errors = 1 |
| 163 |
>>> "There ", p.plural_verb("was", errors), p.no(" error", errors) |
| 164 |
('There ', 'was', ' 1 error') |
| 165 |
>>> errors = 2 |
| 166 |
>>> "There ", p.plural_verb("was", errors), p.no(" error", errors) |
| 167 |
('There ', 'were', ' 2 errors') |
| 168 |
|
| 169 |
Use Default Counts |
| 170 |
^^^^^^^^^^^^^^^^^^ |
| 171 |
|
| 172 |
.. code-block:: python |
| 173 |
|
| 174 |
>>> p.num(1, "") |
| 175 |
'' |
| 176 |
>>> p.plural("I") |
| 177 |
'I' |
| 178 |
>>> p.plural_verb(" saw") |
| 179 |
' saw' |
| 180 |
>>> p.num(2) |
| 181 |
'2' |
| 182 |
>>> p.plural_noun(" saw") |
| 183 |
' saws' |
| 184 |
>>> "There ", p.num(errors, ""), p.plural_verb("was"), p.no(" error") |
| 185 |
('There ', '', 'were', ' 2 errors') |
| 186 |
|
| 187 |
Compare Two Words Number-Intensitively |
| 188 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 189 |
|
| 190 |
.. code-block:: python |
| 191 |
|
| 192 |
>>> p.compare('person', 'person') |
| 193 |
'eq' |
| 194 |
>>> p.compare('person', 'people') |
| 195 |
's:p' |
| 196 |
>>> p.compare_nouns('person', 'people') |
| 197 |
's:p' |
| 198 |
>>> p.compare_verbs('run', 'ran') |
| 199 |
False |
| 200 |
>>> p.compare_verbs('run', 'running') |
| 201 |
False |
| 202 |
>>> p.compare_verbs('run', 'run') |
| 203 |
'eq' |
| 204 |
>>> p.compare_adjs('my', 'mine') |
| 205 |
False |
| 206 |
>>> p.compare_adjs('my', 'our') |
| 207 |
's:p' |
| 208 |
|
| 209 |
Add Correct *a* or *an* for a Given Word |
| 210 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 211 |
|
| 212 |
.. code-block:: python |
| 213 |
|
| 214 |
>>> "Did you want ", p.a('thing'), " or ", p.a('idea') |
| 215 |
('Did you want ', 'a thing', ' or ', 'an idea') |
| 216 |
|
| 217 |
Convert Numerals into Ordinals |
| 218 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 219 |
|
| 220 |
.. code-block:: python |
| 221 |
|
| 222 |
>>> "It was", p.ordinal(1), " from the left" |
| 223 |
('It was', '1st', ' from the left') |
| 224 |
>>> "It was", p.ordinal(2), " from the left" |
| 225 |
('It was', '2nd', ' from the left') |
| 226 |
>>> "It was", p.ordinal(3), " from the left" |
| 227 |
('It was', '3rd', ' from the left') |
| 228 |
>>> "It was", p.ordinal(347), " from the left" |
| 229 |
('It was', '347th', ' from the left') |
| 230 |
|
| 231 |
Convert Numerals to Words |
| 232 |
^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 233 |
Note: This returns a single string. |
| 234 |
|
| 235 |
.. code-block:: python |
| 236 |
|
| 237 |
>>> p.number_to_words(1) |
| 238 |
'one' |
| 239 |
>>> p.number_to_words(38) |
| 240 |
'thirty-eight' |
| 241 |
>>> p.number_to_words(1234) |
| 242 |
'one thousand, two hundred and thirty-four' |
| 243 |
>>> p.number_to_words(p.ordinal(1234)) |
| 244 |
'one thousand, two hundred and thirty-fourth' |
| 245 |
|
| 246 |
Retrieve Words as List of Parts |
| 247 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 248 |
|
| 249 |
.. code-block:: python |
| 250 |
|
| 251 |
>>> p.number_to_words(1234, wantlist=True) |
| 252 |
['one thousand', 'two hundred and thirty-four'] |
| 253 |
|
| 254 |
Grouping Options |
| 255 |
^^^^^^^^^^^^^^^^ |
| 256 |
|
| 257 |
.. code-block:: python |
| 258 |
|
| 259 |
>>> p.number_to_words(12345, group=1) |
| 260 |
'one, two, three, four, five' |
| 261 |
>>> p.number_to_words(12345, group=2) |
| 262 |
'twelve, thirty-four, five' |
| 263 |
>>> p.number_to_words(12345, group=3) |
| 264 |
'one twenty-three, forty-five' |
| 265 |
>>> p.number_to_words(1234, andword="") |
| 266 |
'one thousand, two hundred thirty-four' |
| 267 |
>>> p.number_to_words(1234, andword=", plus") |
| 268 |
'one thousand, two hundred, plus thirty-four' |
| 269 |
>>> p.number_to_words(555_1202, group=1, zero="oh") |
| 270 |
'five, five, five, one, two, oh, two' |
| 271 |
>>> p.number_to_words(555_1202, group=1, one="unity") |
| 272 |
'five, five, five, unity, two, zero, two' |
| 273 |
>>> p.number_to_words(123.456, group=1, decimal="mark") |
| 274 |
'one, two, three, mark, four, five, six' |
| 275 |
|
| 276 |
Apply Threshold for Word-Representation of Numbers |
| 277 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 278 |
Above provided threshold, numberals will remain numerals |
| 279 |
|
| 280 |
.. code-block:: python |
| 281 |
|
| 282 |
>>> p.number_to_words(9, threshold=10) |
| 283 |
'nine' |
| 284 |
>>> p.number_to_words(10, threshold=10) |
| 285 |
'ten' |
| 286 |
>>> p.number_to_words(11, threshold=10) |
| 287 |
'11' |
| 288 |
>>> p.number_to_words(1000, threshold=10) |
| 289 |
'1,000' |
| 290 |
|
| 291 |
Join Words into a List |
| 292 |
^^^^^^^^^^^^^^^^^^^^^^ |
| 293 |
|
| 294 |
.. code-block:: python |
| 295 |
|
| 296 |
>>> p.join(("apple", "banana", "carrot")) |
| 297 |
'apple, banana, and carrot' |
| 298 |
>>> p.join(("apple", "banana")) |
| 299 |
'apple and banana' |
| 300 |
>>> p.join(("apple", "banana", "carrot"), final_sep="") |
| 301 |
'apple, banana and carrot' |
| 302 |
>>> p.join(('apples', 'bananas', 'carrots'), conj='and even') |
| 303 |
'apples, bananas, and even carrots' |
| 304 |
>>> p.join(('apple', 'banana', 'carrot'), sep='/', sep_spaced=False, conj='', conj_spaced=False) |
| 305 |
'apple/banana/carrot' |
| 306 |
|
| 307 |
Require Classical Plurals |
| 308 |
^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 309 |
Adhere to conventions from Classical Latin and Classical Greek |
| 310 |
|
| 311 |
.. code-block:: python |
| 312 |
|
| 313 |
>>> p.classical() |
| 314 |
>>> p.plural_noun("focus", 2) |
| 315 |
'foci' |
| 316 |
>>> p.plural_noun("cherubim", 2) |
| 317 |
'cherubims' |
| 318 |
>>> p.plural_noun("cherub", 2) |
| 319 |
'cherubim' |
| 320 |
|
| 321 |
Other options for classical plurals: |
| 322 |
|
| 323 |
.. code-block:: python |
| 324 |
|
| 325 |
p.classical(all=True) # USE ALL CLASSICAL PLURALS |
| 326 |
p.classical(all=False) # SWITCH OFF CLASSICAL MODE |
| 327 |
|
| 328 |
p.classical(zero=True) # "no error" INSTEAD OF "no errors" |
| 329 |
p.classical(zero=False) # "no errors" INSTEAD OF "no error" |
| 330 |
|
| 331 |
p.classical(herd=True) # "2 buffalo" INSTEAD OF "2 buffalos" |
| 332 |
p.classical(herd=False) # "2 buffalos" INSTEAD OF "2 buffalo" |
| 333 |
|
| 334 |
p.classical(persons=True) # "2 chairpersons" INSTEAD OF "2 chairpeople" |
| 335 |
p.classical(persons=False) # "2 chairpeople" INSTEAD OF "2 chairpersons" |
| 336 |
|
| 337 |
p.classical(ancient=True) # "2 formulae" INSTEAD OF "2 formulas" |
| 338 |
p.classical(ancient=False) # "2 formulas" INSTEAD OF "2 formulae" |
| 339 |
|
| 340 |
|
| 341 |
Support for interpolation |
| 342 |
^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 343 |
Supports string interpolation with the following functions: ``plural()``, ``plural_noun()``, ``plural_verb()``, ``plural_adj()``, ``singular_noun()``, ``a()``, ``an()``, ``num()`` and ``ordinal()``. |
| 344 |
|
| 345 |
.. code-block:: python |
| 346 |
|
| 347 |
>>> p.inflect("The plural of {0} is plural('{0}')".format('car')) |
| 348 |
'The plural of car is cars' |
| 349 |
>>> p.inflect("The singular of {0} is singular_noun('{0}')".format('car')) |
| 350 |
'The singular of car is car' |
| 351 |
>>> p.inflect("I saw {0} plural('cat',{0})".format(3)) |
| 352 |
'I saw 3 cats' |
| 353 |
>>> p.inflect( |
| 354 |
... "plural('I',{0}) " |
| 355 |
... "plural_verb('saw',{0}) " |
| 356 |
... "plural('a',{1}) " |
| 357 |
... "plural_noun('saw',{1})".format(1, 2) |
| 358 |
... ) |
| 359 |
'I saw some saws' |
| 360 |
>>> p.inflect( |
| 361 |
... "num({0}, False)plural('I') " |
| 362 |
... "plural_verb('saw') " |
| 363 |
... "num({1}, False)plural('a') " |
| 364 |
... "plural_noun('saw')".format(N1, 1) |
| 365 |
... ) |
| 366 |
'I saw a saw' |
| 367 |
>>> p.inflect( |
| 368 |
... "num({0}, False)plural('I') " |
| 369 |
... "plural_verb('saw') " |
| 370 |
... "num({1}, False)plural('a') " |
| 371 |
... "plural_noun('saw')".format(2, 2) |
| 372 |
... ) |
| 373 |
'we saw some saws' |
| 374 |
>>> p.inflect("I saw num({0}) plural('cat')\nnum()".format(cat_count)) |
| 375 |
'I saw 3 cats\n' |
| 376 |
>>> p.inflect("There plural_verb('was',{0}) no('error',{0})".format(errors)) |
| 377 |
'There were 2 errors' |
| 378 |
>>> p.inflect("There num({0}, False)plural_verb('was') no('error')".format(errors)) |
| 379 |
'There were 2 errors' |
| 380 |
>>> p.inflect("Did you want a('{0}') or an('{1}')".format(thing, idea)) |
| 381 |
'Did you want a thing or an idea' |
| 382 |
>>> p.inflect("It was ordinal('{0}') from the left".format(2)) |
| 383 |
'It was 2nd from the left' |
| 384 |
|
| 385 |
Add User-Defined Inflections |
| 386 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 387 |
Allows for overriding default rules. |
| 388 |
|
| 389 |
Override noun defaults: |
| 390 |
|
| 391 |
.. code-block:: python |
| 392 |
|
| 393 |
p.defnoun("VAX", "VAXen") # SINGULAR => PLURAL |
| 394 |
|
| 395 |
Override Verb defaults: |
| 396 |
|
| 397 |
.. code-block:: python |
| 398 |
|
| 399 |
p.defverb( |
| 400 |
"will", # 1ST PERSON SINGULAR |
| 401 |
"shall", # 1ST PERSON PLURAL |
| 402 |
"will", # 2ND PERSON SINGULAR |
| 403 |
"will", # 2ND PERSON PLURAL |
| 404 |
"will", # 3RD PERSON SINGULAR |
| 405 |
"will", # 3RD PERSON PLURAL |
| 406 |
) |
| 407 |
|
| 408 |
Override adjective defaults: |
| 409 |
|
| 410 |
.. code-block:: python |
| 411 |
|
| 412 |
>>> p.defadj('hir', 'their') |
| 413 |
1 |
| 414 |
>>> p.plural_adj('hir', 2) |
| 415 |
'their' |
| 416 |
|
| 417 |
Override the words that use the indefinite articles "a" or "an": |
| 418 |
|
| 419 |
.. code-block:: python |
| 420 |
|
| 421 |
>>> p.a('ape', 1) |
| 422 |
'an ape' |
| 423 |
>>> p.defa('a') |
| 424 |
1 |
| 425 |
>>> p.a('ape', 1) |
| 426 |
'an ape' |
| 427 |
>>> p.defa('ape') |
| 428 |
1 |
| 429 |
>>> p.a('ape', 1) |
| 430 |
'a ape' |
| 431 |
>>> p.defan('horrendous.*') |
| 432 |
1 |
| 433 |
>>> p.a('horrendous affectation', 1) |
| 434 |
'an horrendous affectation' |
| 435 |
>>> |
| 436 |
|
| 437 |
|
| 438 |
DESCRIPTION |
| 439 |
=========== |
| 440 |
|
| 441 |
The methods of the class ``engine`` in module ``inflect.py`` provide plural |
| 442 |
inflections, singular noun inflections, "a"/"an" selection for English words, |
| 443 |
and manipulation of numbers as words. |
| 444 |
|
| 445 |
Plural forms of all nouns, most verbs, and some adjectives are |
| 446 |
provided. Where appropriate, "classical" variants (for example: "brother" -> |
| 447 |
"brethren", "dogma" -> "dogmata", etc.) are also provided. |
| 448 |
|
| 449 |
Single forms of nouns are also provided. The gender of singular pronouns |
| 450 |
can be chosen (for example "they" -> "it" or "she" or "he" or "they"). |
| 451 |
|
| 452 |
Pronunciation-based "a"/"an" selection is provided for all English |
| 453 |
words, and most initialisms. |
| 454 |
|
| 455 |
It is also possible to inflect numerals (1,2,3) to ordinals (1st, 2nd, 3rd) |
| 456 |
or to English words ("one", "two", "three"). |
| 457 |
|
| 458 |
In generating these inflections, ``inflect.py`` follows the Oxford |
| 459 |
English Dictionary and the guidelines in Fowler's Modern English |
| 460 |
Usage, preferring the former where the two disagree. |
| 461 |
|
| 462 |
The module is built around standard British spelling, but is designed |
| 463 |
to cope with common American variants as well. Slang, jargon, and |
| 464 |
other English dialects are *not* explicitly catered for. |
| 465 |
|
| 466 |
Where two or more inflected forms exist for a single word (typically a |
| 467 |
"classical" form and a "modern" form), ``inflect.py`` prefers the |
| 468 |
more common form (typically the "modern" one), unless "classical" |
| 469 |
processing has been specified |
| 470 |
(see `MODERN VS CLASSICAL INFLECTIONS`). |
| 471 |
|
| 472 |
FORMING PLURALS AND SINGULARS |
| 473 |
============================= |
| 474 |
|
| 475 |
Inflecting Plurals and Singulars |
| 476 |
-------------------------------- |
| 477 |
|
| 478 |
All of the ``plural...`` plural inflection methods take the word to be |
| 479 |
inflected as their first argument and return the corresponding inflection. |
| 480 |
Note that all such methods expect the *singular* form of the word. The |
| 481 |
results of passing a plural form are undefined (and unlikely to be correct). |
| 482 |
Similarly, the ``si...`` singular inflection method expects the *plural* |
| 483 |
form of the word. |
| 484 |
|
| 485 |
The ``plural...`` methods also take an optional second argument, |
| 486 |
which indicates the grammatical "number" of the word (or of another word |
| 487 |
with which the word being inflected must agree). If the "number" argument is |
| 488 |
supplied and is not ``1`` (or ``"one"`` or ``"a"``, or some other adjective that |
| 489 |
implies the singular), the plural form of the word is returned. If the |
| 490 |
"number" argument *does* indicate singularity, the (uninflected) word |
| 491 |
itself is returned. If the number argument is omitted, the plural form |
| 492 |
is returned unconditionally. |
| 493 |
|
| 494 |
The ``si...`` method takes a second argument in a similar fashion. If it is |
| 495 |
some form of the number ``1``, or is omitted, the singular form is returned. |
| 496 |
Otherwise the plural is returned unaltered. |
| 497 |
|
| 498 |
|
| 499 |
The various methods of ``inflect.engine`` are: |
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
``plural_noun(word, count=None)`` |
| 504 |
|
| 505 |
The method ``plural_noun()`` takes a *singular* English noun or |
| 506 |
pronoun and returns its plural. Pronouns in the nominative ("I" -> |
| 507 |
"we") and accusative ("me" -> "us") cases are handled, as are |
| 508 |
possessive pronouns ("mine" -> "ours"). |
| 509 |
|
| 510 |
|
| 511 |
``plural_verb(word, count=None)`` |
| 512 |
|
| 513 |
The method ``plural_verb()`` takes the *singular* form of a |
| 514 |
conjugated verb (that is, one which is already in the correct "person" |
| 515 |
and "mood") and returns the corresponding plural conjugation. |
| 516 |
|
| 517 |
|
| 518 |
``plural_adj(word, count=None)`` |
| 519 |
|
| 520 |
The method ``plural_adj()`` takes the *singular* form of |
| 521 |
certain types of adjectives and returns the corresponding plural form. |
| 522 |
Adjectives that are correctly handled include: "numerical" adjectives |
| 523 |
("a" -> "some"), demonstrative adjectives ("this" -> "these", "that" -> |
| 524 |
"those"), and possessives ("my" -> "our", "cat's" -> "cats'", "child's" |
| 525 |
-> "childrens'", etc.) |
| 526 |
|
| 527 |
|
| 528 |
``plural(word, count=None)`` |
| 529 |
|
| 530 |
The method ``plural()`` takes a *singular* English noun, |
| 531 |
pronoun, verb, or adjective and returns its plural form. Where a word |
| 532 |
has more than one inflection depending on its part of speech (for |
| 533 |
example, the noun "thought" inflects to "thoughts", the verb "thought" |
| 534 |
to "thought"), the (singular) noun sense is preferred to the (singular) |
| 535 |
verb sense. |
| 536 |
|
| 537 |
Hence ``plural("knife")`` will return "knives" ("knife" having been treated |
| 538 |
as a singular noun), whereas ``plural("knifes")`` will return "knife" |
| 539 |
("knifes" having been treated as a 3rd person singular verb). |
| 540 |
|
| 541 |
The inherent ambiguity of such cases suggests that, |
| 542 |
where the part of speech is known, ``plural_noun``, ``plural_verb``, and |
| 543 |
``plural_adj`` should be used in preference to ``plural``. |
| 544 |
|
| 545 |
|
| 546 |
``singular_noun(word, count=None)`` |
| 547 |
|
| 548 |
The method ``singular_noun()`` takes a *plural* English noun or |
| 549 |
pronoun and returns its singular. Pronouns in the nominative ("we" -> |
| 550 |
"I") and accusative ("us" -> "me") cases are handled, as are |
| 551 |
possessive pronouns ("ours" -> "mine"). When third person |
| 552 |
singular pronouns are returned they take the neuter gender by default |
| 553 |
("they" -> "it"), not ("they"-> "she") nor ("they" -> "he"). This can be |
| 554 |
changed with ``gender()``. |
| 555 |
|
| 556 |
Note that all these methods ignore any whitespace surrounding the |
| 557 |
word being inflected, but preserve that whitespace when the result is |
| 558 |
returned. For example, ``plural(" cat ")`` returns " cats ". |
| 559 |
|
| 560 |
|
| 561 |
``gender(genderletter)`` |
| 562 |
|
| 563 |
The third person plural pronoun takes the same form for the female, male and |
| 564 |
neuter (e.g. "they"). The singular however, depends upon gender (e.g. "she", |
| 565 |
"he", "it" and "they" -- "they" being the gender neutral form.) By default |
| 566 |
``singular_noun`` returns the neuter form, however, the gender can be selected with |
| 567 |
the ``gender`` method. Pass the first letter of the gender to |
| 568 |
``gender`` to return the f(eminine), m(asculine), n(euter) or t(hey) |
| 569 |
form of the singular. e.g. |
| 570 |
gender('f') followed by singular_noun('themselves') returns 'herself'. |
| 571 |
|
| 572 |
Numbered plurals |
| 573 |
---------------- |
| 574 |
|
| 575 |
The ``plural...`` methods return only the inflected word, not the count that |
| 576 |
was used to inflect it. Thus, in order to produce "I saw 3 ducks", it |
| 577 |
is necessary to use: |
| 578 |
|
| 579 |
.. code-block:: python |
| 580 |
|
| 581 |
print("I saw", N, p.plural_noun(animal, N)) |
| 582 |
|
| 583 |
Since the usual purpose of producing a plural is to make it agree with |
| 584 |
a preceding count, inflect.py provides a method |
| 585 |
(``no(word, count)``) which, given a word and a(n optional) count, returns the |
| 586 |
count followed by the correctly inflected word. Hence the previous |
| 587 |
example can be rewritten: |
| 588 |
|
| 589 |
.. code-block:: python |
| 590 |
|
| 591 |
print("I saw ", p.no(animal, N)) |
| 592 |
|
| 593 |
In addition, if the count is zero (or some other term which implies |
| 594 |
zero, such as ``"zero"``, ``"nil"``, etc.) the count is replaced by the |
| 595 |
word "no". Hence, if ``N`` had the value zero, the previous example |
| 596 |
would print (the somewhat more elegant):: |
| 597 |
|
| 598 |
I saw no animals |
| 599 |
|
| 600 |
rather than:: |
| 601 |
|
| 602 |
I saw 0 animals |
| 603 |
|
| 604 |
Note that the name of the method is a pun: the method |
| 605 |
returns either a number (a *No.*) or a ``"no"``, in front of the |
| 606 |
inflected word. |
| 607 |
|
| 608 |
|
| 609 |
Reducing the number of counts required |
| 610 |
-------------------------------------- |
| 611 |
|
| 612 |
In some contexts, the need to supply an explicit count to the various |
| 613 |
``plural...`` methods makes for tiresome repetition. For example: |
| 614 |
|
| 615 |
.. code-block:: python |
| 616 |
|
| 617 |
print( |
| 618 |
plural_adj("This", errors), |
| 619 |
plural_noun(" error", errors), |
| 620 |
plural_verb(" was", errors), |
| 621 |
" fatal.", |
| 622 |
) |
| 623 |
|
| 624 |
inflect.py therefore provides a method |
| 625 |
(``num(count=None, show=None)``) which may be used to set a persistent "default number" |
| 626 |
value. If such a value is set, it is subsequently used whenever an |
| 627 |
optional second "number" argument is omitted. The default value thus set |
| 628 |
can subsequently be removed by calling ``num()`` with no arguments. |
| 629 |
Hence we could rewrite the previous example: |
| 630 |
|
| 631 |
.. code-block:: python |
| 632 |
|
| 633 |
p.num(errors) |
| 634 |
print(p.plural_adj("This"), p.plural_noun(" error"), p.plural_verb(" was"), "fatal.") |
| 635 |
p.num() |
| 636 |
|
| 637 |
Normally, ``num()`` returns its first argument, so that it may also |
| 638 |
be "inlined" in contexts like: |
| 639 |
|
| 640 |
.. code-block:: python |
| 641 |
|
| 642 |
print(p.num(errors), p.plural_noun(" error"), p.plural_verb(" was"), " detected.") |
| 643 |
if severity > 1: |
| 644 |
print( |
| 645 |
p.plural_adj("This"), p.plural_noun(" error"), p.plural_verb(" was"), "fatal." |
| 646 |
) |
| 647 |
|
| 648 |
However, in certain contexts (see `INTERPOLATING INFLECTIONS IN STRINGS`) |
| 649 |
it is preferable that ``num()`` return an empty string. Hence ``num()`` |
| 650 |
provides an optional second argument. If that argument is supplied (that is, if |
| 651 |
it is defined) and evaluates to false, ``num`` returns an empty string |
| 652 |
instead of its first argument. For example: |
| 653 |
|
| 654 |
.. code-block:: python |
| 655 |
|
| 656 |
print(p.num(errors, 0), p.no("error"), p.plural_verb(" was"), " detected.") |
| 657 |
if severity > 1: |
| 658 |
print( |
| 659 |
p.plural_adj("This"), p.plural_noun(" error"), p.plural_verb(" was"), "fatal." |
| 660 |
) |
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
Number-insensitive equality |
| 665 |
--------------------------- |
| 666 |
|
| 667 |
inflect.py also provides a solution to the problem |
| 668 |
of comparing words of differing plurality through the methods |
| 669 |
``compare(word1, word2)``, ``compare_nouns(word1, word2)``, |
| 670 |
``compare_verbs(word1, word2)``, and ``compare_adjs(word1, word2)``. |
| 671 |
Each of these methods takes two strings, and compares them |
| 672 |
using the corresponding plural-inflection method (``plural()``, ``plural_noun()``, |
| 673 |
``plural_verb()``, and ``plural_adj()`` respectively). |
| 674 |
|
| 675 |
The comparison returns true if: |
| 676 |
|
| 677 |
- the strings are equal, or |
| 678 |
- one string is equal to a plural form of the other, or |
| 679 |
- the strings are two different plural forms of the one word. |
| 680 |
|
| 681 |
|
| 682 |
Hence all of the following return true: |
| 683 |
|
| 684 |
.. code-block:: python |
| 685 |
|
| 686 |
p.compare("index", "index") # RETURNS "eq" |
| 687 |
p.compare("index", "indexes") # RETURNS "s:p" |
| 688 |
p.compare("index", "indices") # RETURNS "s:p" |
| 689 |
p.compare("indexes", "index") # RETURNS "p:s" |
| 690 |
p.compare("indices", "index") # RETURNS "p:s" |
| 691 |
p.compare("indices", "indexes") # RETURNS "p:p" |
| 692 |
p.compare("indexes", "indices") # RETURNS "p:p" |
| 693 |
p.compare("indices", "indices") # RETURNS "eq" |
| 694 |
|
| 695 |
As indicated by the comments in the previous example, the actual value |
| 696 |
returned by the various ``compare`` methods encodes which of the |
| 697 |
three equality rules succeeded: "eq" is returned if the strings were |
| 698 |
identical, "s:p" if the strings were singular and plural respectively, |
| 699 |
"p:s" for plural and singular, and "p:p" for two distinct plurals. |
| 700 |
Inequality is indicated by returning an empty string. |
| 701 |
|
| 702 |
It should be noted that two distinct singular words which happen to take |
| 703 |
the same plural form are *not* considered equal, nor are cases where |
| 704 |
one (singular) word's plural is the other (plural) word's singular. |
| 705 |
Hence all of the following return false: |
| 706 |
|
| 707 |
.. code-block:: python |
| 708 |
|
| 709 |
p.compare("base", "basis") # ALTHOUGH BOTH -> "bases" |
| 710 |
p.compare("syrinx", "syringe") # ALTHOUGH BOTH -> "syringes" |
| 711 |
p.compare("she", "he") # ALTHOUGH BOTH -> "they" |
| 712 |
|
| 713 |
p.compare("opus", "operas") # ALTHOUGH "opus" -> "opera" -> "operas" |
| 714 |
p.compare("taxi", "taxes") # ALTHOUGH "taxi" -> "taxis" -> "taxes" |
| 715 |
|
| 716 |
Note too that, although the comparison is "number-insensitive" it is *not* |
| 717 |
case-insensitive (that is, ``plural("time","Times")`` returns false. To obtain |
| 718 |
both number and case insensitivity, use the ``lower()`` method on both strings |
| 719 |
(that is, ``plural("time".lower(), "Times".lower())`` returns true). |
| 720 |
|
| 721 |
Related Functionality |
| 722 |
===================== |
| 723 |
|
| 724 |
Shout out to these libraries that provide related functionality: |
| 725 |
|
| 726 |
* `WordSet <https://jaracotext.readthedocs.io/en/latest/#jaraco.text.WordSet>`_ |
| 727 |
parses identifiers like variable names into sets of words suitable for re-assembling |
| 728 |
in another form. |
| 729 |
|
| 730 |
* `word2number <https://pypi.org/project/word2number/>`_ converts words to |
| 731 |
a number. |
| 732 |
|
| 733 |
|
| 734 |
For Enterprise |
| 735 |
============== |
| 736 |
|
| 737 |
Available as part of the Tidelift Subscription. |
| 738 |
|
| 739 |
This project and the maintainers of thousands of other packages are working with Tidelift to deliver one enterprise subscription that covers all of the open source you use. |
| 740 |
|
| 741 |
`Learn more <https://tidelift.com/subscription/pkg/pypi-PROJECT?utm_source=pypi-PROJECT&utm_medium=referral&utm_campaign=github>`_. |