o
    jo™i÷  ã                   @   sN   d Z ddlmZ ddlZddlmZ ddlmZ ddlmZ G dd„ dƒZ	dS )	z'Combination of Chatspeak model with LM.é    )ÚListN)Úpynutil)Úrewrite)Ú	chatspeakc                   @   sL   e Zd ZdZdededdfdd„Zdedejfd	d
„Zdedefdd„Z	dS )ÚChatspeakModelz.Combination of the Chatspeak model with an LM.Úchat_lexicon_pathÚlm_pathÚreturnNc                 C   s®   t j |¡| _| j ¡ sJ dƒ‚| j ¡ | _dd„ | jD ƒ}t  |¡ ¡ }t 	|¡| _
t |¡| _t ¡ | _t |¡| _t j|d| jd}t |d¡ ¡ | _t  | j¡| _d S )NzNo LM output symbol table foundc                 S   s   g | ]
\}}|d kr|‘qS )r   © )Ú.0ÚlÚwr
   r
   úS/home/ubuntu/.local/lib/python3.10/site-packages/pynini/examples/chatspeak_model.pyÚ
<listcomp>$   s    z+ChatspeakModel.__init__.<locals>.<listcomp>Úbyte)Úinput_token_typeÚoutput_token_typeú )ÚpyniniÚFstÚreadÚ_lmÚoutput_symbolsÚ_lm_symsÚ
string_mapÚoptimizer   ÚDeduplicatorÚ_deduplicatorÚDeabbreviatorÚ_deabbreviatorÚRegexpsÚ_regexpsÚLexiconÚ_lexiconr   ÚjoinÚ_bytes_to_lm_mapperÚinvertÚ_lm_to_bytes_mapper)Úselfr   r   ÚlexiconÚlexicon_fsaÚ	lm_mapperr
   r
   r   Ú__init__    s   
ÿzChatspeakModel.__init__Útokenc                 C   s4   t  | j |¡| j |¡| j |¡| j |¡¡ ¡ S )z…Constructs a "link" of the lattice for a given token.

    Args:
      token: An input token.

    Returns:
      An FST "link".
    )r   Úunionr   Úexpandr   r!   r#   r   )r(   r-   r
   r
   r   Útoken_lattice/   s   	



üûzChatspeakModel.token_latticeÚsentencec                 C   s~   t | ¡ ƒ}t|ƒ}|  |¡}|D ]}| d¡ | |  |¡¡ q| ¡  t || j¡}t || j	¡}t || j
¡}t |¡S )z£Decodes sentence with the Chatspeak model + LM.

    Args:
      sentence: an input sentence.

    Returns:
      String representing the normalized sentence.
    r   )ÚiterÚsplitÚnextr0   Úconcatr   r   Úrewrite_latticer%   r   r'   Úlattice_to_top_string)r(   r1   Úitr-   Úlatticer
   r
   r   Údecode?   s   	


zChatspeakModel.decode)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ústrr,   r   r   r0   r:   r
   r
   r
   r   r      s
    r   )
r>   Útypingr   r   Ú
pynini.libr   r   Úpynini.examplesr   r   r
   r
   r
   r   Ú<module>   s   