Last active
February 2, 2017 00:01
-
-
Save ZuraGuerra/008e59175b5b024988125af3e85cdada to your computer and use it in GitHub Desktop.
Usage: `Spanish.PastTense.conjugate(subject, verb_on_infinitive)`
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
defmodule Spanish.PastTense do | |
alias Spanish.Participle | |
import String, only: [capitalize: 1] | |
@to_have %{"yo" => "he", | |
"tú" => "has", | |
"él" => "ha", # same for "ella" | |
"nosotros" => "hemos", | |
"ustedes" => "han"} # same for "ellos" and "ellas" | |
@singular_exceptions ["ella", "eso"] | |
@plural_exceptions ["ellos", "ellas"] | |
def conjugate(subject, verb) when subject in @singular_exceptions, | |
do: capitalize(subject) <> form_verb("él", verb) | |
def conjugate(subject, verb) when subject in @plural_exceptions, | |
do: capitalize(subject) <> form_verb("ustedes", verb) | |
def conjugate(subject, verb), | |
do: capitalize(subject) <> form_verb(subject, verb) | |
defp form_verb(subject, verb), | |
do: " " <> @to_have[subject] <> " " <> Participle.conjugate(verb) | |
end | |
defmodule Spanish.Participle do | |
@rules %{"ar" => "ado", | |
"ir" => "ido", | |
"er" => "ido"} | |
def conjugate(verb) do | |
Enum.reduce(@rules, verb, fn({ending, new_ending}, verb)-> | |
# Check if `verb` ends with current `ending` | |
# (Regex copied shamelessly from StackOverflow) | |
if Regex.match?(~r/\b\p{L}*#{ending}\b/, verb), | |
do: String.replace(verb, ending, new_ending), else: verb | |
end) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment