a
     ˜ëjÖT  ã                   @   s†   d Z ddlZddlT ddlT ddlmZ ddlmZ ddlm	Z	m
Z
 G dd„ deƒZG d	d
„ d
ƒZG dd„ deƒZG dd„ deƒZdS )z!
Read CoNLL-style chunk fileids.
é    N)Ú*)Úmap_tag)ÚTree)ÚLazyConcatenationÚLazyMapc                   @   s,  e Zd ZdZdZdZdZdZdZdZ	dZ
eeeeee	e
fZd	d
ddded	d	fdd„Zd<dd„Zd=dd„Zd>dd„Zd?dd„Zd@dd„ZdAdd„ZdBdd„ZdCdd„ZdDd d!„ZdEd"d#„ZdFd$d%„ZdGd&d'„Zd(d)„ Zd*d+„ ZdHd,d-„ZdId.d/„ZdJd0d1„ZdKd2d3„Zd4d5„ Z d6d7„ Z!d8d9„ Z"e#d:d;„ ƒZ$d	S )LÚConllCorpusReaderaØ  
    A corpus reader for CoNLL-style files.  These files consist of a
    series of sentences, separated by blank lines.  Each sentence is
    encoded using a table (or "grid") of values, where each line
    corresponds to a single word, and each column corresponds to an
    annotation type.  The set of columns used by CoNLL-style files can
    vary from corpus to corpus; the ``ConllCorpusReader`` constructor
    therefore takes an argument, ``columntypes``, which is used to
    specify the columns that are used by a given corpus. By default
    columns are split by consecutive whitespaces, with the
    ``separator`` argument you can set a string to split by (e.g.
    ``'	'``).


    @todo: Add support for reading from corpora where different
        parallel files contain different columns.
    @todo: Possibly add caching of the grid corpus view?  This would
        allow the same grid view to be used by different data access
        methods (eg words() and parsed_sents() could both share the
        same grid corpus view object).
    @todo: Better support for -DOCSTART-.  Currently, we just ignore
        it, but it could be used to define methods that retrieve a
        document at a time (eg parsed_documents()).
    ÚwordsÚposÚtreeÚchunkÚneÚsrlÚignoreNÚSFTÚutf8c                 C   s‚   |D ]}|| j vrtd| ƒ‚qt|tƒr0|g}|| _dd„ t|ƒD ƒ| _|| _|| _|| _	|	| _
t | |||¡ |
| _|| _d S )NzBad column type %rc                 S   s   i | ]\}}||“qS © r   )Ú.0ÚiÚcr   r   úW/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/nltk/corpus/reader/conll.pyÚ
<dictcomp>W   ó    z.ConllCorpusReader.__init__.<locals>.<dictcomp>)ÚCOLUMN_TYPESÚ
ValueErrorÚ
isinstanceÚstrÚ_chunk_typesÚ	enumerateÚ_colmapÚ_pos_in_treeÚ_root_labelÚ_srl_includes_rolesetÚ_tree_classÚCorpusReaderÚ__init__Ú_tagsetÚsep)ÚselfÚrootÚfileidsÚcolumntypesÚchunk_typesZ
root_labelÚpos_in_treeZsrl_includes_rolesetÚencodingZ
tree_classÚtagsetÚ	separatorÚ
columntyper   r   r   r$   C   s    

zConllCorpusReader.__init__c                 C   s"   |   | j¡ tt| j|  |¡ƒƒS ©N)Ú_requireÚWORDSr   r   Ú
_get_wordsÚ_grids©r'   r)   r   r   r   r   d   s    zConllCorpusReader.wordsc                 C   s   |   | j¡ t| j|  |¡ƒS r1   )r2   r3   r   r4   r5   r6   r   r   r   Úsentsh   s    zConllCorpusReader.sentsc                    s2   ˆ   ˆ jˆ j¡ ‡ ‡fdd„}tt|ˆ  |¡ƒƒS )Nc                    s   ˆ   | ˆ¡S r1   ©Ú_get_tagged_words©Úgrid©r'   r.   r   r   Úget_tagged_wordso   s    z8ConllCorpusReader.tagged_words.<locals>.get_tagged_words)r2   r3   ÚPOSr   r   r5   ©r'   r)   r.   r=   r   r<   r   Útagged_wordsl   s    zConllCorpusReader.tagged_wordsc                    s.   ˆ   ˆ jˆ j¡ ‡ ‡fdd„}t|ˆ  |¡ƒS )Nc                    s   ˆ   | ˆ¡S r1   r8   r:   r<   r   r   r=   w   s    z8ConllCorpusReader.tagged_sents.<locals>.get_tagged_words)r2   r3   r>   r   r5   r?   r   r<   r   Útagged_sentst   s    zConllCorpusReader.tagged_sentsc                    sF   ˆ  ˆjˆjˆj¡ ˆ d u r"ˆj‰ ‡ ‡‡fdd„}tt|ˆ |¡ƒƒS )Nc                    s   ˆ  | ˆ ˆ¡S r1   ©Ú_get_chunked_wordsr:   ©r+   r'   r.   r   r   Úget_chunked_words�   s    z:ConllCorpusReader.chunked_words.<locals>.get_chunked_words)r2   r3   r>   ÚCHUNKr   r   r   r5   ©r'   r)   r+   r.   rE   r   rD   r   Úchunked_words|   s
    zConllCorpusReader.chunked_wordsc                    sB   ˆ  ˆjˆjˆj¡ ˆ d u r"ˆj‰ ‡ ‡‡fdd„}t|ˆ |¡ƒS )Nc                    s   ˆ  | ˆ ˆ¡S r1   rB   r:   rD   r   r   rE   ‹   s    z:ConllCorpusReader.chunked_sents.<locals>.get_chunked_words)r2   r3   r>   rF   r   r   r5   rG   r   rD   r   Úchunked_sents†   s
    zConllCorpusReader.chunked_sentsc                    sB   ˆ  ˆjˆjˆj¡ ˆ d u r"ˆj‰ ‡ ‡‡fdd„}t|ˆ |¡ƒS )Nc                    s   ˆ  | ˆ ˆ¡S r1   )Ú_get_parsed_sentr:   ©r,   r'   r.   r   r   Úget_parsed_sent•   s    z7ConllCorpusReader.parsed_sents.<locals>.get_parsed_sent)r2   r3   r>   ÚTREEr   r   r5   )r'   r)   r,   r.   rL   r   rK   r   Úparsed_sents�   s
    zConllCorpusReader.parsed_sentsc                 C   s   |   | j¡ t| j|  |¡ƒS r1   )r2   ÚSRLr   Ú_get_srl_spansr5   r6   r   r   r   Ú	srl_spansš   s    zConllCorpusReader.srl_spansc                    sT   ˆ  ˆjˆjˆjˆj¡ ˆ d u r&ˆj‰ ‡ ‡fdd„}t|ˆ |¡ƒ}|rPt|ƒ}|S )Nc                    s   ˆ  | ˆ ¡S r1   )Ú_get_srl_instancesr:   ©r,   r'   r   r   Úget_srl_instances£   s    z:ConllCorpusReader.srl_instances.<locals>.get_srl_instances)	r2   r3   r>   rM   rO   r   r   r5   r   )r'   r)   r,   ÚflattenrT   Úresultr   rS   r   Úsrl_instancesž   s    zConllCorpusReader.srl_instancesc                    s6   ˆ   ˆ jˆ jˆ j¡ ‡ ‡fdd„}tt|ˆ  |¡ƒƒS )zÄ
        :return: a list of word/tag/IOB tuples
        :rtype: list(tuple)
        :param fileids: the list of fileids that make up this corpus
        :type fileids: None or str or list
        c                    s   ˆ   | ˆ¡S r1   ©Ú_get_iob_wordsr:   r<   r   r   Úget_iob_words´   s    z2ConllCorpusReader.iob_words.<locals>.get_iob_words)r2   r3   r>   rF   r   r   r5   ©r'   r)   r.   rZ   r   r<   r   Ú	iob_words«   s    zConllCorpusReader.iob_wordsc                    s2   ˆ   ˆ jˆ jˆ j¡ ‡ ‡fdd„}t|ˆ  |¡ƒS )zÌ
        :return: a list of lists of word/tag/IOB tuples
        :rtype: list(list)
        :param fileids: the list of fileids that make up this corpus
        :type fileids: None or str or list
        c                    s   ˆ   | ˆ¡S r1   rX   r:   r<   r   r   rZ   Â   s    z2ConllCorpusReader.iob_sents.<locals>.get_iob_words)r2   r3   r>   rF   r   r5   r[   r   r<   r   Ú	iob_sents¹   s    zConllCorpusReader.iob_sentsc                    s   t ‡ fdd„ˆ  |d¡D ƒƒS )Nc                    s    g | ]\}}t |ˆ j|d �‘qS ))r-   )ZStreamBackedCorpusViewÚ_read_grid_block)r   ZfileidÚenc©r'   r   r   Ú
<listcomp>Ð   s   ÿz,ConllCorpusReader._grids.<locals>.<listcomp>T)ÚconcatZabspathsr6   r   r`   r   r5   Ë   s
    

þÿzConllCorpusReader._gridsc                    s�   g }t |ƒD ]~}| ¡ }|sq‡ fdd„| d¡D ƒ}|d ˆ j dd¡ dkrV|d= |D ]$}t|ƒt|d ƒkrZtd| ƒ‚qZ| |¡ q|S )Nc                    s   g | ]}|  ˆ j¡‘qS r   )Úsplitr&   )r   Úliner`   r   r   ra   Ý   r   z6ConllCorpusReader._read_grid_block.<locals>.<listcomp>Ú
r   r   z
-DOCSTART-z"Inconsistent number of columns:
%s)Zread_blankline_blockÚstriprc   r   ÚgetÚlenr   Úappend)r'   ÚstreamZgridsÚblockr;   Úrowr   r`   r   r^   Ö   s    z"ConllCorpusReader._read_grid_blockc                 C   s   |   || jd ¡S )Nr   )Ú_get_columnr   )r'   r;   r   r   r   r4   ñ   s    zConllCorpusReader._get_wordsc                    sP   ˆ   |ˆ jd ¡}ˆr4ˆˆ jkr4‡ ‡fdd„|D ƒ}ttˆ   |ˆ jd ¡|ƒƒS )Nr	   c                    s   g | ]}t ˆ jˆ|ƒ‘qS r   ©r   r%   ©r   Útr<   r   r   ra   ÷   r   z7ConllCorpusReader._get_tagged_words.<locals>.<listcomp>r   ©rm   r   r%   ÚlistÚzip©r'   r;   r.   Úpos_tagsr   r<   r   r9   ô   s    z#ConllCorpusReader._get_tagged_wordsc              	      s`   ˆ   |ˆ jd ¡}ˆr4ˆˆ jkr4‡ ‡fdd„|D ƒ}ttˆ   |ˆ jd ¡|ˆ   |ˆ jd ¡ƒƒS )Nr	   c                    s   g | ]}t ˆ jˆ|ƒ‘qS r   rn   ro   r<   r   r   ra   ý   r   z4ConllCorpusReader._get_iob_words.<locals>.<listcomp>r   r   rq   rt   r   r<   r   rY   ú   s    ýÿz ConllCorpusReader._get_iob_wordsc                    s0  ˆ   |ˆ jd ¡}ˆ   |ˆ jd ¡}ˆrFˆˆ jkrF‡ ‡fdd„|D ƒ}ˆ   |ˆ jd ¡}tˆ jg ƒg}t|||ƒD ]´\}}	}
|
dkrŽd\}}n|
 d¡\}}|d ur°||vr°d}|d	krÌ||d
  ¡ krÌd}|dv rèt|ƒdkrè| 	¡  |dk�rt|g ƒ}|d
  
|¡ | 
|¡ |d
  
||	f¡ qr|d S )Nr   r	   c                    s   g | ]}t ˆ jˆ|ƒ‘qS r   rn   ro   r<   r   r   ra     r   z8ConllCorpusReader._get_chunked_words.<locals>.<listcomp>r   ÚO)rv   Ú ú-ÚIéÿÿÿÿÚBZBOé   r   )rm   r   r%   r   r    rs   rc   Úlabelrh   Úpopri   )r'   r;   r+   r.   r   ru   Z
chunk_tagsÚstackÚwordÚpos_tagZ	chunk_tagÚstateZ
chunk_typeZ	new_chunkr   r<   r   rC     s,    



z$ConllCorpusReader._get_chunked_wordsc              
      s�  ˆ   |ˆ jd ¡}ˆ   |ˆ jd ¡}ˆrFˆˆ jkrF‡ ‡fdd„|D ƒ}ˆ   |ˆ jd ¡}d}t|||ƒD ]t\}}	}
|dkr~d}|d	krŠd
}|	dkr–d}	|	d	kr¢d
}	|
 d¡\}}| d	¡d	 }||› d|	› d|› d|› �7 }qhzˆ j |¡}W n4 tt	f�y"   ˆ j dˆ j
› d|› d	�¡}Y n0 |�sŒ| ¡ D ]X}t|ƒD ]H\}}t|tƒ�r>t|ƒdk�r>t|d tƒ�r>|d | ¡ f||< �q>�q2|S )Nr   r	   c                    s   g | ]}t ˆ jˆ|ƒ‘qS r   rn   ro   r<   r   r   ra   ,  r   z6ConllCorpusReader._get_parsed_sent.<locals>.<listcomp>r
   rw   ú(z-LRB-ú)z-RRB-r   z (ú z) é   r   )rm   r   r%   rs   rc   Úcountr"   Ú
fromstringr   Ú
IndexErrorr    Zsubtreesr   r   r   rh   r   r}   )r'   r;   r,   r.   r   ru   Z
parse_tagsZtreestrr€   r�   Ú	parse_tagÚleftÚrightr
   Zsubtreer   Úchildr   r<   r   rJ   (  sB     "ÿ
þýz"ConllCorpusReader._get_parsed_sentc                 C   s  | j r,|  || jd d ¡}| jd d }n |  || jd ¡}| jd d }tdd„ |D ƒƒ}g }t|ƒD ]ž}|  ||| ¡}g }g }	t|ƒD ]n\}
}| d¡\}}| d¡D ]}|r®|	 ||
f¡ q®t| d¡ƒD ]&}|	 	¡ \}}| ||
d f|f¡ qÔqŽ| |¡ qj|S )	z;
        list of list of (start, end), tag) tuples
        r   r†   r|   c                 S   s   g | ]}|d kr|‘qS )rx   r   )r   Úpr   r   r   ra   Z  r   z4ConllCorpusReader._get_srl_spans.<locals>.<listcomp>r   rƒ   r„   )
r!   rm   r   rh   Úranger   rc   ri   r‡   r~   )r'   r;   Ú
predicatesZ	start_colZ	num_predsÚ	spanlistsr   ÚcolÚspanlistr   ÚwordnumZsrl_tagr‹   rŒ   ÚtagÚstartr   r   r   rP   M  s*    z ConllCorpusReader._get_srl_spansc              	   C   sð   |   ||¡}|  |¡}| jrF|  || jd d ¡}|  || jd ¡}n |  || jd ¡}d gt|ƒ }t|ƒ}t|ƒD ]t\}}	|	dkrˆqv|D ]6}
|
D ]&\\}}}|t||ƒv r”|dv r” q¾q”qŒ qÐqŒt	d|	 ƒ‚| 
t|||	|| |
ƒ¡ qv|S )Nr   r†   rx   ©ÚVzC-VzNo srl column found for %r)rJ   rP   r!   rm   r   rh   ÚConllSRLInstanceListr   r�   r   ri   ÚConllSRLInstance)r'   r;   r,   r
   r‘   r�   ZrolesetsÚ	instancesr”   Ú	predicater“   r–   Úendr•   r   r   r   rR   m  s,    
ÿz$ConllCorpusReader._get_srl_instancesc                 G   s$   |D ]}|| j vrtd| ƒ‚qd S )Nz)This corpus does not contain a %s column.)r   r   )r'   r*   r0   r   r   r   r2   ‘  s
    
ÿzConllCorpusReader._requirec                    s   ‡ ‡fdd„t tˆƒƒD ƒS )Nc                    s   g | ]}ˆ| ˆ  ‘qS r   r   ©r   r   ©Úcolumn_indexr;   r   r   ra   š  r   z1ConllCorpusReader._get_column.<locals>.<listcomp>)r�   rh   )r;   r    r   rŸ   r   rm   ˜  s    zConllCorpusReader._get_column)N)N)NN)NN)NNN)NNN)NNN)N)NNT)NN)NN)N)N)N)N)N)%Ú__name__Ú
__module__Ú__qualname__Ú__doc__r3   r>   rM   rF   ZNErO   ZIGNOREr   r   r$   r   r7   r@   rA   rH   rI   rN   rQ   rW   r\   r]   r5   r^   r4   r9   rY   rC   rJ   rP   rR   r2   Ústaticmethodrm   r   r   r   r   r      sR   ô
!

















"
% $r   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	rš   z|
    An SRL instance from a CoNLL corpus, which identifies and
    providing labels for the arguments of a single verb.
    c           	      C   s€   g | _ || _|| _|| _g | _|| _|| _| ¡ | _|D ]B\\}}}|dv rf|  j t	t
||ƒƒ7  _ q8| j ||f|f¡ q8d S )Nr—   )ÚverbÚ	verb_headÚ	verb_stemÚrolesetÚ	argumentsÚtagged_spansr
   Úleavesr   rr   r�   ri   )	r'   r
   r§   r¨   r©   r«   r–   r�   r•   r   r   r   r$   ¥  s    
zConllSRLInstance.__init__c                 C   s,   t | jƒdkrdnd}d| jt | jƒ|f S )Nr†   Úsrw   z,<ConllSRLInstance for %r with %d argument%s>)rh   rª   r¨   )r'   Úpluralr   r   r   Ú__repr__Ï  s    ÿzConllSRLInstance.__repr__c           	         sÊ   d  ‡ fdd„ˆ jD ƒ¡}d|›dˆ j›d�}d}tˆ jƒD ]p\}}t|tƒrV|d }ˆ jD ]0\\}}}||kr||d	| 7 }||kr\|d
7 }q\|ˆ jv r d| }||d 7 }q<|tj	| 
dd¡ddd� S )Nr…   c                 3   s   | ]}ˆ j | d  V  qdS )r   N)r   rž   r`   r   r   Ú	<genexpr>Ø  r   z*ConllSRLInstance.pprint.<locals>.<genexpr>zSRL for z (stem=z):
rw   r   z[%s z] z<<%s>>z ]ú]z    )Úinitial_indentÚsubsequent_indent)Újoinr¦   r¨   r   r   r   Útuplerª   ÚtextwrapÚfillÚreplace)	r'   ZverbstrÚhdrr­   r   r€   r–   r�   Úargidr   r`   r   Úpprint×  s"    


ÿzConllSRLInstance.pprintN)r¡   r¢   r£   r¤   r$   r¯   r»   r   r   r   r   rš   �  s   *rš   c                   @   s4   e Zd ZdZddd„Zdd„ Zddd	„Zd
d„ ZdS )r™   z0
    Set of instances for a single sentence
    r   c                 C   s   || _ t | |¡ d S r1   )r
   rr   r$   )r'   r
   r›   r   r   r   r$   ð  s    zConllSRLInstanceList.__init__c                 C   s   |   ¡ S r1   )r»   r`   r   r   r   Ú__str__ô  s    zConllSRLInstanceList.__str__Fc                 C   sH  | D ]}|j | j krtdƒ‚q|r\| j  ¡ }d gt|ƒ }dgt|ƒ }|  | j d|||¡ d}tt|ƒƒD ]Ö}|r®|d||  7 }|d||  7 }|dt||  d¡ƒ 7 }| D ] }||jkr²|d|j	 7 } qàq²|dd 7 }| D ]T}d}|j
D ]8\\}	}
}||	k�rd	|› |› �}||
d
 krò|d7 }qò|d| 7 }qä|d7 }ql|S )NzTree mismatch!r   r   rw   z%-20s z%-8s z
%15s*%-8s rx   rƒ   r†   r„   z%-12s re   )r
   r   r¬   rh   Ú_tree2conllr�   rµ   rc   r§   r¨   r«   )r'   Zinclude_treeÚinstr   r	   Úsyntr­   r   Zargstrr–   r�   rº   r   r   r   r»   ÷  s:    





zConllSRLInstanceList.pprintc                 C   sì   t |tƒsJ ‚t|ƒdkrPt |d tƒrP| ¡ ||< || |d ksHJ ‚|d S t|ƒdkršt |d tƒršt|d ƒdks~J ‚|d \||< ||< |d S d| ¡ › || › �||< |D ]}|  |||||¡}q¸||d   d7  < |S d S )Nr†   r   r|   rƒ   r„   )r   r   rh   r   r}   rµ   r½   )r'   r
   r”   r   r	   r¿   r�   r   r   r   r½     s    z ConllSRLInstanceList._tree2conllN)r   )F)r¡   r¢   r£   r¤   r$   r¼   r»   r½   r   r   r   r   r™   ë  s
   

(r™   c                   @   s   e Zd ZdZddd„ZdS )ÚConllChunkCorpusReaderz`
    A ConllCorpusReader whose data file contains three columns: words,
    pos, and chunk.
    r   Nc              
   C   s   t j| ||d||||d� d S )N)r   r	   r   )r+   r-   r.   r/   )r   r$   )r'   r(   r)   r+   r-   r.   r/   r   r   r   r$   7  s    øzConllChunkCorpusReader.__init__)r   NN)r¡   r¢   r£   r¤   r$   r   r   r   r   rÀ   1  s    ÿrÀ   )r¤   r¶   Znltk.corpus.reader.apiZnltk.corpus.reader.utilZnltk.tagr   Z	nltk.treer   Z	nltk.utilr   r   r#   r   rš   rr   r™   rÀ   r   r   r   r   Ú<module>	   s      
NF