o
    #j'                     @   s|   d Z ddlZddlZddlmZ ddlZddlZddlm	Z	 ddl
mZ g ZdZdZdZd	Zd
ZdZdZG dd deZdS )
    N)defaultdict)_check_exists_and_download)Datasetz2http://paddlemodels.bj.bcebos.com/wmt/wmt16.tar.gzZ 0c38be43600334966403524a40dcd81ei+  iK  z<s>z<e>z<unk>c                   @   sZ   e Zd ZdZ						dddZdd
dZdd Zdd Zdd Zdd Z	dddZ
dS )WMT16a{  
    Implementation of `WMT16 <http://www.statmt.org/wmt16/>`_ test dataset.
    ACL2016 Multimodal Machine Translation. Please see this website for more
    details: http://www.statmt.org/wmt16/multimodal-task.html#task1

    If you use the dataset created for your task, please cite the following paper:
    Multi30K: Multilingual English-German Image Descriptions.

    .. code-block:: text

        @article{elliott-EtAl:2016:VL16,
         author    = {{Elliott}, D. and {Frank}, S. and {Sima"an}, K. and {Specia}, L.},
         title     = {Multi30K: Multilingual English-German Image Descriptions},
         booktitle = {Proceedings of the 6th Workshop on Vision and Language},
         year      = {2016},
         pages     = {70--74},
         year      = 2016
        }

    Args:
        data_file(str): path to data tar file, can be set None if
            :attr:`download` is True. Default None.
        mode(str): 'train', 'test' or 'val'. Default 'train'.
        src_dict_size(int): word dictionary size for source language word. Default -1.
        trg_dict_size(int): word dictionary size for target language word. Default -1.
        lang(str): source language, 'en' or 'de'. Default 'en'.
        download(bool): whether to download dataset automatically if
            :attr:`data_file` is not set. Default True.

    Returns:
        Dataset: Instance of WMT16 dataset. The instance of dataset has 3 fields:
            - src_ids (np.array) - The sequence of token ids of source language.
            - trg_ids (np.array) - The sequence of token ids of target language.
            - trg_ids_next (np.array) - The next sequence of token ids of target language.

    Examples:

        .. code-block:: python

            >>> import paddle
            >>> from paddle.text.datasets import WMT16

            >>> class SimpleNet(paddle.nn.Layer):
            ...     def __init__(self):
            ...         super().__init__()
            ...
            ...     def forward(self, src_ids, trg_ids, trg_ids_next):
            ...         return paddle.sum(src_ids), paddle.sum(trg_ids), paddle.sum(trg_ids_next)

            >>> wmt16 = WMT16(mode='train', src_dict_size=50, trg_dict_size=50)

            >>> for i in range(10):
            ...     src_ids, trg_ids, trg_ids_next = wmt16[i]
            ...     src_ids = paddle.to_tensor(src_ids)
            ...     trg_ids = paddle.to_tensor(trg_ids)
            ...     trg_ids_next = paddle.to_tensor(trg_ids_next)
            ...
            ...     model = SimpleNet()
            ...     src_ids, trg_ids, trg_ids_next = model(src_ids, trg_ids, trg_ids_next)
            ...     print(src_ids.item(), trg_ids.item(), trg_ids_next.item())
            89 32 33
            79 18 19
            55 26 27
            147 36 37
            106 22 23
            135 50 51
            54 43 44
            217 30 31
            146 51 52
            55 24 25
    NtrainenTc                 C   s   |  dv sJ d| |  | _|| _| jd u r)|s J dt|ttd|| _|| _|dks4J d|dks<J dt||dkrDtnt	| _
t||dkrPt	nt| _| ||| _| |dkrcdnd|| _|  | _d S )	N)r   testvalz1mode should be 'train', 'test' or 'val', but got z>data_file is not set and downloading automatically is disabledZwmt16r   z*dict_size should be set as positive numberr	   de)lowermode	data_filer   DATA_URLDATA_MD5langminTOTAL_EN_WORDSTOTAL_DE_WORDSsrc_dict_sizetrg_dict_size
_load_dictsrc_dicttrg_dict
_load_datadata)selfr   r   r   r   r   download r   [/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/text/datasets/wmt16.py__init__q   s6   	


zWMT16.__init__Fc                 C   s   t jtjjjd||f }d}t j|r3t|d}t	|
 |k}W d    n1 s.w   Y  |s<| ||| i }t|d%}t|D ]\}	}
|rW|
  ||	< qH|	||
  < qHW d    |S 1 skw   Y  |S )Nwmt16/%s_%d.dictFrb)ospathjoinpaddledatasetcommon	DATA_HOMEexistsopenlen	readlines_build_dict	enumeratestripdecode)r   r   	dict_sizereverse	dict_pathZ
dict_foundd	word_dictZfdictidxliner   r   r    r      s,   

zWMT16._load_dictc              	   C   sT  t t}tj| jdd>}|dD ]0}| }| d}t	|dkr&q| j
dkr/|d n|d }| D ]
}	||	  d7  < q7qW d    n1 sMw   Y  t|d	I}
|
t d
t d
t d
  tt| dd ddD ]\}}|d |kr n|
|d   |
d quW d    d S W d    d S 1 sw   Y  d S )Nrr   zwmt16/train	   r	   r      wbr   c                 S   s   | d S )Nr>   r   )xr   r   r    <lambda>   s    z#WMT16._build_dict.<locals>.<lambda>T)keyr4         
)r   inttarfiler,   r   extractfiler2   r1   splitr-   r   write
START_MARKEND_MARKUNK_MARKencoder0   sorteditems)r   r5   r3   r   r7   fr9   
line_splitZsenwZfoutr8   wordr   r   r    r/      s4   
 "zWMT16._build_dictc                    s@   j t } j t } j t  jdkrdnd}d| }g  _g  _g  _tj	 j
ddi}|d j D ]V}| }| d}t|dkrKq7||  }|g fd	d
|D  |g }	||  }
 fdd
|
D }||g }|g| } j|	  j|  j| q7W d    d S 1 sw   Y  d S )Nr	   r   r>   r:   r;   zwmt16/r<   r=   c                       g | ]	} j |qS r   )r   get.0rR   r   Zunk_idr   r    
<listcomp>       z$WMT16._load_data.<locals>.<listcomp>c                    rT   r   )r   rU   rV   rX   r   r    rY      rZ   )r   rJ   rK   rL   r   src_idstrg_idstrg_ids_nextrF   r,   r   rG   r   r2   r1   rH   r-   append)r   Zstart_idZend_idZsrc_colZtrg_colrP   r9   rQ   Z	src_wordsr[   Z	trg_wordsr\   r]   r   rX   r    r      s<   




"zWMT16._load_datac                 C   s.   t | j| t | j| t | j| fS N)nparrayr[   r\   r]   )r   r8   r   r   r    __getitem__   s   zWMT16.__getitem__c                 C   s
   t | jS r_   )r-   r[   )r   r   r   r    __len__   s   
zWMT16.__len__c                 C   sV   || j kr| jn| j}tjtjjj	d||f }tj
|s#J d	 	 | ||S )a  
        return the word dictionary for the specified language.

        Args:
            lang(string): A string indicating which language is the source
                          language. Available options are: "en" for English
                          and "de" for Germany.
            reverse(bool): If reverse is set to False, the returned python
                           dictionary will use word as key and use index as value.
                           If reverse is set to True, the returned python
                           dictionary will use index as key and word as value.

        Returns:
            dict: The word dictionary for the specific language.

        Examples:

            .. code-block:: python

                >>> from paddle.text.datasets import WMT16
                >>> wmt16 = WMT16(mode='train', src_dict_size=50, trg_dict_size=50)
                >>> en_dict = wmt16.get_dict('en')

        r"   z Word dictionary does not exist. )r   r   r   r$   r%   r&   r'   r(   r)   r*   r+   r   )r   r   r4   r3   r5   r   r   r    get_dict   s   
zWMT16.get_dict)Nr   r   r   r	   T)F)__name__
__module____qualname____doc__r!   r   r/   r   rb   rc   rd   r   r   r   r    r   (   s    J

,%r   )rh   r$   rF   collectionsr   numpyr`   r'   Zpaddle.dataset.commonr   Z	paddle.ior   __all__r   r   r   r   rJ   rK   rL   r   r   r   r   r    <module>   s"   