Class: Hablaba

Inherits:
Object
  • Object
show all
Defined in:
lib/hablaba.rb,
lib/hablaba/version.rb

Overview

Hablaba conjugates regular Spanish verbs in seven tenses.

Hablaba.conjugate("yo", "hablar")                 # => "hablo"
Hablaba.conjugate("nosotros", "comer", :future)   # => "comeremos"

Only regular verbs are supported. Irregular verbs are not detected, so Hablaba.conjugate("yo", "tener") returns the regular form "teno", not "tengo".

Defined Under Namespace

Classes: Error, UnknownPronounError, UnknownTenseError, UnknownVerbError

Constant Summary collapse

PRONOUNS =

Every accepted pronoun, mapped to its column in a conjugation table (0 = first person singular through 5 = third person plural). Unaccented spellings are accepted so callers do not need a Spanish keyboard.

{
  "yo" => 0,
  "" => 1, "tu" => 1,
  "él" => 2, "el" => 2, "ella" => 2, "usted" => 2,
  "nosotros" => 3, "nosotras" => 3,
  "vosotros" => 4, "vosotras" => 4,
  "ellos" => 5, "ellas" => 5, "ustedes" => 5
}.freeze
TENSES =

Supported tenses, in the order they appear in the README.

%i[
  present preterite imperfect present_subjunctive
  imperfect_subjunctive conditional future
].freeze
INFINITIVE_ENDINGS =

The three regular infinitive endings.

%w[ar er ir].freeze
STEM_ENDINGS =

Endings that attach to the stem (the infinitive minus -ar/-er/-ir).

{
  present: {
    "ar" => %w[o as a amos áis an],
    "er" => %w[o es e emos éis en],
    "ir" => %w[o es e imos ís en]
  },
  preterite: {
    "ar" => %w[é aste ó amos asteis aron],
    "er" => %w[í iste  imos isteis ieron],
    "ir" => %w[í iste  imos isteis ieron]
  },
  imperfect: {
    "ar" => %w[aba abas aba ábamos abais aban],
    "er" => %w[ía ías ía íamos íais ían],
    "ir" => %w[ía ías ía íamos íais ían]
  },
  present_subjunctive: {
    "ar" => %w[e es e emos éis en],
    "er" => %w[a as a amos áis an],
    "ir" => %w[a as a amos áis an]
  }
}.freeze
INFINITIVE_ENDINGS_BY_TENSE =

Endings that attach to the whole infinitive rather than the stem.

{
  conditional: %w[ía ías ía íamos íais ían],
  future: %w[é ás á emos éis án]
}.freeze
VERSION =
"1.0.0"

Class Method Summary collapse

Class Method Details

.conjugate(pronoun, verb, tense = :present) ⇒ Object

Conjugate a regular Spanish verb.

Example:

>> Hablaba.conjugate("yo", "hablar")
=> "hablo"

Arguments:

pronoun: (String) one of PRONOUNS, in any case, with or without accents
verb:    (String) an infinitive ending in -ar, -er or -ir
tense:   (Symbol) one of TENSES, defaults to :present

Raises UnknownPronounError, UnknownVerbError or UnknownTenseError when an argument is not recognized.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/hablaba.rb', line 108

def conjugate(pronoun, verb, tense = :present)
  person = person_for(pronoun)
  infinitive = normalize(verb, "verb")
  ending = infinitive_ending(infinitive)
  stem = infinitive[0..-3]

  if (endings_by_ending = STEM_ENDINGS[tense])
    stem + endings_by_ending.fetch(ending)[person]
  elsif (endings = INFINITIVE_ENDINGS_BY_TENSE[tense])
    infinitive + endings[person]
  elsif tense == :imperfect_subjunctive
    imperfect_subjunctive(person, ending, stem)
  else
    raise UnknownTenseError,
          "#{tense.inspect} is not a supported tense; expected one of #{TENSES.join(', ')}"
  end
end