Conjugate in the Spanish imperfect / Conjugue en el imperfecto de indicativo

13

0

Given an input of a Spanish verb in its infinitive form, output the verb conjugated in all 6 forms of the indicative imperfect.

To conjugate an infinitive in the imperfect, remove the infinitive ending (-ar, -er, -ir, and sometimes -ír) and add the imperfect endings listed below. There are three irregular verbs in the imperfect, also listed below.

Verb ending    Replace with...
--------------------------------------------------------
-ar            -aba, -abas, -aba, -ábamos, -abais, -aban
-er, -ir, -ír  -ía,  -ías,  -ía,  -íamos,  -íais,  -ían
Irreg. verb    Conjugations
--------------------------------------------------------
ser            era,  eras,  era,  éramos,  erais,  eran
ver            veía, veías, veía, veíamos, veíais, veían
ir             iba,  ibas,  iba,  íbamos,  ibais,  iban

For the purposes of this challenge, use capital letters instead of accented letters (á becomes A, é becomes E, and í becomes I). This is only because accented letters are 2 bytes in UTF-8, but they can be different in other encodings, and I'd rather not have to deal with any of that and stay within printable ASCII instead.

Infinitive verbs will always be three or more characters long with the sole exception of the verb ir, will never contain accented letters with the exception of verbs that end in -ír, and will always end in either -ar, -er, -ir, or -ír. Reflexive verbs do not need to be handled.

Output may be in either the form of a whitespace-/comma-separated string (so printing each conjugation to STDOUT on its own line is okay) or an array/list/etc.

Since this is , the shortest code in bytes wins.

Test cases:

In       Out
---------------------------------------------------------------
hablar   hablaba hablabas hablaba hablAbamos hablabais hablaban
comer    comIa comIas comIa comIamos comIais comIan
vivir    vivIa vivIas vivIa vivIamos vivIais vivIan
sonreIr  sonreIa sonreIas sonreIa sonreIamos sonreIais sonreIan
ser      era eras era Eramos erais eran
ver      veIa veIas veIa veIamos veIais veIan
ir       iba ibas iba Ibamos ibais iban

Doorknob

Posted 2016-01-05T00:29:29.687

Reputation: 68 138

1So many exceptions.. :P – Zach Gates – 2016-01-05T04:44:41.343

9@ZachGates See, you say that, but you haven't seen anything until you've seen preterite. Imperfect is one of the tenses with the least exceptions. – Addison Crump – 2016-01-05T04:50:51.110

I've taken Spanish 1-3, hehe. 0/10 would not recommend @FlagAsSpam – Zach Gates – 2016-01-05T04:52:46.970

@ZachGates Me too. :c – Addison Crump – 2016-01-05T04:53:14.877

Answers

6

Retina, 100 82 bytes

\B[^a]r$
I
r$
b
^vI$
veI
^sI$
er
$
a
$
 $_s $_ $_mos $_is $_n
T`l`L`.(?=[^I]amos )

Try it online. Unfortunately, due to the use of $_ it's not possible to modify the code to run all tests at once, so you'll have to copy in the individual verbs. I'm working on a new feature that will make this possible though.

Explanation

\B[^a]r$
I

We start by turning all endings which aren't ar into I, provided the input isn't the irregular verb ir. That takes care of the -er, -ir, -ír conjugations, messes up the irregular verbs ser and ver (but shortens them in the process), and leaves only ir and -ar with a trailing r.

r$
b

If the word still ends in r, we replace that with a b. We've now covered all standard conjugations as well as the irregular verb ir.

^vI$
veI

This fixes the irregular verb ver which has been turned into vI in the first stage.

^sI$
er

This fixes the irregular verb ser which has been turned into sI in the first stage. Now we've modified all possible stems correctly. All that's left to do is append all the possible endings, and then fix the accent for the first person plural in some cases.

$
a

First we append an a to the stem, because that's always part of all endings.

$
 $_s $_ $_mos $_is $_n

This makes use of the rarely seen $_ which inserts the entire string into the substitution (regardless of what was matched). This is actually very useful when duplicating strings, as are $` and $', which are also quite underappreciated.

Before this stage, we've got the first person singular in the string. We now append the other conjugations, by matching the end of the string with $ and building the other forms by appending the appropriate suffixes to the first person singular.

T`l`L`.(?=[^I]amos )

Finally, we fix the accent for the first person plural if applicable. This only needs to be done for a vowel, two characters in front of the -amos unless the character in between is I. So we match such a character and then use a transliteration stage to convert lower to upper case. This makes use of the new character classes l and L which I only added yesterday. They expand to a-z and A-Z respectively, so that new feature saves 4 bytes here.

Martin Ender

Posted 2016-01-05T00:29:29.687

Reputation: 184 808

3

Python 3, 154 232 bytes

M=input()
Q={'ser':'er','ver':'veI','ir':'ib'}.get(M)
H=[(Q if Q else M[:-2]+("ab"if M[-2:]=="ar"else"I"))+j for j in"a as a amos ais an".split(' ')]
if M[-2:]=="ar":H[3]=M[:-2]+"Abamos"
if Q in['er','ib']:H[3]=H[3].title()
print(H)

Fixed the missing capitals in irregular verbs.


Ungolfed

M=input()
Q={'ser':'er','ver':'veI','ir':'ib'}.get(M)

H=[]
for j in "a as a amos ais an".split(' '):
    if Q:
        F = Q
    else:
        if M[-2:] == "ar":
            F = M[:-2] + "ab"
        else:
            F = M[:-2] + "I"
    H += [F + j]

if M[-2:] == "ar":
    H[3] = M[:-2] + "Abamos"

if Q in ['er', 'ib']:
    H[3] = H[3].title()

print(H)

Zach Gates

Posted 2016-01-05T00:29:29.687

Reputation: 6 152

1

Milky Way 1.6.2, 281 bytes

'?{"ser"b_^"er";_}?{"ver"b_^"veI";_}?{"ir"b_^"ib";_}?{_:y2->CH=<1-&{~>;+<1-}^>;+<;_^""0}?{"ar"b_"ab";>+<0_}?{_"I";>+<_^}^;^"an ais amos a as a"" "\?{<y__<^}=3&{~<:>;>;+!^<1-}^?{'C=^"a"b_'C:y2->=^^<ΩG"Abamos"+!^^_}?{<"er"b_"Eramos"!^^_}?{<"ib"b_"Ibamos"!^^_}J:y1-;=<&{~<:>;>;+!^<1-}

Well that took awhile! Looks like I need to add some builtins, hehe...


Usage

./mw <path-to-code> -i <input>

Examples

$ ./mw test.mwg -i "hablar"
hablaba
hablabas
hablaba
hablAbamos
hablabais
hablaban

$ ./mw test.mwg -i "ver"
veIa
veIas
veIa
veIamos
veIais
veIan

Zach Gates

Posted 2016-01-05T00:29:29.687

Reputation: 6 152

1

Ruby, 151 149

Normally, I'll choose a newline over a semicolon where possible in the interests of readability, but as there are no semicolons, this is a one liner.

->n{%w{a as a amos ais an}.map{|z|['3r','veI','1b',n[0..-3]+(n[-2]==?a?'@b':?I)][("ser ver ir  "+n).index(n)/4].tr('31@',['eia','EIA'][z.size/4])+z}}

A feature is the use of placeholders 31@ for the accented vowels which depend on the amos endings. Thus the correct stem is formed, and then the placeholder is substituted for one of eia or EIA as appropriate

There a few uses of negative array indexes in here. Thus n[-2] refers to the second to last character of the input, and n[0..-3] refers to the input with the last 2 characters removed (the infinitive stem).

Ungolfed in test program

f=->n{
%w{a as a amos ais an}.map{|z|            #iterate through each verb ending
    ['3r','veI','1b',                     #array index 0,1,2 formats for irregular stems (note numbers 1 and 3 used as placeholders)
    n[0..-3]+(n[-2]==?a?'@b':?I)][        #array index 3 format for regular stems, AR->@b or ER/IR->I depending if A found at index -2 (@ used as placeholder)
    ("ser ver ir  "+n).index(n)/4]        #find index of input verb n in "ser ver ir  "+n, divide by 4 to obtain 0,1,2,3 above
    .tr('31@',['eia','EIA'][z.size/4])+   #if z is 4 characters long (AMOS) replace 3,1,@ with uppercase E,I,A. Otherwise with lowercase e,i,a
    z                                     #add the verb ending to the stem
  }
}

n=gets.chop
puts f[n]

Level River St

Posted 2016-01-05T00:29:29.687

Reputation: 22 049