
    /hzN                       S r SSKrSSKrSSKJrJrJrJrJ	r	J
r
JrJrJrJr  SSKJrJr  SSKJr  SSKJrJr  SSKJrJrJr  SSKJrJrJrJrJ r J!r!  SS	K"J#r#J$r$  SS
K%J&r&J'r'J(r(J)r)J*r*J+r+J,r,J-r-J.r.J/r/  SSK0J1r1  SSK2J3r3   " S S\5      r4 " S S5      r5 " S S5      r6 " S S5      r7 " S S5      r8 " S S5      r9 " S S\9\.5      r: " S S\9\'5      r; " S S\9\&5      r< " S S \9\+5      r= " S! S"5      r>S# r?\@S$:X  a  \?" 5         S%/rAg)&a  
A graphical tool for exploring chart parsing.

Chart parsing is a flexible parsing algorithm that uses a data
structure called a "chart" to record hypotheses about syntactic
constituents.  Each hypothesis is represented by a single "edge" on
the chart.  A set of "chart rules" determine when new edges can be
added to the chart.  This set of rules controls the overall behavior
of the parser (e.g. whether it parses top-down or bottom-up).

The chart parsing tool demonstrates the process of parsing a single
sentence, with a given grammar and lexicon.  Its display is divided
into three sections: the bottom section displays the chart; the middle
section displays the sentence; and the top section displays the
partial syntax tree corresponding to the selected edge.  Buttons along
the bottom of the window are used to control the execution of the
algorithm.

The chart parsing tool allows for flexible control of the parsing
algorithm.  At each step of the algorithm, you can select which rule
or strategy you wish to apply.  This allows you to experiment with
mixing different strategies (e.g. top-down and bottom-up).  You can
exercise fine-grained control over the algorithm by selecting which
edge you wish to apply a rule to.
    N)
ButtonCanvasCheckbuttonFrameIntVarLabelMenu	ScrollbarTkToplevel)askopenfilenameasksaveasfilename)Font)	showerrorshowinfo)	CFGEditorTreeSegmentWidgettree_to_treesegment)CanvasFrameColorizedListEntryDialogMutableOptionMenuShowTextSymbolWidget)CFGNonterminal)
BottomUpPredictCombineRuleBottomUpPredictRuleChartLeafEdgeLeafInitRuleSingleEdgeFundamentalRuleSteppingChartParserTopDownInitRuleTopDownPredictRuleTreeEdge)Tree)in_idlec                   >    \ rS rSr\R
                  S   rS rS rSr	g)EdgeList[   
rightarrowc                     UR                  SSS9  UR                  SSSS9  UR                  SS	S9  UR                  S
SSS9  g )Nterminalz#006000)
foregroundarrowsymbol0)font	underlinedotz#000000nonterminalblue)	helveticabold)r/   r3   )
tag_config)self
textwidgetoptionss      P/var/www/auris/envauris/lib/python3.13/site-packages/nltk/app/chartparser_app.py_init_colortagsEdgeList._init_colortags^   sX    jY?gHDe	:f3M 	 	
    c                    / nUR                  SUR                  5       -  S45        UR                  U R                  S45        [        UR	                  5       5       Hz  u  p4X1R                  5       :X  a  UR                  S5        [        U[        5      (       a&  UR                  SUR                  5       -  S45        Md  UR                  SU-  S45        M|     UR                  5       (       a  UR                  S5        U$ )Nz%s	r6   r0   )z *r5   z %sz %rr.   )
appendlhsARROW	enumeraterhsr5   
isinstancer   r1   is_complete)r<   itemcontentsielts        r?   
_item_reprEdgeList._item_reprf   s    $((*,m<=W-.
+FAHHJ.#{++!5} EFj 9: , OOM*rB    N)
__name__
__module____qualname____firstlineno__r   SYMBOLSrF   r@   rO   __static_attributes__rQ   rB   r?   r*   r*   [   s      .E
rB   r*   c                       \ rS rSrSr SS jrS rS rS rS r	S r
S	 rS
 rS rS rS rSS jrS rS rS rS rS rS rSS jrS rS rS rSrg)ChartMatrixView{   zK
A view of a chart that displays the contents of the corresponding matrix.
c                 >   X l         / U l        / U l        S U l        U(       am  [	        U5      U l        U R
                  R                  U5        U R
                  R                  SU R                  5        U R                  U R
                  5        O[        U5      U l        U R                  U R
                  5        U R                  U R
                  5        U(       a  U R                  U R
                  5        OS U l        0 U l        SU l        U R#                  5         g )N<Control-q>r   )_chart_cells_marks_selected_cellr   _roottitlebinddestroy
_init_quitr   _init_matrix
_init_list_init_numedges_numedges_label
_callbacks
_num_edgesdraw)r<   parentcharttoplevelrb   show_numedgess         r?   __init__ChartMatrixView.__init__   s     "!&)DJJJU#JJOOM4<<8OODJJ'vDJ$**%

#

+#'D 		rB   c                 P    [        USU R                  S9nUR                  SSSS9  g )NQuittextcommandbottomr   nonesideexpandfill)r   rd   pack)r<   rootquits      r?   re   ChartMatrixView._init_quit   s&    d>		x	7rB   c                     [        USSS9nUR                  SSSSSS	9  [        US
S
SS9U l        U R                  R                  SSS9  g )N   sunkenborderreliefr   ry         top)r|   r}   padxpadyr{      white)widthheight
background)r|   r}   )r   r~   r   _canvas)r<   r   cframes      r?   rf   ChartMatrixView._init_matrix   sO    tAh716FfCP0rB   c                 X    [        USS9U l        U R                  R                  SSSS9  g )Nz0 edgesrv   r   ry   r   )r|   r}   r{   )r   ri   r~   r<   r   s     r?   rh   ChartMatrixView._init_numedges   s,    $T	:!!e!DrB   c                     [        U/ SSS9U l        U R                  R                  SSSSS9  U 4S	 jnU R                  R                  S
U5        U R                  R	                  5         g )N      )r   r   r   r   bothr   )r{   r|   r}   r   c                 (    UR                  SU 5        g Nselect_fire_callbacks)edger<   s     r?   cb&ChartMatrixView._init_list.<locals>.cb         40rB   r   )r*   _listr~   add_callbackfocus)r<   r   r   s      r?   rg   ChartMatrixView._init_list   sZ    dBb;


U16B 	1 	

"-

rB   c                 r    U R                   c  g  U R                   R                  5         S U l         g !    N= fNra   rd   r<   es     r?   rd   ChartMatrixView.destroy   7    ::	JJ  
	   2 6c                 \    XR                   La  Xl         SU l        U R                  5         g g Nr   )r]   rk   rl   r<   rn   s     r?   	set_chartChartMatrixView.set_chart   s&    #KDOIIK $rB   c                 p   U R                   c  g [        U R                  5      n[        U5       VVs/ s H  n[        U5       Vs/ s H  nSPM     snPM!     nnnU R                   H.  nXER                  5          UR                  5       ==   S-  ss'   M0     [        U5       H  n[        X15       H  nXC   U   S:X  a  SnOBSR                  [        SSSXC   U   -  S-  -   5      [        SSSXC   U   -  S-  -
  5      5      nU R                  U   U   nU R                  R                  XvS	9  X24U R                  :X  a8  U R                  R                  US
SS9  U R                  R                  U5        M  U R                  R                  USSS9  M     M     [        U R                  R                  U R                  S95      nU R                   R#                  U5        U R                  R%                  5       U l        U R(                  b  SU R&                  -  U R(                  S'   g g s  snf s  snnf )Nr   r   gray20z#00{:02x}{:02x}   2      
   r}   z#00ffffr   )outliner   black)spanz%d edgesrv   )ra   lenr^   ranger]   startendformatminmaxr   
itemconfigr`   	tag_raiselistr   r   set	num_edgesrk   ri   )	r<   NjrM   
cell_edgesr   colorcell_tagedgess	            r?   updateChartMatrixView.update   s   :: 5:1X>X%(+(Qq(+X
>KKDzz|$TXXZ0A50   qA1[=#q($E-44CcJM!,<&<r&A!ABAsS:=+;%;b%@@AE  ;;q>!,'''=6T000LL++Hiq+QLL**84LL++HgQ+O ! $ T[[''T-@-@'AB

u ++//1++5+GD  ( ,9 ,>s   H2H-H2-H2c                 X    U R                   R                  SSS9  U R                  5         g )Ninactiveboxhiddenstater   r   r   r<   s    r?   activateChartMatrixView.activate   "    X>rB   c                 X    U R                   R                  SSS9  U R                  5         g )Nr   normalr   r   r   s    r?   
inactivateChartMatrixView.inactivate   r   rB   c                 B    SU R                   R                  U0 5      U'   g Nr   rj   
setdefaultr<   eventfuncs      r?   r   ChartMatrixView.add_callback       67""5"-d3rB   Nc                 V    Uc  U R                   U	 g  U R                   U   U	 g !    g = fr   rj   r   s      r?   remove_callbackChartMatrixView.remove_callback   3    <&OOE*40   $ (c                     XR                   ;  a  g [        U R                   U   R                  5       5       H  nU" U6   M
     g r   rj   r   keysr<   r   argscb_funcs       r?   r   ChartMatrixView._fire_callbacks   7    'DOOE2779:GTN ;rB   c                     U R                   c  g X4U R                  :X  a)  U R                  R                  5       U R                  :X  a  g X4U l        U R                  5         U R                  SX5        g )Nselect_cell)ra   r`   r]   r   rk   r   r   r<   rM   r   s      r?   r   ChartMatrixView.select_cell  sa    :: 6T(((T[[-B-B-D-W f 	]A1rB   c                     U R                   c  g S U l        U R                  R                  / 5        U R	                  5         g r   )ra   r`   r   r   r   r   s    r?   deselect_cellChartMatrixView.deselect_cell  s0    ::"

rrB   c                 j    U R                   X4:X  a  U R                  5         g U R                  X5        g r   )r`   r   r   r   s      r?   _click_cellChartMatrixView._click_cell  s,    1&( Q"rB   c                 t    U R                   " UR                  5       6   U R                  R                  U5        g r   )r   r   r   viewr<   r   s     r?   	view_edgeChartMatrixView.view_edge   s&    $))+&

rB   c                     U R                   c  g U R                  " UR                  5       6   U R                  R	                  U5        g r   )ra   r   r   r   markr  s     r?   	mark_edgeChartMatrixView.mark_edge$  s3    ::$))+&

rB   c                 V    U R                   c  g U R                  R                  U5        g r   )ra   r   unmarkr  s     r?   unmark_edgeChartMatrixView.unmark_edge*  s!    ::

$rB   c                     U R                   c  g U R                  " UR                  5       6   U R                  R	                  U5        g r   )ra   r   r   r   markonlyr  s     r?   markonly_edgeChartMatrixView.markonly_edge/  s5    ::$))+&

D!rB   c           
         U R                   c  g S=pSnU R                  nUR                  S5        U R                  R	                  5       S-   n[        US   5      U-
  U-  n[        US   5      U-
  U-
  U-  nUR                  S5        [        U5       H  nUR                  US-
  X-  US-  -   U-   [        U5      SS	9  UR                  X-  US-  -   U-   XW-  U-   S-   [        U5      S
S	9  UR                  UXxS-   -  U-   Xe-  U-   XxS-   -  U-   SS9  UR                  Xh-  U-   UXh-  U-   Xu-  U-   SS9  M     UR                  XXU-  -   Xu-  U-   SS9  [        U5       V	Vs/ s H  n	[        U5       Vs/ s H  nS PM     snPM!     snn	U l        [        U5       Ho  n[        X5       H]  n	UR                  X-  U-   X-  U-   U	S-   U-  U-   US-   U-  U-   SS9n
XR                  U   U	'   XU	4S jnUR                  U
SU5        M_     Mq     [        US   5      [        US   5      pUR                  SSUS-   US-   SSSS9n
UR                  U
5        U R                  5         g s  snf s  snn	f )N   r   allr   r   r   r   r   )rv   anchorn.)dashr   r   r   c                 &    UR                  X#5        g r   )r  )r   r<   rM   r   s       r?   r    ChartMatrixView.draw.<locals>.cbn  s    $$Q*rB   
<Button-1>id   gray50r   r   )r}   r   tag)ra   r   deleter]   
num_leavesintr   create_textreprcreate_linecreate_rectangler^   tag_bind	tag_lowerr   )r<   LEFT_MARGIN
BOT_MARGIN
TOP_MARGINcr   dxdyrM   r   tr   xmaxymaxs                 r?   rl   ChartMatrixView.draw5  s   ::#%%
LL	KK""$q(!G*o+q0!H++j8A=	 qAMMa"q&:!=DGTW   MMa+-#a'!W	   MM!ez)$!ez)   MM$$#  # 4 	
[6%926J;NVW 	 	

 :?qBAeAh/hh/BqA1[&&F[(FZ'UbL;.UbL:-! '  %&Aq!#' + 

1lB/ ! " 7_c!H+&6d3J3J  
 	
A 	? 0Bs   'I1:I,I1,I1c                 <    U R                   R                  " U0 UD6  g r   ra   r~   r<   r   kwargss      r?   r~   ChartMatrixView.pack      

((rB   )
rj   r   r^   r]   r   r_   rk   ri   ra   r`   )TChart MatrixFr   )rR   rS   rT   rU   __doc__rq   re   rf   rh   rg   rd   r   r   r   r   r   r   r   r   r   r  r  r  r  r  rl   r~   rW   rQ   rB   r?   rY   rY   {   s    
 QV<81E#HJ82# 
"L\)rB   rY   c                   d    \ rS rSrSS jrSS jrS rS rS rS r	S	 r
S
 rS rS rS rS rSrg)ChartResultsViewi  c                    X l         X0l        / U l        SU l        / U l        S U l        S U l        U(       aR  [        U5      U l        U R                  R                  S5        U R                  R                  SU R                  5        O[        U5      U l        U(       a  [        U R                  5      nUR                  SSSS9  [        USU R                  S	9R                  S
S9  [        USU R                  S	9R                  SS9  [        USU R                   S	9R                  SS9  [#        U R                  SS9U l        U R$                  R                  SSSS9  U R'                  5         g )Nr   z!Chart Parser Application: Resultsr\   rx   r   xrz   rt   ru   rightr{   z	Print AllleftzPrint Selectionr   )closeenoughr   r   r   )r]   _grammar_trees_y_treewidgets
_selection
_selectboxr   ra   rb   rc   rd   r   r~   r   	print_allprint_selectionr   _cframer   )r<   rm   rn   grammarro   buttonss         r?   rq   ChartResultsView.__init__  s4   !&)DJJJ@AJJOOM4<<8vDJ DJJ'GLLhqsL;7>CCCQ7dnnEJJPVJW7!2D<P<PQVV W 
 #4::2>uQV< 	rB   Nc                    U R                   c  g Ub\  UR                  5       U R                  R                  5       :w  a  g UR	                  5       SU R
                  R                  5       4:w  a  g U R
                  R                  U R                  R                  5       5       H%  nX R                  ;  d  M  U R                  U5        M'     g r   )
ra   rE   rF  r   r   r]   r$  parsesrG  _add)r<   r   parses      r?   r   ChartResultsView.update  s    ::xxzT]]0022yy{q$++"8"8":;;[[''(;(;(=>EKK'		%  ?rB   c                 t   U R                   R                  U5        U R                  R                  5       n[	        X!5      nU R
                  R                  U5        U R                  R                  USU R                  5        UR                  U R                  5        UR                  5       S   S-   U l        g )Nr   r   )rG  rD   rN  canvasr   rI  
add_widgetrH  
bind_click_clickbbox)r<   rU  r/  
treewidgets       r?   rT  ChartResultsView._add  s    5! LL!(2
 	  ,
B8 	dkk* //#A&+rB   c           	          U R                   R                  5       nU R                  b  UR                  U R                  5        Xl        UR                  5       u  p4pVUR                  X4XVSSS9U l        g )Nr   z#088)r   r   )rN  rX  rJ  r#  rK  r\  r)  )r<   widgetr/  x1y1x2y2s          r?   r[  ChartResultsView._click  s^    LL!??&HHT__% !;;=,,RR1f,UrB   c                     X!R                  5       S'   UR                  5        H/  n[        U[        5      (       a  U R	                  X25        M+  X#S'   M1     g )Nr   )labelsubtreesrI   r   _color)r<   r]  r   childs       r?   ri  ChartResultsView._color  sG    &+7#((*E%!233E)!&g	 +rB   c                 T    U R                   c  g U R                  R                  5         g r   )ra   rN  print_to_filer   s     r?   rL  ChartResultsView.print_all  s    ::""$rB   c                 z   U R                   c  g U R                  c  [        SS5        g U R                  R	                  5       nU R
                   H.  nX0R                  Ld  M  U R                  R                  U5        M0     UR                  U R                  5        U R                  R                  5       u  pEpgU R                  R                  SU-
  SU-
  5        SXd-
  S-    SXu-
  S-    3US'   U R                  R                  5         U R                  /U l        U R                  5         U R                  5         g )NzPrint ErrorzNo tree selectedr   z0 0 r    scrollregion)ra   rJ  r   rN  rX  rI  destroy_widgetr#  rK  r\  moverm  clearr   )r<   r   r/  r`  ra  rb  rc  rd  s           r?   rM   ChartResultsView.print_selection  s    ::??"m%78##%A++0LL//7 , HHT__%#335RROO  b"r'2"&rw|nAbgl^ DAnLL&&( "& 1DJJLKKMrB   c                 4   U R                   c  g U R                   H  nU R                  R                  U5        M      / U l        / U l        U R
                  b3  U R                  R                  5       R                  U R                  5        S U l        SU l	        g )Nr   )
ra   rI  rN  rr  rG  rJ  rX  r#  rK  rH  )r<   r]  s     r?   rt  ChartResultsView.clear  sw    ::++JLL''
3 ,??&LL!((9rB   c                 P    U R                  5         Xl        U R                  5         g r   )rt  r]   r   r   s     r?   r   ChartResultsView.set_chart  s    

rB   c                 P    U R                  5         Xl        U R                  5         g r   )rt  rF  r   r<   rO  s     r?   set_grammarChartResultsView.set_grammar	  s    

rB   c                 r    U R                   c  g  U R                   R                  5         S U l         g !    N= fr   r   r   s     r?   rd   ChartResultsView.destroy  r   r   c                 <    U R                   R                  " U0 UD6  g r   r7  r8  s      r?   r~   ChartResultsView.pack  r;  rB   )	rN  r]   rF  ra   rK  rJ  rG  rI  rH  )Tr   )rR   rS   rT   rU   rq   r   rT  r[  ri  rL  rM  rt  r   r|  rd   r~   rW   rQ   rB   r?   r?  r?    s@    B!,$V'%
*


)rB   r?  c                       \ rS rSrSrS\R                  S   \R                  S   S.rS rS r	S	 r
S
 rS rS rS rS rS rS rS rSS/rS rS rS rS rS rS rS rS rS rS rS rS rS  r S! r!S"r"g#)$ChartCompareri   a  

:ivar _root: The root window

:ivar _charts: A dictionary mapping names to charts.  When
    charts are loaded, they are added to this dictionary.

:ivar _left_chart: The left ``Chart``.
:ivar _left_name: The name ``_left_chart`` (derived from filename)
:ivar _left_matrix: The ``ChartMatrixView`` for ``_left_chart``
:ivar _left_selector: The drop-down ``MutableOptionsMenu`` used
      to select ``_left_chart``.

:ivar _right_chart: The right ``Chart``.
:ivar _right_name: The name ``_right_chart`` (derived from filename)
:ivar _right_matrix: The ``ChartMatrixView`` for ``_right_chart``
:ivar _right_selector: The drop-down ``MutableOptionsMenu`` used
      to select ``_right_chart``.

:ivar _out_chart: The out ``Chart``.
:ivar _out_name: The name ``_out_chart`` (derived from filename)
:ivar _out_matrix: The ``ChartMatrixView`` for ``_out_chart``
:ivar _out_label: The label for ``_out_chart``.

:ivar _op_label: A Label containing the most recent operation.
-intersectionunion)r  andorc                    S/S-  n[        U5      U l        SU l        SU l        U R                  U l        U R                  U l        SU R                  0U l        U R                  U l        S U l        [        5       U l
        U R                  R                  S5        U R                  R                  SU R                  5        U R                  R                  SU R                  5        U R                  U R                  5        U R                  U R                  5        U R!                  U R                  5        U R#                  U R                  5        U R%                  U R                  5        U H  nU R'                  U5        M     g )N    NonezChart Comparisonr\   <Control-x>)r   _emptychart
_left_name_right_name_left_chart_right_chart_charts
_out_chart	_operatorr   ra   rb   rc   rd   _init_menubar_init_chartviews_init_divider_init_buttons_init_bindings
load_chart)r<   chart_filenamesfaketokfilenames       r?   rq   ChartComparer.__init__B  s2    $( > !!++ ,,  0 01 **  T


+,

t||4

t||4 	4::&djj)4::&4::&DJJ' (HOOH% (rB   c                 r    U R                   c  g  U R                   R                  5         S U l         g !    N= fr   r   r   s     r?   rd   ChartComparer.destroyh  r   r   c                     g r   )ra   mainloopr8  s      r?   r  ChartComparer.mainloopq  s    rB   c                 v   [        U5      n[        USS9nUR                  SSSU R                  S9  UR                  SSSU R                  S9  UR	                  5         UR                  SS	U R
                  S
S9  UR                  SSUS9  [        USS9nUR                  SU R                  SS9  UR                  SU R                  SS9  UR                  SU R                  SS9  UR	                  5         UR                  SU R                  S9  UR                  SSUS9  U R                  R                  US9  g )Nr   tearoff
Load ChartCtrl-o)rg  acceleratorr4   rw   zSave OutputCtrl-sExitr   Ctrl-xrg  r4   rw   r  Filerg  r4   menuIntersection+)rg  rw   r  Union*
Differencer  Swap Chartsrg  rw   Comparer  )r	   add_commandload_chart_dialogsave_chart_dialogadd_separatorrd   add_cascade_intersection_union_difference_swapchartsra   config)r<   r   menubarfilemenuopmenus        r?   r  ChartComparer._init_menubary  sf   t* + **	 	 	
 	 **	 	 	
 	 At|| 	 	
 	&AHE gq) $*<*<# 	 	
 	$++3O(8(8c 	 	
 	8H8HI)qvF 	

w'rB   c                 <    [        USSS9nUR                  SSSS9  g )Nr   r   r   r   rA  )r{   r}   ipady)r   r~   )r<   r   dividers      r?   r  ChartComparer._init_divider  s"    Qx8%c3rB   c                    SnSn[        USS9nUR                  SSSS9  [        US	S
S9nUR                  SSSSSS9  [        U[        U R                  R                  5       5      U R                  S9U l        U R                  R                  SSSS9  [        XPR                  SSS9U l
        U R                  R                  SSSSSS9  U R                  R                  SU R                  5        U R                  R                  SU R                  5        U R                  R                  5         [        USS	SUS9U l        U R                   R                  SSSS9  [        US	S
S9nUR                  SSSSSS9  [        U[        U R                  R                  5       5      U R"                  S9U l        U R$                  R                  SSSS9  [        X`R                  SSS9U l        U R&                  R                  SSSSSS9  U R&                  R                  SU R                  5        U R&                  R                  SU R                  5        U R&                  R                  5         [        USS	SUS9R                  SSSS9  [        US	S
S9nUR                  SSSSSS9  [        USS9U l        U R(                  R                  SS S!9  [        XpR                  SSS9U l        U R*                  R                  SSSSSS9  U R*                  R                  SU R                  5        U R*                  R                  SU R                  5        U R*                  R                  5         g )"N)r1   )r8   r  z#c0c0c0r   r   r   r   rz   r   groover   rD  r     )r{   r   r   r|   r}   )rw   r   rA  )r{   r   r}   FT)ro   rp   rx   r   r   rp  )rv   r   r   r3   )r{   r   r   =Outputr   	   )r{   r   )r   r~   r   r   r  r   _select_left_left_selectorrY   r  _left_matrixr   select_edger   r   r   	_op_label_select_right_right_selector_right_matrix
_out_label_out_matrix)r<   r   opfonteqfontframe	cv1_frame	cv2_frame	out_frames           r?   r  ChartComparer._init_chartviews  s@    #dy1

af
5 %(;	F16J/tDLL--/0$:K:K
 	  e!# >+''%t
 	H11QVT&&x1A1AB&&}d6F6FG$$& 1
 	aa8 %(;	F16J0tDLL--/0$:L:L 
 	!!u13!?,''%t
 	XAAafU''$2B2BC''t7G7GH%%' 	e#Q96JOOaa 	P 	

 %(;	F16J	9%a0*''%t
 	8!!AFS%%h0@0@A%%mT5E5EF##%rB   c                    [        U5      nUR                  SSSSS9  [        USU R                  S9R                  SS	9  [        US
U R                  S9R                  SS	9  [        USU R
                  S9R                  SS	9  [        USS9R                  SS	9  [        USU R                  S9R                  SS	9  [        USU R                  S9R                  SS	9  g )Nrx   r   rA  r   )r{   r   r}   r|   r  ru   rD  rC  r  r  r   r  r  zDetach OutputrB  )r   r~   r   r  r  r  r  _detach_out)r<   r   rP  s      r?   r  ChartComparer._init_buttons  s    +(Q?w^T5G5GHMM 	N 	
 	wWdkk:??V?Lw\43C3CDIIvIVgR %%6%2w]D4D4DEJJPVJWw_d6F6FGLL 	M 	
rB   c                 <    UR                  SU R                  5        g )N<Control-o>)rc   r  r   s     r?   r  ChartComparer._init_bindings  s    		-!7!78rB   c                     Xl         U R                  U   U l        U R                  R	                  U R                  5        US:X  a  U R                  R                  5         U R                  5         g Nr  )r  r  r  r  r   r   	_apply_opr<   names     r?   r  ChartComparer._select_left  sU    <<-##D$4$456>((*rB   c                     Xl         U R                  U   U l        U R                  R	                  U R                  5        US:X  a  U R                  R                  5         U R                  5         g r  )r  r  r  r  r   r   r  r  s     r?   r  ChartComparer._select_right  sV     LL.$$T%6%676>))+rB   c                     U R                   S:X  a  U R                  5         g U R                   S:X  a  U R                  5         g U R                   S:X  a  U R                  5         g g )Nr  r  r  )r  r  r  r  r   s    r?   r  ChartComparer._apply_op	  sL    >>S ^^t#KKM^^u$  %rB   zPickle file.picklez	All filesr  c                     [        U R                  SS9nU(       d  g  [        US5       n[        R                  " U R
                  U5        S S S 5        g ! , (       d  f       g = f! [         a  n[        SSU< SU 35         S nAg S nAff = f)Nr  	filetypesdefaultextensionwbzError Saving ChartUnable to open file: 
)r   CHART_FILE_TYPESopenpickledumpr  	Exceptionr   r<   r   r  outfiler   s        r?   r  ChartComparer.save_chart_dialog  s~    $++i
 	Wh%DOOW5 &%% 	W*.CH<rRSQT,UVV	Ws4   A& "AA& 
A#A& #A& &
B0BBc                     [        U R                  SS9nU(       d  g  U R                  U5        g ! [         a  n[	        SSU< SU 35         S nAg S nAff = f)Nr  r  zError Loading Chartr  r  )r   r   r  r  r   )r<   r   r  r   s       r?   r  ChartComparer.load_chart_dialog"  sb    "++i
 	XOOH% 	X+/DXLPRSTRU-VWW	Xs   0 
AAAc                    [        US5       n[        R                  " U5      nS S S 5        [        R                  R                  U5      nUR                  S5      (       a  US S nUR                  S5      (       a  US S nWU R                  U'   U R                  R                  U5        U R                  R                  U5        U R                  U R                  L a  U R                  R                  U5        g U R                  U R                  L a  U R                  R                  U5        g g ! , (       d  f       GN= f)Nrbr  iz.charti)r  r  loadospathbasenameendswithr  r  addr  r  r  r   r  )r<   r  infilern   r  s        r?   r  ChartComparer.load_chart-  s    (D!VKK'E "ww)==##9D==""9D"T%  & t///##D)$"2"22  $$T* 3 "!s   D11
E c                     U R                   R                  5         U R                  R                  5         U R                  R                  5         g r   )r  r   r  r  r   s    r?   _update_chartviews ChartComparer._update_chartviews@  s8      "!!#!rB   c                    XR                   ;   a  U R                  R                  U5        OU R                  R                  5         XR                  ;   a  U R
                  R                  U5        OU R
                  R                  5         XR                  ;   a  U R                  R                  U5        g U R                  R                  5         g r   )r  r  r  r  r  r  r  r  r  s     r?   r  ChartComparer.select_edgeI  s    ###++D1))+$$$,,T2**,??"**40((*rB   c                     U R                   R                  X5        U R                  R                  X5        U R                  R                  X5        g r   )r  r   r  r  r   s      r?   r   ChartComparer.select_cellW  s>    %%a+&&q,$$Q*rB   c                    U R                  5       (       d  g [        U R                  R                  5       5      nU R                   H&  nX R                  ;  d  M  UR                  U/ 5        M(     U R                  SU5        g )Nr  _checkcompatr   r  tokensr  insert_updater<   	out_chartr   s      r?   r  ChartComparer._difference`  si      ""$**1134	$$D,,,  r* % 	S)$rB   c                    U R                  5       (       d  g [        U R                  R                  5       5      nU R                   H&  nX R                  ;   d  M  UR                  U/ 5        M(     U R                  SU5        g )Nr  r  r!  s      r?   r  ChartComparer._intersectionk  si      ""$**1134	$$D(((  r* % 	UI&rB   c                 .   U R                  5       (       d  g [        U R                  R                  5       5      nU R                   H  nUR	                  U/ 5        M     U R
                   H  nUR	                  U/ 5        M     U R                  SU5        g )Nr  )r  r   r  r  r  r  r   r!  s      r?   r  ChartComparer._unionv  s}      ""$**1134	$$DT2& %%%DT2& & 	T9%rB   c                     U R                   U R                  p!U R                  R                  U5        U R                  R                  U5        g r   )r  r  r  r   r  )r<   rD  rB  s      r?   r  ChartComparer._swapcharts  s;    oot'7'7e&  &rB   c                    U R                   R                  5       U R                  R                  5       :w  dj  U R                   R                  5       U R                  R                  5       :w  d4  U R                   U R                  :X  d  U R                  U R                  :X  a`  U R                  U l        U R                  R                  U R
                  5        U R                  R                  5         SU R                  S'   gg)Nr  rv   FT)
r  r  r  property_namesr  r  r  r   r   r  r   s    r?   r  ChartComparer._checkcompat  s    ##%):):)A)A)CC..0D4E4E4T4T4VV4#3#33  D$4$44 #..DO&&t7'')&.DOOF#rB   c                    Xl         U R                  U   U R                  S'   X l        U R                  R                  U5        SR                  U R                  U R                   U R                  5      U R                  S'   g )Nrv   z{} {} {})
r  	_OPSYMBOLr  r  r  r   r   r  r  r  )r<   operatorr"  s      r?   r   ChartComparer._update  sg    !!%!9v#""9-","3"3OONN#
rB   c                     U R                   U l        U R                  R                  U R                  5        SU R                  S'   U R                  R                  5         g )Nrp  rv   )r  r  r  r   r  r   r   s    r?   _clear_out_chartChartComparer._clear_out_chart  sG    **""4??3!$v##%rB   c                 \    [        U R                  U R                  U R                  S   S9  g )Nrv   )rb   )rY   ra   r  r  r   s    r?   r  ChartComparer._detach_out  s    

DOO4??6;RSrB   )r  r  r  r  r  r  r  r  r  r  r  r  r  r  r  ra   N)#rR   rS   rT   rU   r=  r   rV   r.  rq   rd   r  r  r  r  r  r  r  r  r  r   r  r  r  r  r  r   r  r  r  r  r  r   r2  r  rW   rQ   rB   r?   r  r     s    8 ##N3""7+I$&L-%(N4;&z
9! 34FG
W	X+&"++	%	'
&'
"	
&TrB   r  c                       \ rS rSrSrSrSrSrSrS*S jr	S r
S+S	 jrS
 rS rS rS rS rS rS rS rS*S jrS rS rS,S jrS rS rS-S jrS.S jrS*S jrS.S jrS rS rS r S r!S  r"S*S! jr#S" r$S# r%S,S$ jr&S% r'S& r(S*S' jr)S( r*S)r+g)/	ChartViewi  as  
A component for viewing charts.  This is used by ``ChartParserApp`` to
allow students to interactively experiment with various chart
parsing techniques.  It is also used by ``Chart.draw()``.

:ivar _chart: The chart that we are giving a view of.  This chart
   may be modified; after it is modified, you should call
   ``update``.
:ivar _sentence: The list of tokens that the chart spans.

:ivar _root: The root window.
:ivar _chart_canvas: The canvas we're using to display the chart
    itself.
:ivar _tree_canvas: The canvas we're using to display the tree
    that each edge spans.  May be None, if we're not displaying
    trees.
:ivar _sentence_canvas: The canvas we're using to display the sentence
    text.  May be None, if we're not displaying the sentence text.
:ivar _edgetags: A dictionary mapping from edges to the tags of
    the canvas elements (lines, etc) used to display that edge.
    The values of this dictionary have the form
    ``(linetag, rhstag1, dottag, rhstag2, lhstag)``.
:ivar _treetags: A list of all the tags that make up the tree;
    used to erase the tree (without erasing the loclines).
:ivar _chart_height: The height of the chart canvas.
:ivar _sentence_height: The height of the sentence canvas.
:ivar _tree_height: The height of the tree

:ivar _text_height: The height of a text string (in the normal
    font).

:ivar _edgelevels: A list of edges at each level of the chart (the
    top level is the 0th element).  This list is used to remember
    where edges should be drawn; and to make sure that no edges
    are overlapping on the chart view.

:ivar _unitsize: Pixel size of one unit (from the location).  This
   is determined by the span of the chart's location, and the
   width of the chart display canvas.

:ivar _fontsize: The current font size

:ivar _marks: A dictionary from edges to marks.  Marks are
    strings, specifying colors (e.g. 'green').
r      (   Nc                    UR                  SS5      nUR                  SS5      nUR                  SS5      U l        Xl        0 U l        / U l        0 U l        0 U l        / U l        SU l        SU l	        / U l
        SU l        UcZ  [        5       nUR                  S5        U4S	 jnU4S
 jnUR                  SU5        [        USUS9n	U	R!                  SS9  X`l        OX l        U R%                  U5        U R'                  U R"                  5      u  U l        U l        SU R*                  S'   SU R*                  S'   U(       a]  [-        U R"                  SSS9n
U
R!                  SSS9  [/        U
SS9U l        SU R0                  S'   U R0                  R!                  SS9  OSU l        U(       a=  U R'                  U R"                  SS5      u  pXsU l        U l        S U R4                  S'   OSU l        U R7                  5         U R9                  5         U R;                  5         U R=                  5         U R*                  R                  S!U R>                  5        g)"z$
Construct a new ``Chart`` display.
	draw_treer   draw_sentencer   fontsizer9   Nz
Chart Viewc                 $    UR                  5         g r   rd   )r   r   s     r?   destroy1$ChartView.__init__.<locals>.destroy1      rB   c                 $    U R                  5         g r   r?  )r   s    r?   destroy2$ChartView.__init__.<locals>.destroy2  rB  rB   qDoneru   rx   rC  i,  r   r  rE  sunkr   r   r   r   )r}   r{   r   r   #e0e0e0r   r   r  rA  r   z<Configure>) get	_fontsizer]   rj   _edgelevels	_edgetagsr_   	_treetoks_treetoks_edge_treetoks_index
_tree_tags_compactr   rb   rc   r   r~   ra   _init_fonts
_sb_canvas	_chart_sb_chart_canvasr   r   _sentence_canvas_tree_sb_tree_canvas_analyzerl   _resize_grow
_configure)r<   rn   r   kwr;  r<  r   r@  rD  br   sbrX  s                r?   rq   ChartView.__init__  s(   
 FF;*	2
C0     "    <$CIIl# #  !  HHS(#s:AFFF!JJ 	 04tzz/J,+'*8$,.=) 4::fQ?FKKV(K3$*6"$=D!2;D!!,/!!&&F&3 %)D! ??4::sC@LR24.T]D-*-Dh' $D 			

 	t?rB   c                     [        SSU R                  S9U l        [        SU R                  S9U l        [        [	        5       S   S9U l        UR                  SU R
                  5        g )Nr8   r:   familyweightsizerf  rh  r3   r3   *Font)r   rM  	_boldfont_fontr   _sysfont
option_addr   s     r?   rU  ChartView._init_fonts?  sM    [dnnU4>>B
&(6"23/rB   c                     [        USSS9nUR                  X2US9  [        USS9n[        USS9nUR                  S	S
S9  UR                  SUSS9  UR                  US'   UR
                  US'   Xv4$ )z;
Helper for __init__: construct a canvas with a scrollbar.
rH  r   rI  )r}   r|   r{   rK  r  vertical)orientrB  yr{   r}   rD  yesr{   r}   r|   rw   yscrollcommand)r   r~   r   r
   yviewr   )r<   r   r|   r}   r{   r   rX  rb  s           r?   rV  ChartView._sb_canvasF  s     tF154895 vj1
W3'd59 9#%66 |rB   c                 >    U R                   R                  SSS5        g )NscrollunitsrX  ry  r   s     r?   	scroll_upChartView.scroll_upY        2w7rB   c                 >    U R                   R                  SSS5        g )Nr|  r   r~  r  r   s     r?   scroll_downChartView.scroll_down\        1g6rB   c                 >    U R                   R                  SSS5        g )Nr|  r}  pagesr  r   s     r?   page_upChartView.page_up_  r  rB   c                 >    U R                   R                  SSS5        g )Nr|  r   r  r  r   s     r?   	page_downChartView.page_downb  r  rB   c                    U R                   R                  5       n[        [        U R                  S   5      XR
                  -  [        R                  S-  -   5      nU R                  R                  US9  U R                  R                  U R                  S   S9  US[        R                  -  -
  U-  U l        U R                  b  U R                  U R                  S'   gg)z
Grow the window, if necessary
r   r   r  r   rJ  N)r]   r$  r   r%  rX  	_unitsizer7  _MARGIN	configurerY  _sentence_height)r<   r   r   s      r?   r^  ChartView._growe  s    
 KK""$""7+,a...@9CTCTWXCX.X
 	$$5$1$$D,>,>x,H$I!i&7&7"771<   ,.2.C.CD!!(+ -rB   c                 6   U R                   R                  [        U5      * S9  U R                  R                  [        U5      * S9  U R                  R                  [        U5      * S9  U R                  5         U R                  5         U R                  5         g N)rh  )rm  r  absrl  rn  r\  r^  rl   r<   rh  s     r?   set_font_sizeChartView.set_font_sizez  sn    

3t9*-  s4yj 1c$iZ0

		rB   c                 ,    [        U R                  5      $ r   )r  rM  r   s    r?   get_font_sizeChartView.get_font_size  s    4>>""rB   c                     U R                   R                  5       nUR                  S[        R                  -  -
  U-  U l        U R                  5         g)z
The configure callback.  This is called whenever the window is
resized.  It is also called when the window is first mapped.
It figures out the unit size, and redraws the contents of each
canvas.
r   N)r]   r$  r   r7  r  r  rl   )r<   r   r   s      r?   r_  ChartView._configure  s?     KK""$''A	(9(9$99Q>		rB   c                 ^   Ube  Xl         / U l        0 U l        U R                  5         U R	                  5         U R                  5         U R                  5         U R                  5         gU R                    H%  nX R                  ;  d  M  U R                  U5        M'     U R                  5         g)a=  
Draw any edges that have not been drawn.  This is typically
called when a after modifies the canvas that a CanvasView is
displaying.  ``update`` will cause any edges that have been
added to the chart to be drawn.

If update is given a ``chart`` argument, then it will replace
the current chart with the given chart.
N)
r]   rN  r_   r\  r^  rl   
erase_treer]  rO  	_add_edge)r<   rn   r   s      r?   r   ChartView.update  s{     K!DDKMMOJJLIIKOOLLN~~-NN4( $ LLNrB   c                     UR                  5       u  p4U R                  U    HL  nUR                  5       u  pgX6s=::  a  U:  d+  O  Xcs=::  a  U:  d  O  X6s=:X  a  Us=:X  a
  U:X  d  MF     g  ML    g   g)z
Return True if the given edge overlaps with any edge on the given
level.  This is used by _add_edge to figure out what level a
new edge should be added to.
TF)r   rN  )r<   r   lvls1e1	otheredges2e2s           r?   _edge_conflictChartView._edge_conflict  se     99;))#.I ~~'HR22=b=b6J"6J6J 7K / rB   c           
         U R                   n[        U[        5      (       a  UR                  5       n/ nUR	                  5        H\  n[        U[
        5      (       a*  UR                  [        UR                  5       5      5        MB  UR                  [        U5      5        M^     SR                  U5      nOUR                  5       nSnX64 H  nUR                  SSXpR                  SSS9nUR                  U5      n	UR                  U5        U	S   n
[        UR!                  5       S5      n[        U R"                  X-  5      U l        [        U R$                  U	S	   U	S   -
  5      U l        M     g
)z
Given a new edge, recalculate:

    - _text_height
    - _unitsize (if the edge text is too big for the current
      _unitsize, then increase _unitsize)
rp  r  r   nwrD  rv   r3   r  justifyr   r   r   N)rX  rI   r&   rE   rH   r   rD   strr1   r'  joinr&  rl  r\  r#  r   lengthr  _text_height)r<   r   r/  rE   rhseltsrN   rH   sr"  r\  r   edgelens               r?   _analyze_edgeChartView._analyze_edge  s)    dH%%((*CGxxzc;//NN3szz|#45NN49-	 "
 ((7#C((*CCA--11>>$   C 66#;DHHSMGE$++-+G ADN #D$5$5tAwa7H ID rB   c                    [        U[        5      (       a  gXR                  ;   a  gU R                  U5        U R	                  5         U R
                  (       dV  U R                  R                  U/5        [        U R                  5      S-
  nU R                  X5        U R                  5         gSn U[        U R                  5      :  aF  U R                  R                  / 5        U R                  5         U[        U R                  5      :  a  MF  X2:  a5  U R                  X5      (       d  U R                  U   R                  U5        OUS-  nM  U R                  X5        g)z
Add a single edge to the ChartView:

    - Call analyze_edge to recalculate display parameters
    - Find an available level
    - Call _draw_edge
Nr   r   )rI   r    rO  r  r^  rT  rN  rD   r   
_draw_edger]  r  )r<   r   minlvlr  s       r?   r  ChartView._add_edge  s"    dH%%>>!4 

}}##TF+d&&'!+COOD&LLN T--..  ''+ T--..
 }T%8%8%C%C  %,,T2 1HC  	"rB   c                 x   S n[        [        U R                  5      5       H  nXR                  U   ;   d  M  Un  O   Uc  g US-   U R                  -  nU R                  S-   nU R
                  R                  SS5        U R                  S:w  a,  U R
                  R                  SXE-
  U R                  -  5        g g )Nr   r   movetog      ?r   )r   r   rN  _chart_level_sizer  rX  ry  _chart_height)r<   r   levelrM   rt  r1  s         r?   r  ChartView.view_edge  s    s4++,-A''** . =QY$000#  3/"$$X$:L:L/LM #rB   c           	         U R                   nUR                  5       U R                  -  [        R                  -   nUR                  5       U R                  -  [        R                  -   nXT:X  a  U[        SU R                  S-  5      -  nUS-   U R                  -  nUR                  XFXVSSS9n[        U[        5      (       a  / nUR                  5        H\  n	[        U	[        5      (       a*  UR                  [        U	R                  5       5      5        MB  UR                  [!        U	5      5        M^     UR#                  5       n
O/ nSn
SR%                  US	U
 5      nSR%                  XS	 5      nUR'                  US-   XkU R(                  S
S9nUR+                  U5      S   S-   nUR+                  U5      S   UR+                  U5      S   -   S-  nUR-                  US-
  US-
  US-   US-   5      nUR'                  US-   XlU R(                  S
S9nUR'                  XE-   S-  U[        UR/                  5       5      SU R0                  S9nX}UUU4U R2                  U'   X4S jnUR5                  USU5        UR5                  USU5        UR5                  USU5        UR5                  USU5        UR5                  USU5        U R7                  U5        g	)z&
Draw a single edge on the ChartView.
   r   r   lastr   )r0   r   r   rp  Nr  )rv   r3   r  r      r  rv   r  r3   c                 (    UR                  SU5        g r   r   )r   r<   r   s      r?   r    ChartView._draw_edge.<locals>.cb<  r   rB   r  )rX  r   r  r7  r  r   r   r  r(  rI   r&   rH   r   rD   r  r1   r'  r5   r  r&  rm  r\  create_ovalrE   rl  rO  r*  _color_edge)r<   r   r  r/  ra  rc  rt  linetagrH   rN   posrhs1rhs2rhstag1dotxdotydottagrhstag2lhstagr   s                       r?   r  ChartView._draw_edge  s     ZZ\DNN*Y->->>XXZ$..(9+<+<<8#a!+,,B1W...--rF!-D dH%%Cxxzc;//JJs3::<01JJtCy)	 "
 ((*CCCxxDS	"xxD	"--Q4::d-Svvgq!A%w"QVVG_Q%771<tax4!8TAXF--q!TZZPT-UWM13txxz?3T^^  

 !(&'6Jt   	1 	


7L"-	

7L"-	

7L"-	

6<,	

6<,rB   c                    XR                   ;  a  gU R                  nUb  Ub  XR                  ;   a  U R                  U   nU R                   U   nUR                  US   US9  UR                  US   US9  UR                  US   X3S9  UR                  US   US9  UR                  US   US9  gU R                  R                  5       nXR                  ;   a  U R                  U R                  U   5        UR                  5       (       a*  UR                  5       SU4:X  a  U R                  US	S
5        g[        U[        5      (       a  U R                  USS5        gU R                  USS5        g)z}
Color in an edge with the given colors.
If no colors are specified, use intelligent defaults
(dependent on selection, etc.)
Nr   r   r   r   r}   r   r   r  #084#042z#48cz#246z#00fz#008)rO  rX  r_   r   r]   r$  r  rJ   r   rI   r    )r<   r   	linecolor	textcolorr/  tagsr   s          r?   r  ChartView._color_edgeG  sJ    ~~% Y%:{{" KK-	>>$'DLLayL1LLayL1LLayLDLLayL1LLayL1&&(A{{"  T!23!!diikaV&;  vv6D(++  vv6  vv6rB   c                 B    X R                   U'   U R                  U5        g)z
Mark an edge
N)r_   r  r<   r   r
  s      r?   r  ChartView.mark_edgef  s     !DrB   c                     UcE  [        U R                  R                  5       5      n0 U l        U H  nU R                  U5        M     gU R                  U	 U R                  U5        g)z
Unmark an edge (or all edges)
N)r   r_   r   r  )r<   r   old_marked_edgess      r?   r  ChartView.unmark_edgem  s\     <#DKK$4$4$67DK(  & ) D!T"rB   c                 F    U R                  5         U R                  X5        g r   )r  r  r  s      r?   r  ChartView.markonly_edgez  s    t"rB   c           
         SnSnU R                   nU R                  R                  5        H  nUR                  SS[	        U5      U R
                  SSS9nUR                  U5      nUR                  U5        US   [        R                  -   n[        Xq5      n[        X&S   US   -
  5      nM     Xl        X l        U R                  S[        R                  -  -   U l        U R                  R                  5        H  nU R!                  U5        M     U R                  S-  U l        S[        R$                  U R                  -   -  U l        U R)                  5         g	)
zi
Analyze the sentence string, to figure out how big a unit needs
to be, How big the tree should be, etc.
F   r   r  rD  r  r   r   r   N)rX  r]   leavesr&  r'  rm  r\  r#  r7  _LEAF_SPACINGr   r  r  r  r  r   r  r  _TREE_LEVEL_SIZE_tree_heightr]  )	r<   unitsizetext_heightr/  leafr"  r\  r   r   s	            r?   r\  ChartView._analyze~  s;     KK&&(D--14:DJJtV   C 66#;DHHSMGi555E5+Hk7T!W+<=K ) "' $ 1 1A	8I8I4I I KK%%'Dt$ ( "&!2!2Q!6 !;!;d>O>O!OP 	rB   c                 j   U R                   nU R                  R                  5       U R                  -  [        R
                  S-  -   n[        U R                  5      nUS-   U R                  -  U l	        SSX R                  4US'   U R                  (       a  SSX R                  4U R                  S'   gg)z
Update the scroll-regions for each canvas.  This ensures that
everything is within a scroll-region, so the user can use the
scrollbars to view the entire display.  This does *not*
resize the window.
r   r   rq  N)rX  r]   r$  r  r7  r  r   rN  r  r  r[  r  )r<   r/  r   levelss       r?   r]  ChartView._resize  s      &&(4>>9I<M<MPQ<QQT%%&$qjD,B,BB5*<*<=. 12Au>O>O0PDn- rB   c           	         SnU R                   nU R                  nU R                  n[        R                  n/ U l        [        SU R                  R                  5       S-   5       GH[  nX`R                  -  U-   nU(       a$  UR                  USXq5      nUR                  U5        U(       a.  UR                  USXpR                  5      n	UR                  U	5        UR                  USXq5      n
UR                  U
5        UR                  US-   S[        U5      SU R                  S9nUR                  U5        US-  S:X  aA  U(       a  UR!                  WSS9  U(       a  UR!                  W	SS9  UR!                  U
SS9  GM  U(       a  UR!                  WS	S9  U(       a  UR!                  W	S	S9  UR!                  U
S	S9  GM^     g
)z^
Draw location lines.  These are vertical gridlines used to
show where each location unit is.
iP  r   r   r   r  r  gray60r   gray80N)r[  rY  rX  r7  r  	_loclinesr   r]   r$  r  r(  r+  r  r&  r'  rm  r   )r<   BOTTOMc1c2c3marginrM   rA  t1t2t3t4s               r?   _draw_loclinesChartView._draw_loclines  s   
 """"q$++002Q67ANN"V+A^^Aq!4R ^^Aq!-B-BCR 1a0BLLAqtAwt$**UBLL
 1uzMM"8M4MM"8M4bx0MM"8M4MM"8M4bx09 8rB   c           
      ^   U R                   R                  5       S:X  a  gU R                  n[        R                  n[        R                  n[        U R                   R                  5       5       H  u  pEX@R                  -  U-   nX`R                  -   nXg-   S-  nUR                  X[        U5      U R                  SSS9n	UR                  U	5      n
UR                  US-   U
S   [        R                  S-  -
  US-
  U
S   [        R                  S-  -   S	S	S
9nUR                  U5        M     g)zDraw the sentence string.r   Nr   r  rD  r  r   r   z#f0f0f0r  )r]   r$  rY  r7  r  rG   r  r  r&  r'  rm  r\  r)  r  r+  )r<   r/  r  rt  rM   r  ra  rc  rA  r"  r\  rts               r?   _draw_sentenceChartView._draw_sentence  s   ;;!!#q(!!"" !3!3!56GA^^#f,Bnn$BAA--4:DJJsF   C 66#;D##QQ922Q67QQ922Q67! $ B KKO! 7rB   c                     U R                    H  nU R                  R                  U5        M      / U l        S U l        SU l        g r   )rS  r[  r#  rP  rQ  rR  )r<   r"  s     r?   r  ChartView.erase_tree  s;    ??C$$S) #" rB   c                 .   Uc  U R                   c  g Uc  U R                   nU R                   U:w  aU  U R                  R                  U5       Vs/ s H  n[        U[        5      (       d  M  UPM     snU l        Xl         SU l        [        U R
                  5      S:X  a  g U R                   H  nU R                  R                  U5        M      U R
                  U R                     nU R                  XAR                  5       5        U R                  5         U R                  R                  5       U R                  -  S[         R"                  -  -   nUR%                  5       [         R&                  U R(                  -   -  nSSXV4U R                  S'   g s  snf )Nr   r   rq  )rQ  r]   treesrI   r'   rP  rR  r   rS  r[  r#  _draw_treetokr   _draw_treecycler$  r  r7  r  r   r  r  )r<   r   r2  r"  treewhs          r?   r;  ChartView.draw_tree  sP   <D//7<&&D $&)-):):4)@X)@AJqRVDWa)@XDN"&#$D  t~~!# ??C$$S) # ~~d2234. 	 KK""$t~~5I<M<M8MMKKMY77$:K:KKL-.1L.)- Ys   F+Fc                     U R                   S-   [        U R                  5      -  U l         U R                  U R                  5        g r   )rR  r   rP  r;  rQ  r   s    r?   
cycle_treeChartView.cycle_tree"  s5     $ 4 4q 8C<OOt**+rB   c                    [        U R                  5      S::  a  g S[        U R                  5      -  nU R                  n[        R                  nU R
                  R                  5       U R                  -  U-   S-
  nUR                  USSXR                  S9nU R                  R                  U5        UR                  U5      u      pg[        [        U R                  5      5       H  nUS[        U R                  5      U-
  S-
  -  -
  n	XR                  :X  a  Sn
OSn
UR                  XS	-   U	S
-
  XyS	-
  US	-   U
SS9nU R                  R                  U5        X4S jnUR!                  USU5        M     g )Nr   z%d Treesr   ne)r  rv   r3   r   r  z#fffr   r   r   r  c                 0    X!l         UR                  5         g r   )rR  r;  )r   r<   rM   s      r?   r   %ChartView._draw_treecycle.<locals>.cbA  s    '($ rB   r  )r   rP  r[  r7  r  r]   r$  r  r&  rl  rS  rD   r\  r   rR  create_polygonr*  )r<   rg  r/  r  rB  r"  _rt  rM   rA  r}   r   s               r?   r  ChartView._draw_treecycle&  sY   t~~!# S00""&&(4>>9FBQFmmE1TNNmSs#VVC[
1a s4>>*+Ac$..1A59::A(((""r61q5!VQV$ # C OO""3'  $ ! JJsL"-# ,rB   c                     U R                   n[        R                  n/ nU Hq  n[        U[        5      (       a*  U R                  XrUS-   5      u  pUR                  U5        MB  UR                  SU-  S-   U R                  -  S-  U-   5        US-  nMs     U(       a  [        U5      [        U5      -  n	O SU-  S-   U R                  -  S-  U-   n	US-  nU[        R                  U R                  -   -  n
UR                  U	U
SS[        UR                  5       5      SU R                  S9nU R                   R                  U5        U
[        R                  -   U R                  -   n[#        Xa5       GH  u  p[        U[        5      (       aB  U(       a;  UR%                  U	XR                  -   UUSSS9nU R                   R                  U5        [        U[        5      (       aC  U(       d<  UR%                  U	XR                  -   UUSS	S
S9nU R                   R                  U5        [        U[        5      (       a  M  UR%                  U	XR                  -   USSSS9nU R                   R                  U5        GM
     X4$ )zm
:param index: The index of the first leaf in the tree.
:return: The index of the first leaf after the tree.
r   r   r  centerr  )r  r  rv   r}   r3   r  )r   r}   z#048z2 3)r   r}   r  i'  )r[  r7  r  rI   r'   r  rD   r  sumr   r  r  r&  r  rg  rl  rS  zipr(  )r<   treetokindexdepthr/  r  child_xsrj  child_xnodexnodeyr"  childychildxs                 r?   r  ChartView._draw_treetokG  sd   
 "" E%&&!%!3!3E%!)!L(UQ$.. @1 Dv MN
  MCM1E Y]dnn4q86AEQJE 33d6G6GGHmmW]]_%  
 	s# 333d6G6GG 3MF%&&5mm--- $  &&s+%&&umm--- $  &&s+eT**mm--- $  &&s+E 4H |rB   c                 8   U R                   (       a+  U R                   R                  S5        U R                  5         U R                  (       a+  U R                  R                  S5        U R	                  5         U R
                  R                  S5        0 U l        [        [        U R                  5      5       H*  nU R                  U    H  nU R                  X!5        M     M,     U R                   H  nU R                  U5        M     U R                  5         g)z!
Draw everything (from scratch).
r  N)r[  r#  r;  rY  r  rX  rO  r   r   rN  r  r]   r  r  )r<   r  r   s      r?   rl   ChartView.draw  s     $$U+NN  !!((/!!!%( T--./C((-* . 0 KKDNN4    	rB   c                 B    SU R                   R                  U0 5      U'   g r   r   r   s      r?   r   ChartView.add_callback  r   rB   c                 V    Uc  U R                   U	 g  U R                   U   U	 g !    g = fr   r   r   s      r?   r   ChartView.remove_callback  r   r   c                     XR                   ;  a  g [        U R                   U   R                  5       5       H  nU" U6   M
     g r   r   r   s       r?   r   ChartView._fire_callbacks  r   rB   )rl  rj   r]   rX  r  r  rW  rT  rN  rO  rm  rM  r  r_   ra   rY  r  rn  r  r[  r  rZ  rS  rP  rQ  rR  r  r   )rt  r   rx   )r   )NN)#0df),rR   rS   rT   rU   r=  r  r  r  _CHART_LEVEL_SIZErq   rU  rV  r  r  r  r  r^  r  r  r_  r   r  r  r  r  r  r  r  r  r  r\  r]  r  r  r  r;  r  r  r  rl   r   r   r   rW   rQ   rB   r?   r7  r7    s    ,\ MGW@r0&8787D*#	2 JD)#VN3j7>##$LQ('1R4!9@,.BN`28rB   r7  c                   D   ^  \ rS rSrSrU 4S jrU 4S jrU 4S jrSrU =r	$ )EdgeRulei  z
To create an edge rule, make an empty base class that uses
EdgeRule as the first base class, and the basic rule as the
second base class.  (Order matters!)
c                 l   > U R                   R                  S   nXl        UR                  S-
  U l        g r   )	__class__	__bases___edge	NUM_EDGES)r<   r   superr6  s      r?   rq   EdgeRule.__init__  s,    ((+
1,rB   c              '      >#    U R                   R                  S   nX0R                  4-  nUR                  " XU/UQ76  S h  vN   g  N7fr   )r6  r7  r8  apply)r<   rn   rO  r   r:  r6  s        r?   r=  EdgeRule.apply  sA     ((+**;;tG<e<<<s   A AA	Ac                 X   > U R                   R                  S   nUR                  U 5      $ r   )r6  r7  __str__)r<   r:  r6  s     r?   r@  EdgeRule.__str__  s&    ((+}}T""rB   )r9  r8  )
rR   rS   rT   rU   r=  rq   r=  r@  rW   __classcell__)r6  s   @r?   r4  r4    s    -
=
# #rB   r4  c                       \ rS rSrSrg)TopDownPredictEdgeRulei  rQ   NrR   rS   rT   rU   rW   rQ   rB   r?   rD  rD        rB   rD  c                       \ rS rSrSrg)BottomUpEdgeRulei  rQ   NrE  rQ   rB   r?   rH  rH    rF  rB   rH  c                       \ rS rSrSrg)BottomUpLeftCornerEdgeRulei  rQ   NrE  rQ   rB   r?   rJ  rJ    rF  rB   rJ  c                       \ rS rSrSrg)FundamentalEdgeRulei  rQ   NrE  rQ   rB   r?   rL  rL    rF  rB   rL  c                      \ rS rSrS5S jrS rS rS rS rS r	S r
S	 rS
 rS rS rS rS rS rS rS rS rS rS rSS/r/ SQrS rS rS rS rS rS rS rS r S  r!S! r"S" r#S# r$S$ r%S% r&S6S' jr'S( r(S7S) jr)S* r*S+ r+\," 5       /r-\." 5       /r/\0" 5       /r1\2" 5       /r3\4" 5       /r5\-\/-   \5-   r6\1\5-   r7\3\5-   r8S, r9S- r:S. r;S/ r<S0 r=S1 r>S2 r?S3 r@S4rAg&)8ChartParserAppi  c                    U R                  X5        S U l         [        5       U l        U R                  R                  U5        U R                  R	                  SU R
                  5        [        U R                  5      n[        U R                  5      n[        U R                  5      nUR                  SSS9  UR                  SSS9  UR                  SSSS9  U R                  U R                  5        U R                  5         U R                  U5        U R                  U5        U R                  U5        U R                  5         S U l        S U l        U R!                  5         g !   [#        S	5        U R                  5         e = f)
Nr\   rx   ry   ru  rA  r   r   rw  zError creating Tree View)_init_parserra   r   rb   rc   rd   r   r~   rU  _init_animation_init_chartview_init_rulelabelr  r  _matrix_resultsr  print)r<   rO  r  rb   frame3frame2frame1s          r?   rq   ChartParserApp.__init__  s7   '*
	DJJJU#JJOOM4<<8 4::&F4::&F4::&FKKXFK3KKXCK0KKXF1K=TZZ(  "  (  (v& DL DM !	,-LLNs   EE' 'Fc                 b    U R                   c  g U R                   R                  5         S U l         g r   r   r<   r   s     r?   rd   ChartParserApp.destroy  s&    ::


rB   c                 \    [        5       (       a  gU R                  R                  " U0 UD6  g)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(   ra   r  r8  s      r?   r  ChartParserApp.mainloop!  s%     99

T,V,rB   c                 <    Xl         X l        U R                  5         g r   )rF  _tokens_reset_parser)r<   rO  r  s      r?   rP  ChartParserApp._init_parser0  s    rB   c                 v   [        U R                  5      U l        U R                  R                  U R                  5        U R                  R                  5       U l        [        5       R                  U R                  U R                  5       H  nM     U R                  R                  5       U l
        S U l        g r   )r#   rF  _cp
initializera  rn   r]   r!   r=  step_cpsteprJ  )r<   	_new_edges     r?   rb  ChartParserApp._reset_parser5  s}    &t}}5DLL)hhnn& &--dkk4==II J xx}} rB   c                    [        [        5       S   S9U l        UR                  SU R                  5        [	        U5      U l        U R
                  R                  U R                  R                  S5      5        [        SSU R
                  R                  5       S9U l	        [        SU R
                  R                  5       S9U l
        g )	Nr3   rj  rk  rh  r8   r:   re  ri  )r   r   rn  ro  r   _sizer   cgetrL  rl  rm  r   s     r?   rU  ChartParserApp._init_fontsD  s    &(6"23/ D\


t}}))&12[djjnnFVW4::>>3CD
rB   c                     [        U R                  5      U l        U R                  R                  S5        [        U R                  5      U l        U R                  R                  S5        SU l        g )Nr   r   r   )r   ra   _stepr   _animate
_animatingr   s    r?   rQ  ChartParserApp._init_animationP  sO    DJJ'


q tzz*! rB   c                     [        U R                  USSS9U l        U R                  R                  SU R                  5        g )Nr   )r;  r<  r   )r7  r]   _cvr   _click_cv_edge)r<   rm   s     r?   rR  ChartParserApp._init_chartview\  s2    T[[&AQOh(;(;<rB   c                    Sn[        XU R                  S9U l        [        USSSU R                  S9U l        U R                  R	                  SS9  U R                  R	                  SS9  [        XR                  S	S
9nUR	                  SS9  g )NzLast edge generated by:)rv   r3   r9  r  r  )r   r   r  r3   rD  rC  Step)variablerv   rB  )r   rl  _rulelabel1_rulelabel2r~   r   rp  )r<   rm   ruletxtrg  s       r?   rS  ChartParserApp._init_rulelabel`  s    + DNNK "Xc
 	6*6*6JJVD		w	rB   c                    [        U5      n[        U5      nUR                  SSS9  UR                  SSS9  [        USSSU R                  S	9R                  S
S9  [        USSSU R                  S	9R                  SS9  [        USSSU R
                  S	9R                  SS9  [        USSSU R                  S	9R                  SS9  [        USSSU R                  S	9R                  SS9  [        USSSU R                  S	9R                  SS9  [        USS9R                  SS9  [        USSSU R                  S	9R                  SS9  [        USS9R                  SS9  [        USSSU R                  S	9R                  SS9  [        USS9R                  SS9  [        USSSU R                  S	9R                  SS9  g )Nrx   rA  ru  r   ry   zReset
Parserz#90c0d0r   )rv   r   r/   rw   rB  rC  zTop Down
StrategyrD  zBottom Up
StrategyzBottom Up
Left-Corner StrategyzTop Down Init
Rulez#90f090zTop Down Predict
Ruler   r  zBottom Up Predict
Rulez"Bottom Up Left-Corner
Predict RulezFundamental
Rule)r   r~   r   resettop_down_strategybottom_up_strategybottom_up_leftcorner_strategytop_down_inittop_down_predict	bottom_upbottom_up_leftcornerfundamental)r<   rm   rY  rX  s       r?   r  ChartParserApp._init_buttonsl  s   vv,V,  JJ	
 $G$

 	% **	
 $F$
& ++	
 $F$
2 66	
 $F$
& &&	
 $F$
) ))	
 $F$
fB$$&$1* NN	
 $F$
fB$$&$16 --	
 $F$
fB$$&$1$ $$	
 $F$
rB   c                 d   U R                   R                  SU R                  R                  5        U R                   R                  SU R                  R                  5        U R                   R                  SU R                  R
                  5        U R                   R                  SU R                  R                  5        U R                   R                  SU R                  5        U R                   R                  SU R                  5        U R                   R                  SU R                  5        U R                   R                  SU R                  5        U R                   R                  S	U R                  5        U R                   R                  S
U R                  5        U R                   R                  SU R                  5        U R                   R                  SU R                  5        U R                   R                  SU R                  5        U R                   R                  SU R                  5        U R                   R                  SU R                   5        U R                   R                  SU R"                  5        U R                   R                  SU R$                  4S j5        U R                   R                  SU R$                  4S j5        U R                   R                  SU R$                  4S j5        U R                   R                  SU R&                  4S j5        g )Nz<Up>z<Down>z<Prior>z<Next>r\   r  z<F1>z<Control-s>r  z<Control-r>r2  ra  r/  z<space>z<Control-g>z<Control-t>r  c                 $    UR                  S5      $ r   r   r   as     r?   <lambda>/ChartParserApp._init_bindings.<locals>.<lambda>      arB   r  c                 $    UR                  S5      $ )Nr   r  r  s     r?   r  r    r  rB   r  c                 $    UR                  S5      $ )Nr   r  r  s     r?   r  r    r  rB   r  c                 J    UR                  UR                  5       (       + 5      $ r   )r   rL  )r   r  s     r?   r  r    s    QUUquuw;5GrB   )ra   rc   ru  r  r  r  r  rd   help
save_chartr  r  r  r  r  _stop_animationedit_grammaredit_sentencerq  rp  r   s    r?   r  ChartParserApp._init_bindings  s   

 2 23

$(("6"67

	488#3#34

$(("4"45

t||4

t||4

		*

t7

t7

tzz2

T334

T445

T??@

	4#7#78

t'8'89

t'9'9: 	

@A

@A

@A 	

GHrB   c                 <	   [        U R                  5      n[        USS9nUR                  SSU R                  SS9  UR                  SSU R                  SS9  UR                  SSU R
                  S	S9  UR                  5         UR                  S
U R                  S9  UR                  SU R                  S9  UR                  5         UR                  SSU R                  SS9  UR                  SSUS9  [        USS9nUR                  SSU R                  SS9  UR                  SSU R                  SS9  UR                  SSUS9  [        USS9nUR                  SSU R                  S9  UR                  SSU R                  S9  UR                  SSUS9  [        USS9nUR                  SSU R                  SS9  UR                  SSU R                   S S9  UR                  S!SU R"                  S"S9  UR                  5         UR                  S#U R$                  S9  UR                  S$U R&                  S9  UR                  S%U R(                  S9  UR                  S&U R*                  S9  UR                  S'U R,                  S9  UR                  S(SUS9  [        USS9nUR/                  S)SU R0                  S*S+9  UR                  5         UR3                  S,SU R4                  SS-9  UR3                  S.SU R4                  SS/S09  UR3                  S1SU R4                  S2S3S09  UR3                  S4SU R4                  S5S6S09  UR                  S7SUS9  [        USS9nUR3                  S8U R6                  SS9U R8                  S:9  UR3                  S;U R6                  SS<U R8                  S:9  UR3                  S=U R6                  SS>U R8                  S:9  UR3                  S?U R6                  SS@U R8                  S:9  UR3                  SAU R6                  SSBU R8                  S:9  UR                  SCSUS9  [        USS9nUR                  SDSU R:                  S9  UR                  SESU R<                  SFS9  UR                  SGSUS9  U R                  R?                  USH9  g )INr   r  z
Save Chartr  r  r  r  zReset ChartzCtrl-rzSave Grammarr  zLoad Grammarr  r   r  r  r  zEdit Grammarr   zCtrl-g	Edit TextzCtrl-tEditr<  r  )rg  r4   rw   ResultsViewzTop Down Strategyr2  zBottom Up Strategyra  zBottom Up Left-Corner Strategyr/  zBottom Up RulezBottom Up Left-Corner RulezTop Down Init RulezTop Down Predict RulezFundamental RuleApplyry  r  )rg  r4   rz  r  zNo Animation)rg  r4   rz  valuezSlow Animationr  )rg  r4   rz  r  r  zNormal Animationr   r  zFast Animationr   r  AnimateTinyr   )rg  rz  r4   r  rw   Smallr8  Medium   Large   Huge   ZoomAboutInstructionsF1Helpr  ) r	   ra   r  r  r  r  r  save_grammarload_grammarrd   r  r  r  view_matrixview_resultsr  r  r  r  r  r  r  r  add_checkbuttonrp  add_radiobuttonrq  rl  resizeaboutr  r  )	r<   r  r  editmenuviewmenurulemenuanimatemenuzoommenuhelpmenus	            r?   r  ChartParserApp._init_menubar  s[   tzz"+OO 	 	 	
 	OO 	 	 	
 	1djjh 	 	
 	 >4;L;LM>4;L;LM At|| 	 	
 	&AHE+ %% 	 	 	
 	&& 	 	 	
 	&AHE+ At7G7G 	 	
 	94CTCTU&AHE+%**	 	 	
 	&++	 	 	
 	266	 	 	
 	 #3T^^L.8Q8Q 	 	
 	#7ASAST)43H3H 	 	
 	#5t?O?OP'QXF7A.##A

 	$ 	
 	!!### AQ 	$ 	
 	##"]] 	$ 	
 	##$]] 	$ 	
 	##"]] 	$ 	
 	)q{K+  ZZKK 	! 	
 	  ZZKK 	! 	
 	  ZZKK 	! 	
 	  ZZKK 	! 	
 	  ZZKK 	! 	
 	&AHE+7aL Atyyd 	 	
 	&AHE

w'rB   c                 z    XR                   :w  a  U R                  U5        g U R                  R                  5         g r   )rJ  _select_edgeru  r  r  s     r?   rv  ChartParserApp._click_cv_edge}  s,    ??"d# HH!rB   c                 \    U R                  U5        U R                  R                  U5        g r   )r  ru  r  r  s     r?   _select_matrix_edge"ChartParserApp._select_matrix_edge  s"    $4 rB   c                 0   Xl         U R                  R                  US5        U R                  R                  U5        U R                  (       a  U R                  R                  U5        U R                  (       a  U R                  R                  U5        g g )Nz#f00)rJ  ru  r  r;  rT  r  r  s     r?   r  ChartParserApp._select_edge  sd    tV,4 <<LL&&t,<<LL""4( rB   c                     S U l         U R                  R                  5         U R                  R                  5         U R                  (       a  U R                  R                  5         g g r   )rJ  ru  r  r  rT  r   s    r?   _deselect_edgeChartParserApp._deselect_edge  sE    <<LL$$& rB   c                    U R                  U R                  R                  5       5        U R                  R	                  5         U R                  R                  U5        U R                  R                  US5        U R                  R                  U5        U R                  (       a  U R                  R	                  5         U R                  (       a  U R                  R                  U5        U R                  (       a  U R                  R                  U5        U R                  (       a  U R                  R	                  U5        g g )Nr1  )
_display_rulere  current_chartruleru  r   r;  r  r  rT  rU  r  s     r?   _show_new_edgeChartParserApp._show_new_edge  s    48855784 tV,4 <<LL!<<LL&&t,<<LL""4(==MM  & rB   c                     SU l          [        U R                  S[        =(       d    SR	                  5       SSS9  g !   [        U R                  S[        =(       d    SR	                  5       SS9   g = f)Nr   zHelp: Chart Parser Applicationr  K   fixed)r   r3   r  )rr  r   ra   r=  stripr   s     r?   r  ChartParserApp.help  sb    	

0B%%'	

0B%%'	s	   2< 3A1c                      Sn[        SU5        g )Nz5NLTK Chart Parser Application
Written by Edward LoperzAbout: Chart Parser Application)r   )r<   r   ABOUTs      r?   r  ChartParserApp.about  s    M2E:rB   r  r  ))zPlaintext grammar file.cfgr  r  c                 \   [        U R                  SS9nU(       d  g [        US5       n[        R                  " U5      nSSS5        WU l        U R                  R                  U5        U R                  (       a  U R                  R                  U5        U R                  (       a  U R                  R                  5         U R                  (       a  U R                  R                  U5        U R                  R                  U5        g! , (       d  f       N= f! [         a  ne SnAff = f)zLoad a chart from a pickle filer  r  Nr  )r   r   r  r  r  r]   ru  r   rT  r   r   rU  re  r  r   )r<   r   r  r  rn   r   s         r?   r  ChartParserApp.load_chart  s    "++i
 	Sh%F+ &DKHHOOE"||&&u-||**,}}''.HHu% &%  	S	Ss/   D D
CD 

DD 
D+%D&&D+c                     [        U R                  SS9nU(       d  g [        US5       n[        R                  " U R
                  U5        SSS5        g! , (       d  f       g= f! [         a  ne SnAff = f)zSave a chart to a pickle filer  r  Nr  )r   r   r  r  r  r]   r  r   r  s        r?   r  ChartParserApp.save_chart  sc    $++i
 	Rh%DKK1 &%% 	R	Rs4   A& "AA& 
A#A& #A& &
A60A11A6c                    [        U R                  SS9nU(       d  g UR                  S5      (       a,  [        US5       n[        R
                  " U5      nSSS5        O8[        U5       n[        R                  " UR                  5       5      nSSS5        U R                  W5        g! , (       d  f       N = f! , (       d  f       N1= f! [         a  n[        SSU-  5         SnAgSnAff = f)z!Load a grammar from a pickle filer  r  Nr  r  zError Loading GrammarUnable to open file: %r)r   GRAMMAR_FILE_TYPESr  r  r  r  r   
fromstringreadr|  r  r   )r<   r   r  r  rO  r   s         r?   r  ChartParserApp.load_grammar  s    "--
 		U  ++(D)V$kk&1G *) (^v!nnV[[];G $W% *) $^  	U-/H8/STT	UsG   "C  B*C +%B;C *
B84C ;
C	C 
C/C**C/c                 b   [        U R                  SS9nU(       d  g  UR                  S5      (       aC  [        US5       n[        R
                  " U R                  U R                  4U5        S S S 5        g [        US5       nU R                  R                  5       nU Vs/ s H2  oUR                  5       U R                  R                  5       :X  d  M0  UPM4     nnU Vs/ s H2  oUR                  5       U R                  R                  5       :w  d  M0  UPM4     nnU H  nUR                  SU-  5        M     U H  nUR                  SU-  5        M     S S S 5        g ! , (       d  f       g = fs  snf s  snf ! , (       d  f       g = f! [         a  n	[        SSU-  5         S n	A	g S n	A	ff = f)	Nr  r  r  r  r  z%s
zError Saving Grammarr  )r   r  r  r  r  r  r]   ra  rF  productionsrE   r   writer  r   )
r<   r   r  r  prodspr   restprodr   s
             r?   r  ChartParserApp.save_grammar	  sZ   $--
 	T  ++(D)WKKdll ;WE *) (C(G MM557E(-R1DMM<O<O<Q1QQER',Qu!4==;N;N;P0PAuDQ %ftm4 !& $ftm4 !% )( *)
 SQ )(  	T,.G(.RSS	Ts   "F  .E.F 7F E:"/E0E0E:!/E5E5<E:F 
E-)F -F 0
E::
FF F 
F.F))F.c                    SU l         U R                  5         U R                  R                  U R                  5        U R
                  (       a%  U R
                  R                  U R                  5        U R
                  (       a  U R
                  R                  5         U R                  (       a&  U R                  R                  U R                  5        g g r   )	rr  rb  ru  r   r]   rT  r   r   rU  r\  s     r?   r  ChartParserApp.reset$	  s    $<<LL""4;;/<<LL&&(==MM##DKK0 rB   c                 Z    [        U R                  U R                  U R                  5        g r   )r   ra   rF  r|  r   s     r?   r  ChartParserApp.edit_grammar3	  s    $**dmmT-=-=>rB   c                     Xl         U R                  R                  U5        U R                  (       a  U R                  R                  U5        g g r   )rF  re  r|  rU  r{  s     r?   r|  ChartParserApp.set_grammar6	  s7    W%==MM%%g. rB   c                     SR                  U R                  5      nSnSn[        U R                  X$U R                  U5        g )Nrp  r  zEnter a new sentence to parse.)r  ra  r   ra   set_sentence)r<   r   sentencerb   instrs        r?   r  ChartParserApp.edit_sentence<	  s6    88DLL)0DJJ1B1BEJrB   c                 `    [        UR                  5       5      U l        U R                  5         g r   )r   splitra  r  )r<   r  s     r?   r  ChartParserApp.set_sentenceB	  s    HNN,-

rB   c                     U R                   b  U R                   R                  5         [        U R                  U R                  5      U l         U R                   R                  SU R                  5        g r   )rT  rd   rY   ra   r]   r   r  r   s     r?   r  ChartParserApp.view_matrixJ	  sL    <<#LL  "&tzz4;;?!!(D,D,DErB   c                     U R                   b  U R                   R                  5         [        U R                  U R                  U R
                  5      U l         g r   )rU  rd   r?  ra   r]   rF  r   s     r?   r  ChartParserApp.view_resultsP	  s8    ==$MM!!#(T[[$--PrB   c                 d    SU l         U R                  U R                  R                  5       5        g r   )rr  r  rl  rL  r   s    r?   r  ChartParserApp.resizeY	  s"    4::>>+,rB   c                    U R                   R                  U5        U R                  R                  [	        U5      * S9  U R
                  R                  [	        U5      * S9  U R                  R                  [	        U5      * S9  g r  )ru  r  rm  r  r  rl  rn  r  s     r?   r  ChartParserApp.set_font_size]	  se    t$

3t9*-  s4yj 1c$iZ0rB   c                 H    [        U R                  R                  5       5      $ r   )r  rl  rL  r   s    r?   r  ChartParserApp.get_font_sizec	  s    4::>>#$$rB   Nc                 x   U R                   (       a  SU l         g U R                  S 5        U R                  R                  5       (       a  U R                  nUb[  UbX  U R
                  R                  U" U5      /5        U R                  5       nUc"  U R                  R                  5         S U l        g g U R
                  R                  U5        U R                  5         g U R
                  R                  U5        U R                  R                  5       (       a  SU l         U R                  5         g U R                   H  nUb  M    O   U R                  R                  5         U R                  (       a  U R                  R                  5         U R                  (       a  U R                  R                  5         g g )Nr   r   )rr  r  rp  rL  rJ  re  set_strategy_apply_strategyru  r  rq  _animate_strategyrh  r   rT  rU  )r<   strategyedge_strategy	selectionnewedger   s         r?   apply_strategyChartParserApp.apply_strategyj	  sH   ??DO 	4  ::>>I%M,E%%}Y'?&@A..0 ?HH((*&*DO # %%h/$$& HH!!(+}}  """#&&( LLD| ) !<<LL'')==MM((* !rB   c                     SU l         g r   )rr  r   s     r?   r  ChartParserApp._stop_animation	  s	    rB   c                 $   U R                   S:X  a  g U R                  5       b  U R                  R                  5       S:X  d  U R                  R                  5       S:X  a  g U R                  R                  5       S:X  a'  U R
                  R                  SU R                  5        g U R                  R                  5       S:X  a'  U R
                  R                  SU R                  5        g U R
                  R                  SU R                  5        g g )Nr   r   i  r   i  r   )rr  r  rq  rL  rp  ra   afterr  )r<   speeds     r?   r   ChartParserApp._animate_strategy	  s    ??a!-}}  "a'4::>>+;q+@}}  "a'

  t'='=>""$)

  t'='=>

  T%;%;< .rB   c                 X    [        U R                  5      nUb  U R                  U5        U$ r   )nextrh  r  )r<   new_edges     r?   r  ChartParserApp._apply_strategy	  s)    %)rB   c                     Uc  SU R                   S'   g [        U5      nX R                   S'   U R                  R                  5       ng )Nr  rv   )r|  r  ru  r  )r<   ruler  rh  s       r?   r  ChartParserApp._display_rule	  s@    <')DV$t9D'+V$88))+DrB   c                 <    U R                  U R                  S 5        g r   )r
  _TD_INITr   s     r?   r  ChartParserApp.top_down_init	  s    DMM40rB   c                 D    U R                  U R                  [        5        g r   )r
  _TD_PREDICTrD  r   s     r?   r  ChartParserApp.top_down_predict	  s    D,,.DErB   c                 D    U R                  U R                  [        5        g r   )r
  _BU_RULErH  r   s     r?   r  ChartParserApp.bottom_up	  s    DMM+;<rB   c                 D    U R                  U R                  [        5        g r   )r
  _BU_LC_RULErJ  r   s     r?   r  #ChartParserApp.bottom_up_leftcorner	  s    D,,.HIrB   c                 D    U R                  U R                  [        5        g r   )r
  _FUNDAMENTALrL  r   s     r?   r  ChartParserApp.fundamental	  s    D--/BCrB   c                 D    U R                  U R                  [        5        g r   )r
  _BU_STRATEGYrH  r   s     r?   r  !ChartParserApp.bottom_up_strategy	  s    D--/?@rB   c                 D    U R                  U R                  [        5        g r   )r
  _BU_LC_STRATEGYrJ  r   s     r?   r  ,ChartParserApp.bottom_up_leftcorner_strategy	  s    D002LMrB   c                 D    U R                  U R                  [        5        g r   )r
  _TD_STRATEGYrD  r   s     r?   r   ChartParserApp.top_down_strategy	  s    D--/EFrB   )rq  rr  rl  r]   re  rh  ru  rm  rF  rT  rU  ra   r{  r|  rJ  rl  rp  rn  ra  )zChart Parser Applicationr   )r   )BrR   rS   rT   rU   rq   rd   r  rP  rb  rU  rQ  rR  rS  r  r  r  rv  r  r  r  r  r  r  r   r  r  r  r  r  r  r  r|  r  r  r  r  r  r  r  r
  r  r  r  r  r$   r  r%   r  r   r   r   r#  r"   r&  r/  r)  r,  r  r  r  r  r  r  r  r  rW   rQ   rB   r?   rN  rN    sq   #J	-

E
=
 ObI:](F"!	)'',&; 34FGS.RU$T,	1?/KFQ-1%&+P=,  !"H%'(K#%&H-/0K-/0L k)L8Ll*L!L0O1F=JDANGrB   rN  c                  Z   [         R                  " S5      n SnSn[        UR                  5       5      n[	        S5        U R                  5        H  n[	        S[        U5      S-   45        M     [	        S5        [	        SU-  5        [	        S	5        [        X5      R                  5         g )
NaW  
    # Grammatical productions.
        S -> NP VP
        VP -> VP PP | V NP | V
        NP -> Det N | NP PP
        PP -> P NP
    # Lexical productions.
        NP -> 'John' | 'I'
        Det -> 'the' | 'my' | 'a'
        N -> 'dog' | 'cookie' | 'table' | 'cake' | 'fork'
        V -> 'ate' | 'saw'
        P -> 'on' | 'under' | 'with'
    z*John ate the cake on the table with a forkzJohn ate the cake on the tablez
grammar= (z    ,)ztokens = %rz,Calling "ChartParserApp(grammar, tokens)"...)	r   r  r   r  rV  r  r'  rN  r  )rO  sentr  r  s       r?   appr5  	  s    nn	G  8D+D$**,F	,##%vtDzC'() &	#J	-&
 !	
897#,,.rB   __main__r5  )Br=  os.pathr  r  tkinterr   r   r   r   r   r   r	   r
   r   r   tkinter.filedialogr   r   tkinter.fontr   tkinter.messageboxr   r   	nltk.drawr   r   r   nltk.draw.utilr   r   r   r   r   r   nltk.grammarr   r   nltk.parse.chartr   r   r   r    r!   r"   r#   r$   r%   r&   	nltk.treer'   	nltk.utilr(   r*   rY   r?  r  r7  r4  rD  rH  rJ  rL  rN  r5  rR   __all__rQ   rB   r?   <module>rC     s  <     B  2 G G  *    } @I) I)bL) L)hJT JTdN Np# #,	X'9 		x!4 		+E 		($= 	cG cGL/< zE 'rB   