Skip to content

Instantly share code, notes, and snippets.

@allanbatista
Created January 15, 2019 15:37
Show Gist options
  • Select an option

  • Save allanbatista/edcfcef7edc881353b0b4489039e02e4 to your computer and use it in GitHub Desktop.

Select an option

Save allanbatista/edcfcef7edc881353b0b4489039e02e4 to your computer and use it in GitHub Desktop.
Transforma palavras no plural para palavras no singular.
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