o
    ±ƒðj  ã                   @   s~  d Z ddlZddlmZ ddlmZmZmZ ddlZ	ddl
mZ eeƒjZed ZdZdd	„ ed
dƒD ƒZegdd	„ eddƒD ƒ dg ZdZdZeeegZdZeegZg d¢e Zg d¢ZeeddƒƒZe d¡Zde de fdd„Z!de de fdd„Z"de de#fd d!„Z$de de#fd"d#„Z%de de#fd$d%„Z&d&ge e Z'd&ge Z(d&ge Z)d&ge Z*d'd(„ e+e'ƒD ƒZ,G d)d*„ d*ƒZ-dS )+aV  Hebrew diacritization (niqqud) with Nakdimon.

Hebrew is normally written without niqqud (vowel points), but espeak-ng needs the
points to produce correct phonemes. This module restores them with the Nakdimon
model, mirroring the Arabic ``libtashkeel`` path in :mod:`piper.tashkeel`.

Inference-only port of Nakdimon (https://github.com/elazarg/nakdimon, MIT).
The character tables and merge logic are vendored from ``nakdimon/hebrew.py`` and
``nakdimon/dataset.py`` so the package is self-contained (no upstream/TensorFlow
dependency at runtime). See ``LICENSE`` and ``SOURCE`` in this directory.
é    N)ÚPath)ÚListÚOptionalÚUnion)ÚInferenceSessionznakdimon.onnxu   Ö¿c                 C   ó   g | ]}t |ƒ‘qS © ©Úchr©Ú.0Úcr   r   ú—/tmp/claude-1000/-var-www-html-academy/3f42efd1-c2b7-4d08-9b05-30d86bb35479/scratchpad/piper/venv/lib/python3.10/site-packages/piper/hebrew/__init__.pyÚ
<listcomp>   ó    r   iÐ  ië  c                 C   r   r   r	   r   r   r   r   r       r   i°  i½  u   Ö·u   ×�u   ×‚u   Ö¼)ú ú!ú"ú'ú(ú)ú,ú-Ú.ú:ú;ú?)ÚHÚOÚ5u
   ×š×�×Ÿ×£×¥u
   ×›×ž× ×¤×¦u   [Ö°-Ö¼Ö¿×�×‚×‡]ÚtextÚreturnc                 C   s   t  d| ¡S )z<Strip any existing points so the model sees bare consonants.Ú )Ú_NIQQUD_PATTERNÚsub)r    r   r   r   Úremove_niqqud>   s   r%   r   c                 C   sŒ   | t v r| S | tv rt|  S | dv rdS | dv rdS | dkr dS | dkr&dS | d	v r,d
S | dv r2dS |  ¡ r8dS | dkr>dS | dv rDdS dS )z.Map a character to the model's input alphabet.)Ú
ú	r   )u   Ö¾u   â€’u   â€“u   â€”u   â€•u   âˆ’r   ú[r   ú]r   )õ   Â´u   â€˜u   â€™r   )u   â€œu   â€�u   ×´r   r   u   â€¦r   )u   ×²u   ×°u   ×±r   r   )ÚVALID_LETTERSÚENDINGS_TO_REGULARÚisdigit)r   r   r   r   Ú	normalizeC   s.   r.   Úletterc                 C   ó   | dv S )Nu(   ×‘×’×“×”×•×–×˜×™×›×œ×ž× ×¡×¤×¦×§×©×ª×š×£r   ©r/   r   r   r   Ú
can_dagesh^   ó   r2   c                 C   s   | dkS )Nu   ×©r   r1   r   r   r   Úcan_sinb   r3   r4   c                 C   r0   )Nu0   ×�×‘×’×“×”×•×–×—×˜×™×›×œ×ž× ×¡×¢×¤×¦×§×¨×©×ª×š×Ÿr   r1   r   r   r   Ú
can_niqqudf   r3   r5   r"   c                 C   s   i | ]\}}||“qS r   r   )r   Úir   r   r   r   Ú
<dictcomp>s   s    r7   c                   @   s\   e Zd ZdZefdeeef ddfdd„Zddede	e
 defd	d
„Zdedefdd„ZdS )ÚNakdimonDiacritizerz(Add niqqud to Hebrew text with Nakdimon.Ú
model_pathr!   Nc                 C   s*   t t|ƒdgd�| _| j ¡ d j| _dS )zInitialize diacritizer.ÚCPUExecutionProvider)Ú	providersr   N)r   ÚstrÚsessionÚ
get_inputsÚnameÚ_input_name)Úselfr9   r   r   r   Ú__init__y   s   
ÿzNakdimonDiacritizer.__init__r    Útaskeen_thresholdc                 C   s
   |   |¡S )zCAdd niqqud (``taskeen_threshold`` accepted for API parity, unused).)Ú
diacritize)rA   r    rC   r   r   r   Ú__call__€   s   
zNakdimonDiacritizer.__call__c                 C   s  t |ƒ}t|ƒ}|s|S tjdd„ |D ƒgtjd�}| j d| j|i¡\}}}tj|d dd�}tj|d dd�}tj|d dd�}	g }
t	||||	ƒD ],\}}}}|
 
|¡ t|ƒra|
 
t| ¡ t|ƒrl|
 
t| ¡ t|ƒrw|
 
t| ¡ qKd |
¡ td¡S )	z%Return ``text`` with niqqud restored.c                 S   s   g | ]}t t|ƒ ‘qS r   )Ú_CHAR_TO_IDr.   r   r   r   r   r   Ž   s    z2NakdimonDiacritizer.diacritize.<locals>.<listcomp>)ÚdtypeNr   éÿÿÿÿ)Úaxisr"   )r%   ÚlistÚnpÚarrayÚfloat32r=   Úrunr@   ÚargmaxÚzipÚappendr2   Ú_DAGESH_CHARSr4   Ú
_SIN_CHARSr5   Ú_NIQQUD_CHARSÚjoinÚreplaceÚRAFE)rA   r    ÚlettersÚ	input_idsÚn_outÚd_outÚs_outÚniqqudÚdageshÚsinÚoutr/   Ún_idÚd_idÚs_idr   r   r   rD   „   s,   ÿ
€zNakdimonDiacritizer.diacritize)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚDEFAULT_MODEL_PATHr   r<   r   rB   r   ÚfloatrE   rD   r   r   r   r   r8   v   s
    r8   ).rg   ÚreÚpathlibr   Útypingr   r   r   ÚnumpyrK   Úonnxruntimer   Ú__file__ÚparentÚ
HEBREW_DIRrh   rW   ÚrangeÚHEBREW_LETTERSÚNIQQUDÚSHIN_YEMANITÚSHIN_SMALITÚ
NIQQUD_SINÚDAGESH_LETTERÚDAGESHr+   ÚSPECIAL_TOKENSÚdictrP   r,   Úcompiler#   r<   r%   r.   Úboolr2   r4   r5   Ú_LETTER_CHARSrT   rR   rS   Ú	enumeraterF   r8   r   r   r   r   Ú<module>   sB    
 
ó



