o
    Qeù  ã                   @   sˆ   d dl mZ d dlZd dlZd dlZd dlZd dlm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dZG dd„ deƒZdS )é    )Úprint_functionN)ÚDataset)Ú_check_exists_and_downloadzJhttp://www-lium.univ-lemans.fr/~schwenk/cslm_joint_paper/data/dev+test.tgzZ 7d7897317ddd8ba0ae5c5fa7248d3ff5z/http://paddlemodels.bj.bcebos.com/wmt/wmt14.tgzZ 0791583d57d5beb693b9414c5b36798cz<s>z<e>z<unk>é   c                   @   sD   e Zd ZdZ				ddd„Zdd	„ Zd
d„ Zdd„ Zddd„ZdS )ÚWMT14a^  
    Implementation of `WMT14 <http://www.statmt.org/wmt14/>`_ test dataset.
    The original WMT14 dataset is too large and a small set of data for set is
    provided. This module will download dataset from
    http://paddlemodels.bj.bcebos.com/wmt/wmt14.tgz .

    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 'gen'. Default 'train'
        dict_size(int): word dictionary size. Default -1.
        download(bool): whether to download dataset automatically if
            :attr:`data_file` is not set. Default True

    Returns:
        Dataset: Instance of WMT14 dataset
            - 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 WMT14

            class SimpleNet(paddle.nn.Layer):
                def __init__(self):
                    super(SimpleNet, self).__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)

            wmt14 = WMT14(mode='train', dict_size=50)

            for i in range(10):
                src_ids, trg_ids, trg_ids_next = wmt14[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.numpy(), trg_ids.numpy(), trg_ids_next.numpy())

    NÚtrainéÿÿÿÿTc                 C   st   |  ¡ dv sJ d |¡ƒ‚|  ¡ | _|| _| jd u r)|s J dƒ‚t|ttd|ƒ| _|dks1J dƒ‚|| _|  ¡  d S )N)r   ÚtestÚgenz3mode should be 'train', 'test' or 'gen', but got {}z>data_file is not set and downloading automatically is disabledZwmt14r   z*dict_size should be set as positive number)	ÚlowerÚformatÚmodeÚ	data_filer   Ú	URL_TRAINÚ	MD5_TRAINÚ	dict_sizeÚ
_load_data)Úselfr   r   r   Údownload© r   úJD:\Projects\ConvertPro\env\Lib\site-packages\paddle/text/datasets/wmt14.pyÚ__init__Z   s   ÿ

þzWMT14.__init__c                    sÌ  dd„ }g ˆ_ g ˆ_g ˆ_tjˆjdd��Ç}dd„ |D ƒ}t|ƒdks%J ‚|| |d ¡ˆjƒˆ_	d	d„ |D ƒ}t|ƒdks@J ‚|| |d ¡ˆjƒˆ_
d
 ˆjˆj¡‰ ‡ fdd„|D ƒ}|D ]t}| |¡D ]l}t |¡}| ¡  d¡}t|ƒdkr{qf|d }| ¡ }‡fdd„tg| tg D ƒ}	|d }
|
 ¡ }‡fdd„|D ƒ}t|	ƒdks¯t|ƒdkr°qf|ˆj
t g }ˆj
t g| }ˆj  |	¡ ˆj |¡ ˆj |¡ qfq_W d   ƒ d S 1 sßw   Y  d S )Nc                 S   s<   t ƒ }t| ƒD ]\}}||k r||t | ¡ ¡< q |S |S ©N)ÚdictÚ	enumerateÚcptÚto_textÚstrip)ÚfdÚsizeZout_dictÚ
line_countÚliner   r   r   Z	__to_dictq   s   z#WMT14._load_data.<locals>.__to_dictÚr)r   c                 S   ó   g | ]}|j  d ¡r|j ‘qS )zsrc.dict©ÚnameÚendswith©Ú.0Z	each_itemr   r   r   Ú
<listcomp>~   ó    
þÿz$WMT14._load_data.<locals>.<listcomp>é   r   c                 S   r#   )ztrg.dictr$   r'   r   r   r   r)   „   r*   z{}/{}c                    s   g | ]}|j  ˆ ¡r|j ‘qS r   r$   r'   )Ú	file_namer   r   r)   Œ   r*   ú	r   c                    ó   g | ]	}ˆ j  |t¡‘qS r   )Úsrc_dictÚgetÚUNK_IDX©r(   Úw©r   r   r   r)   ˜   s    ÿÿc                    r.   r   )Útrg_dictr0   r1   r2   r4   r   r   r)   Ÿ   s    éP   )Úsrc_idsÚtrg_idsÚtrg_ids_nextÚtarfileÚopenr   ÚlenÚextractfiler   r/   r5   r   r   r   r   r   ÚsplitÚSTARTÚENDÚappend)r   Z_WMT14__to_dictÚfÚnamesr%   r!   Z
line_splitZsrc_seqZ	src_wordsr7   Ztrg_seqZ	trg_wordsr8   r9   r   )r,   r   r   r   o   sV   	ÿÿ
ÿ

þèÿ"ízWMT14._load_datac                 C   s.   t  | j| ¡t  | j| ¡t  | j| ¡fS r   )ÚnpÚarrayr7   r8   r9   )r   Úidxr   r   r   Ú__getitem__«   s   ÿzWMT14.__getitem__c                 C   s
   t | jƒS r   )r<   r7   r4   r   r   r   Ú__len__¯   s   
zWMT14.__len__Fc                 C   sB   | j | j}}|rdd„ t |¡D ƒ}dd„ t |¡D ƒ}||fS )a  
        Get the source and target dictionary.

        Args:
            reverse (bool): wether to reverse key and value in dictionary,
                i.e. key: value to value: key.
    
        Returns:
            Two dictionaries, the source and target dictionary.
    
        Examples:
    
            .. code-block:: python
    
                from paddle.text.datasets import WMT14
                wmt14 = WMT14(mode='train', dict_size=50)
                src_dict, trg_dict = wmt14.get_dict()
        c                 S   ó   i | ]\}}||“qS r   r   ©r(   ÚkÚvr   r   r   Ú
<dictcomp>Ç   ó    z"WMT14.get_dict.<locals>.<dictcomp>c                 S   rI   r   r   rJ   r   r   r   rM   È   rN   )r/   r5   ÚsixÚ	iteritems)r   Úreverser/   r5   r   r   r   Úget_dict²   s
   zWMT14.get_dict)Nr   r   T)F)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   rG   rH   rR   r   r   r   r   r   *   s    0
ü<r   )Ú
__future__r   r:   ÚnumpyrD   ÚgziprO   Z	paddle.ior   Zpaddle.compatÚcompatr   Zpaddle.dataset.commonr   Ú__all__ZURL_DEV_TESTZMD5_DEV_TESTr   r   r?   r@   ZUNKr1   r   r   r   r   r   Ú<module>   s$   