a
     ˜ëjÇ`  ã                   @   s@   d Z ddlZddlZddlmZ ddlmZ G dd„ deƒZdS )z‰
An NLTK interface to the VerbNet verb lexicon

For details about VerbNet see:
https://verbs.colorado.edu/~mpalmer/projects/verbnet.html
é    N)Údefaultdict)ÚXMLCorpusReaderc                   @   s*  e Zd ZdZd@dd„Ze d¡Ze d¡Ze d¡Z	dAd	d
„Z
dBdd„ZdCdd„Zdd„ ZdDdd„Z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#d$„ Zd%d&„ Zd'd(„ Zd)d*„ Zd+d,„ ZdEd.d/„ZdFd0d1„ZdGd2d3„ZdHd4d5„ZdId6d7„Z dJd8d9„Z!dKd:d;„Z"dLd<d=„Z#dMd>d?„Z$dS )NÚVerbnetCorpusReadera  
    An NLTK interface to the VerbNet verb lexicon.

    From the VerbNet site: "VerbNet (VN) (Kipper-Schuler 2006) is the largest
    on-line verb lexicon currently available for English. It is a hierarchical
    domain-independent, broad-coverage verb lexicon with mappings to other
    lexical resources such as WordNet (Miller, 1990; Fellbaum, 1998), XTAG
    (XTAG Research Group, 2001), and FrameNet (Baker et al., 1998)."

    For details about VerbNet see:
    https://verbs.colorado.edu/~mpalmer/projects/verbnet.html
    Fc                 C   s<   t  | |||¡ ttƒ| _ttƒ| _i | _i | _|  ¡  d S )N)	r   Ú__init__r   ÚlistÚ_lemma_to_classÚ_wordnet_to_classÚ_class_to_fileidÚ_shortid_to_longidÚ_quick_index)ÚselfÚrootÚfileidsZ
wrap_etree© r   úY/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/nltk/corpus/reader/verbnet.pyr   %   s    

zVerbnetCorpusReader.__init__z([^\-\.]*)-([\d+.\-]+)$z
[\d+.\-]+$zH<MEMBER name="\??([^"]+)" wn="([^"]*)"[^>]+>|<VNSUBCLASS ID="([^"]+)"/?>Nc                 C   sB   |du rt | j ¡ ƒS t|tƒr*|  |¡}dd„ | d¡D ƒS dS )zx
        Return a list of all verb lemmas that appear in any class, or
        in the ``classid`` if specified.
        Nc                 S   s   g | ]}|  d ¡‘qS )Úname©Úget©Ú.0Úmemberr   r   r   Ú
<listcomp>S   ó    z.VerbnetCorpusReader.lemmas.<locals>.<listcomp>úMEMBERS/MEMBER)Úsortedr   ÚkeysÚ
isinstanceÚstrÚvnclassÚfindall©r   r   r   r   r   ÚlemmasH   s
    

zVerbnetCorpusReader.lemmasc                 C   sH   |du rt | j ¡ ƒS t|tƒr*|  |¡}tdd„ | d¡D ƒg ƒS dS )z|
        Return a list of all wordnet identifiers that appear in any
        class, or in ``classid`` if specified.
        Nc                 s   s   | ]}|  d d¡ ¡ V  qdS )ÚwnÚ N)r   Úsplitr   r   r   r   Ú	<genexpr>a   s   ÿz1VerbnetCorpusReader.wordnetids.<locals>.<genexpr>r   )r   r   r   r   r   r   Úsumr   r    r   r   r   Ú
wordnetidsU   s    

þûzVerbnetCorpusReader.wordnetidsc                    s|   ˆ dur ‡ fdd„| j  ¡ D ƒS |dur2| j| S |durD| j| S |durj|  |¡}dd„ | d¡D ƒS t| j  ¡ ƒS dS )a—  
        Return a list of the VerbNet class identifiers.  If a file
        identifier is specified, then return only the VerbNet class
        identifiers for classes (and subclasses) defined by that file.
        If a lemma is specified, then return only VerbNet class
        identifiers for classes that contain that lemma as a member.
        If a wordnetid is specified, then return only identifiers for
        classes that contain that wordnetid as a member.  If a classid
        is specified, then return only identifiers for subclasses of
        the specified VerbNet class.
        If nothing is specified, return all classids within VerbNet
        Nc                    s   g | ]\}}|ˆ kr|‘qS r   r   )r   ÚcÚf©Úfileidr   r   r   v   r   z0VerbnetCorpusReader.classids.<locals>.<listcomp>c                 S   s   g | ]}|  d ¡‘qS ©ÚIDr   ©r   Úsubclassr   r   r   r   }   s   ÿúSUBCLASSES/VNSUBCLASS)r	   Úitemsr   r   r   r   r   r   )r   ZlemmaZ	wordnetidr+   ÚclassidÚxmltreer   r*   r   Úclassidsh   s    


þzVerbnetCorpusReader.classidsc                 C   s–   || j v r|  |¡S |  |¡}|| jv r„| j|  |¡ }|  |¡}|| d¡krT|S | d¡D ]}|| d¡kr^|  S q^ds’J ‚ntd|› �ƒ‚dS )a·  Returns VerbNet class ElementTree

        Return an ElementTree containing the xml for the specified
        VerbNet class.

        :param fileid_or_classid: An identifier specifying which class
            should be returned.  Can be a file identifier (such as
            ``'put-9.1.xml'``), or a VerbNet class identifier (such as
            ``'put-9.1'``) or a short VerbNet class identifier (such as
            ``'9.1'``).
        r-   z.//VNSUBCLASSFzUnknown identifier N)Ú_fileidsÚxmlÚlongidr	   r   r   Ú
ValueError)r   Zfileid_or_classidr2   r+   Útreer/   r   r   r   r   „   s    






zVerbnetCorpusReader.vnclassc                    s@   |du rˆ j S t|tƒr*ˆ jˆ  |¡ gS ‡ fdd„|D ƒS dS )z¼
        Return a list of fileids that make up this corpus.  If
        ``vnclass_ids`` is specified, then return the fileids that make
        up the specified VerbNet class(es).
        Nc                    s   g | ]}ˆ j ˆ  |¡ ‘qS r   )r	   r7   )r   Z
vnclass_id©r   r   r   r   °   s   ÿz/VerbnetCorpusReader.fileids.<locals>.<listcomp>)r5   r   r   r	   r7   )r   Zvnclass_idsr   r:   r   r   ¥   s    

þzVerbnetCorpusReader.fileidsc              	   C   s\   t |tƒr|  |¡}g }| d¡}|D ]0}| |  |¡|  |¡|  |¡|  |¡dœ¡ q&|S )ap  Given a VerbNet class, this method returns VerbNet frames

        The members returned are:
        1) Example
        2) Description
        3) Syntax
        4) Semantics

        :param vnclass: A VerbNet class identifier; or an ElementTree
            containing the xml contents of a VerbNet class.
        :return: frames - a list of frame dictionaries
        zFRAMES/FRAME)ÚexampleÚdescriptionÚsyntaxÚ	semantics)	r   r   r   r   ÚappendÚ_get_example_within_frameÚ_get_description_within_frameÚ _get_syntactic_list_within_frameÚ_get_semantics_within_frame)r   r   ÚframesZvnframesÚvnframer   r   r   rD   µ   s    


üÿzVerbnetCorpusReader.framesc                 C   s,   t |tƒr|  |¡}dd„ | d¡D ƒ}|S )aA  Returns subclass ids, if any exist

        Given a VerbNet class, this method returns subclass ids (if they exist)
        in a list of strings.

        :param vnclass: A VerbNet class identifier; or an ElementTree
            containing the xml contents of a VerbNet class.
        :return: list of subclasses
        c                 S   s   g | ]}|  d ¡‘qS r,   r   r.   r   r   r   r   Þ   s   z2VerbnetCorpusReader.subclasses.<locals>.<listcomp>r0   )r   r   r   r   )r   r   Ú
subclassesr   r   r   rF   Ñ   s    


ÿzVerbnetCorpusReader.subclassesc                 C   sR   t |tƒr|  |¡}g }| d¡D ]*}| | d¡dd„ | d¡D ƒdœ¡ q"|S )ab  Returns thematic roles participating in a VerbNet class

        Members returned as part of roles are-
        1) Type
        2) Modifiers

        :param vnclass: A VerbNet class identifier; or an ElementTree
            containing the xml contents of a VerbNet class.
        :return: themroles: A list of thematic roles in the VerbNet class
        zTHEMROLES/THEMROLEÚtypec                 S   s"   g | ]}|  d ¡|  d¡dœ‘qS ©ÚValuerG   ©ÚvaluerG   r   ©r   Úrestrr   r   r   r   ö   s   ÿz1VerbnetCorpusReader.themroles.<locals>.<listcomp>úSELRESTRS/SELRESTR)rG   Ú	modifiers)r   r   r   r   r?   r   )r   r   Ú	themrolesZtroler   r   r   rP   ã   s    

þþÿ	zVerbnetCorpusReader.themrolesc                 C   s"   | j D ]}|  |  |¡|¡ qdS )aC  
        Initialize the indexes ``_lemma_to_class``,
        ``_wordnet_to_class``, and ``_class_to_fileid`` by scanning
        through the corpus fileids.  This is fast if ElementTree
        uses the C implementation (<0.1 secs), but quite slow (>10 secs)
        if only the python implementation is available.
        N)r5   Ú_index_helperr6   )r   r+   r   r   r   Ú_index  s    
zVerbnetCorpusReader._indexc                 C   s�   |  d¡}|| j|< || j|  |¡< | d¡D ]@}| j|  d¡  |¡ |  dd¡ ¡ D ]}| j|  |¡ qXq.| d¡D ]}|  	||¡ qzdS )zHelper for ``_index()``r-   r   r   r"   r#   r0   N)
r   r	   r
   Úshortidr   r   r?   r$   r   rQ   )r   r3   r+   r   r   r"   r/   r   r   r   rQ     s    

z!VerbnetCorpusReader._index_helperc              	   C   s  | j D ]ö}|dd… }|| j|< || j|  |¡< |  |¡�²}| j | ¡ ¡D ]�}| ¡ }|d durœ| j	|d   
|¡ |d  ¡ D ]}| j|  
|¡ q„qL|d durÐ|| j|d < |d }|| j|  |¡< qLdsLJ dƒ‚qLW d  ƒ q1 sò0    Y  qdS )aÄ  
        Initialize the indexes ``_lemma_to_class``,
        ``_wordnet_to_class``, and ``_class_to_fileid`` by scanning
        through the corpus fileids.  This doesn't do proper xml parsing,
        but is good enough to find everything in the standard VerbNet
        corpus -- and it runs about 30 times faster than xml parsing
        (with the python ElementTree; only 2-3 times faster
        if ElementTree uses the C implementation).
        Néüÿÿÿr   é   é   Fzunexpected match condition)r5   r	   r
   rS   ÚopenÚ	_INDEX_REÚfinditerÚreadÚgroupsr   r?   r$   r   )r   r+   r   ÚfpÚmr[   r"   r   r   r   r     s     

z VerbnetCorpusReader._quick_indexc              
   C   sl   | j  |¡r|S | j |¡s(td| ƒ‚z| j| W S  tyf } ztd| ƒ|‚W Y d}~n
d}~0 0 dS )zÔReturns longid of a VerbNet class

        Given a short VerbNet class identifier (eg '37.10'), map it
        to a long id (eg 'confess-37.10').  If ``shortid`` is already a
        long id, then return it as-isúvnclass identifier %r not foundN)Ú
_LONGID_REÚmatchÚ_SHORTID_REr8   r
   ÚKeyError)r   rS   Úer   r   r   r7   ;  s    zVerbnetCorpusReader.longidc                 C   s:   | j  |¡r|S | j |¡}|r*| d¡S td| ƒ‚dS )zÖReturns shortid of a VerbNet class

        Given a long VerbNet class identifier (eg 'confess-37.10'),
        map it to a short id (eg '37.10').  If ``longid`` is already a
        short id, then return it as-is.rV   r^   N)ra   r`   r_   Úgroupr8   )r   r7   r]   r   r   r   rS   J  s    
zVerbnetCorpusReader.shortidc                 C   sN   g }|  d¡D ]:}dd„ |  d¡D ƒ}| | d¡|| d¡dkdœ¡ q|S )	an  Returns semantics within a single frame

        A utility function to retrieve semantics within a frame in VerbNet
        Members of the semantics dictionary:
        1) Predicate value
        2) Arguments

        :param vnframe: An ElementTree containing the xml contents of
            a VerbNet frame.
        :return: semantics: semantics dictionary
        zSEMANTICS/PREDc                 S   s"   g | ]}|  d ¡|  d¡dœ‘qS )rG   rK   )rG   rK   r   )r   Úargr   r   r   r   j  s   ÿzCVerbnetCorpusReader._get_semantics_within_frame.<locals>.<listcomp>zARGS/ARGrK   Úboolú!)Úpredicate_valueÚ	argumentsÚnegated)r   r?   r   )r   rE   Zsemantics_within_single_frameÚpredri   r   r   r   rC   \  s    þýÿz/VerbnetCorpusReader._get_semantics_within_framec                 C   s"   |  d¡}|dur|j}nd}|S )a'  Returns example within a frame

        A utility function to retrieve an example within a frame in VerbNet.

        :param vnframe: An ElementTree containing the xml contents of
            a VerbNet frame.
        :return: example_text: The example sentence for this particular frame
        zEXAMPLES/EXAMPLENr#   )ÚfindÚtext)r   rE   Zexample_elementZexample_textr   r   r   r@   w  s
    	
z-VerbnetCorpusReader._get_example_within_framec                 C   s"   |  d¡}|jd | dd¡dœS )ab  Returns member description within frame

        A utility function to retrieve a description of participating members
        within a frame in VerbNet.

        :param vnframe: An ElementTree containing the xml contents of
            a VerbNet frame.
        :return: description: a description dictionary with members - primary and secondary
        ZDESCRIPTIONÚprimaryÚ	secondaryr#   )rn   ro   )rl   Úattribr   )r   rE   Zdescription_elementr   r   r   rA   ‡  s    


þz1VerbnetCorpusReader._get_description_within_framec                 C   s€   g }|  d¡D ]l}|j}tƒ }d|jv r2| d¡nd|d< dd„ | d¡D ƒ|d< dd„ | d	¡D ƒ|d
< | ||dœ¡ q|S )a[  Returns semantics within a frame

        A utility function to retrieve semantics within a frame in VerbNet.
        Members of the syntactic dictionary:
        1) POS Tag
        2) Modifiers

        :param vnframe: An ElementTree containing the xml contents of
            a VerbNet frame.
        :return: syntax_within_single_frame
        ZSYNTAXrK   r#   c                 S   s"   g | ]}|  d ¡|  d¡dœ‘qS rH   r   rL   r   r   r   r   ¨  s   ÿzHVerbnetCorpusReader._get_syntactic_list_within_frame.<locals>.<listcomp>rN   Ú	selrestrsc                 S   s"   g | ]}|  d ¡|  d¡dœ‘qS rH   r   rL   r   r   r   r   ¬  s   ÿzSYNRESTRS/SYNRESTRÚ	synrestrs)Úpos_tagrO   )rl   ÚtagÚdictrp   r   r   r?   )r   rE   Zsyntax_within_single_frameÚeltrs   rO   r   r   r   rB   —  s    þ
þ
ÿz4VerbnetCorpusReader._get_syntactic_list_within_framec                 C   sŠ   t |tƒr|  |¡}| d¡d }|| j|dd�d 7 }|| j|dd�d 7 }|d7 }|| j|dd�d 7 }|d7 }|| j|dd�7 }|S )a%  Returns pretty printed version of a VerbNet class

        Return a string containing a pretty-printed representation of
        the given VerbNet class.

        :param vnclass: A VerbNet class identifier; or an ElementTree
            containing the xml contents of a VerbNet class.
        r-   Ú
ú  ©Úindentz  Thematic roles:
ú    z
  Frames:
)r   r   r   r   Úpprint_subclassesÚpprint_membersÚpprint_themrolesÚpprint_frames)r   r   Úsr   r   r   Úpprint¹  s    	

zVerbnetCorpusReader.pprintr#   c                 C   sL   t |tƒr|  |¡}|  |¡}|s(dg}dd |¡ }tj|d||d d�S )a>  Returns pretty printed version of subclasses of VerbNet class

        Return a string containing a pretty-printed representation of
        the given VerbNet class's subclasses.

        :param vnclass: A VerbNet class identifier; or an ElementTree
            containing the xml contents of a VerbNet class.
        ú(none)zSubclasses: ú éF   rx   ©Úinitial_indentÚsubsequent_indent)r   r   r   rF   ÚjoinÚtextwrapÚfill)r   r   rz   rF   r€   r   r   r   r|   Î  s    	


ÿz%VerbnetCorpusReader.pprint_subclassesc                 C   sL   t |tƒr|  |¡}|  |¡}|s(dg}dd |¡ }tj|d||d d�S )a?  Returns pretty printed version of members in a VerbNet class

        Return a string containing a pretty-printed representation of
        the given VerbNet class's member verbs.

        :param vnclass: A VerbNet class identifier; or an ElementTree
            containing the xml contents of a VerbNet class.
        r‚   z	Members: rƒ   r„   rx   r…   )r   r   r   r!   rˆ   r‰   rŠ   )r   r   rz   Úmembersr€   r   r   r   r}   â  s    	


ÿz"VerbnetCorpusReader.pprint_membersc                 C   sx   t |tƒr|  |¡}g }|  |¡D ]J}|d | d¡ }dd„ |d D ƒ}|rb|d d |¡¡7 }| |¡ q"d |¡S )	aH  Returns pretty printed version of thematic roles in a VerbNet class

        Return a string containing a pretty-printed representation of
        the given VerbNet class's thematic roles.

        :param vnclass: A VerbNet class identifier; or an ElementTree
            containing the xml contents of a VerbNet class.
        ú* rG   c                 S   s   g | ]}|d  |d  ‘qS rJ   r   )r   Úmodifierr   r   r   r     s   ÿz8VerbnetCorpusReader.pprint_themroles.<locals>.<listcomp>rO   ú[{}]rƒ   rw   )r   r   r   rP   r   Úformatrˆ   r?   )r   r   rz   ÚpiecesZthemroleÚpiecerO   r   r   r   r~   ö  s    	

þz$VerbnetCorpusReader.pprint_themrolesc                 C   sD   t |tƒr|  |¡}g }|  |¡D ]}| |  ||¡¡ q"d |¡S )a?  Returns pretty version of all frames in a VerbNet class

        Return a string containing a pretty-printed representation of
        the list of frames within the VerbNet class.

        :param vnclass: A VerbNet class identifier; or an ElementTree
            containing the xml contents of a VerbNet class.
        rw   )r   r   r   rD   r?   Ú_pprint_single_framerˆ   )r   r   rz   r�   rE   r   r   r   r     s    	

z!VerbnetCorpusReader.pprint_framesc                 C   sd   |   ||¡d }||  ||d ¡d 7 }||  ||d ¡d 7 }||d 7 }||  ||d ¡7 }|S )a  Returns pretty printed version of a single frame in a VerbNet class

        Returns a string containing a pretty-printed representation of
        the given frame.

        :param vnframe: An ElementTree containing the xml contents of
            a VerbNet frame.
        rw   rƒ   z
  Syntax: z  Semantics:
r{   )Ú _pprint_description_within_frameÚ_pprint_example_within_frameÚ_pprint_syntax_within_frameÚ_pprint_semantics_within_frame)r   rE   rz   Zframe_stringr   r   r   r’     s    	ÿz(VerbnetCorpusReader._pprint_single_framec                 C   s   |d r|d |d  S dS )a&  Returns pretty printed version of example within frame in a VerbNet class

        Return a string containing a pretty-printed representation of
        the given VerbNet frame example.

        :param vnframe: An ElementTree containing the xml contents of
            a Verbnet frame.
        r;   z
 Example: Nr   )r   rE   rz   r   r   r   r”   0  s    	z0VerbnetCorpusReader._pprint_example_within_framec                 C   s6   ||d d  }|d d r2|d  |d d ¡7 }|S )a  Returns pretty printed version of a VerbNet frame description

        Return a string containing a pretty-printed representation of
        the given VerbNet frame description.

        :param vnframe: An ElementTree containing the xml contents of
            a VerbNet frame.
        r<   rn   ro   z ({})©r�   )r   rE   rz   r<   r   r   r   r“   <  s    	z4VerbnetCorpusReader._pprint_description_within_framec                 C   sž   g }|d D ]‚}|d }g }d|d v rF|d d rF|  |d d ¡ |dd„ |d d |d d  D ƒ7 }|r„|d	 d
 |¡¡7 }|  |¡ q|d
 |¡ S )a&  Returns pretty printed version of syntax within a frame in a VerbNet class

        Return a string containing a pretty-printed representation of
        the given VerbNet frame syntax.

        :param vnframe: An ElementTree containing the xml contents of
            a VerbNet frame.
        r=   rs   rK   rO   c                 S   s    g | ]}d   |d |d ¡‘qS )z{}{}rK   rG   r—   rL   r   r   r   r   Y  s   ÿzCVerbnetCorpusReader._pprint_syntax_within_frame.<locals>.<listcomp>rq   rr   rŽ   rƒ   )r?   r�   rˆ   )r   rE   rz   r�   Úelementr‘   Zmodifier_listr   r   r   r•   J  s    	

ÿýz/VerbnetCorpusReader._pprint_syntax_within_framec              	      sl   g }|d D ]F}dd„ |d D ƒ}|  |d r2dnd› |d › d	d
 |¡› d�¡ qd ‡ fdd„|D ƒ¡S )a,  Returns a pretty printed version of semantics within frame in a VerbNet class

        Return a string containing a pretty-printed representation of
        the given VerbNet frame semantics.

        :param vnframe: An ElementTree containing the xml contents of
            a VerbNet frame.
        r>   c                 S   s   g | ]}|d  ‘qS )rK   r   )r   Úargumentr   r   r   r   q  r   zFVerbnetCorpusReader._pprint_semantics_within_frame.<locals>.<listcomp>ri   rj   õ   Â¬r#   rh   ú(z, ú)rw   c                 3   s   | ]}ˆ › d |› �V  qdS )rŒ   Nr   )r   r‘   ry   r   r   r%   u  r   zEVerbnetCorpusReader._pprint_semantics_within_frame.<locals>.<genexpr>)r?   rˆ   )r   rE   rz   r�   Ú	predicateri   r   ry   r   r–   f  s    	(ÿz2VerbnetCorpusReader._pprint_semantics_within_frame)F)N)N)NNNN)N)r#   )r#   )r#   )r#   )r#   )r#   )r#   )r#   )r#   )%Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚreÚcompiler_   ra   rX   r!   r'   r4   r   r   rD   rF   rP   rR   rQ   r   r7   rS   rC   r@   rA   rB   r�   r|   r}   r~   r   r’   r”   r“   r•   r–   r   r   r   r   r      sD   


ÿ


!
""







r   )r¡   r¢   r‰   Úcollectionsr   Znltk.corpus.reader.xmldocsr   r   r   r   r   r   Ú<module>   s
   