def hyphenate(word, numSyls):
    """See mainopt_syllables"""
    orig = word
    try:
        word, isu8 = word.decode('utf-8'), True
    except:
        isu8 = False
    pre = []
    post = []
    while word and not 'a' <= word[:1].lower() <= 'z':
        pre.append(word[:1])
        word = word[1:]
    while word and not 'a' <= word[-1].lower() <= 'z':
        post.insert(0, word[-1:])
        word = word[:-1]
    if numSyls > len(word):
        return orig
    l = int((len(word) + numSyls / 2) / numSyls)
    syls = []
    for i in range(numSyls):
        if i == numSyls - 1:
            syls.append(word[i * l:])
        else:
            syls.append(word[i * l:(i + 1) * l])
        if len(syls) > 1:
            if syls[-1].startswith('-') or len(syls[-1]) > 2 and syls[-1][:1
                ] == syls[-1][1:2] and not syls[-1][:1].lower() in 'aeiou':
                syls[-2] += syls[-1][:1]
                syls[-1] = syls[-1][1:]
            elif len(syls[-1]) > 2 and syls[-1][1] == '-':
                syls[-2] += syls[-1][:2]
                syls[-1] = syls[-1][2:]
            elif (len(syls[-2]) > 2 and syls[-2][-1] == syls[-2][-2] and 
                not syls[-2][-1].lower() in 'aeiou' or syls[-1] and syls[-1
                ][:1].lower() in 'aeiouy' and len(syls[-2]) > 2) and list(
                filter(lambda x: x.lower() in 'aeiou', list(syls[-2][:-1]))):
                syls[-1] = syls[-2][-1] + syls[-1]
                syls[-2] = syls[-2][:-1]
    word = ''.join(pre) + '- '.join(syls) + ''.join(post)
    if isu8:
        word = word.encode('utf-8')
    return word
