# Path: latin/latin_syllable.py
# def separate_syllaba(word_uc):
#     def type(ch):
#         return VOWEL if is_vowel(ch) else CONSONANT
# 
#     last_type = None
#     chunks = []
#     for ch in word_uc:
#         curr_type = type(ch)
#         if last_type != curr_type:
#             chunks.append([curr_type])
#             last_type = curr_type
#         chunks[-1].append(ch)
#     # caesar: [1 c] [0 a e] [1 s] [0 a] [1 r]
#     # novembris: [1 n] [0 o] [1 v] [0 e] [1 m b r] [0 i] [1 s]
#     # deus: [1 d] [0 e u] [1 s]
# 
#     chunks = map(joint, chunks)
#     # caesar: [1 c] [0 ae] [1 s] [0 a] [1 r]
#     # novembris: [1 n] [0 o] [1 v] [0 e] [1 m br] [0 i] [1 s]
#     # deus: [1 d] [0 e u] [1 s]
# 
#     pre = []
#     vowel = []
# 
#     res = []
# 
#     for i, chunk in enumerate(chunks):
#         if chunk[0] == VOWEL:
#             vowel = chunk[1:]
#         else: # consonant
#             if len(chunk[1:]) == 1:
#                 # 排出
#                 if vowel != []:
#                     if len(vowel) == 2 and i == len(chunks)-1:
#                         res.append(pre + vowel[:1] + [])
#                         res.append(vowel[1:] + chunk[1:])
#                         pre = []
#                         vowel = []
#                     else:
#                         res.append(pre + vowel + [])
#                         pre = chunk[1:]
#                         vowel = []
#                 else:
#                     pre = chunk[1:]
#             else:
#                 res.append(pre + vowel + [chunk[1]])
#                 pre = chunk[2:]
#                 vowel = []
# 
#     if vowel != []:
#         res.append(pre + vowel)
#     elif pre != []:
#         if res:
#             res[-1] += pre
#         else:
#             res = pre
#     # caesar: [c ae] [s a r]
#     # novembris: [n o] [v e m] [br i s]
#     # deus: [d e] [u s]
# 
#     syl = [u''.join(r) for r in res]
#     # caesar: [cae sar]
#     # novembris: [no vem bris]
#     # deus: [de us]
# 
#     return syl
# 
# def locate_accent(syllables):
#     def is_long(syl):
#         if is_vowel(syl[-1]):
#             return syl[-1] in [u'ā', u'ē', u'ī', u'ō', u'ū', u'ȳ']
#         else:
#             return True
#     len_syl = len(syllables)
#     if len_syl == 1:
#         return None
#     elif len_syl == 2:
#         return 0
#     elif len_syl >= 3:
#         paenultima = len_syl-2
#         if is_long(syllables[paenultima]):
#             return paenultima
#         else:
#             return paenultima-1


import sys
import time
import math
from latin.latin_syllable import separate_syllaba, locate_accent
    from AppKit import NSSpeechSynthesizer
def analyze_latin_sentence(sentence_uc, debug_mode=False):
    phonemes = []

    for word_uc in sentence_uc.split(u' '):
        phonemes_in_word = analyze_latin_word_phonemes(word_uc, debug_mode=debug_mode)
        # silence
        duration = latin_time_unit
        phonemes_in_word.append('%s {D %d}' % ('%', duration))
        # phonemes.append('~')
        phonemes_in_word.append('.')

        if debug_mode:
            for phoneme in phonemes_in_word:
                print "  ", phoneme

        phonemes += phonemes_in_word

    return phonemes


def say_latin(text_uc, debug_mode=False, pause=False):
    phonemes = analyze_latin_sentence(text_uc, debug_mode)
    tune_text = ''.join(phonemes)
    say(tune_text, input='TUNE', show_phonemes=False, pause=pause)


if __name__ == '__main__':
    # init_synth('Alex')
    init_synth('Victoria')
    text = u'In Crētā īnsulā māgnum labyrinthum Daedalus aedificāvit plēnum viārum flexuōsārum.'

# Path: latin/latin_syllable.py
# def separate_syllaba(word_uc):
#     def type(ch):
#         return VOWEL if is_vowel(ch) else CONSONANT
# 
#     last_type = None
#     chunks = []
#     for ch in word_uc:
#         curr_type = type(ch)
#         if last_type != curr_type:
#             chunks.append([curr_type])
#             last_type = curr_type
#         chunks[-1].append(ch)
#     # caesar: [1 c] [0 a e] [1 s] [0 a] [1 r]
#     # novembris: [1 n] [0 o] [1 v] [0 e] [1 m b r] [0 i] [1 s]
#     # deus: [1 d] [0 e u] [1 s]
# 
#     chunks = map(joint, chunks)
#     # caesar: [1 c] [0 ae] [1 s] [0 a] [1 r]
#     # novembris: [1 n] [0 o] [1 v] [0 e] [1 m br] [0 i] [1 s]
#     # deus: [1 d] [0 e u] [1 s]
# 
#     pre = []
#     vowel = []
# 
#     res = []
# 
#     for i, chunk in enumerate(chunks):
#         if chunk[0] == VOWEL:
#             vowel = chunk[1:]
#         else: # consonant
#             if len(chunk[1:]) == 1:
#                 # 排出
#                 if vowel != []:
#                     if len(vowel) == 2 and i == len(chunks)-1:
#                         res.append(pre + vowel[:1] + [])
#                         res.append(vowel[1:] + chunk[1:])
#                         pre = []
#                         vowel = []
#                     else:
#                         res.append(pre + vowel + [])
#                         pre = chunk[1:]
#                         vowel = []
#                 else:
#                     pre = chunk[1:]
#             else:
#                 res.append(pre + vowel + [chunk[1]])
#                 pre = chunk[2:]
#                 vowel = []
# 
#     if vowel != []:
#         res.append(pre + vowel)
#     elif pre != []:
#         if res:
#             res[-1] += pre
#         else:
#             res = pre
#     # caesar: [c ae] [s a r]
#     # novembris: [n o] [v e m] [br i s]
#     # deus: [d e] [u s]
# 
#     syl = [u''.join(r) for r in res]
#     # caesar: [cae sar]
#     # novembris: [no vem bris]
#     # deus: [de us]
# 
#     return syl
# 
# def locate_accent(syllables):
#     def is_long(syl):
#         if is_vowel(syl[-1]):
#             return syl[-1] in [u'ā', u'ē', u'ī', u'ō', u'ū', u'ȳ']
#         else:
#             return True
#     len_syl = len(syllables)
#     if len_syl == 1:
#         return None
#     elif len_syl == 2:
#         return 0
#     elif len_syl >= 3:
#         paenultima = len_syl-2
#         if is_long(syllables[paenultima]):
#             return paenultima
#         else:
#             return paenultima-1


# Path: speak_latin.py
import sys
import time
import math
from latin.latin_syllable import separate_syllaba, locate_accent
    from AppKit import NSSpeechSynthesizer

def analyze_latin_sentence(sentence_uc, debug_mode=False):
    phonemes = []

    for word_uc in sentence_uc.split(u' '):
        phonemes_in_word = analyze_latin_word_phonemes(word_uc, debug_mode=debug_mode)
        # silence
        duration = latin_time_unit
        phonemes_in_word.append('%s {D %d}' % ('%', duration))
        # phonemes.append('~')
        phonemes_in_word.append('.')

        if debug_mode:
            for phoneme in phonemes_in_word:
                print "  ", phoneme

        phonemes += phonemes_in_word

    return phonemes


def say_latin(text_uc, debug_mode=False, pause=False):
    phonemes = analyze_latin_sentence(text_uc, debug_mode)
    tune_text = ''.join(phonemes)
    say(tune_text, input='TUNE', show_phonemes=False, pause=pause)


if __name__ == '__main__':
    # init_synth('Alex')
    init_synth('Victoria')
    text = u'In Crētā īnsulā māgnum labyrinthum Daedalus aedificāvit plēnum viārum flexuōsārum.'

    say_latin(text, debug_mode=False, pause=True)