o
    Qe+1  ã                   @   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 d dlmZ g ZdZdZd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)ÚcPickle)ÚDataset)Ú_check_exists_and_downloadzBhttp://paddlemodels.bj.bcebos.com/conll05st/conll05st-tests.tar.gzZ 387719152ae52d60422c016e92a742fcz:http://paddlemodels.bj.bcebos.com/conll05st%2FwordDict.txtZ ea7fb7d4c75cc6254716f0177a506baaz:http://paddlemodels.bj.bcebos.com/conll05st%2FverbDict.txtZ 0d2977293bbb6cbefab5b0f97db1e77cz<http://paddlemodels.bj.bcebos.com/conll05st%2FtargetDict.txtZ d8c7f03ceb5fc2e5a0fa7503a4353751z1http://paddlemodels.bj.bcebos.com/conll05st%2FembZ bf436eb0faa1f6f9103017f8be57cdb7c                   @   s^   e Zd ZdZ						ddd„Zdd„ Zdd	„ Zd
d„ Zdd„ Zdd„ Z	dd„ Z
dd„ ZdS )Ú	Conll05sta   
    Implementation of `Conll05st <https://www.cs.upc.edu/~srlconll/soft.html>`_
    test dataset.

    Note: only support download test dataset automatically for that
          only test dataset of Conll05st is public.

    Args:
        data_file(str): path to data tar file, can be set None if
            :attr:`download` is True. Default None
        word_dict_file(str): path to word dictionary file, can be set None if
            :attr:`download` is True. Default None
        verb_dict_file(str): path to verb dictionary file, can be set None if
            :attr:`download` is True. Default None
        target_dict_file(str): path to target dictionary file, can be set None if
            :attr:`download` is True. Default None
        emb_file(str): path to embedding dictionary file, only used for
            :code:`get_embedding` can be set None if :attr:`download` is
            True. Default None
        download(bool): whether to download dataset automatically if
            :attr:`data_file` :attr:`word_dict_file` :attr:`verb_dict_file`
            :attr:`target_dict_file` is not set. Default True

    Returns:
        Dataset: instance of conll05st dataset

    Examples:

        .. code-block:: python

            import paddle
            from paddle.text.datasets import Conll05st

            class SimpleNet(paddle.nn.Layer):
                def __init__(self):
                    super(SimpleNet, self).__init__()

                def forward(self, pred_idx, mark, label):
                    return paddle.sum(pred_idx), paddle.sum(mark), paddle.sum(label)


            conll05st = Conll05st()

            for i in range(10):
                pred_idx, mark, label= conll05st[i][-3:]
                pred_idx = paddle.to_tensor(pred_idx)
                mark = paddle.to_tensor(mark)
                label = paddle.to_tensor(label)

                model = SimpleNet()
                pred_idx, mark, label= model(pred_idx, mark, label)
                print(pred_idx.numpy(), mark.numpy(), label.numpy())

    NTc                 C   s  || _ | j d u r|sJ dƒ‚t|ttd|ƒ| _ || _| jd u r.|s%J dƒ‚t|ttd|ƒ| _|| _| jd u rE|s<J dƒ‚t|tt	d|ƒ| _|| _
| j
d u r\|sSJ dƒ‚t|ttd|ƒ| _
|| _| jd u rs|sjJ dƒ‚t|ttd|ƒ| _|  | j¡| _|  | j¡| _|  | j
¡| _|  ¡  d S )Nz>data_file is not set and downloading automatically is disabledZ	conll05stzCword_dict_file is not set and downloading automatically is disabledzCverb_dict_file is not set and downloading automatically is disabledzEtarget_dict_file is not set and downloading automatically is disabledz=emb_file is not set and downloading automatically is disabled)Ú	data_filer   ÚDATA_URLÚDATA_MD5Úword_dict_fileÚWORDDICT_URLÚWORDDICT_MD5Úverb_dict_fileÚVERBDICT_URLÚVERBDICT_MD5Útarget_dict_fileÚTRGDICT_URLÚTRGDICT_MD5Úemb_fileÚEMB_URLÚEMB_MD5Ú
_load_dictÚ	word_dictÚpredicate_dictÚ_load_label_dictÚ
label_dictÚ
_load_anno)Úselfr   r
   r   r   r   Údownload© r   úLD:\Projects\ConvertPro\env\Lib\site-packages\paddle/text/datasets/conll05.pyÚ__init__c   sN   
þ
þ
þ
þ
þzConll05st.__init__c           	      C   sÎ   t ƒ }tƒ }t|dƒ�R}t|ƒD ]%\}}| ¡ }| d¡r'| |dd … ¡ q| d¡r5| |dd … ¡ qd}|D ]}||d| < |d7 }||d| < |d7 }q:||d< W d   ƒ |S 1 s`w   Y  |S )NÚrúB-é   úI-r   é   ÚO)ÚdictÚsetÚopenÚ	enumerateÚstripÚ
startswithÚadd)	r   ÚfilenameÚdZtag_dictÚfÚiÚlineÚindexÚtagr   r   r   r   ”   s*   

€


óòzConll05st._load_label_dictc                 C   sT   t ƒ }t|dƒ�}t|ƒD ]
\}}||| ¡ < qW d   ƒ |S 1 s#w   Y  |S )Nr!   )r'   r)   r*   r+   )r   r.   r/   r0   r1   r2   r   r   r   r   §   s   ÿ
ÿýzConll05st._load_dictc              
      sÊ  t  | j¡}| d¡}| d¡}g | _g | _g | _tj|d���.}tj|d���}g }g }g }t	||ƒD �]\}	}
t
 |	 ¡ ¡}	t
 |
 ¡  ¡ ¡}
t|
ƒdk�r,tt|d ƒƒD ]‰ ‡ fdd„|D ƒ}| |¡ qXt|ƒdk�r%g }|d D ]}|dkr�| |¡ qvt|dd … ƒD ]š\‰ }d	}d
}g }d}|D ]w}|dkr¨|d
kr¨| d	¡ q˜|dkr¸|dkr¸| d| ¡ q˜|dkrÆ| d| ¡ d
}q˜| d¡dkrç| d¡dkrç|d| d¡… }| d| ¡ d
}q˜| d¡dk�r
| d¡dk�r
|d| d¡… }| d| ¡ d}q˜td| ƒ‚| j |¡ | j |ˆ  ¡ | j |¡ qŠg }g }g }q4| |	¡ | |
¡ q4W d   ƒ n	1 �sBw   Y  W d   ƒ n	1 �sRw   Y  | ¡  | ¡  | ¡  d S )Nz2conll05st-release/test.wsj/words/test.wsj.words.gzz2conll05st-release/test.wsj/props/test.wsj.props.gz)Úfileobjr   c                    s   g | ]}|ˆ  ‘qS r   r   )Ú.0Úx©r1   r   r   Ú
<listcomp>Â   s    z(Conll05st._load_anno.<locals>.<listcomp>r%   ú-r&   FÚ Ú*Tr$   z*)ú(éÿÿÿÿú)r"   zUnexpected label: %s)Útarfiler)   r   ÚextractfileÚ	sentencesÚ
predicatesÚlabelsÚgzipÚGzipFileÚzipÚcptZto_textr+   ÚsplitÚlenÚrangeÚappendr*   ÚfindÚRuntimeErrorÚclose)r   ÚtfZwfÚpfZ
words_fileZ
props_filerB   rD   Zone_segÚwordÚlabelZa_kind_lableZ	verb_listr7   ZlblZcur_tagZis_in_bracketZlbl_seqZ	verb_wordÚlr   r8   r   r   ®   sŠ   ÿÿÿ
€ ÿ
Ïû€ 8zConll05st._load_annoc                    sÜ  ˆ j | }ˆ j| }ˆ j| }t|ƒ}| d¡}dgt|ƒ }|dkr0d||d < ||d  }nd}|dkrCd||d < ||d  }	nd}	d||< || }
|t|ƒd k rbd||d < ||d  }nd}|t|ƒd k ryd||d < ||d  }nd}‡ fdd„|D ƒ}ˆ j |	t¡g| }ˆ j |t¡g| }ˆ j |
t¡g| }ˆ j |t¡g| }ˆ j |t¡g| }ˆ j |¡g| }‡ fd	d„|D ƒ}t	 
|¡t	 
|¡t	 
|¡t	 
|¡t	 
|¡t	 
|¡t	 
|¡t	 
|¡t	 
|¡f	S )
NzB-Vr   r%   Zbosr#   Zeosc                    s   g | ]	}ˆ j  |t¡‘qS r   )r   ÚgetÚUNK_IDX©r6   Úw©r   r   r   r9     s    z)Conll05st.__getitem__.<locals>.<listcomp>c                    s   g | ]}ˆ j  |¡‘qS r   )r   rU   rW   rY   r   r   r9      s    )rB   rC   rD   rJ   r3   r   rU   rV   r   ÚnpÚarray)r   ÚidxZsentenceÚ	predicaterD   Zsen_lenZ
verb_indexÚmarkZctx_n1Zctx_n2Zctx_0Zctx_p1Zctx_p2Zword_idxZ
ctx_n2_idxZ
ctx_n1_idxZ	ctx_0_idxZ
ctx_p1_idxZ
ctx_p2_idxZpred_idxZ	label_idxr   rY   r   Ú__getitem__ó   sH   



þzConll05st.__getitem__c                 C   s
   t | jƒS )N)rJ   rB   rY   r   r   r   Ú__len__&  s   
zConll05st.__len__c                 C   s   | j | j| jfS )a6  
        Get the word, verb and label dictionary of Wikipedia corpus.

        Examples:
    
            .. code-block:: python
    
            	from paddle.text.datasets import Conll05st

            	conll05st = Conll05st()
            	word_dict, predicate_dict, label_dict = conll05st.get_dict()
        )r   r   r   rY   r   r   r   Úget_dict)  s   zConll05st.get_dictc                 C   s   | j S )a  
        Get the embedding dictionary file.

        Examples:
    
            .. code-block:: python
    
            	from paddle.text.datasets import Conll05st

            	conll05st = Conll05st()
            	emb_file = conll05st.get_embedding()
        )r   rY   r   r   r   Úget_embedding8  s   zConll05st.get_embedding)NNNNNT)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r    r   r   r   r_   r`   ra   rb   r   r   r   r   r   +   s     8
ú1E3r   )Ú
__future__r   rE   r@   ÚnumpyrZ   ÚsixZ	six.movesr   ÚpickleZ	paddle.ior   Zpaddle.compatÚcompatrH   Zpaddle.dataset.commonr   Ú__all__r   r	   r   r   r   r   r   r   r   r   rV   r   r   r   r   r   Ú<module>   s,   