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

from .basestemmer import BaseStemmer
from .among import Among


class IndonesianStemmer(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"kah", -1, 1),
        Among(u"lah", -1, 1),
        Among(u"pun", -1, 1)
    ]

    a_1 = [
        Among(u"nya", -1, 1),
        Among(u"ku", -1, 1),
        Among(u"mu", -1, 1)
    ]

    a_2 = [
        Among(u"i", -1, 1, "_IndonesianStemmer__r_SUFFIX_I_OK"),
        Among(u"an", -1, 1, "_IndonesianStemmer__r_SUFFIX_AN_OK"),
        Among(u"kan", 1, 1, "_IndonesianStemmer__r_SUFFIX_KAN_OK")
    ]

    a_3 = [
        Among(u"di", -1, 1),
        Among(u"ke", -1, 2),
        Among(u"me", -1, 1),
        Among(u"mem", 2, 5),
        Among(u"men", 2, 1),
        Among(u"meng", 4, 1),
        Among(u"meny", 4, 3, "_IndonesianStemmer__r_VOWEL"),
        Among(u"pem", -1, 6),
        Among(u"pen", -1, 2),
        Among(u"peng", 8, 2),
        Among(u"peny", 8, 4, "_IndonesianStemmer__r_VOWEL"),
        Among(u"ter", -1, 1)
    ]

    a_4 = [
        Among(u"be", -1, 3, "_IndonesianStemmer__r_KER"),
        Among(u"belajar", 0, 4),
        Among(u"ber", 0, 3),
        Among(u"pe", -1, 1),
        Among(u"pelajar", 3, 2),
        Among(u"per", 3, 1)
    ]

    g_vowel = [17, 65, 16]

    I_prefix = 0
    I_measure = 0


    def __r_remove_particle(self):
        # (, line 50
        # [, line 51
        self.ket = self.cursor
        # substring, line 51
        if self.find_among_b(IndonesianStemmer.a_0) == 0:
            return False
        # ], line 51
        self.bra = self.cursor
        # (, line 52
        # delete, line 52
        if not self.slice_del():
            return False

        self.I_measure -= 1
        return True

    def __r_remove_possessive_pronoun(self):
        # (, line 56
        # [, line 57
        self.ket = self.cursor
        # substring, line 57
        if self.find_among_b(IndonesianStemmer.a_1) == 0:
            return False
        # ], line 57
        self.bra = self.cursor
        # (, line 58
        # delete, line 58
        if not self.slice_del():
            return False

        self.I_measure -= 1
        return True

    def __r_SUFFIX_KAN_OK(self):
        # (, line 63
        # and, line 85
        if not self.I_prefix != 3:
            return False
        if not self.I_prefix != 2:
            return False
        return True

    def __r_SUFFIX_AN_OK(self):
        # (, line 89
        if not self.I_prefix != 1:
            return False
        return True

    def __r_SUFFIX_I_OK(self):
        # (, line 91
        if not self.I_prefix <= 2:
            return False
        # not, line 128
        v_1 = self.limit - self.cursor
        try:
            # literal, line 128
            if not self.eq_s_b(u"s"):
                raise lab0()
            return False
        except lab0: pass
        self.cursor = self.limit - v_1
        return True

    def __r_remove_suffix(self):
        # (, line 131
        # [, line 132
        self.ket = self.cursor
        # substring, line 132
        if self.find_among_b(IndonesianStemmer.a_2) == 0:
            return False
        # ], line 132
        self.bra = self.cursor
        # (, line 134
        # delete, line 134
        if not self.slice_del():
            return False

        self.I_measure -= 1
        return True

    def __r_VOWEL(self):
        # (, line 141
        if not self.in_grouping(IndonesianStemmer.g_vowel, 97, 117):
            return False
        return True

    def __r_KER(self):
        # (, line 143
        if not self.out_grouping(IndonesianStemmer.g_vowel, 97, 117):
            return False
        # literal, line 143
        if not self.eq_s(u"er"):
            return False
        return True

    def __r_remove_first_order_prefix(self):
        # (, line 145
        # [, line 146
        self.bra = self.cursor
        # substring, line 146
        among_var = self.find_among(IndonesianStemmer.a_3)
        if among_var == 0:
            return False
        # ], line 146
        self.ket = self.cursor
        if among_var == 1:
            # (, line 147
            # delete, line 147
            if not self.slice_del():
                return False

            self.I_prefix = 1
            self.I_measure -= 1
        elif among_var == 2:
            # (, line 148
            # delete, line 148
            if not self.slice_del():
                return False

            self.I_prefix = 3
            self.I_measure -= 1
        elif among_var == 3:
            # (, line 149
            self.I_prefix = 1
            # <-, line 149
            if not self.slice_from(u"s"):
                return False
            self.I_measure -= 1
        elif among_var == 4:
            # (, line 150
            self.I_prefix = 3
            # <-, line 150
            if not self.slice_from(u"s"):
                return False
            self.I_measure -= 1
        elif among_var == 5:
            # (, line 151
            self.I_prefix = 1
            self.I_measure -= 1
            # or, line 151
            try:
                v_1 = self.cursor
                try:
                    # and, line 151
                    v_2 = self.cursor
                    if not self.in_grouping(IndonesianStemmer.g_vowel, 97, 117):
                        raise lab1()
                    self.cursor = v_2
                    # <-, line 151
                    if not self.slice_from(u"p"):
                        return False
                    raise lab0()
                except lab1: pass
                self.cursor = v_1
                # delete, line 151
                if not self.slice_del():
                    return False

            except lab0: pass
        elif among_var == 6:
            # (, line 152
            self.I_prefix = 3
            self.I_measure -= 1
            # or, line 152
            try:
                v_3 = self.cursor
                try:
                    # and, line 152
                    v_4 = self.cursor
                    if not self.in_grouping(IndonesianStemmer.g_vowel, 97, 117):
                        raise lab3()
                    self.cursor = v_4
                    # <-, line 152
                    if not self.slice_from(u"p"):
                        return False
                    raise lab2()
                except lab3: pass
                self.cursor = v_3
                # delete, line 152
                if not self.slice_del():
                    return False

            except lab2: pass
        return True

    def __r_remove_second_order_prefix(self):
        # (, line 156
        # [, line 162
        self.bra = self.cursor
        # substring, line 162
        among_var = self.find_among(IndonesianStemmer.a_4)
        if among_var == 0:
            return False
        # ], line 162
        self.ket = self.cursor
        if among_var == 1:
            # (, line 163
            # delete, line 163
            if not self.slice_del():
                return False

            self.I_prefix = 2
            self.I_measure -= 1
        elif among_var == 2:
            # (, line 164
            # <-, line 164
            if not self.slice_from(u"ajar"):
                return False
            self.I_measure -= 1
        elif among_var == 3:
            # (, line 165
            # delete, line 165
            if not self.slice_del():
                return False

            self.I_prefix = 4
            self.I_measure -= 1
        elif among_var == 4:
            # (, line 166
            # <-, line 166
            if not self.slice_from(u"ajar"):
                return False
            self.I_prefix = 4
            self.I_measure -= 1
        return True

    def _stem(self):
        # (, line 171
        self.I_measure = 0
        # do, line 173
        v_1 = self.cursor
        try:
            # (, line 173
            # repeat, line 173
            try:
                while True:
                    try:
                        v_2 = self.cursor
                        try:
                            # (, line 173
                            # gopast, line 173
                            try:
                                while True:
                                    try:
                                        if not self.in_grouping(IndonesianStemmer.g_vowel, 97, 117):
                                            raise lab5()
                                        raise lab4()
                                    except lab5: pass
                                    if self.cursor >= self.limit:
                                        raise lab3()
                                    self.cursor += 1
                            except lab4: pass
                            self.I_measure += 1
                            raise lab2()
                        except lab3: pass
                        self.cursor = v_2
                        raise lab1()
                    except lab2: pass
            except lab1: pass
        except lab0: pass
        self.cursor = v_1
        if not self.I_measure > 2:
            return False
        self.I_prefix = 0
        # backwards, line 176
        self.limit_backward = self.cursor
        self.cursor = self.limit
        # (, line 176
        # do, line 177
        v_4 = self.limit - self.cursor
        # call remove_particle, line 177
        self.__r_remove_particle()
        self.cursor = self.limit - v_4
        if not self.I_measure > 2:
            return False
        # do, line 179
        v_5 = self.limit - self.cursor
        # call remove_possessive_pronoun, line 179
        self.__r_remove_possessive_pronoun()
        self.cursor = self.limit - v_5
        self.cursor = self.limit_backward
        if not self.I_measure > 2:
            return False
        # or, line 188
        try:
            v_6 = self.cursor
            try:
                # test, line 182
                v_7 = self.cursor
                # (, line 182
                # call remove_first_order_prefix, line 183
                if not self.__r_remove_first_order_prefix():
                    raise lab7()
                # do, line 184
                v_8 = self.cursor
                try:
                    # (, line 184
                    # test, line 185
                    v_9 = self.cursor
                    # (, line 185
                    if not self.I_measure > 2:
                        raise lab8()
                    # backwards, line 185
                    self.limit_backward = self.cursor
                    self.cursor = self.limit
                    # call remove_suffix, line 185
                    if not self.__r_remove_suffix():
                        raise lab8()
                    self.cursor = self.limit_backward
                    self.cursor = v_9
                    if not self.I_measure > 2:
                        raise lab8()
                    # call remove_second_order_prefix, line 186
                    if not self.__r_remove_second_order_prefix():
                        raise lab8()
                except lab8: pass
                self.cursor = v_8
                self.cursor = v_7
                raise lab6()
            except lab7: pass
            self.cursor = v_6
            # (, line 188
            # do, line 189
            v_10 = self.cursor
            # call remove_second_order_prefix, line 189
            self.__r_remove_second_order_prefix()
            self.cursor = v_10
            # do, line 190
            v_11 = self.cursor
            try:
                # (, line 190
                if not self.I_measure > 2:
                    raise lab9()
                # backwards, line 190
                self.limit_backward = self.cursor
                self.cursor = self.limit
                # call remove_suffix, line 190
                if not self.__r_remove_suffix():
                    raise lab9()
                self.cursor = self.limit_backward
            except lab9: pass
            self.cursor = v_11
        except lab6: pass
        return True


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


class lab8(BaseException): pass


class lab9(BaseException): pass
