import re


def _make_pattern(d, exact_match=True, plurals=2):
    """
    Internal function.
    Generates a compiled regex pattern based on a given alias array.
    
    Parameters:
        d - the alias map/dict to compile into a regex pattern
        exact_match - compile with pattern starting and ending anchors
        plurals     - If > 0, add 's' aliases to members greater than this value.
    
    Return:
        Compiled regex pattern.
    """

    entries = []
    [ entries.extend([y for y in x]) for x in d.values() ]
    
    
    if plurals > 0:
        entries = [ ( x + '[s]?' if len(x) > plurals else x ) for x in entries ]
        
    if exact_match:
        return re.compile('^(' + '|'.join(entries) + ')$', re.I)
    else:
        return re.compile('(' + '|'.join(entries) + ')', re.I)