'Singularizes Spanish nouns.'

def singularize(self, word):

    rules = [[u'(?i)^([bcdfghjklmn\xf1pqrstvwxyz]*)([aeiou])([ns])es$', u'\\1\\2\\3'], [u'(?i)([aeiou])([ns])es$', u'~1\\2'], [u'(?i)oides$', u'oide'], [u'(?i)(ces)$/i', u'z'], [u'(?i)(sis|tis|xis)+$', u'\\1'], [u'(?i)(\xe9)s$', u'\\1'], [u'(?i)([^e])s$', u'\\1'], [u'(?i)([bcdfghjklmn\xf1prstvwxyz]{2,}e)s$', u'\\1'], [u'(?i)([gh\xf1pv]e)s$', u'\\1'], [u'(?i)es$', u'']]
    uncountable_words = [u'paraguas', u'tijeras', u'gafas', u'vacaciones', u'v\xedveres', u'lunes', u'martes', u'mi\xe9rcoles', u'jueves', u'viernes', u'cumplea\xf1os', u'virus', u'atlas', u'sms']
    irregular_words = {u'jersey': u'jers\xe9is', u'esp\xe9cimen': u'espec\xedmenes', u'car\xe1cter': u'caracteres', u'r\xe9gimen': u'reg\xedmenes', u'men\xfa': u'men\xfas', u'r\xe9gimen': u'reg\xedmenes', u'curriculum': u'curr\xedculos', u'ultim\xe1tum': u'ultimatos', u'memor\xe1ndum': u'memorandos', u'refer\xe9ndum': u'referendos', u's\xe1ndwich': u's\xe1ndwiches'}
    lower_cased_word = word.lower()
    for uncountable_word in uncountable_words:
        if (lower_cased_word[((-1) * len(uncountable_word)):] == uncountable_word):
            return word
    for irregular in irregular_words.keys():
        match = re.search(((u'(u' + irregular) + u')$'), word, re.IGNORECASE)
        if match:
            return re.sub(((u'(?i)' + irregular) + u'$'), (match.expand(u'\\1')[0] + irregular_words[irregular][1:]), word)
    for rule in range(len(rules)):
        match = re.search(rules[rule][0], word, re.IGNORECASE)
        if match:
            groups = match.groups()
            replacement = rules[rule][1]
            if re.match(u'~', replacement):
                for k in range(1, len(groups)):
                    replacement = replacement.replace((u'~' + str(k)), self.string_replace(groups[(k - 1)], u'AEIOUaeiou', u'\xc1\xc9\xcd\xd3\xda\xe1\xe9\xed\xf3\xfa'))
            result = re.sub(rules[rule][0], replacement, word)
            match = re.search(u'(?i)([\xe1\xe9\xed\xf3\xfa]).*([\xe1\xe9\xed\xf3\xfa])', result)
            if (match and (len(match.groups()) > 1) and (not re.search(u'(?i)[\xe1\xe9\xed\xf3\xfa]', word))):
                result = self.string_replace(result, u'\xc1\xc9\xcd\xd3\xda\xe1\xe9\xed\xf3\xfa', u'AEIOUaeiou')
            return result
    return word
