o
    rZh7                     @   s  d dl Zd dlZd dlmZmZmZmZmZm	Z	m
Z
mZmZmZmZmZmZ d dlmZ d dlmZmZmZmZmZmZmZmZmZmZmZ d dlm Z  d dl!m"Z" dZ#dZ$d	Z%d
Z&i ddd ddd ddd ddd ddd ddd ddd d
dd ddd ddd dd d d!d"d d#d$d d%d&d d'd(d d)d*d d+d,d Z'G d-d. d.Z(G d/d0 d0Z)d1d2 Z*e+d3kre*  d2gZ,dS )4    N)ENDLEFTSUNKENButtonFrameIntVarLabelMenu
OptionMenu	Scrollbar	StringVarTextTk)Font)alpinobrowncess_catcess_espflorestaindian
mac_morphomachadonps_chatsinica_treebanktreebank)FreqDist)in_idlez<<CL_EVENT>>z<<ELC_EVENT>>d   zEnglish: Brown Corpus (Humor)zCatalan: CESS-CAT Corpusc                   C      t  S N)r   words r!   r!   H/var/www/auris/lib/python3.10/site-packages/nltk/app/collocations_app.py<lambda>3       r#   zEnglish: Brown Corpusc                   C   r   r   r   r    r!   r!   r!   r"   r#   4   r$   zEnglish: Brown Corpus (Press)c                   C   s   t jg ddS )N)newsZ	editorialZreviews
categoriesr%   r!   r!   r!   r"   r#   5   s    z English: Brown Corpus (Religion)c                   C      t jddS )NZreligionr'   r%   r!   r!   r!   r"   r#   8       zEnglish: Brown Corpus (Learned)c                   C   r)   )NZlearnedr'   r%   r!   r!   r!   r"   r#   9   r*   z'English: Brown Corpus (Science Fiction)c                   C   r)   )NZscience_fictionr'   r%   r!   r!   r!   r"   r#   :   s    zEnglish: Brown Corpus (Romance)c                   C   r)   )NZromancer'   r%   r!   r!   r!   r"   r#   =   r*   c                   C   r)   )NZhumorr'   r%   r!   r!   r!   r"   r#   >   r*   zEnglish: NPS Chat Corpusc                   C   r   r   )r   r    r!   r!   r!   r"   r#   ?   r$   z#English: Wall Street Journal Corpusc                   C   r   r   )r   r    r!   r!   r!   r"   r#   @   r$   zChinese: Sinica Corpusc                   C   r   r   )r   r    r!   r!   r!   r"   r#   A   r$   zDutch: Alpino Corpusc                   C   r   r   )r   r    r!   r!   r!   r"   r#   B   r$   zHindi: Indian Languages Corpusc                   C   r)   )Nz	hindi.pos)files)r   r    r!   r!   r!   r"   r#   C   r*   z&Portuguese: Floresta Corpus (Portugal)c                   C   r   r   )r   r    r!   r!   r!   r"   r#   D   r$   z&Portuguese: MAC-MORPHO Corpus (Brazil)c                   C   r   r   )r   r    r!   r!   r!   r"   r#   E   r$   z#Portuguese: Machado Corpus (Brazil)c                   C   r   r   )r   r    r!   r!   r!   r"   r#   F   r$   zSpanish: CESS-ESP Corpusc                   C   r   r   )r   r    r!   r!   r!   r"   r#   G   r$   c                   @   s   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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d.d/ Zd0d1 Zd2d3 Zd4S )5CollocationsViewz#FFFc                 C   sb   t  | _t| j| _t | _| | j |   | 	| j | 
| jj | jt| j| _d S r   )qQueuequeueCollocationsModelmodelr   top	_init_top_init_menubar_init_widgetsload_corpusDEFAULT_CORPUSafterPOLL_INTERVAL_pollselfr!   r!   r"   __init__N   s   
zCollocationsView.__init__c                 C   s@   | d |d |d| j |d| j |dd d S )Nz550x650+50+50zNLTK Collocations Listz<Control-q>ZWM_DELETE_WINDOWi&  i  )ZgeometrytitlebinddestroyprotocolZminsize)r<   r2   r!   r!   r"   r3   X   s
   

zCollocationsView._init_topc                 C   s^   t |t| jdddd| _| | j | | j | | j | | j | jjddd d S )N   )
backgroundpadxpadyborderbothT)fillexpand)	r   dict_BACKGROUND_COLOURZ
main_frame_init_corpus_select_init_results_box_init_paging_init_statuspackr<   parentr!   r!   r"   r5   _   s   zCollocationsView._init_widgetsc              	   C   s   t || jd}t|| _| j| jj t|td| jddddj	dd t
| jj | jj}t|| j| jjg| j R d	| ji}d|d
< d|d< |j	dd |j	dddd d S )NrC   z	 Corpus:    rB   r   )justifytextrC   rD   rE   rF   left)sidecommandborderwidthhighlightthicknessr2   xnrX   rH   anchor)r   rK   r   varsetr1   r7   r   r   rP   listCORPORAkeysremover
   non_default_corporacorpus_selected)r<   rR   
innerframeZother_corporaomr!   r!   r"   rL   i   s<   

z$CollocationsView._init_corpus_selectc              	   C   s.   t |tt| jdddd| _| jjddd d S )Nr   rB   )rU   ZreliefrC   rF   rD   rE   r2   swrX   r_   )r   r   r   rK   statusrP   rQ   r!   r!   r"   rO      s   	zCollocationsView._init_statusc                 C   s   t | j| _t| j}t|ddd}|jdd| jdd |jdd|d t|dd	}t|dd	}|jd
| jdd| jd |jd| jdd| jd |jd| jdd| jd |	d |jdd|d |jdd|d | jj
|d d S )Nr   )tearoffrZ   ZExitrB   zCtrl-q)label	underlinerY   ZacceleratorFile)rn   ro   menu)rm   20   )rn   variablero   valuerY   Z502   100r   zResult CountZEdit)rq   )r   r2   _result_sizer	   Zadd_commandr@   Zadd_cascadeZadd_radiobuttonset_result_sizeZinvokeconfig)r<   ZmenubarZfilemenuZeditmenuZ
rescntmenur!   r!   r"   r4      sD   


zCollocationsView._init_menubarc                 K   s   | j  | j_d S r   )rx   getr1   result_count)r<   kwargsr!   r!   r"   ry      s   z CollocationsView.set_result_sizec                 C   s   t |}t |}t |}t|dd}t|ddd}t|tddddd|j|jd	d
ddd
| _| jjdddd |jdddd |j| jjd |jddddd |j| jj	d t
|d| jdjddd |jddddd |jdddd |jdddd d S )NrB   )rZ   Zhoriz)rZ   ZorientZcourierZ16)familysizedisablednoneZ40rr   )	ZfontstaterZ   ZyscrollcommandZxscrollcommandwrapwidthheightZexportselectionrW   rG   T)rX   rH   rI   yer^   )rY   r\   w)rX   rH   rI   r_   z   )rV   rC   rk   r2   r]   bottoms)r   r   r   r   ra   results_boxrP   rz   ZyviewZxviewr   rK   )r<   rR   rh   i1i2Z
vscrollbarZ
hscrollbarr!   r!   r"   rM      s8   
z"CollocationsView._init_results_boxc              	   C   s   t || jd}t|d| jddddd | _}|jddd	 t|d
| jddddd | _}|jddd	 |jddd |   d S )NrS   ZPrevious10rB   r   )rV   rY   r   rZ   r[   r   rW   centerrk   ZNextrightr2   r   )rX   rH   )	r   rK   r   previousprevrP   __next__nextreset_current_page)r<   rR   rh   r   r   r!   r!   r"   rN      s.   		zCollocationsView._init_pagingc                 C   s
   d| _ d S N)current_pager;   r!   r!   r"   r      s   
z#CollocationsView.reset_current_pagec                 C   sd   z	| j jdd}W n
 tjy   Y nw |tkr| | n	|tkr'| | | j	t
| j| _	d S )NF)block)r/   r{   r-   EmptyCORPUS_LOADED_EVENThandle_corpus_loadedERROR_LOADING_CORPUS_EVENThandle_error_loading_corpusr2   r8   r9   r:   r<   eventr!   r!   r"   r:      s   
zCollocationsView._pollc                 C   s8   d| j   | jd< |   |   |   |   d S )NzError in loading rV   )r`   r{   rl   unfreeze_editableclear_results_boxfreeze_editabler   r   r!   r!   r"   r     s
   z,CollocationsView.handle_error_loading_corpusc                 C   sZ   | j  d | jd< |   |   |   | j| jd }| 	| |  jd7  _d S )Nz
 is loadedrV   rB   )
r`   r{   rl   r   r   r   r1   r   r   write_results)r<   r   collocationsr!   r!   r"   r   
  s   
z%CollocationsView.handle_corpus_loadedc                 G   s   | j  }| | d S r   )r`   r{   r6   )r<   argsZnew_selectionr!   r!   r"   rg        
z CollocationsView.corpus_selectedc                 C   sD   |    | j| jd }| jd | _|   | | |   d S NrB   )r   r1   r   r   r   r   r   r<   r   r!   r!   r"   r     s   
zCollocationsView.previousc                 C   sF   |    | j| jd }|   | | |  jd7  _|   d S r   )r   r1   r   r   r   r   r   r   r!   r!   r"   r      s   
zCollocationsView.__next__c                 C   s:   | j j|krd| d | jd< |   | j | d S d S )NzLoading z...rV   )r1   selected_corpusrl   r   r6   )r<   Z	selectionr!   r!   r"   r6   (  s
   zCollocationsView.load_corpusc                 C   s   d| j d< d| jd< d S )Nr   r   )r   r   r;   r!   r!   r"   r   .  r   z CollocationsView.freeze_editablec                 C   s&   d| j d< | j dt d| j d< d S )Nnormalr   z1.0r   )r   deleter   r;   r!   r!   r"   r   2  s   
z"CollocationsView.clear_results_boxc                 C   s   | j j|dd d S )Ntail)when)r2   Zevent_generater   r!   r!   r"   
fire_event7  s   zCollocationsView.fire_eventc                 G   s0   | j d u rd S | j | j | j   d | _ d S r   )r2   Zafter_cancelr8   r@   )r<   r   r!   r!   r"   r@   ;  s
   


zCollocationsView.destroyc                 O   s    t  rd S | jj|i | d S r   )r   r2   mainloop)r<   r   r}   r!   r!   r"   r   B  s   zCollocationsView.mainloopc                 C   s   |    d S r   )set_paging_button_statesr;   r!   r!   r"   r   G  s   z"CollocationsView.unfreeze_editablec                 C   sT   | j dks
| j dkrd| jd< nd| jd< | j| j r#d| jd< d S d| jd< d S )Nr   r   r   r   r   )r   r   r1   is_last_pager   r;   r!   r!   r"   r   J  s   
z)CollocationsView.set_paging_button_statesc                 C   sX   d| j d< d}|D ]}| j t|d |d d |d  d  |d7 }q	d| j d< d S )	Nr   r   rB   z.0r    
r   )r   insertstr)r<   resultsrowZeachr!   r!   r"   r   T  s   
*
zCollocationsView.write_resultsN)__name__
__module____qualname__rK   r=   r3   r5   rL   rO   r4   ry   rM   rN   r   r:   r   r   rg   r   r   r6   r   r   r   r@   r   r   r   r   r!   r!   r!   r"   r,   K   s6    

'

r,   c                   @   sV   e Zd Zdd Zdd Zdd Zdd Zd	d
 Zdd Zdd Z	G dd de
jZdS )r0   c                 C   s0   d | _ d | _d | _t| _t| _|| _|   d S r   )	r|   r   r   _CORPORArc   _DEFAULTr7   r/   reset_results)r<   r/   r!   r!   r"   r=   ^  s   zCollocationsModel.__init__c                 C   s   g | _ d| _d S )Nr   )result_pagesresults_returnedr;   r!   r!   r"   r   g  s   
zCollocationsModel.reset_resultsc                 C   s,   || _ d | _| || }|  |   d S r   )r   r   
LoadCorpusstartr   )r<   nameZrunner_threadr!   r!   r"   r6   k  s
   zCollocationsModel.load_corpusc                 C   s0   g }| t| j  || j |  |S r   )extendrb   rc   rd   re   r7   sort)r<   copyr!   r!   r"   rf   r  s
   z%CollocationsModel.non_default_corporac                 C   s6   |t | jk r	dS | j|t | j | j  t | jkS )NF)lenr   r   r|   r   )r<   numberr!   r!   r"   r   y  s   zCollocationsModel.is_last_pagec                 C   sh   t | jd |k r/t|t | jd  D ]}| j| j| j| j| j   |  j| j7  _q| j| S r   )r   r   rangeappendr   r   r|   )r<   pageir!   r!   r"   r     s   
zCollocationsModel.nextc                 C   s   |dkrg S | j | S r   )r   )r<   r   r!   r!   r"   r     s   
zCollocationsModel.prevc                   @   s   e Zd Zdd Zdd ZdS )zCollocationsModel.LoadCorpusc                 C   s   t j|  ||| _| _d S r   )	threadingThreadr=   r1   r   )r<   r   r1   r!   r!   r"   r=     s   z%CollocationsModel.LoadCorpus.__init__c              
      s   zR| j j| j  }ddlm} dd |D tfddttd D  t fdd D }|j|dd	d
 t	t
|d|| j _| j jt W d S  typ } zt| | j jt W Y d }~d S d }~ww )Nr   )
itemgetterc                 S   s   g | ]
}t |d kr|qS )rT   )r   ).0r   r!   r!   r"   
<listcomp>  s    z4CollocationsModel.LoadCorpus.run.<locals>.<listcomp>c                 3   s$    | ]}t  ||d   V  qdS )rT   N)tuple)r   r   )rV   r!   r"   	<genexpr>  s   " z3CollocationsModel.LoadCorpus.run.<locals>.<genexpr>rB   c                    s8   g | ]\}}||f ||f d  | |   fqS )   r!   )r   Zw1Zw2)fdvocabr!   r"   r     s    &T)keyreverse)r1   rc   r   operatorr   r   r   r   r   rb   mapr   r/   putr   	Exceptionprintr   )r<   r    r   Zscoredr   r!   )r   rV   r   r"   run  s"   "z CollocationsModel.LoadCorpus.runN)r   r   r   r=   r   r!   r!   r!   r"   r     s    r   N)r   r   r   r=   r   r6   rf   r   r   r   r   r   r   r!   r!   r!   r"   r0   ]  s    	r0   c                  C   s   t  } |   d S r   )r,   r   )cr!   r!   r"   app  s   r   __main__)-r/   r-   r   tkinterr   r   r   r   r   r   r   r	   r
   r   r   r   r   Ztkinter.fontr   Znltk.corpusr   r   r   r   r   r   r   r   r   r   r   Znltk.probabilityr   Z	nltk.utilr   r   r   r9   r   r   r,   r0   r   r   __all__r!   r!   r!   r"   <module>   sl   	<4  Q
