#!/usr/bin/env python

# Copyright (c) 2006 Bermi Ferrer Martinez
# bermi a-t bermilabs - com
#
# See the end of this file for the free software, open source license (BSD-style).

import re
from .base import Base

class English(Base):
    """
    Inflector for pluralize and singularize English nouns.

    This is the default Inflector for the Inflector obj
    """

    def pluralize(self, word) :
        '''Pluralizes English nouns.'''

        rules = [
            ['(quiz)$' , '\\1zes'],
            ['^(ox)$' , '\\1en'],
            ['([m|l])ouse$' , '\\1ice'],
            ['(matr|vert|ind)ix|ex$' , '\\1ices'],
            ['(x|ch|ss|sh)$' , '\\1es'],
            ['([^aeiouy]|qu)ies$' , '\\1y'],
            ['([^aeiouy]|qu)y$' , '\\1ies'],
            ['(hive)$' , '\\1s'],
            ['(?:([^f])fe|([lr])f)$' , '\\1\\2ves'],
            ['sis$' , 'ses'],
            ['([ti])um$' , '\\1a'],
            ['(buffal|tomat)o$' , '\\1oes'],
            ['(bu)s$' , '\\1ses'],
            ['(alias|status)' , '\\1es'],
            ['(octop|vir)us$' , '\\1i'],
            ['(ax|test)is$' , '\\1es'],
            ['s$' , 's'],
            ['$' , 's']
        ]

        uncountable_words = ['equipment', 'information', 'rice', 'money',
            'species', 'series', 'fish', 'sheep', 'deer', 'moose']

        irregular_words = {
            'person' : 'people',
            'man' : 'men',
            'child' : 'children',
            'sex' : 'sexes',
            'move' : 'moves',
            'staff' : 'staves',
            'foot' : 'feet',
            'goose' : 'geese',
        }

        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('('+irregular+')$',word, re.IGNORECASE)
            if match:
                return re.sub(''+irregular+'$', match.expand('\\1')[0]+irregular_words[irregular][1:], word, flags=re.IGNORECASE)

        for rule in range(len(rules)):
            match = re.search(rules[rule][0], word, re.IGNORECASE)
            if match :
                groups = match.groups()
                for k in range(0,len(groups)) :
                    if groups[k] == None :
                        rules[rule][1] = rules[rule][1].replace('\\'+str(k+1), '')

                return re.sub(rules[rule][0], rules[rule][1], word, flags=re.IGNORECASE)

        return word


    def singularize(self, word) :
        '''Singularizes English nouns.'''

        rules = [
            ['(quiz)zes$' , '\\1'],
            ['(matr)ices$' , '\\1ix'],
            ['(vert|ind)ices$' , '\\1ex'],
            ['^(ox)en' , '\\1'],
            ['(alias|status)es$' , '\\1'],
            ['([octop|vir])i$' , '\\1us'],
            ['(cris|ax|test)es$' , '\\1is'],
            ['(shoe)s$' , '\\1'],
            ['(o)es$' , '\\1'],
            ['(bus)es$' , '\\1'],
            ['([m|l])ice$' , '\\1ouse'],
            ['(x|ch|ss|sh)es$' , '\\1'],
            ['(m)ovies$' , '\\1ovie'],
            ['(s)eries$' , '\\1eries'],
            ['([^aeiouy]|qu)ies$' , '\\1y'],
            ['([lr])ves$' , '\\1f'],
            ['(tive)s$' , '\\1'],
            ['(hive)s$' , '\\1'],
            ['([^f])ves$' , '\\1fe'],
            ['(^analy)ses$' , '\\1sis'],
            ['((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$' , '\\1\\2sis'],
            ['([ti])a$' , '\\1um'],
            ['(n)ews$' , '\\1ews'],
            ['s$' , ''],
        ];

        uncountable_words = ['equipment', 'information', 'rice', 'money', 'species', 'series', 'fish', 'sheep','sms'];

        irregular_words = {
            'people' : 'person',
            'men' : 'man',
            'children' : 'child',
            'sexes' : 'sex',
            'moves' : 'move'
        }

        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('('+irregular+')$',word, re.IGNORECASE)
            if match:
                return re.sub(''+irregular+'$', match.expand('\\1')[0]+irregular_words[irregular][1:], word, flags=re.IGNORECASE)


        for rule in range(len(rules)):
            match = re.search(rules[rule][0], word, re.IGNORECASE)
            if match :
                groups = match.groups()
                for k in range(0,len(groups)) :
                    if groups[k] == None :
                        rules[rule][1] = rules[rule][1].replace('\\'+str(k+1), '')

                return re.sub(rules[rule][0], rules[rule][1], word, flags=re.IGNORECASE)

        return word



# Copyright (c) 2006 Bermi Ferrer Martinez
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software to deal in this software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of this software, and to permit
# persons to whom this software is furnished to do so, subject to the following
# condition:
#
# THIS SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THIS SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THIS SOFTWARE.
