a
     ˜ëjÝ  ã                   @   sr   d dl Z d dlmZmZ d dlmZmZmZ d dlm	Z	 dd„ Z
G dd„ deƒZd	d
„ Zdd„ Zedkrneƒ  dS )é    N)ÚCorpusReaderÚSyntaxCorpusReader)ÚFileSystemPathPointerÚfind_corpus_fileidsÚread_blankline_block)ÚDependencyGraphc                 C   s   d  dd„ | D ƒ¡S )Nú/c                 s   s"   | ]}|d  dkr|d  V  qdS )r   ÚEOSN© ©Ú.0Úmr
   r
   úV/mnt/bigstor/www/usecases/.venv/lib/python3.9/site-packages/nltk/corpus/reader/knbc.pyÚ	<genexpr>   ó    z<lambda>.<locals>.<genexpr>©Újoin©Zmorphsr
   r
   r   Ú<lambda>   r   r   c                   @   s@   e Zd ZdZdefdd„Zdd„ Zdd„ Zdd
d„Zdd„ Z	d	S )ÚKNBCorpusReadera£  
    This class implements:
      - ``__init__``, which specifies the location of the corpus
        and a method for detecting the sentence blocks in corpus files.
      - ``_read_block``, which reads a block from the input stream.
      - ``_word``, which takes a block and returns a list of list of words.
      - ``_tag``, which takes a block and returns a list of list of tagged
        words.
      - ``_parse``, which takes a block and returns a list of parsed
        sentences.

    The structure of tagged words:
      tagged_word = (word(str), tags(tuple))
      tags = (surface, reading, lemma, pos1, posid1, pos2, posid2, pos3, posid3, others ...)

    Usage example

    >>> from nltk.corpus.util import LazyCorpusLoader
    >>> knbc = LazyCorpusLoader(
    ...     'knbc/corpus1',
    ...     KNBCorpusReader,
    ...     r'.*/KN.*',
    ...     encoding='euc-jp',
    ... )

    >>> len(knbc.sents()[0])
    9

    Úutf8c                 C   s   t  | |||¡ || _dS )z–
        Initialize KNBCorpusReader
        morphs2str is a function to convert morphlist to str for tree representation
        for _parse()
        N)r   Ú__init__Ú
morphs2str)ÚselfÚrootÚfileidsÚencodingr   r
   r
   r   r   7   s    zKNBCorpusReader.__init__c                 C   s   t |ƒS ©N)r   )r   Ústreamr
   r
   r   Ú_read_block@   s    zKNBCorpusReader._read_blockc                 C   s>   g }|  ¡ D ],}t d|¡s| ¡  d¡}| |d ¡ q|S )NúEOS|\*|\#|\+ú r   )Ú
splitlinesÚreÚmatchÚstripÚsplitÚappend)r   ÚtÚresÚlineÚcellsr
   r
   r   Ú_wordD   s    zKNBCorpusReader._wordNc              	   C   sP   g }|  ¡ D ]>}t d|¡s| ¡  d¡}| |d d |dd … ¡f¡ q|S )Nr    r!   r   é   )r"   r#   r$   r%   r&   r'   r   )r   r(   Ztagsetr)   r*   r+   r
   r
   r   Ú_tagO   s    "zKNBCorpusReader._tagc           
      C   s.  t ƒ }d}| ¡ D ]æ}|d dv r¬| ¡  dd¡}t d|d ¡}|d usNJ ‚|j| }| || d¡g dœ¡ t	| d¡ƒ}|d	krŽ||_
n|j| d
  |¡ |d7 }q|d dkr| ¡  d¡}|d d |dd … ¡f}	|j|d  d  |	¡ q| j�r&|j ¡ D ]}|  |d ¡|d< �q| ¡ S )Nr   z*+r!   é   z([\-0-9]*)([ADIP])r-   é   )ÚaddressÚrelÚwordéÿÿÿÿÚdepsú#r3   )r   r"   r%   r&   r#   r$   ÚnodesÚupdateÚgroupÚintr   r'   r   r   ÚvaluesÚtree)
r   r(   ZdgÚir*   r+   r   ÚnodeZ
dep_parentZmorphr
   r
   r   Ú_parseZ   s,    

zKNBCorpusReader._parse)N)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__Ú_morphs2str_defaultr   r   r,   r.   r?   r
   r
   r
   r   r      s   	
r   c                  C   s   dd l } ddlm} | j d¡}dd„ tt|ƒdƒD ƒ}dd„ }|d	tt||d
�dd�}t	| 
¡ d d… ƒ t	d | ¡ d d… ¡ƒ t	d dd„ | ¡ d d… D ƒ¡ƒ dd„ |_t	d dd„ | ¡ d d… D ƒ¡ƒ t	d dd„ | ¡ dd… D ƒ¡ƒ d S )Nr   ©ÚLazyCorpusLoaderzcorpora/knbc/corpus1c                 S   s   g | ]}t  d |¡r|‘qS )z\d\-\d\-[\d]+\-[\d]+)r#   Úsearch)r   Úfr
   r
   r   Ú
<listcomp>‰   s   þzdemo.<locals>.<listcomp>z.*c                 S   s2   |   d¡}|d t|d ƒt|d ƒt|d ƒfS )Nú-r   r-   r0   r/   )r&   r:   )Úxr+   r
   r
   r   Ú_knbc_fileids_sort�   s    
z demo.<locals>._knbc_fileids_sortúknbc/corpus1)Úkeyúeuc-jp©r   é
   Ú éd   z

c                 s   s   | ]}t |ƒV  qd S r   )Ústr©r   r<   r
   r
   r   r   �   r   zdemo.<locals>.<genexpr>r0   c                 S   s   d  dd„ | D ƒ¡ d¡S )Nr   c                 s   s8   | ]0}|d  dkrd  |d  |d  d¡d ¡V  qdS )r   r	   z{}({})r-   r!   r0   N©Úformatr&   r   r
   r
   r   r   Ÿ   s   z)demo.<locals>.<lambda>.<locals>.<genexpr>zutf-8)r   Úencoder   r
   r
   r   r   Ÿ   s   
ÿþzdemo.<locals>.<lambda>c                 s   s   | ]}d | V  qdS )z%sNr
   rU   r
   r
   r   r   £   r   Ú
c                 s   s"   | ]}d   dd„ |D ƒ¡V  qdS )r!   c                 s   s,   | ]$}d   |d |d  d¡d ¡V  qdS )z{}/{}r   r-   r!   r0   NrV   )r   Úwr
   r
   r   r   §   r   z!demo.<locals>.<genexpr>.<genexpr>Nr   )r   Úsentr
   r
   r   r   ¦   s   ÿ)ÚnltkÚnltk.corpus.utilrF   ÚdataÚfindr   r   r   ÚsortedÚprintr   r   ÚwordsZparsed_sentsr   Útagged_sents)r\   rF   r   r   rL   Úknbcr
   r
   r   Údemo„   s.    þ
ü$
$
þÿre   c                  C   s€   ddl m}  | dtddd�}t| ¡ d tƒs2J ‚t| ¡ d d tƒsLJ ‚t| ¡ d tƒsbJ ‚t| 	¡ d d tƒs|J ‚d S )Nr   rE   rM   z.*/KN.*rO   rP   )
r]   rF   r   Ú
isinstancerb   rT   ZsentsZtagged_wordsÚtuplerc   )rF   rd   r
   r
   r   Útest­   s    ÿrh   Ú__main__)r#   Znltk.corpus.reader.apir   r   Znltk.corpus.reader.utilr   r   r   Z
nltk.parser   rD   r   re   rh   r@   r
   r
   r
   r   Ú<module>
   s   l)