o
    ±ƒðj1  ã                   @   s  U d Z ddlZddlZddlZddlmZmZmZ ddlm	Z	 e 
e¡ZedƒZddiZeeef ed	< d
Ze d¡ZdZe d¡Ze dddœ¡Ze dd¡Ze d¡Ze d¡Ze d¡Ze d¡Ze d¡ZedƒZe d¡Z dZ!G dd„ dƒZ"dedee fdd „Z#dS )!uû  Thai phonemization: TLTK grapheme-to-phoneme -> IPA + tone digits.

espeak-ng's Thai voice is a placeholder that cannot be trained on. The
maintainer who added it said so when he landed it (espeak-ng#757): the phonemes
and tones were copied from Shan, "many of tones are probably set wrong", and the
rules in ``th_list``/``th_rules`` are "very simple just to hear something". In
practice ``th_dict`` carries no lexicon (2 KB, one of the smallest in
espeak-ng), so there is no word segmentation for a script written without
spaces; the leading vowels à¹€ à¹� à¹‚ à¹ƒ à¹„ are emitted in writing order instead of
being reordered after their consonant (à¹€à¸‚à¸² comes out as "e-kha"); no tone is
ever produced; and a tone mark deletes the syllable's vowel outright, so
à¸›à¹ˆà¸²/à¸›à¹‰à¸²/à¸›à¹Šà¸²/à¸›à¹‹à¸² all collapse to the same phonemes. Measured over the TSync2
corpus, ~11% of the tokens espeak-ng returns contain no vowel at all.

TLTK (BSD-3-Clause) does dictionary-based word segmentation and returns
syllabified IPA with an explicit tone per syllable, which is what Thai actually
needs: it has five phonemic tones and minimal pairs are everywhere
(à¸‚à¹ˆà¸²à¸§ "news" vs à¸‚à¹‰à¸²à¸§ "rice").

Tones are emitted as the digits 1-5 -- 1 mid, 2 low, 3 falling, 4 high, 5
rising -- one per syllable, immediately after that syllable's segments. This is
the representation :mod:`piper.phonemize_chinese` already uses for Mandarin
tones. Every symbol produced here is a single codepoint that already has an id
in :data:`piper.phoneme_ids.DEFAULT_PHONEME_ID_MAP`, so Thai voices stay
compatible with the IPA-based (espeak) warmstart -- the same trick
:mod:`piper.phonemize_hebrew` and :mod:`piper.phonemize_japanese` use.

Requires ``pip install tltk unicode-rbnf`` (the ``th`` extra).
é    N)ÚDictÚListÚOptionalé   )ÚDEFAULT_PHONEME_ID_MAPÚ12345u   á´�u   É”Ú_TLTK_TO_IPAz<s/>u   [à¸�-à¸®à¸°-à¸ºà¹€-à¹Ž]+)u	   à¸¯à¸¥à¸¯u   à¸¥à¸°u   [à¸¯à¹�à¹šà¹›â€‹â€Œâ€�ï»¿]u   à¸‚u   à¸„)u   à¸ƒu   à¸…u   à¹�à¹‘à¹’à¹“à¹”à¹•à¹–à¹—à¹˜à¹™Ú
0123456789u   à¹�([à¹ˆ-à¹‹]?)à¸²z(?<=\d),(?=\d{3}\b)z(?<!\w)-?\d+(?:\.\d+)?u   à¸¿\s*(-?[\d.]+)z(-?[\d.]+)\s*%z.,?!:;-z(?<=[.?!])\s+|\n+ú c                   @   s¼   e Zd ZdZddeddfdd„Zdedeee  fd	d
„Zdedee fdd„Z	dedee fdd„Z
dededee fdd„Zdedefdd„Zdedefdd„Zdejdefdd„ZdS )ÚThaiPhonemizerz@Convert Thai text to IPA phonemes with per-syllable tone digits.TÚexpand_numbersÚreturnNc                 C   s<   ddl m} || _d| _|rddlm} | d¡| _dS dS )z÷Initialize the phonemizer.

        :param expand_numbers: Spell digits out as Thai words before
            segmentation. TLTK does not read numbers -- it transcribes "1250"
            as the phonemes "2351" -- so this is on by default.
        r   )Úth2ipaN)Ú
RbnfEngineÚth)Útltk.nlpr   Ú_th2ipaÚnumber_engineÚunicode_rbnfr   Úfor_language)Úselfr   r   r   © r   ú–/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/piper/phonemize_thai.pyÚ__init__l   s   ýzThaiPhonemizer.__init__Útextc                 C   s0   g }t  |¡D ]}|  |¡}|r| |¡ q|S )zÔReturn IPA phonemes with tone digits, grouped by sentence.

        Each phoneme is a single codepoint, matching how espeak phonemes are
        keyed in :data:`piper.phoneme_ids.DEFAULT_PHONEME_ID_MAP`.
        )Ú_SENTENCE_SPLITÚsplitÚ_phonemize_sentenceÚappend)r   r   Úall_phonemesÚsentenceÚsentence_phonemesr   r   r   Ú	phonemize}   s   

€zThaiPhonemizer.phonemizer    c                 C   sÄ   |   |¡}g }d}t |¡D ]}| t||| ¡ … ƒ¡ | |  | ¡ ¡¡ | ¡ }q| t||d … ƒ¡ |rL|d t	krL| 
d¡ |rL|d t	ks?|r`|d t	kr`| 
¡  |r`|d t	ksT|S )Nr   éÿÿÿÿ)Ú
_normalizeÚ	_THAI_RUNÚfinditerÚextendÚ_punctuation_phonemesÚstartÚ_phonemize_runÚgroupÚendÚ_WORD_BREAKÚpop)r   r    ÚphonemesÚpositionÚmatchr   r   r   r   Œ   s   


ÿÿz"ThaiPhonemizer._phonemize_sentenceÚrunc                 C   s¤   z|   |¡}W n ty   t d|¡ g  Y S w g }| td¡ ¡ D ]$}g }t d|¡D ]}| |  	||¡¡ q-|rG|rB| 
t¡ | |¡ q#|sPt d|¡ |S )a,  Segment and transcribe one contiguous run of Thai letters.

        Runs are fed to TLTK one at a time. Anything else -- spaces, Latin
        letters, punctuation -- makes ``th2ipa`` raise ``ValueError: too many
        values to unpack``, which silently loses whole utterances if the input
        is pre-segmented (it drops 70% of the Porjai corpus, whose transcripts
        are space-delimited). One run per call sidesteps that, and TLTK still
        does its own word segmentation inside the run, which is the part we
        actually want.
        z&TLTK could not transcribe Thai run: %sr
   z[.+]zNo phonemes for Thai run: %s)r   Ú	ExceptionÚ_LOGGERÚwarningÚreplaceÚ_CHUNK_MARKERr   Úrer'   Ú_syllable_phonemesr   r-   )r   r2   Útranscriptionr/   ÚwordÚword_phonemesÚsyllabler   r   r   r*   ¢   s&   þ

€zThaiPhonemizer._phonemize_runr=   c                 C   sŽ   |sg S d }|d t v r|d d… |d }}g }|D ]}t ||¡}|tvr2t d||¡ g   S | |¡ q|s<g S |d urE| |¡ |S )Nr#   z2Dropping untranscribed symbol %r from Thai run: %s)ÚTONESr   Úgetr   r4   r5   r   )r   r=   r2   Útoner/   Úcharr   r   r   r9   Ì   s&   ÿ
z!ThaiPhonemizer._syllable_phonemesc                 C   sR   t  d|¡}| t¡ t¡}t d|¡}|jtŽ }| j	d ur#|  
|¡}t d|¡S )NÚNFCu   \1à¸³Ú )ÚunicodedataÚ	normalizeÚ	translateÚ_THAI_DIGITSÚ	_OBSOLETEÚ_SARA_AMÚsubr6   Ú
_ET_CETERAr   Ú_numbers_to_wordsÚ_SILENT©r   r   r   r   r   r$   í   s   


zThaiPhonemizer._normalizec                 C   sF   t  d|¡}t dd„ |¡}t dd„ |¡}t | j|¡}| dd¡S )NrC   c                 S   ó   |   d¡› d�S )Nr   u
    à¸šà¸²à¸—©r+   ©Úmr   r   r   Ú<lambda>ý   ó    z2ThaiPhonemizer._numbers_to_words.<locals>.<lambda>c                 S   rO   )Nr   u"    à¹€à¸›à¸­à¸£à¹Œà¹€à¸‹à¹‡à¸™à¸•à¹ŒrP   rQ   r   r   r   rS   þ   rT   u   â€‹r
   )Ú_GROUPED_NUMBERrJ   Ú_BAHTÚ_PERCENTÚ_NUMBERÚ
_th_numberr6   rN   r   r   r   rL   û   s
   z ThaiPhonemizer._numbers_to_wordsr1   c              	   C   sL   | j d usJ ‚z| j  | d¡¡jW S  ty%   t d| d¡¡ Y dS w )Nr   zCould not spell out number: %srC   )r   Úformat_numberr+   r   r3   r4   r5   )r   r1   r   r   r   rY     s   þzThaiPhonemizer._th_number)T)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úboolr   Ústrr   r"   r   r*   r9   r$   rL   r8   ÚMatchrY   r   r   r   r   r   i   s    *!r   r   r   c                 C   sT   g }| D ]#}|t v r| |¡ q| ¡ rt d|¡ |r"|d tkr'| t¡ q|S )zñTurn the non-Thai text between two runs into break phonemes.

    Everything unrecognized (Latin letters, emoji, stray symbols) collapses to a
    single word break rather than being passed through, so it can never reach
    the id map.
    zDropping non-Thai character: %rr#   )Ú_PUNCTUATIONr   Úisalnumr4   r5   r-   )r   r/   rA   r   r   r   r(     s   

€r(   )$r^   Úloggingr8   rD   Útypingr   r   r   Úphoneme_idsr   Ú	getLoggerr[   r4   Ú	frozensetr>   r   r`   Ú__annotations__r7   Úcompiler%   rK   rM   Ú	maketransrH   rG   rI   rU   rX   rV   rW   rb   r   r-   r   r(   r   r   r   r   Ú<module>   s4    








 (