# This file was generated automatically by the Snowball to Python compiler
# http://snowballstem.org/

from .basestemmer import BaseStemmer
from .among import Among


class DanishStemmer(BaseStemmer):
    '''
    This class was automatically generated by a Snowball to Python compiler
    It implements the stemming algorithm defined by a snowball script.
    '''

    a_0 = [
        Among(u"hed", -1, 1),
        Among(u"ethed", 0, 1),
        Among(u"ered", -1, 1),
        Among(u"e", -1, 1),
        Among(u"erede", 3, 1),
        Among(u"ende", 3, 1),
        Among(u"erende", 5, 1),
        Among(u"ene", 3, 1),
        Among(u"erne", 3, 1),
        Among(u"ere", 3, 1),
        Among(u"en", -1, 1),
        Among(u"heden", 10, 1),
        Among(u"eren", 10, 1),
        Among(u"er", -1, 1),
        Among(u"heder", 13, 1),
        Among(u"erer", 13, 1),
        Among(u"s", -1, 2),
        Among(u"heds", 16, 1),
        Among(u"es", 16, 1),
        Among(u"endes", 18, 1),
        Among(u"erendes", 19, 1),
        Among(u"enes", 18, 1),
        Among(u"ernes", 18, 1),
        Among(u"eres", 18, 1),
        Among(u"ens", 16, 1),
        Among(u"hedens", 24, 1),
        Among(u"erens", 24, 1),
        Among(u"ers", 16, 1),
        Among(u"ets", 16, 1),
        Among(u"erets", 28, 1),
        Among(u"et", -1, 1),
        Among(u"eret", 30, 1)
    ]

    a_1 = [
        Among(u"gd", -1, -1),
        Among(u"dt", -1, -1),
        Among(u"gt", -1, -1),
        Among(u"kt", -1, -1)
    ]

    a_2 = [
        Among(u"ig", -1, 1),
        Among(u"lig", 0, 1),
        Among(u"elig", 1, 1),
        Among(u"els", -1, 1),
        Among(u"l\u00F8st", -1, 2)
    ]

    g_c = [119, 223, 119, 1]

    g_v = [17, 65, 16, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 48, 0, 128]

    g_s_ending = [239, 254, 42, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 16]

    I_x = 0
    I_p1 = 0
    S_ch = ""


    def __r_mark_regions(self):
        # (, line 31
        self.I_p1 = self.limit
        # test, line 35
        v_1 = self.cursor
        # (, line 35
        # hop, line 35
        c = self.cursor + 3
        if 0 > c or c > self.limit:
            return False
        self.cursor = c
        # setmark x, line 35
        self.I_x = self.cursor
        self.cursor = v_1
        # goto, line 36
        try:
            while True:
                v_2 = self.cursor
                try:
                    if not self.in_grouping(DanishStemmer.g_v, 97, 248):
                        raise lab1()
                    self.cursor = v_2
                    raise lab0()
                except lab1: pass
                self.cursor = v_2
                if self.cursor >= self.limit:
                    return False
                self.cursor += 1
        except lab0: pass
        # gopast, line 36
        try:
            while True:
                try:
                    if not self.out_grouping(DanishStemmer.g_v, 97, 248):
                        raise lab3()
                    raise lab2()
                except lab3: pass
                if self.cursor >= self.limit:
                    return False
                self.cursor += 1
        except lab2: pass
        # setmark p1, line 36
        self.I_p1 = self.cursor
        # try, line 37
        try:
            # (, line 37
            if not self.I_p1 < self.I_x:
                raise lab4()
            self.I_p1 = self.I_x
        except lab4: pass
        return True

    def __r_main_suffix(self):
        # (, line 42
        # setlimit, line 43
        if self.cursor < self.I_p1:
            return False
        v_2 = self.limit_backward
        self.limit_backward = self.I_p1
        # (, line 43
        # [, line 43
        self.ket = self.cursor
        # substring, line 43
        among_var = self.find_among_b(DanishStemmer.a_0)
        if among_var == 0:
            self.limit_backward = v_2
            return False
        # ], line 43
        self.bra = self.cursor
        self.limit_backward = v_2
        if among_var == 1:
            # (, line 50
            # delete, line 50
            if not self.slice_del():
                return False

        elif among_var == 2:
            # (, line 52
            if not self.in_grouping_b(DanishStemmer.g_s_ending, 97, 229):
                return False
            # delete, line 52
            if not self.slice_del():
                return False

        return True

    def __r_consonant_pair(self):
        # (, line 56
        # test, line 57
        v_1 = self.limit - self.cursor
        # (, line 57
        # setlimit, line 58
        if self.cursor < self.I_p1:
            return False
        v_3 = self.limit_backward
        self.limit_backward = self.I_p1
        # (, line 58
        # [, line 58
        self.ket = self.cursor
        # substring, line 58
        if self.find_among_b(DanishStemmer.a_1) == 0:
            self.limit_backward = v_3
            return False
        # ], line 58
        self.bra = self.cursor
        self.limit_backward = v_3
        self.cursor = self.limit - v_1
        # next, line 64
        if self.cursor <= self.limit_backward:
            return False
        self.cursor -= 1
        # ], line 64
        self.bra = self.cursor
        # delete, line 64
        if not self.slice_del():
            return False

        return True

    def __r_other_suffix(self):
        # (, line 67
        # do, line 68
        v_1 = self.limit - self.cursor
        try:
            # (, line 68
            # [, line 68
            self.ket = self.cursor
            # literal, line 68
            if not self.eq_s_b(u"st"):
                raise lab0()
            # ], line 68
            self.bra = self.cursor
            # literal, line 68
            if not self.eq_s_b(u"ig"):
                raise lab0()
            # delete, line 68
            if not self.slice_del():
                return False

        except lab0: pass
        self.cursor = self.limit - v_1
        # setlimit, line 69
        if self.cursor < self.I_p1:
            return False
        v_3 = self.limit_backward
        self.limit_backward = self.I_p1
        # (, line 69
        # [, line 69
        self.ket = self.cursor
        # substring, line 69
        among_var = self.find_among_b(DanishStemmer.a_2)
        if among_var == 0:
            self.limit_backward = v_3
            return False
        # ], line 69
        self.bra = self.cursor
        self.limit_backward = v_3
        if among_var == 1:
            # (, line 72
            # delete, line 72
            if not self.slice_del():
                return False

            # do, line 72
            v_4 = self.limit - self.cursor
            # call consonant_pair, line 72
            self.__r_consonant_pair()
            self.cursor = self.limit - v_4
        elif among_var == 2:
            # (, line 74
            # <-, line 74
            if not self.slice_from(u"l\u00F8s"):
                return False
        return True

    def __r_undouble(self):
        # (, line 77
        # setlimit, line 78
        if self.cursor < self.I_p1:
            return False
        v_2 = self.limit_backward
        self.limit_backward = self.I_p1
        # (, line 78
        # [, line 78
        self.ket = self.cursor
        if not self.in_grouping_b(DanishStemmer.g_c, 98, 122):
            self.limit_backward = v_2
            return False
        # ], line 78
        self.bra = self.cursor
        # -> ch, line 78
        self.S_ch = self.slice_to()
        if self.S_ch == '':
            return False
        self.limit_backward = v_2
        # name ch, line 79
        if not self.eq_s_b(self.S_ch):
            return False
        # delete, line 80
        if not self.slice_del():
            return False

        return True

    def _stem(self):
        # (, line 84
        # do, line 86
        v_1 = self.cursor
        # call mark_regions, line 86
        self.__r_mark_regions()
        self.cursor = v_1
        # backwards, line 87
        self.limit_backward = self.cursor
        self.cursor = self.limit
        # (, line 87
        # do, line 88
        v_2 = self.limit - self.cursor
        # call main_suffix, line 88
        self.__r_main_suffix()
        self.cursor = self.limit - v_2
        # do, line 89
        v_3 = self.limit - self.cursor
        # call consonant_pair, line 89
        self.__r_consonant_pair()
        self.cursor = self.limit - v_3
        # do, line 90
        v_4 = self.limit - self.cursor
        # call other_suffix, line 90
        self.__r_other_suffix()
        self.cursor = self.limit - v_4
        # do, line 91
        v_5 = self.limit - self.cursor
        # call undouble, line 91
        self.__r_undouble()
        self.cursor = self.limit - v_5
        self.cursor = self.limit_backward
        return True


class lab0(BaseException): pass


class lab1(BaseException): pass


class lab2(BaseException): pass


class lab3(BaseException): pass


class lab4(BaseException): pass
