a
     ˜ëjeC  ã                   @   sº   d dl Z d dlmZ d dlmZ d dlT d dlT d dlmZ d dl	m
Z
 G dd„ deƒZG d	d
„ d
ƒZG dd„ dƒZG dd„ deƒZG dd„ deƒZeG dd„ deƒƒZG dd„ dƒZdS )é    N)Útotal_ordering)ÚElementTree)Ú*)Úraise_unorderable_types)ÚTreec                   @   sV   e Zd ZdZddd„Zddd„Zd	d
„ Zdd„ Zddd„Zdd„ Z	dd„ fdd„Z
dS )ÚPropbankCorpusReadera•  
    Corpus reader for the propbank corpus, which augments the Penn
    Treebank with information about the predicate argument structure
    of every verb instance.  The corpus consists of two parts: the
    predicate-argument annotations themselves, and a set of "frameset
    files" which define the argument labels used by the annotations,
    on a per-verb basis.  Each "frameset file" contains one or more
    predicates, such as ``'turn'`` or ``'turn_on'``, each of which is
    divided into coarse-grained word senses called "rolesets".  For
    each "roleset", the frameset file provides descriptions of the
    argument roles, along with examples.
    Ú NÚutf8c                 C   sV   t |tƒrt||ƒ}t|ƒ}t | |||g| |¡ || _|| _|| _|| _	|| _
dS )aÜ  
        :param root: The root directory for this corpus.
        :param propfile: The name of the file containing the predicate-
            argument annotations (relative to ``root``).
        :param framefiles: A list or regexp specifying the frameset
            fileids for this corpus.
        :param parse_fileid_xform: A transform that should be applied
            to the fileids in this corpus.  This should be a function
            of one argument (a fileid) that returns a string (the new
            fileid).
        :param parse_corpus: The corpus containing the parse trees
            corresponding to this corpus.  These parse trees are
            necessary to resolve the tree pointers used by propbank.
        N)Ú
isinstanceÚstrZfind_corpus_fileidsÚlistÚCorpusReaderÚ__init__Ú	_propfileÚ_framefilesÚ
_verbsfileÚ_parse_fileid_xformÚ_parse_corpus)ÚselfÚrootZpropfileÚ
framefilesZ	verbsfileÚparse_fileid_xformÚparse_corpusÚencoding© r   úZ/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/nltk/corpus/reader/propbank.pyr       s    

zPropbankCorpusReader.__init__c                    sD   i ‰ˆ dur‡ fdd„ˆd< t ˆ ˆj¡‡‡fdd„ˆ ˆj¡d�S )zŠ
        :return: a corpus view that acts as a list of
            ``PropBankInstance`` objects, one for each noun in the corpus.
        Nc                    s
   | j ˆ kS ©N©Úbaseform©Úinstr   r   r   Ú<lambda>M   ó    z0PropbankCorpusReader.instances.<locals>.<lambda>Úinstance_filterc                    s   ˆj | fi ˆ ¤ŽS r   )Ú_read_instance_block)Ústream)Úkwargsr   r   r   r!   P   r"   ©r   )ÚStreamBackedCorpusViewÚabspathr   r   )r   r   r   )r   r&   r   r   Ú	instancesF   s    

ýzPropbankCorpusReader.instancesc                 C   s   t |  | j¡t|  | j¡d�S )z‘
        :return: a corpus view that acts as a list of strings, one for
            each line in the predicate-argument annotation file.
        r'   )r(   r)   r   Úread_line_blockr   ©r   r   r   r   ÚlinesT   s
    

ýzPropbankCorpusReader.linesc                 C   s¦   |  d¡d }d| }|| jvr,td| ƒ‚|  |¡ ¡ �}t |¡ ¡ }W d  ƒ n1 s^0    Y  | d¡D ]}|j	d |krr|  S qrtd|› d	|› �ƒ‚dS )
zE
        :return: the xml description for the given roleset.
        Ú.r   úframes/%s.xmlúFrameset file for %s not foundNúpredicate/rolesetÚidzRoleset z not found in )
Úsplitr   Ú
ValueErrorr)   Úopenr   ÚparseÚgetrootÚfindallÚattrib)r   Z
roleset_idr   Ú	framefileÚfpÚetreeÚrolesetr   r   r   r=   _   s    
,
zPropbankCorpusReader.rolesetc              	   C   s–   |dur.d| }|| j vr&td| ƒ‚|g}n| j }g }|D ]P}|  |¡ ¡ �}t |¡ ¡ }W d  ƒ n1 sr0    Y  | | d¡¡ q<t	|ƒS )zA
        :return: list of xml descriptions for rolesets.
        Nr/   r0   r1   )
r   r4   r)   r5   r   r6   r7   Úappendr8   ZLazyConcatenation)r   r   r:   r   Zrsetsr;   r<   r   r   r   Úrolesetsq   s    
,zPropbankCorpusReader.rolesetsc                 C   s   t |  | j¡t|  | j¡d�S )z…
        :return: a corpus view that acts as a list of all verb lemmas
            in this corpus (from the verbs.txt file).
        r'   )r(   r)   r   r+   r   r,   r   r   r   Úverbs†   s
    

ýzPropbankCorpusReader.verbsc                 C   s   dS )NTr   r   r   r   r   r!   ‘   r"   zPropbankCorpusReader.<lambda>c                 C   sJ   g }t dƒD ]8}| ¡  ¡ }|rt || j| j¡}||ƒr| |¡ q|S )Néd   )ÚrangeÚreadlineÚstripÚPropbankInstancer6   r   r   r>   )r   r%   r#   ÚblockÚiÚliner    r   r   r   r$   ‘   s    
ÿz)PropbankCorpusReader._read_instance_block)r   NNNr	   )N)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r*   r-   r=   r?   r@   r$   r   r   r   r   r      s        ø
&

r   c                   @   sl   e Zd Zddd„Zedd„ ƒZedd„ ƒZedd	„ ƒZd
d„ Zdd„ Z	dd„ Z
ee
dd�Zeddd„ƒZdS )rE   Nc
           
      C   s>   || _ || _|| _|| _|| _|| _|| _t|ƒ| _|	| _	d S r   )
ÚfileidÚsentnumÚwordnumÚtaggerr=   Ú
inflectionÚ	predicateÚtupleÚ	argumentsr   )
r   rM   rN   rO   rP   r=   rQ   rR   rT   r   r   r   r   r   §   s    
zPropbankInstance.__init__c                 C   s   | j  d¡d S )zThe baseform of the predicate.r.   r   ©r=   r3   r,   r   r   r   r   Ü   s    zPropbankInstance.baseformc                 C   s   | j  d¡d S )z"The sense number of the predicate.r.   é   rU   r,   r   r   r   Úsensenumberá   s    zPropbankInstance.sensenumberc                 C   s   dS )zIdentifier of the predicate.Úrelr   r,   r   r   r   Úpredidæ   s    zPropbankInstance.predidc                 C   s   d  | j| j| j¡S )Nz(<PropbankInstance: {}, sent {}, word {}>)ÚformatrM   rN   rO   r,   r   r   r   Ú__repr__ë   s
    ýzPropbankInstance.__repr__c                 C   s\   d  | j| j| j| j| j| j¡}| j| jdff }t	|ƒD ]\}}|d|› d|› �7 }q:|S )Nz{} {} {} {} {} {}rX   ú ú-)
rZ   rM   rN   rO   rP   r=   rQ   rT   rR   Úsorted)r   ÚsÚitemsÚarglocÚargidr   r   r   Ú__str__ò   s    úzPropbankInstance.__str__c                 C   s6   | j d u rd S | j| j  ¡ vr"d S | j  | j¡| j S r   )r   rM   ZfileidsZparsed_sentsrN   r,   r   r   r   Ú	_get_tree   s
    
zPropbankInstance._get_treezs
        The parse tree corresponding to this instance, or None if
        the corresponding tree is not available.)Údocc              
   C   s  |   ¡ }t|ƒdk r td|  ƒ‚|d d… \}}}}}}	dd„ |dd … D ƒ}
dd„ |dd … D ƒ}t|
ƒdkr|td|  ƒ‚|d urŒ||ƒ}t|ƒ}t|ƒ}t |	¡}	t |
d d d	… ¡}g }|D ](}|  d
d¡\}}| t |¡|f¡ qÄt||||||	|||ƒ	S )Né   z!Badly formatted propbank line: %ré   c                 S   s   g | ]}|  d ¡r|‘qS ©z-rel©Úendswith©Ú.0Úpr   r   r   Ú
<listcomp>  r"   z*PropbankInstance.parse.<locals>.<listcomp>c                 S   s   g | ]}|  d ¡s|‘qS rh   ri   rk   r   r   r   rn     r"   rV   r   éüÿÿÿr]   )	r3   Úlenr4   ÚintÚPropbankInflectionr6   ÚPropbankTreePointerr>   rE   )r_   r   r   ÚpiecesrM   rN   rO   rP   r=   rQ   rX   ÚargsrR   rT   Úargra   rb   r   r   r   r6     s:    
÷zPropbankInstance.parse)N)NN)rI   rJ   rK   r   Úpropertyr   rW   rY   r[   rc   rd   ÚtreeÚstaticmethodr6   r   r   r   r   rE   ¦   s"    ö
5


þrE   c                   @   s   e Zd ZdZdd„ ZdS )ÚPropbankPointera„  
    A pointer used by propbank to identify one or more constituents in
    a parse tree.  ``PropbankPointer`` is an abstract base class with
    three concrete subclasses:

      - ``PropbankTreePointer`` is used to point to single constituents.
      - ``PropbankSplitTreePointer`` is used to point to 'split'
        constituents, which consist of a sequence of two or more
        ``PropbankTreePointer`` pointers.
      - ``PropbankChainTreePointer`` is used to point to entire trace
        chains in a tree.  It consists of a sequence of pieces, which
        can be ``PropbankTreePointer`` or ``PropbankSplitTreePointer`` pointers.
    c                 C   s   | j tkrtƒ ‚d S r   )Ú	__class__rz   ÚNotImplementedErrorr,   r   r   r   r   L  s    
zPropbankPointer.__init__N)rI   rJ   rK   rL   r   r   r   r   r   rz   =  s   rz   c                   @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
ÚPropbankChainTreePointerc                 C   s
   || _ d S r   ©rt   ©r   rt   r   r   r   r   R  s    z!PropbankChainTreePointer.__init__c                 C   s   d  dd„ | jD ƒ¡S )Nr   c                 s   s   | ]}d | V  qdS ©z%sNr   rk   r   r   r   Ú	<genexpr>Y  r"   z3PropbankChainTreePointer.__str__.<locals>.<genexpr>©Újoinrt   r,   r   r   r   rc   X  s    z PropbankChainTreePointer.__str__c                 C   s   d|  S )Nz<PropbankChainTreePointer: %s>r   r,   r   r   r   r[   [  s    z!PropbankChainTreePointer.__repr__c                    s*   ˆ d u rt dƒ‚td‡ fdd„| jD ƒƒS )NúParse tree not availablez*CHAIN*c                    s   g | ]}|  ˆ ¡‘qS r   ©Úselectrk   ©rx   r   r   rn   a  r"   z3PropbankChainTreePointer.select.<locals>.<listcomp>©r4   r   rt   ©r   rx   r   r‡   r   r†   ^  s    zPropbankChainTreePointer.selectN©rI   rJ   rK   r   rc   r[   r†   r   r   r   r   r}   Q  s   r}   c                   @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
ÚPropbankSplitTreePointerc                 C   s
   || _ d S r   r~   r   r   r   r   r   e  s    z!PropbankSplitTreePointer.__init__c                 C   s   d  dd„ | jD ƒ¡S )Nú,c                 s   s   | ]}d | V  qdS r€   r   rk   r   r   r   r�   k  r"   z3PropbankSplitTreePointer.__str__.<locals>.<genexpr>r‚   r,   r   r   r   rc   j  s    z PropbankSplitTreePointer.__str__c                 C   s   d|  S )Nz<PropbankSplitTreePointer: %s>r   r,   r   r   r   r[   m  s    z!PropbankSplitTreePointer.__repr__c                    s*   ˆ d u rt dƒ‚td‡ fdd„| jD ƒƒS )Nr„   z*SPLIT*c                    s   g | ]}|  ˆ ¡‘qS r   r…   rk   r‡   r   r   rn   s  r"   z3PropbankSplitTreePointer.select.<locals>.<listcomp>rˆ   r‰   r   r‡   r   r†   p  s    zPropbankSplitTreePointer.selectNrŠ   r   r   r   r   r‹   d  s   r‹   c                   @   s\   e Zd ZdZdd„ Ze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 )rs   z@
    wordnum:height*wordnum:height*...
    wordnum:height,

    c                 C   s   || _ || _d S r   ©rO   Úheight)r   rO   rŽ   r   r   r   r   ~  s    zPropbankTreePointer.__init__c                 C   sŒ   |   d¡}t|ƒdkr(tdd„ |D ƒƒS |   d¡}t|ƒdkrPtdd„ |D ƒƒS |   d¡}t|ƒdkrrtd	|  ƒ‚tt|d
 ƒt|d ƒƒS )Nr   rV   c                 S   s   g | ]}t  |¡‘qS r   ©rs   r6   ©rl   Úeltr   r   r   rn   ˆ  r"   z-PropbankTreePointer.parse.<locals>.<listcomp>rŒ   c                 S   s   g | ]}t  |¡‘qS r   r�   r�   r   r   r   rn   �  r"   ú:é   zbad propbank pointer %rr   )r3   rp   r}   r‹   r4   rs   rq   )r_   rt   r   r   r   r6   ‚  s    
ÿ
ÿ
zPropbankTreePointer.parsec                 C   s   | j › d| j› �S )Nr’   r�   r,   r   r   r   rc   ˜  s    zPropbankTreePointer.__str__c                 C   s   d| j | jf S )NzPropbankTreePointer(%d, %d)r�   r,   r   r   r   r[   ›  s    zPropbankTreePointer.__repr__c                 C   sD   t |ttfƒr|jd }q t |tƒs,| |u S | j|jkoB| j|jkS ©Nr   )r
   r}   r‹   rt   rs   rO   rŽ   ©r   Úotherr   r   r   Ú__eq__ž  s
    
zPropbankTreePointer.__eq__c                 C   s
   | |k S r   r   r•   r   r   r   Ú__ne__§  s    zPropbankTreePointer.__ne__c                 C   sP   t |ttfƒr|jd }q t |tƒs4t| ƒt|ƒk S | j| j f|j|j fk S r”   )r
   r}   r‹   rt   rs   r2   rO   rŽ   r•   r   r   r   Ú__lt__ª  s
    
zPropbankTreePointer.__lt__c                 C   s   |d u rt dƒ‚||  |¡ S )Nr„   )r4   Útreeposr‰   r   r   r   r†   ³  s    zPropbankTreePointer.selectc                 C   sÔ   |du rt dƒ‚|g}g }d}t|d tƒr–t|ƒt|ƒk rH| d¡ n|d  d7  < |d t|d ƒk r„| |d |d  ¡ qÎ| ¡  | ¡  q|| jkr¾t|dt|ƒ| j d … ƒS |d7 }| ¡  qdS )z}
        Convert this pointer to a standard 'tree position' pointer,
        given that it points to the given tree.
        Nr„   r   éÿÿÿÿrV   )	r4   r
   r   rp   r>   ÚpoprO   rS   rŽ   )r   rx   Ústackrš   rO   r   r   r   rš   ¸  s"    

zPropbankTreePointer.treeposN)rI   rJ   rK   rL   r   ry   r6   rc   r[   r—   r˜   r™   r†   rš   r   r   r   r   rs   v  s   
		rs   c                   @   st   e Zd ZdZdZdZdZdZdZdZ	dZ
dZdZd	Zd
ZdZdZddd„Zdd„ Zdd„ Ze d¡Zedd„ ƒZdS )rr   rG   Úgrm   ÚvÚfÚnÚoÚbÚ3Úar]   c                 C   s"   || _ || _|| _|| _|| _d S r   ©ÚformÚtenseÚaspectÚpersonÚvoice)r   r§   r¨   r©   rª   r«   r   r   r   r   ò  s
    zPropbankInflection.__init__c                 C   s   | j | j | j | j | j S r   r¦   r,   r   r   r   rc   ù  s    zPropbankInflection.__str__c                 C   s   d|  S )Nz<PropbankInflection: %s>r   r,   r   r   r   r[   ü  s    zPropbankInflection.__repr__z"[igpv\-][fpn\-][pob\-][3\-][ap\-]$c                 C   s>   t | tƒstdƒ‚t| ƒdks*tj | ¡s6td|  ƒ‚t| Ž S )Nzexpected a stringé   z!Bad propbank inflection string %r)r
   r   Ú	TypeErrorrp   rr   Ú	_VALIDATEÚmatchr4   )r_   r   r   r   r6     s
    
zPropbankInflection.parseN)r]   r]   r]   r]   r]   )rI   rJ   rK   Z
INFINITIVEZGERUNDZ
PARTICIPLEZFINITEZFUTUREZPASTZPRESENTZPERFECTZPROGRESSIVEZPERFECT_AND_PROGRESSIVEZTHIRD_PERSONZACTIVEZPASSIVEÚNONEr   rc   r[   ÚreÚcompiler®   ry   r6   r   r   r   r   rr   Û  s(   

rr   )r±   Ú	functoolsr   Z	xml.etreer   Znltk.corpus.reader.apiZnltk.corpus.reader.utilZnltk.internalsr   Z	nltk.treer   r   r   rE   rz   r}   r‹   rs   rr   r   r   r   r   Ú<module>   s      d