o
    ±ƒðj­-  ã                   @   s&  U d Z ddlZddlZddlmZmZmZmZ e e	¡Z
dZdZdZdZdZd	ZeeeeeeehƒZi d
d
“dd“dd“dd“dd“dd“dd“dd“dd“dd“dd“dd“dd“dd“dd “d!d!“d"d"“i d#d$“d%d&“d'd'“d(d)“d*d*“d+d,“d-d-“d.d/“d0d1“d2d2“d3d4“d5d5“d6d7“d8d8“d9d:“d;d“d<d=“¥d>d?d@dAdBdCœ¥Zeeef edD< eh dE£ƒZeh dF£ƒZe dG¡Ze dH¡ZdIZG dJdK„ dKƒZdLedMedNefdOdP„ZdQedNee fdRdS„ZdQedTdUdNe fdVdW„Z!	X	YdadQedZe d[e dNeee  fd\d]„Z"	dbd^eee  dNee fd_d`„Z#dS )cu  Japanese phonemization: OpenJTalk full-context labels -> IPA + prosody symbols.

espeak-ng's Japanese voice has no kanji coverage at all: it falls back to reading
Unicode character names, so ä»Šæ—¥ becomes the phonemes for "Chinese letter"
(twice). It also mis-reads the topic particles ã�¯/ã�¸ as "ha"/"he" even for
kana-only input, and it carries no pitch accent. Instead we use OpenJTalk
(pyopenjtalk), which does full morphological analysis and gives correct readings
plus accent information.

Pitch accent is the dominant naturalness lever in Japanese TTS, so we don't stop
at segmental phonemes. OpenJTalk's full-context labels are parsed into the
accent-annotated form used by ESPnet's `pyopenjtalk_prosody` G2P, then mapped to
IPA so the output uses Piper's default phoneme id map. That keeps Japanese voices
compatible with the IPA-based (espeak) warmstart, the same trick
:mod:`piper.phonemize_hebrew` uses.

Prosody symbols (all present in the default id map):

- ``â†‘``  accent rise (low -> high, within an accent phrase)
- ``â†“``  accent fall (the accent nucleus)
- ``#``  accent phrase boundary
- ``,``  pause (OpenJTalk ``pau``, e.g. from ã€�)
- ``.``  end of a declarative sentence
- ``?``  end of an interrogative sentence

ESPnet spells the last three ``_``, ``$`` and ``?``. We avoid ``_`` and ``$``
because Piper reserves them for padding and end-of-sentence
(see :mod:`piper.const`).
é    N)ÚDictÚListÚOptionalÚSequenceu   â†‘u   â†“ú#ú,Ú.ú?ÚaÚiÚuu   É¯ÚeÚoÚkÚkyu   kÊ²Úkwu   kÊ·Úgu   É¡Úgyu   É¡Ê²Úgwu   É¡Ê·ÚsÚshu   É•ÚzÚju   dÊ‘ÚtÚtsÚtyu   tÊ²Úchu   tÉ•ÚdÚdyu   dÊ²ÚnÚnyu   nÊ²ÚhÚhyu   hÊ²Úfu   É¸ÚbÚbyu   bÊ²ÚpÚpyu   pÊ²ÚmÚmyu   mÊ²ÚyÚru   É¾u   É¾Ê²ÚwÚvu   É´u   Ê”)Úryr,   r-   ÚNÚclÚOPENJTALK_TO_IPA>   ÚAÚEÚIÚOÚU>   r2   r3   r4   r/   r5   r6   r
   r   r   r   r   r0   u4   [ã€‚ï¼Žï¼�ï¼Ÿ!?]+[ã€�ã€�ï¼‰ã€‘ã€‰ã€‹â€�â€™\"')\]\s]*z	\-(.*?)\+iÎÿÿÿc                   @   sv   e Zd ZdZ		ddededdfdd	„Zd
edeee  fdd„Zd
edeee  fdd„Z	dedee fdd„Z
dS )ÚJapanesePhonemizerz?Phonemize Japanese text with OpenJTalk, including pitch accent.TFÚdrop_devoiced_vowelsÚ
use_marineÚreturnNc              
   C   sT   ddl }|| _ || _|| _|r(z| ¡  W dS  ty' } ztdƒ|‚d}~ww dS )aÖ  Initialize phonemizer.

        :param drop_devoiced_vowels: Fold OpenJTalk's devoiced vowels (A I U E O)
            into their voiced counterparts. Devoicing is largely predictable from
            context, so keeping it mainly enlarges the phoneme inventory.
        :param use_marine: Use the neural `marine` model to estimate accent
            instead of OpenJTalk's rules. More accurate on compound words, but
            requires ``pip install marine``.
        r   NzMFailed to load the marine accent model. Install it with 'pip install marine'.)Úpyopenjtalkr8   r9   Úload_marine_modelÚ	ExceptionÚRuntimeError)Úselfr8   r9   r;   Úexc© rA   úš/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/piper/phonemize_japanese.pyÚ__init__   s    ÿý€ÿýzJapanesePhonemizer.__init__Útextc                 C   sr   g }|   |¡D ]/}g }|D ]!}|tv r| |¡ qt |¡}|du r)t d|¡ q| |¡ q|r6| |¡ q|S )zØReturn IPA phonemes with prosody symbols, grouped by sentence.

        Each phoneme is a single codepoint, matching how espeak phonemes are
        keyed in :data:`piper.phoneme_ids.DEFAULT_PHONEME_ID_MAP`.
        Nz&No IPA mapping for OpenJTalk phone: %s)Úphonemize_openjtalkÚPROSODY_SYMBOLSÚappendr1   ÚgetÚ_LOGGERÚwarningÚextend)r?   rD   Úall_phonemesÚsentence_phonesÚipa_phonemesÚphoneÚiparA   rA   rB   Ú	phonemizeœ   s    


€zJapanesePhonemizer.phonemizec                    s   ‡ ‡fdd„t |ƒD ƒS )uý   Return OpenJTalk phones with prosody symbols, grouped by sentence.

        This is the intermediate representation, before the IPA mapping. It is
        the readable form ("ky o o â†‘ w a ..."), useful for inspecting accent
        placement.
        c                    s   g | ]}ˆ  |¡ ‰ rˆ ‘qS rA   )Ú_phonemize_sentence)Ú.0Úsentence©Úphonesr?   rA   rB   Ú
<listcomp>À   s    ýÿz:JapanesePhonemizer.phonemize_openjtalk.<locals>.<listcomp>)Ú_split_sentences)r?   rD   rA   rU   rB   rE   ¹   s   þz&JapanesePhonemizer.phonemize_openjtalkrT   c                 C   s°  z| j r| jj|dd�}n| j |¡}| j |¡}W n ty+   t d|¡ g  Y S w t|ƒ}g }t|ƒD ]Ÿ\}}t	 
|¡}|du rDq6| d¡}	| jrT|	tv rT|	 ¡ }	|	dkro||d krntd|ƒdk}
| |
rktnt¡ q6|	dkry| t¡ q6| |	¡ ||d kr…q6td	|ƒ}td
|ƒ}td|ƒ}td|ƒ}td
||d  ƒ}|dkr´|dkr´|	tv r´| t¡ q6|dkrÈ||d krÈ||krÈ| t¡ q6|dkrÕ|dkrÕ| t¡ q6|S )z?Parse one sentence's full-context labels into phones + prosody.T)Ú
run_marinez Failed to phonemize sentence: %sNé   Úsilz!(\d+)_Úpauz/A:([0-9\-]+)\+z	\+(\d+)\+z\+(\d+)/z	/F:(\d+)_r   é   )r9   r;   Úrun_frontendÚ
make_labelr=   rI   Ú	exceptionÚlenÚ	enumerateÚ_CURRENT_PHONE_PATTERNÚsearchÚgroupr8   ÚDEVOICED_VOWELSÚlowerÚ_numeric_featurerG   ÚINTERROGATIVE_ENDÚDECLARATIVE_ENDÚPAUSEÚ_MORA_FINAL_PHONESÚACCENT_PHRASE_BOUNDARYÚACCENT_FALLÚACCENT_RISE)r?   rT   Únjd_featuresÚlabelsÚ
num_labelsrV   Ú	label_idxÚlabelÚmatchrO   Úis_questionÚa1Úa2Úa3Úf1Úa2_nextrA   rA   rB   rR   Æ   sT   þ








€z&JapanesePhonemizer._phonemize_sentence©TF)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚboolrC   Ústrr   rQ   rE   rR   rA   rA   rA   rB   r7   |   s    ýþý
ür7   Úpatternrt   r:   c                 C   s@   t  | |¡}|du rtS zt| d¡ƒW S  ty   t Y S w )z3Extract an integer field from a full-context label.NrZ   )Úrerd   Ú_NO_FEATUREÚintre   Ú
ValueError)rƒ   rt   ru   rA   rA   rB   rh     s   þrh   rD   c                 C   sp   g }d}t  | ¡D ]}t| |ƒrq	| || ¡ …  ¡ }|r"| |¡ | ¡ }q	| |d…  ¡ }|r6| |¡ |S )z9Split text into sentences, keeping the final punctuation.r   N)Ú_SENTENCE_END_PATTERNÚfinditerÚ_is_decimal_pointÚendÚstriprG   )rD   Ú	sentencesÚstartru   rT   ÚtailrA   rA   rB   rX     s   



rX   ru   zre.Match[str]c                 C   sb   |  d¡dkr	dS | ¡ dkr| | ¡ d  nd}| ¡ t| ƒk r'| | ¡  nd}| ¡ o0| ¡ S )z<True for the '.' in "1.5", which is not a sentence boundary.r   r   FrZ   Ú )re   rŽ   r‹   ra   Úisdigit)rD   ru   ÚbeforeÚafterrA   rA   rB   rŠ   .  s
     rŠ   TFr8   r9   c                 C   s   t ||d� | ¡S )zCPhonemize Japanese text without holding onto a phonemizer instance.)r8   r9   )r7   rQ   )rD   r8   r9   rA   rA   rB   rQ   :  s
   ÿþrQ   Úid_mapc                 C   sP   | du rddl m} t|ƒ} t| ƒ}ttƒ}t ¡ D ]}| |¡ qt|| ƒS )zËReturn any phoneme this module can emit that is not in the id map.

    Useful as a sanity check before training: the output should be empty for
    :data:`piper.phoneme_ids.DEFAULT_PHONEME_ID_MAP`.
    NrZ   )ÚDEFAULT_PHONEME_ID_MAP)	Úphoneme_idsr•   ÚlistÚsetrF   r1   ÚvaluesÚupdateÚsorted)r”   r•   ÚknownÚemittedrP   rA   rA   rB   Úmissing_phonemesE  s   rž   r|   )N)$r€   Úloggingr„   Útypingr   r   r   r   Ú	getLoggerr}   rI   ro   rn   rm   rk   rj   ri   Ú	frozensetrF   r1   r‚   Ú__annotations__rf   rl   Úcompilerˆ   rc   r…   r7   r†   rh   rX   r�   rŠ   rQ   rž   rA   rA   rA   rB   Ú<module>   sø    
úÿþýüûúø	÷
öõôóòñðïîíìëêéèçæåäãâá à!ß"Þ#Ý$Ü%Ö.ÿ

 ýÿþý

üÿ
ÿþ