o
    jo™i^  ã                   @   sB   d Z ddlmZ ddlZddlmZ ddlmZ G dd„ dƒZdS )zãSimple implementation of a T9 processor.

For a given T9 input it returns a lattice of decoded phrases consistent with
the provided lexicon. The user can choose to rescore (e.g., with a language
model) this lattice if desired.
é    )ÚIterableN)Úpynutil)Úrewritec                   @   sÈ   e Zd ZdZddgfdg d¢fdg d¢fdg d	¢fd
g d¢fdg d¢fdg d¢fdg d¢fdg d¢fg	Zdee fdd„Zd#dd„Zdee ddfdd„Z	de
jde
jfdd„Zd e
jdefd!d"„ZdS )$ÚT9z(Simple implementation of a T9 processor.Ú0ú Ú2)ÚaÚbÚcÚ3)ÚdÚeÚfÚ4)ÚgÚhÚiÚ5)ÚjÚkÚlÚ6)ÚmÚnÚoÚ7)ÚpÚqÚrÚsÚ8)ÚtÚuÚvÚ9)ÚwÚxÚyÚzÚlexiconc                 C   s   |   ¡  |  |¡ d S ©N)Ú	_make_fstÚ_make_lexicon)Úselfr*   © r/   úF/home/ubuntu/.local/lib/python3.10/site-packages/pynini/examples/t9.pyÚ__init__&   s   zT9.__init__ÚreturnNc                 C   sV   t  ¡ | _| jD ]\}}|  jt  |t j|Ž ¡O  _q| j ¡  ¡  t  | j¡| _	d S r+   )
ÚpyniniÚFstÚ_decoderÚ_t9_mapÚcrossÚunionÚclosureÚoptimizeÚinvertÚ_encoder)r.   ÚinpÚoutsr/   r/   r0   r,   *   s
   
zT9._make_fstc                 C   s    t  |¡}t |d¡ ¡ | _d S )Nr   )r3   Ú
string_mapr   Újoinr:   Ú_lexicon)r.   r*   Úlexicon_fstr/   r/   r0   r-   1   s   
zT9._make_lexiconÚt9_inputc                 C   s   t  || j¡}t || j¡S r+   )r   Úrewrite_latticer5   r3   Ú	intersectrA   )r.   rC   Úlatticer/   r/   r0   Údecode5   s   z	T9.decodeÚtextc                 C   s   t  || j¡S r+   )r   Útop_rewriter<   )r.   rH   r/   r/   r0   Úencode9   s   z	T9.encode)r2   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r6   r   Ústrr1   r,   r-   r3   ÚFstLiker4   rG   rJ   r/   r/   r/   r0   r      s    ý
r   )rN   Útypingr   r3   Ú
pynini.libr   r   r   r/   r/   r/   r0   Ú<module>   s   