Returns the plural of a given word, e.g., child => children. Handles nouns and adjectives, using classical inflection by default (i.e., where "matrix" pluralizes to "matrices" and not "matrixes"). The custom dictionary is for user-defined replacements.
(word, pos=NOUN, custom={}, classical=True)
| 383 | } |
| 384 | |
| 385 | def pluralize(word, pos=NOUN, custom={}, classical=True): |
| 386 | """ Returns the plural of a given word, e.g., child => children. |
| 387 | Handles nouns and adjectives, using classical inflection by default |
| 388 | (i.e., where "matrix" pluralizes to "matrices" and not "matrixes"). |
| 389 | The custom dictionary is for user-defined replacements. |
| 390 | """ |
| 391 | if word in custom: |
| 392 | return custom[word] |
| 393 | # Recurse genitives. |
| 394 | # Remove the apostrophe and any trailing -s, |
| 395 | # form the plural of the resultant noun, and then append an apostrophe (dog's => dogs'). |
| 396 | if word.endswith(("'", "'s")): |
| 397 | w = word.rstrip("'s") |
| 398 | w = pluralize(w, pos, custom, classical) |
| 399 | if w.endswith("s"): |
| 400 | return w + "'" |
| 401 | else: |
| 402 | return w + "'s" |
| 403 | # Recurse compound words |
| 404 | # (e.g., Postmasters General, mothers-in-law, Roman deities). |
| 405 | w = word.replace("-", " ").split(" ") |
| 406 | if len(w) > 1: |
| 407 | if w[1] == "general" or \ |
| 408 | w[1] == "General" and \ |
| 409 | w[0] not in plural_categories["general-generals"]: |
| 410 | return word.replace(w[0], pluralize(w[0], pos, custom, classical)) |
| 411 | elif w[1] in plural_prepositions: |
| 412 | return word.replace(w[0], pluralize(w[0], pos, custom, classical)) |
| 413 | else: |
| 414 | return word.replace(w[-1], pluralize(w[-1], pos, custom, classical)) |
| 415 | # Only a very few number of adjectives inflect. |
| 416 | n = range(len(plural_rules)) |
| 417 | if pos.startswith(ADJECTIVE): |
| 418 | n = [0, 1] |
| 419 | # Apply pluralization rules. |
| 420 | for i in n: |
| 421 | for suffix, inflection, category, classic in plural_rules[i]: |
| 422 | # A general rule, or a classic rule in classical mode. |
| 423 | if category is None: |
| 424 | if not classic or (classic and classical): |
| 425 | if suffix.search(word) is not None: |
| 426 | return suffix.sub(inflection, word) |
| 427 | # A rule pertaining to a specific category of words. |
| 428 | if category is not None: |
| 429 | if word in plural_categories[category] and (not classic or (classic and classical)): |
| 430 | if suffix.search(word) is not None: |
| 431 | return suffix.sub(inflection, word) |
| 432 | return word |
| 433 | |
| 434 | #print pluralize("part-of-speech") |
| 435 | #print pluralize("child") |
no test coverage detected
searching dependent graphs…