Returns the singular of a given word.
(word, pos=NOUN, custom={})
| 588 | } |
| 589 | |
| 590 | def singularize(word, pos=NOUN, custom={}): |
| 591 | """ Returns the singular of a given word. |
| 592 | """ |
| 593 | if word in custom.keys(): |
| 594 | return custom[word] |
| 595 | # Recurse compound words (e.g. mothers-in-law). |
| 596 | if "-" in word: |
| 597 | w = word.split("-") |
| 598 | if len(w) > 1 and w[1] in plural_prepositions: |
| 599 | return singularize(w[0], pos, custom)+"-"+"-".join(w[1:]) |
| 600 | # dogs' => dog's |
| 601 | if word.endswith("'"): |
| 602 | return singularize(word[:-1]) + "'s" |
| 603 | w = word.lower() |
| 604 | for x in singular_uninflected: |
| 605 | if x.endswith(w): |
| 606 | return word |
| 607 | for x in singular_uncountable: |
| 608 | if x.endswith(w): |
| 609 | return word |
| 610 | for x in singular_ie: |
| 611 | if w.endswith(x+"s"): |
| 612 | return w |
| 613 | for x in singular_irregular.keys(): |
| 614 | if w.endswith(x): |
| 615 | return re.sub('(?i)'+x+'$', singular_irregular[x], word) |
| 616 | for suffix, inflection in singular_rules: |
| 617 | m = suffix.search(word) |
| 618 | g = m and m.groups() or [] |
| 619 | if m: |
| 620 | for k in range(len(g)): |
| 621 | if g[k] is None: |
| 622 | inflection = inflection.replace('\\' + str(k + 1), '') |
| 623 | return suffix.sub(inflection, word) |
| 624 | return word |
| 625 | |
| 626 | #### VERB CONJUGATION ############################################################################## |
| 627 |
no test coverage detected
searching dependent graphs…