3
da                 @   s   d Z ddl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 ddlmZmZmZmZ ddlmZ ddlmZ ddlmZ G d	d
 d
Zdd Zedkre  dgZdS )ad  
A graphical tool for exploring the shift-reduce parser.

The shift-reduce parser maintains a stack, which records the structure
of the portion of the text that has been parsed.  The stack is
initially empty.  Its contents are shown on the left side of the main
canvas.

On the right side of the main canvas is the remaining text.  This is
the portion of the text which has not yet been considered by the
parser.

The parser builds up a tree structure for the text using two
operations:

  - "shift" moves the first token from the remaining text to the top
    of the stack.  In the demo, the top of the stack is its right-hand
    side.
  - "reduce" uses a grammar production to combine the rightmost stack
    elements into a single tree token.

You can control the parser's operation by using the "shift" and
"reduce" buttons; or you can use the "step" button to let the parser
automatically decide which operation to apply.  The parser uses the
following rules to decide which operation to apply:

  - Only shift if no reductions are available.
  - If multiple reductions are available, then apply the reduction
    whose CFG production is listed earliest in the grammar.

The "reduce" button applies the reduction whose CFG production is
listed earliest in the grammar.  There are two ways to manually choose
which reduction to apply:

  - Click on a CFG production from the list of available reductions,
    on the left side of the main window.  The reduction based on that
    production will be applied to the top of the stack.
  - Click on one of the stack elements.  A popup window will appear,
    containing all available reductions.  Select one, and it will be
    applied to the top of the stack.

Note that reductions can only be applied to the top of the stack.

Keyboard Shortcuts::
      [Space]	 Perform the next shift or reduce operation
      [s]	 Perform a shift operation
      [r]	 Perform a reduction operation
      [Ctrl-z]	 Undo most recent operation
      [Delete]	 Reset the parser
      [g]	 Show/hide available production list
      [Ctrl-a]	 Toggle animations
      [h]	 Help
      [Ctrl-p]	 Print
      [q]	 Quit

    )ButtonFrameIntVarLabelListboxMenu	ScrollbarTk)Font)	CFGEditorTreeSegmentWidgettree_to_treesegment)CanvasFrameEntryDialogShowText
TextWidget)SteppingShiftReduceParser)Tree)in_idlec               @   s,  e Zd ZdZdJd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dKd*d+Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 Zd:d; Zd<d= Z d>d? Z!d@dA Z"dBdC Z#dDdE Z$dFdG Z%dHdI Z&d)S )LShiftReduceAppa  
    A graphical tool for exploring the shift-reduce parser.  The tool
    displays the parser's stack and the remaining text, and allows the
    user to control the parser's operation.  In particular, the user
    can shift tokens onto the stack, and can perform reductions on the
    top elements of the stack.  A "step" button simply steps through
    the parsing process, performing the operations that
    ``nltk.parse.ShiftReduceParser`` would use.
    r   c             C   s   || _ t||| _t | _| jjd d| _t| j| _| jj	d t| j| _
| j
j	d | j| j | j  | j| j | j| j | j| j | j| j | j| j t| jdd| _| j  d| jd< d S )NzShift Reduce Parser Applicationr   
      )tearoff text)_sentr   _parserr	   _toptitle_animating_lockr   _animateset_show_grammar_init_fonts_init_bindings_init_menubar_init_buttons_init_feedback_init_grammar_init_canvasr   _canvas_reduce_menureset
_lastoper1)selfgrammarsenttrace r2   5/tmp/pip-build-v9q4h5k9/nltk/nltk/app/srparser_app.py__init__b   s&    zShiftReduceApp.__init__c             C   sl   t t d d| _|jd| j t|| _| jj| jjd t dd| jj d| _	t d| jj d| _
d S )	Nfont)r5   z*FontsizeZ	helveticaZbold)familyZweightr6   )r7   r6   )r
   r   _sysfontZ
option_addr   _sizer!   Zcgetget	_boldfont_font)r.   rootr2   r2   r3   r#      s    
zShiftReduceApp._init_fontsc          
   C   s4  t | | _}| jjdddd t| j| jdd| _| jj  t| jddd	d
| jddd| _| jjdddd t	| j
j j | _x | jD ]}| jjdd|  qW | jjtt| jdd t| jdd}| jj|jd |j| jjd |jddd | jjd| j d| _| jjd| j | jjd| j d S )Nbothleft   )fillsidepadxzAvailable Reductions)r5   r   ZsingleZgroovewhitez#909090z#004040z#c0f0c0)Z
selectmoderelief
background
foregroundr5   ZselectforegroundZselectbackgroundrightr   )rB   rA   expandendz %s   )heightZvertical)Zorient)Zyscrollcommand)commandy)rB   rA   z<<ListboxSelect>>z<Motion>z<Leave>)r   
_prodframepackr   r;   Z_prodlist_labelr   r<   	_prodlistlistr   r/   productions_productionsinsertconfigminlenr   r!   Zyviewbind_prodlist_select_hover_highlight_hover_clear_hover)r.   parentZ	listframe
productionZ
listscrollr2   r2   r3   r(      s6    

zShiftReduceApp._init_grammarc             C   s  | j jd| j | j jd| j | j jd| j | j jd| j | j jd| j | j jd| j | j jd| j | j jd| j | j jd	| j | j jd
| j | j jd| j | j jd| j | j jd| j | j jd| j | j jd| j | j jd| j | j jd| j | j jd| j | j jd| j	 | j jd| j	 | j jd| j
 | j jd| j | j jd| jfdd | j jd| jfdd | j jd| jfdd d S )Nz<Control-q>z<Control-x>z<Alt-q>z<Alt-x>z<space>z<s>z<Alt-s>z<Control-s>z<r>z<Alt-r>z<Control-r>z<Delete>z<u>z<Alt-u>z<Control-u>z<Control-z>z<BackSpace>z<Control-p>z<Control-h>z<F1>z<Control-g>z<Control-t>-c             S   s
   |j dS )N   )r!   )ear2   r2   r3   <lambda>   s    z/ShiftReduceApp._init_bindings.<locals>.<lambda>=c             S   s
   |j dS )Nr   )r!   )rc   rd   r2   r2   r3   re      s    +c             S   s
   |j dS )N   )r!   )rc   rd   r2   r2   r3   re      s    )r   rZ   destroystepshiftreducer,   undo
postscripthelpedit_grammaredit_sentencer    )r.   r2   r2   r3   r$      s2    zShiftReduceApp._init_bindingsc             C   s   t | | _}|jddd t|ddd| jdjdd	 t|d
ddd| jdjdd	 t|dddd| jdjdd	 t|dddd| jdjdd	 d S )Nnonebottom)rA   rB   Stepz#90c0d0black)r   rF   rG   rM   r?   )rB   Shiftr   z#90f090)r   	underlinerF   rG   rM   ReduceUndoz#f0a0a0)r   Z_buttonframerQ   r   rj   rk   rl   rm   )r.   r_   Zbuttonframer2   r2   r3   r&      sB    



zShiftReduceApp._init_buttonsc       	      C   s  t |}t |dd}|jdd| jdd |jdd| jdd |jdd	| jd
d |jdd|d t |dd}|jdd| jdd |jdd| jdd |jdd|d t |dd}|jdd	| jdd |j	  |jdd| j
dd |jdd| jdd |j	  |jdd| jdd |jdd|d t |dd}|jdd| j| jd |j	  |jd| jdd| jd  |jd!| jdd"| jd  |jd#| jdd$| jd  |jd%| jdd&| jd  |jd'| jdd(| jd  |jd)d|d t |dd}|jd*d| jdd+ |jd,d| jd-d.d/ |jd0d| jdd1d/ |jd2d| jd3d4d/ |jd5d	|d t |dd}|jd6d| jd7 |jd8d| jd9d |jd:d|d |j|d; d S )<Nr   )r   zReset ParserZDel)labelrw   rM   acceleratorzPrint to PostscriptzCtrl-pZExitr   zCtrl-xZFile)rz   rw   menuzEdit Grammar   zCtrl-gz	Edit TextzCtrl-tZEditrt   ZSpacerv   zCtrl-srx   zCtrl-rry   zCtrl-uZApplyzShow Grammar)rz   rw   variablerM   ZTinyr   )rz   r~   rw   valuerM   ZSmall   ZMedium   ZLarge   ZHuge   ZViewzNo Animation)rz   rw   r~   r   zSlow Animationrb   ra   )rz   rw   r~   r   r{   zNormal Animationrf   zFast Animationrh   rg   ZAnimateZAbout)rz   rw   rM   ZInstructionsZF1ZHelp)r|   )r   add_commandr,   rn   ri   Zadd_cascaderp   rq   rj   Zadd_separatorrk   rl   rm   Zadd_checkbuttonr"   _toggle_grammarZadd_radiobuttonr9   resizer    aboutro   rW   )	r.   r_   ZmenubarZfilemenuZeditmenuZrulemenuZviewmenuZanimatemenuZhelpmenur2   r2   r3   r%     s    





zShiftReduceApp._init_menubarc             C   s   t | | _}|jddddd t|d| jd| _| jjdd t |d	d
d}|jddd
dd t|dd| jd| _t|dddd| jd| _| jjdd | jjddd
d d S )Nxrs      )rA   rB   rC   padyzLast Operation:)r   r5   r?   )rB   sunkenr   )rE   borderrH   r}   )rA   rB   rI   rC   z#007070z#f0f0f0)rG   rF   r5   w   z#004040)anchorwidthrG   rF   r5   )rB   rA   rI   )r   _feedbackframerQ   r   r<   Z_lastoper_labelr-   
_lastoper2)r.   r_   ZfeedbackframeZlastoperframer2   r2   r3   r'     s$    zShiftReduceApp._init_feedbackc             C   s   t |dddddd| _| jjddd	dd
 | jj  }| _g | _g | _|jddddddd| _|j	dddddd| _
|j	dddddd| _| jj d }t|dd| jd| _t|dd| jd| _| jj| j | jj| j d S )NrD   i  r   r@   r   )rF   r   Zcloseenoughr   rE   r   r>   top)rI   rA   rB   r   r   z#c0f0f0ru   )rA   outline.)Zdashz#408080)rA   rh   ZStackz#004040)colorr5   zRemaining Text)r   _cframerQ   canvasr*   _stackwidgets_rtextwidgetsZcreate_rectangle	_titlebarZcreate_line	_exprline	_stacktopr9   r:   r   r;   _stacklabel_rtextlabel
add_widget)r.   r_   r   r6   r2   r2   r3   r)     s,    
zShiftReduceApp._init_canvasc             C   sP  | j d j }dd |D \}}}}x| jD ]}| jj| q,W g | _x| jD ]}| jj| qNW g | _| jj \}}	}
}||	 d }| j j| j	ddd|d  | j j| j
d|d d d|d d  | jj \}}	}
}| jjd	| d
|	  | jj \}}	}
}| jj||
 d	 d
|	  d	}x| jj D ]}t|trndd| jdd| jd}t| j |f|}d|j d< nt| j |d| jd}|j| j | jj| | jj||| |j d d }q*W d}xT| jj D ]F}t| j |d| jd}| jj| | jj||| |j d d }qW t| jdkr<|| jd j 7 }t|| jj d }| jj d }||t|| kr|t|| }x$| jD ]}|jd| | d qW | jj|| jj d  d	 d || t|| d }| j j| j|d|d | jj \}}	}
}t| jdkrD|| fdd}| jd j| | jd j| j  | j!  d S )Nscrollregionc             s   s   | ]}t |V  qd S )N)int).0cr2   r2   r3   	<genexpr>  s    z)ShiftReduceApp._redraw.<locals>.<genexpr>r   i  r   rh   r@   r}   r   z#4080a0z#006060)Z
tree_colorZ
tree_widthZ	node_fontZ
node_colorZ
leaf_colorZ	leaf_fontz#000000r   )r   r5   rK   c             S   s&   | j  d |k r|j  n|j  d S )Nr   )bboxrk   _redraw)widgetmidxr.   r2   r2   r3   
drag_shift  s    
z*ShiftReduceApp._redraw.<locals>.drag_shiftix)"r*   splitr   r   Zdestroy_widgetr   r   r   coordsr   r   mover   r   stack
isinstancer   r;   r<   r   rz   r   Z
bind_click_popup_reduceappendr   Zremaining_textrY   r   maxr   Z	bind_dragrk   _highlight_productions)r.   r   Zcx1Zcy1Zcx2Zcy2stackwidgetZrtextwidgetx1y1x2y2rN   ZstackxtokZattribsr   Z
rtextwidthZrlabelwidthr   r   r2   r2   r3   r     sn    &
 zShiftReduceApp._redrawc             C   s*   |j  d d }| jj| j|d|d d S )Nr@   2   r   i  )r   r*   r   r   )r.   r   r   r2   r2   r3   _draw_stack_top  s    zShiftReduceApp._draw_stack_topc             C   s>   | j jdd x*| jj D ]}| jj|}| j j| qW d S )Nr   rJ   )rR   selection_clearr   reducible_productionsrU   indexselection_set)r.   prodr   r2   r2   r3   r     s    z%ShiftReduceApp._highlight_productionsc             G   s"   | j d krd S | j j  d | _ d S )N)r   ri   )r.   rc   r2   r2   r3   ri   )  s    

zShiftReduceApp.destroyc             G   s.   | j j| j d| jd< d| jd< | j  d S )Nz	Reset Appr   r   )r   Z
initializer   r-   r   r   )r.   rc   r2   r2   r3   r,   /  s    

zShiftReduceApp.resetc             G   sT   | j  rdS | j rdS t| jj r<d| jd< d| jd< nd| jd< d| jd< d S )NTz	Finished:r   ZSuccessZFailure)rl   rk   rS   r   Zparsesr-   r   )r.   rc   r2   r2   r3   rj   5  s    

zShiftReduceApp.stepc             G   s^   | j r
d S | jj rZ| jj d }d| jd< d| | jd< | jj rN| j  n| j	  dS dS )Nr   zShift:r   z%rTFrO   )
r   r   rk   r   r-   r   r    r:   _animate_shiftr   )r.   rc   r   r2   r2   r3   rk   B  s    



zShiftReduceApp.shiftc             G   sP   | j r
d S | jj }|rLd| jd< d| | jd< | jj rD| j  n| j  |S )NzReduce:r   z%s)	r   r   rl   r-   r   r    r:   _animate_reducer   )r.   rc   r`   r2   r2   r3   rl   P  s    



zShiftReduceApp.reducec             G   s    | j r
d S | jj r| j  d S )N)r   r   rm   r   )r.   rc   r2   r2   r3   rm   ]  s    
zShiftReduceApp.undoc             G   s   | j j  d S )N)r   Zprint_to_file)r.   rc   r2   r2   r3   rn   c  s    zShiftReduceApp.postscriptc             O   s   t  r
dS | jj|| dS )z
        Enter the Tkinter mainloop.  This function must be called if
        this demo is created from a non-interactive program (e.g.
        from a secript); otherwise, the demo will close as soon as
        the script completes.
        N)r   r   mainloop)r.   argskwargsr2   r2   r3   r   f  s    zShiftReduceApp.mainloopNc             C   sf   |d k	r| j j| | j j }| jjt| d | jjt| d | jjt| d | j  d S )N)r6   )	r9   r!   r:   r<   	configureabsr;   r8   r   )r.   r6   r2   r2   r3   r   u  s    
	zShiftReduceApp.resizec             G   sL   y t | jdtpdj ddd W n&   t | jdtp6dj dd Y nX d S )Nz%Help: Shift-Reduce Parser Applicationr   K   Zfixed)r   r5   )r   )r   r   __doc__strip)r.   rc   r2   r2   r3   ro     s    

zShiftReduceApp.helpc          
   G   sH   d}d}y ddl m} |||dj  W n   t| j|| Y nX d S )Nz%NLTK Shift-Reduce Parser Application
zWritten by Edward Loperz&About: Shift-Reduce Parser Applicationr   )Message)messager   z<NLTK Shift-Reduce Parser Application
Written by Edward Loper)Ztkinter.messageboxr   showr   r   )r.   rc   ZABOUTZTITLEr   r2   r2   r3   r     s    zShiftReduceApp.aboutc             G   s   t | j| jj | j d S )N)r   r   r   r/   set_grammar)r.   rc   r2   r2   r3   rp     s    zShiftReduceApp.edit_grammarc             C   sN   | j j| t|j | _| jjdd x | jD ]}| jjdd|  q0W d S )Nr   rJ   z %s)r   r   rS   rT   rU   rR   deleterV   )r.   r/   r`   r2   r2   r3   r     s
    zShiftReduceApp.set_grammarc             G   s,   dj | j}d}d}t| j||| j| d S )N z	Edit TextzEnter a new sentence to parse.)joinr   r   r   set_sentence)r.   rc   Zsentencer   Zinstrr2   r2   r3   rq     s    zShiftReduceApp.edit_sentencec             C   s   |j  | _| j  d S )N)r   r   r,   )r.   r0   r2   r2   r3   r     s    
zShiftReduceApp.set_sentencec             G   sN   | j j r,| jjddd| jd d| jd< n| jj  d| jd< d| jd< d S )	Nr>   r?   r@   )rA   rB   rC   afterzShow Grammarr   zHide Grammarr   )r"   r:   rP   rQ   r   r-   Zpack_forgetr   )r.   rc   r2   r2   r3   r     s    


zShiftReduceApp._toggle_grammarc             C   s   | j j }t|dkrd S t|d }| jj| j| }|rrd| jd< d| | jd< | j	j
 rh| j  q| j  n:| j jdd x*| jj D ]}| jj|}| j j| qW d S )Nr   r   zReduce:r   z%srJ   )rR   curselectionrY   r   r   rl   rU   r-   r   r    r:   r   r   r   r   r   r   )r.   event	selectionr   r`   r   r2   r2   r3   r[     s    




zShiftReduceApp._prodlist_selectc             C   sj   | j j }t|dkrd S | jjdd x"|D ]}| jjt|| jd q.W | jj| j	j
 | j	j  d S )Nr   rJ   )rz   rM   )r   r   rY   r+   r   r   strrl   postr*   Zwinfo_pointerxZwinfo_pointery)r.   r   rT   r`   r2   r2   r3   r     s    

zShiftReduceApp._popup_reducec             C   sl   | j d }|j d }t| jdkr*d}n| jd j d d }| jj }|| d | }| j||| d S )Nr   r}   r   r@   r   g      ?rO   )r   r   rY   r   r    r:   _animate_shift_frame)r.   r   rH   r?   dtdxr2   r2   r3   r     s    

zShiftReduceApp._animate_shiftc             C   sf   |dkr6d| _ |j|d | jjd| j|d || n,| jd= | jj| d| _ | j| | j	  d S )Nr   r   r   )
r   r   r   r   r   r   r   r   r   r   )r.   framer   r   r2   r2   r3   r     s    
z#ShiftReduceApp._animate_shift_framec             C   s   t | jj d }| j| d  }t|d trFd|d j j  }nd|d j  }| jj	 }|d | }| j
|d || d S )Nr   r      g       @r@   rO   )rY   r   r   r   r   r   rz   rL   r    r:   _animate_reduce_frame)r.   Z
numwidgetswidgetsZydistr   dyr2   r2   r3   r     s    
zShiftReduceApp._animate_reducec             C   s4  |dkrDd| _ x|D ]}|jd| qW | jjd| j|d || n| jt| d = x|D ]}| jj| q\W | j	j
 d	 }t|tst t| jt|j d| jd}t| j||dd}| jj \}}}	}
|
| d }| jsd}n| jd
 j d d }| jj||| | jj| | j| | j  d| _ d S )Nr   r   r   z#006060)r   r5   r@   )r   r}   rO   rO   )r   r   r   r   r   r   rY   r   Zremove_widgetr   r   r   r   
ValueErrorr   r*   r   rz   r;   r   r   r   r   r   r   r   )r.   r   r   r   r   r   rz   r   r   r   r   rN   r   r2   r2   r3   r     s0    



z$ShiftReduceApp._animate_reduce_framec             C   s   | j j|j}| j|krd S | j  dd | j j D }||krt| j| j }x8| j	| d  D ]$}t
|trd|j d< qdd|d< qdW || _d S )Nc             S   s   g | ]}t |qS r2   )r   )r   sr2   r2   r3   
<listcomp>d  s    z3ShiftReduceApp._highlight_hover.<locals>.<listcomp>z#00a000r   )rR   ZnearestrN   r\   r^   r   rY   rU   rhsr   r   r   rz   )r.   r   r   r   Zrhslenr   r2   r2   r3   r]   Y  s    

zShiftReduceApp._highlight_hoverc             G   sH   | j dkrd S d| _ x.| jD ]$}t|tr8d|j d< qd|d< qW d S )Nr   ru   r   rO   rO   )r\   r   r   r   rz   )r.   r   r   r2   r2   r3   r^   p  s    

zShiftReduceApp._clear_hover)r   )N)'__name__
__module____qualname__r   r4   r#   r(   r$   r&   r%   r'   r)   r   r   r   ri   r,   rj   rk   rl   rm   rn   r   r   ro   r   rp   r   rq   r   r   r[   r   r   r   r   r   r]   r^   r2   r2   r2   r3   r   W   sJ   	
*)"$ !W

>r   c                 s,  ddl m} m m} d} fdd|j D \	}}}}}}}	}
}||||g||||g||||g||||g|||
||g|||
|g||||g||dg||dg||dg||d	g||
d
g||dg||dg||dg||dg||dg||dgf}| ||}dj }t||j  dS )zP
    Create a shift reduce parser app, using a simple grammar and
    text.
    r   )CFGNonterminal
ProductionzS VP NP PP P N Name V Detc             3   s   | ]} |V  qd S )Nr2   )r   r   )r   r2   r3   r     s    zapp.<locals>.<genexpr>IZtherd   ZmanZsawinwithZparkZdogZstatueZmyz*my dog saw a man in the park with a statueN)Znltk.grammarr   r   r   r   r   r   )r   r   ZnonterminalsSZVPZNPZPPPNNameVZDetrT   r/   r0   r2   )r   r3   app|  s0    (










r   __main__N)r   tkinterr   r   r   r   r   r   r   r	   Ztkinter.fontr
   Z	nltk.drawr   r   r   Znltk.draw.utilr   r   r   r   Z
nltk.parser   Z	nltk.treer   Z	nltk.utilr   r   r   r   __all__r2   r2   r2   r3   <module>?   s$   (      +*