a
     ˜ëjä=  ã                   @   s¦   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mZ dd„ Z	G dd„ deƒZ
G dd	„ d	eƒZG d
d„ dƒZG dd„ deƒZG dd„ deƒZG dd„ deƒZdS )é    N©Úconcat)ÚXMLCorpusReaderÚXMLCorpusViewc                    s   t  ˆ ¡d‡ fdd„	ƒ}|S )zj
    Wraps function arguments:
    if fileids not specified then function set NKJPCorpusReader paths.
    Nc                    s   |s
| j }ˆ | |fi |¤ŽS ©N)Ú_paths©ÚselfÚfileidsÚkwargs©Úfun© úV/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/nltk/corpus/reader/nkjp.pyÚ	decorator   s    z_parse_args.<locals>.decorator)N)Ú	functoolsÚwraps)r   r   r   r   r   Ú_parse_args   s    r   c                   @   sŽ   e Zd ZdZdZdZdZddd„Zdd	„ Zd
d„ Z	ddd„Z
dd„ Zeddd„ƒZeddd„ƒZeddd„ƒZed dd„ƒZed!dd„ƒZdS )"ÚNKJPCorpusReaderr   é   é   é   ú.*c                 C   sD   t |tƒrt | ||d ¡ nt | |dd„ |D ƒ¡ |  ¡ | _dS )aN  
        Corpus reader designed to work with National Corpus of Polish.
        See http://nkjp.pl/ for more details about NKJP.
        use example:
        import nltk
        import nkjp
        from nkjp import NKJPCorpusReader
        x = NKJPCorpusReader(root='/home/USER/nltk_data/corpora/nkjp/', fileids='') # obtain the whole corpus
        x.header()
        x.raw()
        x.words()
        x.tagged_words(tags=['subst', 'comp'])  #Link to find more tags: nkjp.pl/poliqarp/help/ense2.html
        x.sents()
        x = NKJPCorpusReader(root='/home/USER/nltk_data/corpora/nkjp/', fileids='Wilk*') # obtain particular file(s)
        x.header(fileids=['WilkDom', '/home/USER/nltk_data/corpora/nkjp/WilkWilczy'])
        x.tagged_words(fileids=['WilkDom', '/home/USER/nltk_data/corpora/nkjp/WilkWilczy'], tags=['subst', 'comp'])
        z.*/header.xmlc                 S   s   g | ]}|d  ‘qS )z/header.xmlr   ©Ú.0Úfileidr   r   r   Ú
<listcomp><   ó    z-NKJPCorpusReader.__init__.<locals>.<listcomp>N)Ú
isinstanceÚstrr   Ú__init__Ú	get_pathsr   )r	   Úrootr
   r   r   r   r    &   s    
ÿzNKJPCorpusReader.__init__c                    s   ‡ fdd„ˆ j D ƒS )Nc                    s*   g | ]"}t j tˆ jƒ| d ¡d ¡‘qS ©ú
header.xmlr   )ÚosÚpathÚjoinr   Ú_rootÚsplit©r   Úf©r	   r   r   r   A   s   ÿz.NKJPCorpusReader.get_paths.<locals>.<listcomp>©Z_fileidsr,   r   r,   r   r!   @   s    
þzNKJPCorpusReader.get_pathsc                 C   s   dd„ | j D ƒS )zf
        Returns a list of file identifiers for the fileids that make up
        this corpus.
        c                 S   s   g | ]}|  d ¡d ‘qS r#   ©r)   r*   r   r   r   r   K   r   z,NKJPCorpusReader.fileids.<locals>.<listcomp>r-   r,   r   r   r   r
   F   s    zNKJPCorpusReader.fileidsNc                 K   sv   |  dtj¡}|tju r$t||d�S |tju r:t||d�S |tju rPt||d�S |tju rjt	||t	jd�S t
dƒ‚dS )zQ
        Returns a view specialised for use with particular corpus file.
        Úmode)Útags)r0   r/   zNo such mode!N)Úpopr   Ú
WORDS_MODEÚNKJPCorpus_Morph_ViewÚ
SENTS_MODEÚNKJPCorpus_Segmentation_ViewÚHEADER_MODEÚNKJPCorpus_Header_ViewÚRAW_MODEÚNKJPCorpus_Text_ViewÚ	NameError)r	   Úfilenamer0   r   r/   r   r   r   Ú_viewM   s    



ÿzNKJPCorpusReader._viewc                 C   s   | j |v r|S | j | S )z<
        Add root if necessary to specified fileid.
        )r"   )r	   r   r   r   r   Úadd_root`   s    
zNKJPCorpusReader.add_rootc                    s   t ‡ ‡fdd„|D ƒƒS )z9
        Returns header(s) of specified fileids.
        c                    s.   g | ]&}ˆj ˆ |¡fd tjiˆ ¤Ž ¡ ‘qS ©r/   )r<   r=   r   r6   Úhandle_queryr   ©r   r	   r   r   r   n   s   ýÿÿÿz+NKJPCorpusReader.header.<locals>.<listcomp>r   r   r   r@   r   Úheaderh   s
    üÿzNKJPCorpusReader.headerc                    s   t ‡ ‡fdd„|D ƒƒS )z9
        Returns sentences in specified fileids.
        c                    s.   g | ]&}ˆj ˆ |¡fd tjiˆ ¤Ž ¡ ‘qS r>   )r<   r=   r   r4   r?   r   r@   r   r   r   |   s   ýÿÿÿz*NKJPCorpusReader.sents.<locals>.<listcomp>r   r   r   r@   r   Úsentsv   s
    üÿzNKJPCorpusReader.sentsc                    s   t ‡ ‡fdd„|D ƒƒS )ú5
        Returns words in specified fileids.
        c                    s.   g | ]&}ˆj ˆ |¡fd tjiˆ ¤Ž ¡ ‘qS r>   ©r<   r=   r   r2   r?   r   r@   r   r   r   ‹   s   ýÿÿÿz*NKJPCorpusReader.words.<locals>.<listcomp>r   r   r   r@   r   Úwords„   s
    üÿzNKJPCorpusReader.wordsc                    s&   ˆ   dg ¡‰t‡ ‡‡fdd„|D ƒƒS )z…
        Call with specified tags as a list, e.g. tags=['subst', 'comp'].
        Returns tagged words in specified fileids.
        r0   c                    s0   g | ](}ˆj ˆ |¡ftjˆd œˆ ¤Ž ¡ ‘qS ))r/   r0   rD   r   ©r   r	   r0   r   r   r   ›   s   úÿýüz1NKJPCorpusReader.tagged_words.<locals>.<listcomp>)r1   r   r   r   rF   r   Útagged_words“   s    ùÿzNKJPCorpusReader.tagged_wordsc                    s   t ‡ ‡fdd„|D ƒƒS )rC   c                    s.   g | ]&}ˆj ˆ |¡fd tjiˆ ¤Ž ¡ ‘qS r>   )r<   r=   r   r8   r?   r   r@   r   r   r   ¬   s   ýÿÿÿz(NKJPCorpusReader.raw.<locals>.<listcomp>r   r   r   r@   r   Úraw¦   s
    üÿzNKJPCorpusReader.raw)r   )N)N)N)N)N)N)Ú__name__Ú
__module__Ú__qualname__r2   r4   r6   r8   r    r!   r
   r<   r=   r   rA   rB   rE   rG   rH   r   r   r   r   r       s&   

r   c                   @   s$   e Zd Zdd„ Zdd„ Zdd„ ZdS )r7   c                 K   s   d| _ t | |d | j ¡ dS )zƒ
        HEADER_MODE
        A stream backed corpus view specialized for use with
        header.xml files in NKJP corpus.
        z.*/sourceDesc$r$   N)Útagspecr   r    ©r	   r;   r   r   r   r   r    ¶   s    zNKJPCorpus_Header_View.__init__c                 C   s@   |   ¡  g }t | | j¡}t|ƒdkr(q4| |¡ q|  ¡  |S ©Nr   )Ú_openr   Ú
read_blockÚ_streamÚlenÚextendÚclose)r	   rA   Úsegmr   r   r   r?   ¿   s    z#NKJPCorpus_Header_View.handle_queryc                 C   sö   |  d¡}g }|r&d dd„ |D ƒ¡}|  d¡}g }|rLd dd„ |D ƒ¡}|  d¡}g }|rrd dd„ |D ƒ¡}|  d	¡}	g }
|	r˜d d
d„ |	D ƒ¡}
|  d¡}g }|r¾d dd„ |D ƒ¡}|  d¡}g }|räd dd„ |D ƒ¡}||||
||dœS )Nz
bibl/titleÚ
c                 s   s   | ]}|j  ¡ V  qd S r   ©ÚtextÚstrip)r   Útitler   r   r   Ú	<genexpr>Î   r   z4NKJPCorpus_Header_View.handle_elt.<locals>.<genexpr>zbibl/authorc                 s   s   | ]}|j  ¡ V  qd S r   rW   )r   Úauthorr   r   r   r[   Ó   r   z	bibl/datec                 s   s   | ]}|j  ¡ V  qd S r   rW   )r   Údater   r   r   r[   Ø   r   zbibl/publisherc                 s   s   | ]}|j  ¡ V  qd S r   rW   )r   Ú	publisherr   r   r   r[   Ý   r   z	bibl/idnoc                 s   s   | ]}|j  ¡ V  qd S r   rW   )r   Úidnor   r   r   r[   â   r   z	bibl/notec                 s   s   | ]}|j  ¡ V  qd S r   rW   )r   Únoter   r   r   r[   ç   r   )rZ   r\   r]   r^   r_   r`   )Úfindallr'   )r	   ÚeltÚcontextÚtitlesrZ   Zauthorsr\   Údatesr]   Z
publishersr^   Zidnosr_   Znotesr`   r   r   r   Ú
handle_eltÊ   s>    





úz!NKJPCorpus_Header_View.handle_eltN)rI   rJ   rK   r    r?   rf   r   r   r   r   r7   µ   s   	r7   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚXML_Toola  
    Helper class creating xml file to one without references to nkjp: namespace.
    That's needed because the XMLCorpusView assumes that one can find short substrings
    of XML that are valid XML, which is not true if a namespace is declared at top level
    c                 C   s"   t j ||¡| _tjdd�| _d S )NF)Údelete)r%   r&   r'   Ú	read_fileÚtempfileÚNamedTemporaryFileÚ
write_file)r	   r"   r;   r   r   r   r    ú   s    zXML_Tool.__init__c              
   C   sð   z¸t | jƒ}| j}d}t|ƒr | ¡ }t d|¡}d |¡}t d|¡}d |¡}t d|¡}d |¡}t d|¡}d |¡}t d|¡}d |¡}| |¡ q| 	¡  | 	¡  | jj
W S  tyê } z|  ¡  t|‚W Y d }~n
d }~0 0 d S )Nú znkjp:[^ ]* z<nkjp:paren>z</nkjp:paren>z<choice>z	</choice>)Úopenri   rl   rR   ÚreadlineÚrer)   r'   ÚwriterT   ÚnameÚ	ExceptionÚremove_preprocessed_file)r	   ÚfrÚfwÚlineÚxÚretÚer   r   r   Úbuild_preprocessed_fileþ   s.    






z XML_Tool.build_preprocessed_filec                 C   s   t  | jj¡ d S r   )r%   Úremoverl   rr   r,   r   r   r   rt     s    z!XML_Tool.remove_preprocessed_fileN)rI   rJ   rK   Ú__doc__r    r{   rt   r   r   r   r   rg   ó   s   rg   c                   @   sP   e Zd Z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S )r5   zm
    A stream backed corpus view specialized for use with
    ann_segmentation.xml files in NKJP corpus.
    c                 K   sF   d| _ t|tjd�| _| j ¡  t|dƒ| _t | | j 	¡ | j ¡ d S )Nz.*p/.*sr>   zann_segmentation.xml)
rL   r9   r4   Ú	text_viewr?   rg   Úxml_toolr   r    r{   rM   r   r   r   r    !  s    ÿ
ÿz%NKJPCorpus_Segmentation_View.__init__c                 C   s   |  d¡d   d¡d S )Nú(r   ú,r   r.   )r	   Zexample_wordr   r   r   Úget_segm_id/  s    z(NKJPCorpus_Segmentation_View.get_segm_idc                 C   s   t | d¡d ƒS )Nr�   r   )Úintr)   )r	   Zbeg_wordr   r   r   Úget_sent_beg2  s    z)NKJPCorpus_Segmentation_View.get_sent_begc                 C   s,   |  d¡d   d¡}t|d ƒt|d ƒ S )Nú)r   r�   r   r   )r)   rƒ   )r	   Zend_wordZsplittedr   r   r   Úget_sent_end6  s    z)NKJPCorpus_Segmentation_View.get_sent_endc                 C   sJ   |   |d ¡}| jj| }|  |d ¡}|  |t|ƒd  ¡}|||… S )Nr   r   )r‚   r~   Ú	segm_dictr„   r†   rR   )r	   Ú	sent_segmÚidrU   ÚbegÚendr   r   r   Úget_sentences;  s
    z*NKJPCorpus_Segmentation_View.get_sentencesc                 C   sV   g }d}d}|D ]@}|   |¡}|  |¡|d ks8||krL| |¡ |  |¡}|}q|S )Néÿÿÿÿr   )r‚   r„   Úappendr†   )r	   rU   ry   Zprev_txt_endZprev_txt_nrÚwordZtxt_nrr   r   r   Úremove_choiceC  s    


z*NKJPCorpus_Segmentation_View.remove_choicec              
   C   s    zf|   ¡  g }t | | j¡}t|ƒdkr*qP|D ]}|  |¡}| |  |¡¡ q.q|  ¡  | j	 
¡  |W S  tyš } z| j	 
¡  t|‚W Y d }~n
d }~0 0 d S rN   )rO   r   rP   rQ   rR   r�   rŽ   rŒ   rT   r   rt   rs   )r	   Ú	sentencesrˆ   rU   rz   r   r   r   r?   Q  s    


z)NKJPCorpus_Segmentation_View.handle_queryc                 C   s"   g }|D ]}|  | d¡¡ q|S )NZcorresp)rŽ   Úget)r	   rb   rc   ry   Úsegr   r   r   rf   c  s    z'NKJPCorpus_Segmentation_View.handle_eltN)rI   rJ   rK   r}   r    r‚   r„   r†   rŒ   r�   r?   rf   r   r   r   r   r5     s   r5   c                   @   sB   e Zd ZdZdZdZdd„ Zdd„ Zdd	d
„Zdd„ Z	dd„ Z
dS )r9   za
    A stream backed corpus view specialized for use with
    text.xml files in NKJP corpus.
    r   r   c                 K   sB   |  dd¡| _d| _tƒ | _t|dƒ| _t | | j 	¡ | j¡ d S )Nr/   r   z	.*/div/abztext.xml)
r1   r/   rL   Údictr‡   rg   r   r   r    r{   rM   r   r   r   r    s  s    ÿzNKJPCorpus_Text_View.__init__c              
   C   sf   z,|   ¡  |  | j¡}|  ¡  | j ¡  |W S  ty` } z| j ¡  t|‚W Y d }~n
d }~0 0 d S r   )rO   rP   rQ   rT   r   rt   rs   )r	   rx   rz   r   r   r   r?   ~  s    

z!NKJPCorpus_Text_View.handle_queryNc                 C   sJ   g }t  | |¡}t|ƒdkrq4|D ]}| |¡ q"qd dd„ |D ƒ¡gS )z6
        Returns text as a list of sentences.
        r   rm   c                 S   s   g | ]}|‘qS r   r   )r   rU   r   r   r   r   •  r   z3NKJPCorpus_Text_View.read_block.<locals>.<listcomp>)r   rP   rR   rŽ   r'   )r	   ÚstreamrL   Zelt_handlerÚtxtrU   Úpartr   r   r   rP   ‰  s    zNKJPCorpus_Text_View.read_blockc                 C   s(   |j D ]}| d¡r| |¡  S qd S )Nr‰   )ÚattribÚendswithr’   )r	   rb   Úattrr   r   r   r‚   —  s    

z NKJPCorpus_Text_View.get_segm_idc                 C   s$   | j tju r|j| j|  |¡< |jS r   )r/   r9   r4   rX   r‡   r‚   )r	   rb   rc   r   r   r   rf   œ  s    zNKJPCorpus_Text_View.handle_elt)NN)rI   rJ   rK   r}   r4   r8   r    r?   rP   r‚   rf   r   r   r   r   r9   j  s   
r9   c                   @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	r3   zm
    A stream backed corpus view specialized for use with
    ann_morphosyntax.xml files in NKJP corpus.
    c                 K   s:   |  dd ¡| _d| _t|dƒ| _t | | j ¡ | j¡ d S )Nr0   z	.*/seg/fszann_morphosyntax.xml)r1   r0   rL   rg   r   r   r    r{   rM   r   r   r   r    ©  s    ÿzNKJPCorpus_Morph_View.__init__c              
   C   s˜   z^|   ¡  g }t | | j¡}t|ƒdkr*qH|D ]}|d ur.| |¡ q.q|  ¡  | j ¡  |W S  t	y’ } z| j ¡  t	|‚W Y d }~n
d }~0 0 d S rN   )
rO   r   rP   rQ   rR   rŽ   rT   r   rt   rs   )r	   rE   rU   r—   rz   r   r   r   r?   ±  s    

z"NKJPCorpus_Morph_View.handle_queryc           
      C   s&  d}d}d}| j d u rd}|D � ]ð}d| ¡ v rZ|jd dkrZ|D ]}|jdkrB|j}qBqd| ¡ v r|jd dkr|D ]–}d| ¡ v rx|jd d	krx|D ]r}d| ¡ v rš|jd d
krš|D ]N}	d|	 ¡ v rì| j d urì|	jd | j v rìd}q¼d|	 ¡ v r¼|	jd dkr¼d}q¼qšqxq|�r"|�r"|S d S )NÚ FTrr   ZorthÚstringZinterpsÚtypeÚlexZctagÚvalueZinterp)r0   Úkeysr˜   ÚtagrX   )
r	   rb   rc   r�   ÚflagZis_not_interpÚchildÚsymbolZsymbol2Zsymbol3r   r   r   rf   Ã  sB    




ÿþ
ÿþý
ÿþz NKJPCorpus_Morph_View.handle_eltN)rI   rJ   rK   r}   r    r?   rf   r   r   r   r   r3   £  s   r3   )r   r%   rp   rj   Znltk.corpus.reader.utilr   Znltk.corpus.reader.xmldocsr   r   r   r   r7   rg   r5   r9   r3   r   r   r   r   Ú<module>   s    >(O9