a
     ˜ëj¢=  ã                   @   s¤   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dS )é    )Ú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 )ÚNombankCorpusReadera”  
    Corpus reader for the nombank corpus, which augments the Penn
    Treebank with information about the predicate argument structure
    of every noun 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-noun 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   sL   t |tƒrt||ƒ| _t|ƒ| _t | |||¡ || _|| _|| _	|| _
dS )aÚ  
        :param root: The root directory for this corpus.
        :param nomfile: 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 nombank.
        N)Ú
isinstanceÚstrZfind_corpus_fileidsZ_fileidsÚlistÚCorpusReaderÚ__init__Ú_nomfileÚ
_nounsfileÚ_parse_fileid_xformÚ_parse_corpus)ÚselfÚrootZnomfileÚ
framefilesZ	nounsfileÚparse_fileid_xformÚparse_corpusÚencoding© r   úY/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/nltk/corpus/reader/nombank.pyr       s    

zNombankCorpusReader.__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
            ``NombankInstance`` objects, one for each noun in the corpus.
        Nc                    s
   | j ˆ kS ©N©Úbaseform©Úinstr   r   r   Ú<lambda>M   ó    z/NombankCorpusReader.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NombankCorpusReader.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NombankCorpusReader.linesc                 C   sÈ   |  d¡d }| dd¡}| dd¡ dd¡}d| }||  ¡ vrNtd	| ƒ‚|  |¡ ¡ �}t |¡ ¡ }W d
  ƒ n1 s€0    Y  | 	d¡D ]}|j
d |kr”|  S q”td|› d|› �ƒ‚d
S )zE
        :return: the xml description for the given roleset.
        Ú.r   ú	perc-signú%Úoneslashonezeroú1/10ú
1-slash-10úframes/%s.xmlúFrameset file for %s not foundNúpredicate/rolesetÚidzRoleset z not found in )ÚsplitÚreplaceÚfileidsÚ
ValueErrorr(   Úopenr   ÚparseÚgetrootÚfindallÚattrib)r   Z
roleset_idr   Ú	framefileÚfpÚetreeÚrolesetr   r   r   rC   _   s    ÿ,
zNombankCorpusReader.rolesetc              	   C   sš   |dur0d| }||   ¡ vr(td| ƒ‚|g}n|   ¡ }g }|D ]P}|  |¡ ¡ �}t |¡ ¡ }W d  ƒ n1 sv0    Y  | | d¡¡ q@t	|ƒS )zA
        :return: list of xml descriptions for rolesets.
        Nr3   r4   r5   )
r9   r:   r(   r;   r   r<   r=   Úappendr>   ZLazyConcatenation)r   r   r@   r   ZrsetsrA   rB   r   r   r   Úrolesetsu   s    ,zNombankCorpusReader.rolesetsc                 C   s   t |  | j¡t|  | j¡d�S )z�
        :return: a corpus view that acts as a list of all noun lemmas
            in this corpus (from the nombank.1.0.words file).
        r&   )r'   r(   r   r*   r   r+   r   r   r   ÚnounsŠ   s
    

ýzNombankCorpusReader.nounsc                 C   s   dS )NTr   r   r   r   r   r    •   r!   zNombankCorpusReader.<lambda>c                 C   sJ   g }t dƒD ]8}| ¡  ¡ }|rt || j| j¡}||ƒr| |¡ q|S )Néd   )ÚrangeÚreadlineÚstripÚNombankInstancer<   r   r   rD   )r   r$   r"   ÚblockÚiÚliner   r   r   r   r#   •   s    
ÿz(NombankCorpusReader._read_instance_block)r   NNNr	   )N)N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r)   r,   rC   rE   rF   r#   r   r   r   r   r      s        ø
&

r   c                   @   sT   e Zd Zd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 )rK   Nc
           
      C   s>   || _ || _|| _|| _|| _|| _|| _t|ƒ| _|	| _	d S r   )
ÚfileidÚsentnumÚwordnumr   ÚsensenumberÚ	predicateÚpredidÚtupleÚ	argumentsr   )
r   rS   rT   rU   r   rV   rW   rX   rZ   r   r   r   r   r   «   s    
zNombankInstance.__init__c                 C   s2   | j  dd¡}| dd¡ dd¡}|› d| j› �S )z¬The name of the roleset used by this instance's predicate.
        Use ``nombank.roleset() <NombankCorpusReader.roleset>`` to
        look up information about the roleset.r/   r.   r1   r2   r0   r-   )r   r8   rV   )r   Úrr   r   r   rC   Ü   s    zNombankInstance.rolesetc                 C   s   d  | j| j| j¡S )Nz'<NombankInstance: {}, sent {}, word {}>)ÚformatrS   rT   rU   r+   r   r   r   Ú__repr__å   s
    ýzNombankInstance.__repr__c                 C   sX   d  | j| j| j| j| j¡}| j| jdff }t|ƒD ]\}}|d|› d|› �7 }q6|S )Nz{} {} {} {} {}Úrelú ú-)	r\   rS   rT   rU   r   rV   rZ   rW   Úsorted)r   ÚsÚitemsÚarglocÚargidr   r   r   Ú__str__ì   s    ûzNombankInstance.__str__c                 C   s6   | j d u rd S | j| j  ¡ vr"d S | j  | j¡| j S r   )r   rS   r9   Zparsed_sentsrT   r+   r   r   r   Ú	_get_treeù   s
    
zNombankInstance._get_treezs
        The parse tree corresponding to this instance, or None if
        the corresponding tree is not available.)Údocc              
      sø   |   ¡ }t|ƒdk r td|  ƒ‚|d d… \}}}}}|dd … ‰ ‡ fdd„tˆ ƒD ƒ}	t|	ƒdkrptd|  ƒ‚|d ur€||ƒ}t|ƒ}t|ƒ}|	d   dd¡\}
}t |
¡}g }ˆ D ](}|  dd¡\}}| t |¡|f¡ q¶t|||||||||ƒ	S )	Né   z Badly formatted nombank line: %ré   c                    s"   g | ]\}}d |v rˆ   |¡‘qS )z-rel)Úpop)Ú.0rM   Úp©Úargsr   r   Ú
<listcomp>  r!   z)NombankInstance.parse.<locals>.<listcomp>é   r   r`   )	r7   Úlenr:   Ú	enumerateÚintÚNombankTreePointerr<   rD   rK   )rb   r   r   ÚpiecesrS   rT   rU   r   rV   r^   ZpredlocrX   rW   rZ   Úargrd   re   r   rn   r   r<     s:    
÷zNombankInstance.parse)N)NN)rO   rP   rQ   r   ÚpropertyrC   r]   rf   rg   ÚtreeÚstaticmethodr<   r   r   r   r   rK   ª   s    ö
1
þrK   c                   @   s   e Zd ZdZdd„ ZdS )ÚNombankPointeran  
    A pointer used by nombank to identify one or more constituents in
    a parse tree.  ``NombankPointer`` is an abstract base class with
    three concrete subclasses:

    - ``NombankTreePointer`` is used to point to single constituents.
    - ``NombankSplitTreePointer`` is used to point to 'split'
      constituents, which consist of a sequence of two or more
      ``NombankTreePointer`` pointers.
    - ``NombankChainTreePointer`` is used to point to entire trace
      chains in a tree.  It consists of a sequence of pieces, which
      can be ``NombankTreePointer`` or ``NombankSplitTreePointer`` pointers.
    c                 C   s   | j tkrtƒ ‚d S r   )Ú	__class__r{   ÚNotImplementedErrorr+   r   r   r   r   E  s    
zNombankPointer.__init__N)rO   rP   rQ   rR   r   r   r   r   r   r{   6  s   r{   c                   @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
ÚNombankChainTreePointerc                 C   s
   || _ d S r   ©rv   ©r   rv   r   r   r   r   K  s    z NombankChainTreePointer.__init__c                 C   s   d  dd„ | jD ƒ¡S )Nr   c                 s   s   | ]}d | V  qdS ©z%sNr   ©rl   rm   r   r   r   Ú	<genexpr>R  r!   z2NombankChainTreePointer.__str__.<locals>.<genexpr>©Újoinrv   r+   r   r   r   rf   Q  s    zNombankChainTreePointer.__str__c                 C   s   d|  S )Nz<NombankChainTreePointer: %s>r   r+   r   r   r   r]   T  s    z NombankChainTreePointer.__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   ©Úselectr‚   ©ry   r   r   rp   Z  r!   z2NombankChainTreePointer.select.<locals>.<listcomp>©r:   r   rv   ©r   ry   r   r‰   r   rˆ   W  s    zNombankChainTreePointer.selectN©rO   rP   rQ   r   rf   r]   rˆ   r   r   r   r   r~   J  s   r~   c                   @   s,   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	S )
ÚNombankSplitTreePointerc                 C   s
   || _ d S r   r   r€   r   r   r   r   ^  s    z NombankSplitTreePointer.__init__c                 C   s   d  dd„ | jD ƒ¡S )Nú,c                 s   s   | ]}d | V  qdS r�   r   r‚   r   r   r   rƒ   d  r!   z2NombankSplitTreePointer.__str__.<locals>.<genexpr>r„   r+   r   r   r   rf   c  s    zNombankSplitTreePointer.__str__c                 C   s   d|  S )Nz<NombankSplitTreePointer: %s>r   r+   r   r   r   r]   f  s    z NombankSplitTreePointer.__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‡   r‚   r‰   r   r   rp   l  r!   z2NombankSplitTreePointer.select.<locals>.<listcomp>rŠ   r‹   r   r‰   r   rˆ   i  s    zNombankSplitTreePointer.selectNrŒ   r   r   r   r   r�   ]  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 )ru   z@
    wordnum:height*wordnum:height*...
    wordnum:height,

    c                 C   s   || _ || _d S r   ©rU   Úheight)r   rU   r�   r   r   r   r   w  s    zNombankTreePointer.__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   rq   c                 S   s   g | ]}t  |¡‘qS r   ©ru   r<   ©rl   Úeltr   r   r   rp   �  r!   z,NombankTreePointer.parse.<locals>.<listcomp>rŽ   c                 S   s   g | ]}t  |¡‘qS r   r‘   r’   r   r   r   rp   ˆ  r!   ú:é   zbad nombank pointer %rr   )r7   rr   r~   r�   r:   ru   rt   )rb   rv   r   r   r   r<   {  s    
ÿ
ÿ
zNombankTreePointer.parsec                 C   s   | j › d| j› �S )Nr”   r�   r+   r   r   r   rf   ‘  s    zNombankTreePointer.__str__c                 C   s   d| j | jf S )NzNombankTreePointer(%d, %d)r�   r+   r   r   r   r]   ”  s    zNombankTreePointer.__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�   rv   ru   rU   r�   ©r   Úotherr   r   r   Ú__eq__—  s
    
zNombankTreePointer.__eq__c                 C   s
   | |k S r   r   r—   r   r   r   Ú__ne__   s    zNombankTreePointer.__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�   rv   ru   r6   rU   r�   r—   r   r   r   Ú__lt__£  s
    
zNombankTreePointer.__lt__c                 C   s   |d u rt dƒ‚||  |¡ S )Nr†   )r:   Útreeposr‹   r   r   r   rˆ   ¬  s    zNombankTreePointer.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   éÿÿÿÿrq   )	r:   r
   r   rr   rD   rk   rU   rY   r�   )r   ry   Ústackrœ   rU   r   r   r   rœ   ±  s"    

zNombankTreePointer.treeposN)rO   rP   rQ   rR   r   rz   r<   rf   r]   r™   rš   r›   rˆ   rœ   r   r   r   r   ru   o  s   
		ru   N)Ú	functoolsr   Z	xml.etreer   Znltk.corpus.reader.apiZnltk.corpus.reader.utilZnltk.internalsr   Z	nltk.treer   r   r   rK   r{   r~   r�   ru   r   r   r   r   Ú<module>	   s     