o
    i                     @   sh   d Z ddlmZ ddlZddlZddlmZmZ G dd deZG dd deeZ	G d	d
 d
eeZ
dS )zNgram lm implement.    )ABCN)BatchScorerInterfacePartialScorerInterfacec                   @   s(   e Zd ZdZdd Zdd Zdd ZdS )		Ngrambasez/Ngram base implemented through ScorerInterface.c                 C   s6   dd |D | _ t| j | _t|| _t | _dS )zInitialize Ngrambase.

        Args:
            ngram_model: ngram model path
            token_list: token list from dict or model.json

        c                 S   s   g | ]
}|d kr
|ndqS )z<eos>z</s> ).0xr   r   M/home/ubuntu/.local/lib/python3.10/site-packages/espnet/nets/scorers/ngram.py
<listcomp>   s    z&Ngrambase.__init__.<locals>.<listcomp>N)chardictlencharlenkenlmLanguageModellmStatetmpkenlmstate)selfngram_model
token_listr   r   r	   __init__   s   zNgrambase.__init__c                 C   s   t  }| j| |S )zInitialize tmp state.)r   r   r   NullContextWrite)r   r   stater   r   r	   
init_state   s   zNgrambase.init_statec           
      C   s   t  }|jd dkr| j|d  nd}| j||| tj||j|j	d}t
|D ]\}}	| j|| j|	 | j||< q*||fS )  Score interface for both full and partial scorer.

        Args:
            y: previous char
            next_token: next token need to be score
            state: previous state
            x: encoded feature

        Returns:
            tuple[torch.Tensor, List[Any]]: Tuple of
                batchfied scores for next token with shape of `(n_batch, n_vocab)`
                and next state list for ys.

        r      z<s>)dtypedevice)r   r   shaper   r   	BaseScoretorch
empty_liker   r   	enumerater   )
r   y
next_tokenr   r   	out_stateysscoresijr   r   r	   score_partial_!   s    
zNgrambase.score_partial_N)__name__
__module____qualname____doc__r   r   r+   r   r   r   r	   r      s
    r   c                   @   s   e Zd ZdZdd ZdS )NgramFullScorerzFullscorer for ngram.c                 C   s   |  |tt| j||S )an  Score interface for both full and partial scorer.

        Args:
            y: previous char
            state: previous state
            x: encoded feature

        Returns:
            tuple[torch.Tensor, List[Any]]: Tuple of
                batchfied scores for next token with shape of `(n_batch, n_vocab)`
                and next state list for ys.

        )r+   r!   tensorranger   )r   r$   r   r   r   r   r	   score>   s   zNgramFullScorer.scoreN)r,   r-   r.   r/   r3   r   r   r   r	   r0   ;   s    r0   c                   @   s    e Zd ZdZdd Zdd ZdS )NgramPartScorerzPartialscorer for ngram.c                 C   s   |  ||||S )r   )r+   )r   r$   r%   r   r   r   r   r	   score_partialR   s   zNgramPartScorer.score_partialc                 C   s   |S )z(Empty select state for scorer interface.r   )r   r   r)   r   r   r	   select_statec   s   zNgramPartScorer.select_stateN)r,   r-   r.   r/   r5   r6   r   r   r   r	   r4   O   s    r4   )r/   abcr   r   r!   espnet.nets.scorer_interfacer   r   r   r0   r4   r   r   r   r	   <module>   s    0