Created
January 15, 2019 15:37
-
-
Save allanbatista/edcfcef7edc881353b0b4489039e02e4 to your computer and use it in GitHub Desktop.
Transforma palavras no plural para palavras no singular.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import re | |
| def rule(match, replace): | |
| return (re.compile("^([a-zA-z]*){}$".format(match)), re.compile("{}$".format(match)), replace) | |
| def singularize(word): | |
| rules = [ | |
| rule("as", "a"), | |
| rule("es", "e"), | |
| rule("is", "i"), | |
| rule("os", "o"), | |
| rule("us", "u"), | |
| rule("res", "r"), | |
| rule("zes", "z"), | |
| rule("is" , "il"), | |
| rule("ais", "al"), | |
| rule("eis", "el"), | |
| rule("ois", "ol"), | |
| rule("uis", "ul"), | |
| rule("ns", "m"), | |
| rule("oes", "ao"), | |
| rule("aes", "ao"), | |
| rule("aos", "ao") | |
| ] | |
| rules.reverse() | |
| for r in rules: | |
| if r[0].match(word): | |
| return r[1].sub(r[2], word) | |
| return word |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment