#-*- coding: utf-8 -*-
# Generated from spanish.sbl by Snowball 3.0.1 - https://snowballstem.org/

from .basestemmer import BaseStemmer
from .among import Among


class SpanishStemmer(BaseStemmer):
    '''
    This class implements the stemming algorithm defined by a snowball script.
    Generated from spanish.sbl by Snowball 3.0.1 - https://snowballstem.org/
    '''

    g_v = {u"a", u"e", u"i", u"o", u"u", u"á", u"é", u"í", u"ó", u"ú", u"ü"}

    I_p2 = 0
    I_p1 = 0
    I_pV = 0

    def __r_mark_regions(self):
        self.I_pV = self.limit
        self.I_p1 = self.limit
        self.I_p2 = self.limit
        v_1 = self.cursor
        try:
            try:
                v_2 = self.cursor
                try:
                    if not self.in_grouping(SpanishStemmer.g_v):
                        raise lab2()
                    try:
                        v_3 = self.cursor
                        try:
                            if not self.out_grouping(SpanishStemmer.g_v):
                                raise lab4()
                            if not self.go_out_grouping(SpanishStemmer.g_v):
                                raise lab4()
                            self.cursor += 1
                            raise lab3()
                        except lab4: pass
                        self.cursor = v_3
                        if not self.in_grouping(SpanishStemmer.g_v):
                            raise lab2()
                        if not self.go_in_grouping(SpanishStemmer.g_v):
                            raise lab2()
                        self.cursor += 1
                    except lab3: pass
                    raise lab1()
                except lab2: pass
                self.cursor = v_2
                if not self.out_grouping(SpanishStemmer.g_v):
                    raise lab0()
                try:
                    v_4 = self.cursor
                    try:
                        if not self.out_grouping(SpanishStemmer.g_v):
                            raise lab6()
                        if not self.go_out_grouping(SpanishStemmer.g_v):
                            raise lab6()
                        self.cursor += 1
                        raise lab5()
                    except lab6: pass
                    self.cursor = v_4
                    if not self.in_grouping(SpanishStemmer.g_v):
                        raise lab0()
                    if self.cursor >= self.limit:
                        raise lab0()
                    self.cursor += 1
                except lab5: pass
            except lab1: pass
            self.I_pV = self.cursor
        except lab0: pass
        self.cursor = v_1
        v_5 = self.cursor
        try:
            if not self.go_out_grouping(SpanishStemmer.g_v):
                raise lab7()
            self.cursor += 1
            if not self.go_in_grouping(SpanishStemmer.g_v):
                raise lab7()
            self.cursor += 1
            self.I_p1 = self.cursor
            if not self.go_out_grouping(SpanishStemmer.g_v):
                raise lab7()
            self.cursor += 1
            if not self.go_in_grouping(SpanishStemmer.g_v):
                raise lab7()
            self.cursor += 1
            self.I_p2 = self.cursor
        except lab7: pass
        self.cursor = v_5
        return True

    def __r_postlude(self):
        while True:
            v_1 = self.cursor
            try:
                self.bra = self.cursor
                among_var = self.find_among(SpanishStemmer.a_0)
                self.ket = self.cursor
                if among_var == 1:
                    if not self.slice_from(u"a"):
                        return False
                elif among_var == 2:
                    if not self.slice_from(u"e"):
                        return False
                elif among_var == 3:
                    if not self.slice_from(u"i"):
                        return False
                elif among_var == 4:
                    if not self.slice_from(u"o"):
                        return False
                elif among_var == 5:
                    if not self.slice_from(u"u"):
                        return False
                else:
                    if self.cursor >= self.limit:
                        raise lab0()
                    self.cursor += 1
                continue
            except lab0: pass
            self.cursor = v_1
            break
        return True

    def __r_RV(self):
        return self.I_pV <= self.cursor

    def __r_R1(self):
        return self.I_p1 <= self.cursor

    def __r_R2(self):
        return self.I_p2 <= self.cursor

    def __r_attached_pronoun(self):
        self.ket = self.cursor
        if self.find_among_b(SpanishStemmer.a_1) == 0:
            return False
        self.bra = self.cursor
        among_var = self.find_among_b(SpanishStemmer.a_2)
        if among_var == 0:
            return False
        if not self.__r_RV():
            return False
        if among_var == 1:
            self.bra = self.cursor
            if not self.slice_from(u"iendo"):
                return False
        elif among_var == 2:
            self.bra = self.cursor
            if not self.slice_from(u"ando"):
                return False
        elif among_var == 3:
            self.bra = self.cursor
            if not self.slice_from(u"ar"):
                return False
        elif among_var == 4:
            self.bra = self.cursor
            if not self.slice_from(u"er"):
                return False
        elif among_var == 5:
            self.bra = self.cursor
            if not self.slice_from(u"ir"):
                return False
        elif among_var == 6:
            if not self.slice_del():
                return False

        else:
            if not self.eq_s_b(u"u"):
                return False
            if not self.slice_del():
                return False

        return True

    def __r_standard_suffix(self):
        self.ket = self.cursor
        among_var = self.find_among_b(SpanishStemmer.a_6)
        if among_var == 0:
            return False
        self.bra = self.cursor
        if among_var == 1:
            if not self.__r_R2():
                return False
            if not self.slice_del():
                return False

        elif among_var == 2:
            if not self.__r_R2():
                return False
            if not self.slice_del():
                return False

            v_1 = self.limit - self.cursor
            try:
                self.ket = self.cursor
                if not self.eq_s_b(u"ic"):
                    self.cursor = self.limit - v_1
                    raise lab0()
                self.bra = self.cursor
                if not self.__r_R2():
                    self.cursor = self.limit - v_1
                    raise lab0()
                if not self.slice_del():
                    return False

            except lab0: pass
        elif among_var == 3:
            if not self.__r_R2():
                return False
            if not self.slice_from(u"log"):
                return False
        elif among_var == 4:
            if not self.__r_R2():
                return False
            if not self.slice_from(u"u"):
                return False
        elif among_var == 5:
            if not self.__r_R2():
                return False
            if not self.slice_from(u"ente"):
                return False
        elif among_var == 6:
            if not self.__r_R1():
                return False
            if not self.slice_del():
                return False

            v_2 = self.limit - self.cursor
            try:
                self.ket = self.cursor
                among_var = self.find_among_b(SpanishStemmer.a_3)
                if among_var == 0:
                    self.cursor = self.limit - v_2
                    raise lab1()
                self.bra = self.cursor
                if not self.__r_R2():
                    self.cursor = self.limit - v_2
                    raise lab1()
                if not self.slice_del():
                    return False

                if among_var == 1:
                    self.ket = self.cursor
                    if not self.eq_s_b(u"at"):
                        self.cursor = self.limit - v_2
                        raise lab1()
                    self.bra = self.cursor
                    if not self.__r_R2():
                        self.cursor = self.limit - v_2
                        raise lab1()
                    if not self.slice_del():
                        return False

            except lab1: pass
        elif among_var == 7:
            if not self.__r_R2():
                return False
            if not self.slice_del():
                return False

            v_3 = self.limit - self.cursor
            try:
                self.ket = self.cursor
                if self.find_among_b(SpanishStemmer.a_4) == 0:
                    self.cursor = self.limit - v_3
                    raise lab2()
                self.bra = self.cursor
                if not self.__r_R2():
                    self.cursor = self.limit - v_3
                    raise lab2()
                if not self.slice_del():
                    return False

            except lab2: pass
        elif among_var == 8:
            if not self.__r_R2():
                return False
            if not self.slice_del():
                return False

            v_4 = self.limit - self.cursor
            try:
                self.ket = self.cursor
                if self.find_among_b(SpanishStemmer.a_5) == 0:
                    self.cursor = self.limit - v_4
                    raise lab3()
                self.bra = self.cursor
                if not self.__r_R2():
                    self.cursor = self.limit - v_4
                    raise lab3()
                if not self.slice_del():
                    return False

            except lab3: pass
        else:
            if not self.__r_R2():
                return False
            if not self.slice_del():
                return False

            v_5 = self.limit - self.cursor
            try:
                self.ket = self.cursor
                if not self.eq_s_b(u"at"):
                    self.cursor = self.limit - v_5
                    raise lab4()
                self.bra = self.cursor
                if not self.__r_R2():
                    self.cursor = self.limit - v_5
                    raise lab4()
                if not self.slice_del():
                    return False

            except lab4: pass
        return True

    def __r_y_verb_suffix(self):
        if self.cursor < self.I_pV:
            return False
        v_2 = self.limit_backward
        self.limit_backward = self.I_pV
        self.ket = self.cursor
        if self.find_among_b(SpanishStemmer.a_7) == 0:
            self.limit_backward = v_2
            return False
        self.bra = self.cursor
        self.limit_backward = v_2
        if not self.eq_s_b(u"u"):
            return False
        if not self.slice_del():
            return False

        return True

    def __r_verb_suffix(self):
        if self.cursor < self.I_pV:
            return False
        v_2 = self.limit_backward
        self.limit_backward = self.I_pV
        self.ket = self.cursor
        among_var = self.find_among_b(SpanishStemmer.a_8)
        if among_var == 0:
            self.limit_backward = v_2
            return False
        self.bra = self.cursor
        self.limit_backward = v_2
        if among_var == 1:
            v_3 = self.limit - self.cursor
            try:
                if not self.eq_s_b(u"u"):
                    self.cursor = self.limit - v_3
                    raise lab0()
                v_4 = self.limit - self.cursor
                if not self.eq_s_b(u"g"):
                    self.cursor = self.limit - v_3
                    raise lab0()
                self.cursor = self.limit - v_4
            except lab0: pass
            self.bra = self.cursor
            if not self.slice_del():
                return False

        else:
            if not self.slice_del():
                return False

        return True

    def __r_residual_suffix(self):
        self.ket = self.cursor
        among_var = self.find_among_b(SpanishStemmer.a_9)
        if among_var == 0:
            return False
        self.bra = self.cursor
        if among_var == 1:
            if not self.__r_RV():
                return False
            if not self.slice_del():
                return False

        else:
            if not self.__r_RV():
                return False
            if not self.slice_del():
                return False

            v_1 = self.limit - self.cursor
            try:
                self.ket = self.cursor
                if not self.eq_s_b(u"u"):
                    self.cursor = self.limit - v_1
                    raise lab0()
                self.bra = self.cursor
                v_2 = self.limit - self.cursor
                if not self.eq_s_b(u"g"):
                    self.cursor = self.limit - v_1
                    raise lab0()
                self.cursor = self.limit - v_2
                if not self.__r_RV():
                    self.cursor = self.limit - v_1
                    raise lab0()
                if not self.slice_del():
                    return False

            except lab0: pass
        return True

    def _stem(self):
        self.__r_mark_regions()
        self.limit_backward = self.cursor
        self.cursor = self.limit
        v_2 = self.limit - self.cursor
        self.__r_attached_pronoun()
        self.cursor = self.limit - v_2
        v_3 = self.limit - self.cursor
        try:
            try:
                v_4 = self.limit - self.cursor
                try:
                    if not self.__r_standard_suffix():
                        raise lab2()
                    raise lab1()
                except lab2: pass
                self.cursor = self.limit - v_4
                try:
                    if not self.__r_y_verb_suffix():
                        raise lab3()
                    raise lab1()
                except lab3: pass
                self.cursor = self.limit - v_4
                if not self.__r_verb_suffix():
                    raise lab0()
            except lab1: pass
        except lab0: pass
        self.cursor = self.limit - v_3
        v_5 = self.limit - self.cursor
        self.__r_residual_suffix()
        self.cursor = self.limit - v_5
        self.cursor = self.limit_backward
        v_6 = self.cursor
        self.__r_postlude()
        self.cursor = v_6
        return True

    a_0 = [
        Among(u"", -1, 6),
        Among(u"á", 0, 1),
        Among(u"é", 0, 2),
        Among(u"í", 0, 3),
        Among(u"ó", 0, 4),
        Among(u"ú", 0, 5)
    ]

    a_1 = [
        Among(u"la", -1, -1),
        Among(u"sela", 0, -1),
        Among(u"le", -1, -1),
        Among(u"me", -1, -1),
        Among(u"se", -1, -1),
        Among(u"lo", -1, -1),
        Among(u"selo", 5, -1),
        Among(u"las", -1, -1),
        Among(u"selas", 7, -1),
        Among(u"les", -1, -1),
        Among(u"los", -1, -1),
        Among(u"selos", 10, -1),
        Among(u"nos", -1, -1)
    ]

    a_2 = [
        Among(u"ando", -1, 6),
        Among(u"iendo", -1, 6),
        Among(u"yendo", -1, 7),
        Among(u"ándo", -1, 2),
        Among(u"iéndo", -1, 1),
        Among(u"ar", -1, 6),
        Among(u"er", -1, 6),
        Among(u"ir", -1, 6),
        Among(u"ár", -1, 3),
        Among(u"ér", -1, 4),
        Among(u"ír", -1, 5)
    ]

    a_3 = [
        Among(u"ic", -1, -1),
        Among(u"ad", -1, -1),
        Among(u"os", -1, -1),
        Among(u"iv", -1, 1)
    ]

    a_4 = [
        Among(u"able", -1, 1),
        Among(u"ible", -1, 1),
        Among(u"ante", -1, 1)
    ]

    a_5 = [
        Among(u"ic", -1, 1),
        Among(u"abil", -1, 1),
        Among(u"iv", -1, 1)
    ]

    a_6 = [
        Among(u"ica", -1, 1),
        Among(u"ancia", -1, 2),
        Among(u"encia", -1, 5),
        Among(u"adora", -1, 2),
        Among(u"osa", -1, 1),
        Among(u"ista", -1, 1),
        Among(u"iva", -1, 9),
        Among(u"anza", -1, 1),
        Among(u"logía", -1, 3),
        Among(u"idad", -1, 8),
        Among(u"able", -1, 1),
        Among(u"ible", -1, 1),
        Among(u"ante", -1, 2),
        Among(u"mente", -1, 7),
        Among(u"amente", 13, 6),
        Among(u"acion", -1, 2),
        Among(u"ucion", -1, 4),
        Among(u"ación", -1, 2),
        Among(u"ución", -1, 4),
        Among(u"ico", -1, 1),
        Among(u"ismo", -1, 1),
        Among(u"oso", -1, 1),
        Among(u"amiento", -1, 1),
        Among(u"imiento", -1, 1),
        Among(u"ivo", -1, 9),
        Among(u"ador", -1, 2),
        Among(u"icas", -1, 1),
        Among(u"ancias", -1, 2),
        Among(u"encias", -1, 5),
        Among(u"adoras", -1, 2),
        Among(u"osas", -1, 1),
        Among(u"istas", -1, 1),
        Among(u"ivas", -1, 9),
        Among(u"anzas", -1, 1),
        Among(u"logías", -1, 3),
        Among(u"idades", -1, 8),
        Among(u"ables", -1, 1),
        Among(u"ibles", -1, 1),
        Among(u"aciones", -1, 2),
        Among(u"uciones", -1, 4),
        Among(u"adores", -1, 2),
        Among(u"antes", -1, 2),
        Among(u"icos", -1, 1),
        Among(u"ismos", -1, 1),
        Among(u"osos", -1, 1),
        Among(u"amientos", -1, 1),
        Among(u"imientos", -1, 1),
        Among(u"ivos", -1, 9)
    ]

    a_7 = [
        Among(u"ya", -1, 1),
        Among(u"ye", -1, 1),
        Among(u"yan", -1, 1),
        Among(u"yen", -1, 1),
        Among(u"yeron", -1, 1),
        Among(u"yendo", -1, 1),
        Among(u"yo", -1, 1),
        Among(u"yas", -1, 1),
        Among(u"yes", -1, 1),
        Among(u"yais", -1, 1),
        Among(u"yamos", -1, 1),
        Among(u"yó", -1, 1)
    ]

    a_8 = [
        Among(u"aba", -1, 2),
        Among(u"ada", -1, 2),
        Among(u"ida", -1, 2),
        Among(u"ara", -1, 2),
        Among(u"iera", -1, 2),
        Among(u"ía", -1, 2),
        Among(u"aría", 5, 2),
        Among(u"ería", 5, 2),
        Among(u"iría", 5, 2),
        Among(u"ad", -1, 2),
        Among(u"ed", -1, 2),
        Among(u"id", -1, 2),
        Among(u"ase", -1, 2),
        Among(u"iese", -1, 2),
        Among(u"aste", -1, 2),
        Among(u"iste", -1, 2),
        Among(u"an", -1, 2),
        Among(u"aban", 16, 2),
        Among(u"aran", 16, 2),
        Among(u"ieran", 16, 2),
        Among(u"ían", 16, 2),
        Among(u"arían", 20, 2),
        Among(u"erían", 20, 2),
        Among(u"irían", 20, 2),
        Among(u"en", -1, 1),
        Among(u"asen", 24, 2),
        Among(u"iesen", 24, 2),
        Among(u"aron", -1, 2),
        Among(u"ieron", -1, 2),
        Among(u"arán", -1, 2),
        Among(u"erán", -1, 2),
        Among(u"irán", -1, 2),
        Among(u"ado", -1, 2),
        Among(u"ido", -1, 2),
        Among(u"ando", -1, 2),
        Among(u"iendo", -1, 2),
        Among(u"ar", -1, 2),
        Among(u"er", -1, 2),
        Among(u"ir", -1, 2),
        Among(u"as", -1, 2),
        Among(u"abas", 39, 2),
        Among(u"adas", 39, 2),
        Among(u"idas", 39, 2),
        Among(u"aras", 39, 2),
        Among(u"ieras", 39, 2),
        Among(u"ías", 39, 2),
        Among(u"arías", 45, 2),
        Among(u"erías", 45, 2),
        Among(u"irías", 45, 2),
        Among(u"es", -1, 1),
        Among(u"ases", 49, 2),
        Among(u"ieses", 49, 2),
        Among(u"abais", -1, 2),
        Among(u"arais", -1, 2),
        Among(u"ierais", -1, 2),
        Among(u"íais", -1, 2),
        Among(u"aríais", 55, 2),
        Among(u"eríais", 55, 2),
        Among(u"iríais", 55, 2),
        Among(u"aseis", -1, 2),
        Among(u"ieseis", -1, 2),
        Among(u"asteis", -1, 2),
        Among(u"isteis", -1, 2),
        Among(u"áis", -1, 2),
        Among(u"éis", -1, 1),
        Among(u"aréis", 64, 2),
        Among(u"eréis", 64, 2),
        Among(u"iréis", 64, 2),
        Among(u"ados", -1, 2),
        Among(u"idos", -1, 2),
        Among(u"amos", -1, 2),
        Among(u"ábamos", 70, 2),
        Among(u"áramos", 70, 2),
        Among(u"iéramos", 70, 2),
        Among(u"íamos", 70, 2),
        Among(u"aríamos", 74, 2),
        Among(u"eríamos", 74, 2),
        Among(u"iríamos", 74, 2),
        Among(u"emos", -1, 1),
        Among(u"aremos", 78, 2),
        Among(u"eremos", 78, 2),
        Among(u"iremos", 78, 2),
        Among(u"ásemos", 78, 2),
        Among(u"iésemos", 78, 2),
        Among(u"imos", -1, 2),
        Among(u"arás", -1, 2),
        Among(u"erás", -1, 2),
        Among(u"irás", -1, 2),
        Among(u"ís", -1, 2),
        Among(u"ará", -1, 2),
        Among(u"erá", -1, 2),
        Among(u"irá", -1, 2),
        Among(u"aré", -1, 2),
        Among(u"eré", -1, 2),
        Among(u"iré", -1, 2),
        Among(u"ió", -1, 2)
    ]

    a_9 = [
        Among(u"a", -1, 1),
        Among(u"e", -1, 2),
        Among(u"o", -1, 1),
        Among(u"os", -1, 1),
        Among(u"á", -1, 1),
        Among(u"é", -1, 2),
        Among(u"í", -1, 1),
        Among(u"ó", -1, 1)
    ]


class lab0(BaseException): pass


class lab1(BaseException): pass


class lab2(BaseException): pass


class lab3(BaseException): pass


class lab4(BaseException): pass


class lab5(BaseException): pass


class lab6(BaseException): pass


class lab7(BaseException): pass
