HiveBrain v1.2.0
Get Started
← Back to all entries
snippetpythonMinor

Python function to generate Spanish conjugations

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
functionconjugationsgeneratepythonspanish

Problem

I tried to make a Python function to generate present tense Spanish conjugations.

from unidecode import unidecode
def solveSpanishPresent(pronoun,verb):
  if unidecode(verb).endswith("ar"):
    arDict = {"yo": "o", "tú": "as", "usted": "a", "él": "a", "ella": "a", "nosotros": "amos", "vosotros": "áis", "ustedes": "an", "ellos": "an", "ellas": "an"}
    verb = verb[:-2] + arDict[pronoun]
    return verb
  if unidecode(verb).endswith("er") or unidecode(verb).endswith("ir"):
    erDict = {"yo":"o", "tú":"es", "usted":"e", "él":"e", "ella":"e", "nosotros":"emos", "vosotros":"éis", "ustedes":"en", "ellos":"en", "ellas":"en"}
    irDict = {"nosotros":"imos", "vosotros":"ís"}
    if (pronoun == "nosotros" or pronoun == "vosotros") and verb.endswith("ir"):
        verb = verb[:-2] + irDict[pronoun]
    else:
        verb = verb[:-2] + erDict[pronoun]
    return verb

Solution

I think that there are too many special cases in the code. A data-driven approach would be easier to read and maintain than hard-coded logic. The entire function could be done using just dictionary lookups.

Furthermore, I think that you are missing a translation layer. You can classify the three pronouns "usted", "él", and "ella" as "third person singular", and it will be clear that they all lead to the same conjugation. It would be more worthwhile to "compress" your conjugation tables that way than to try to override a few entries of the erDict with irDict.

I would also format the dictionaries to look more like the tables that would typically be presented in grammar books. The line containing your arDict stretches to column 163, which is too wide to be readable.

from unidecode import unidecode

PRONOUN_CLASSIFICATION = {
    'yo': '1s',     'nosotros': '1p',
    'tú': '2s',     'vosotros': '2p',
    'usted': '3s',  'ustedes': '3p',
    'él': '3s',     'ellos': '3p',
    'ella': '3s',   'ellas': '3p',
}

PRESENT_CONJUGATION_ENDINGS = {
    'ar': {
        '1s': 'o',      '1p': 'amos',
        '2s': 'as',     '2p': 'áis',
        '3s': 'a',      '3p': 'an',
    },
    'er': {
        '1s': 'o',      '1p': 'emos',
        '2s': 'es',     '2p': 'éis',
        '3s': 'e',      '3p': 'en',
    },
    'ir': {
        '1s': 'o',      '1p': 'imos',
        '2s': 'es',     '2p': 'ís',
        '3s': 'e',      '3p': 'en',
    },
}

def conjugate_spanish_present(pronoun, infinitive):
    person = PRONOUN_CLASSIFICATION[pronoun]
    base, ending = infinitive[:-2], unidecode(infinitive)[-2:]
    try:
        return base + PRESENT_CONJUGATION_ENDINGS[ending][person]
    except KeyError:
        return infinitive

Code Snippets

from unidecode import unidecode

PRONOUN_CLASSIFICATION = {
    'yo': '1s',     'nosotros': '1p',
    'tú': '2s',     'vosotros': '2p',
    'usted': '3s',  'ustedes': '3p',
    'él': '3s',     'ellos': '3p',
    'ella': '3s',   'ellas': '3p',
}

PRESENT_CONJUGATION_ENDINGS = {
    'ar': {
        '1s': 'o',      '1p': 'amos',
        '2s': 'as',     '2p': 'áis',
        '3s': 'a',      '3p': 'an',
    },
    'er': {
        '1s': 'o',      '1p': 'emos',
        '2s': 'es',     '2p': 'éis',
        '3s': 'e',      '3p': 'en',
    },
    'ir': {
        '1s': 'o',      '1p': 'imos',
        '2s': 'es',     '2p': 'ís',
        '3s': 'e',      '3p': 'en',
    },
}

def conjugate_spanish_present(pronoun, infinitive):
    person = PRONOUN_CLASSIFICATION[pronoun]
    base, ending = infinitive[:-2], unidecode(infinitive)[-2:]
    try:
        return base + PRESENT_CONJUGATION_ENDINGS[ending][person]
    except KeyError:
        return infinitive

Context

StackExchange Code Review Q#160597, answer score: 5

Revisions (0)

No revisions yet.