JL izNdZddlZddlZddlmZmZmZmZm Z m Z m Z m Z m Z mZddlmZmZddlmZddlmZmZddlmZmZmZddlmZmZmZmZm Z m!Z!dd l"m#Z#m$Z$dd l%m&Z&m'Z'm(Z(m)Z)m*Z*m+Z+m,Z,m-Z-m.Z.m/Z/dd l0m1Z1dd l2m3Z3Gd deZ4GddZ5GddZ6GddZ7GddZ8GddZ9Gdde9e.Z:Gdde9e'Z;Gdde9e&Z<Gdd e9e+Z=Gd!d"Z>d#Z?e@d$k(re?d%gZAy)&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) ButtonCanvas CheckbuttonFrameIntVarLabelMenu ScrollbarTkToplevel)askopenfilenameasksaveasfilename)Font) showerrorshowinfo) CFGEditorTreeSegmentWidgettree_to_treesegment) CanvasFrame ColorizedList EntryDialogMutableOptionMenuShowText SymbolWidget)CFG Nonterminal) BottomUpPredictCombineRuleBottomUpPredictRuleChartLeafEdge LeafInitRuleSingleEdgeFundamentalRuleSteppingChartParserTopDownInitRuleTopDownPredictRuleTreeEdge)Tree)in_idlec6eZdZejdZdZdZy)EdgeList rightarrowc|jdd|jddd|jdd |jd d d y)Nterminalz#006000) foregroundarrowsymbol0)font underlinedotz#000000 nonterminalblue) helveticabold)r.r2) tag_config)self textwidgetoptionss ^/mnt/ssd/data/python-lab/Trading/venv/lib/python3.12/site-packages/nltk/app/chartparser_app.py_init_colortagszEdgeList._init_colortags^sXjY?gHDe : f3M  cg}|jd|jzdf|j|jdft|j D]t\}}||j k(r|jdt |tr%|jd|jzdf_|jd|zdfv|jr|jd|S)Nz%s r5r/)z *r4z %sz %rr-) appendlhsARROW enumeraterhsr4 isinstancerr0 is_complete)r;itemcontentsielts r> _item_reprzEdgeList._item_reprfs$((*,m<=W-. + ;FAsDHHJ .#{+!5} EFj 9:  ;     OOM *r@N)__name__ __module__ __qualname__rSYMBOLSrDr?rMr@r>r*r*[s   .E  r@r*ceZdZdZ ddZdZdZdZdZdZ dZ d Z d Z d Z d ZddZdZdZdZdZdZdZddZdZdZdZy )ChartMatrixViewzS A view of a chart that displays the contents of the corresponding matrix. c,||_g|_g|_d|_|rmt ||_|j j ||j jd|j|j|j nt||_|j|j |j|j |r|j|j nd|_i|_d|_|j#y)N r)_chart_cells_marks_selected_cellr _roottitlebinddestroy _init_quitr _init_matrix _init_list_init_numedges_numedges_label _callbacks _num_edgesdraw)r;parentcharttoplevelr\ show_numedgess r>__init__zChartMatrixView.__init__s   " !&)DJ JJ  U # JJOOM4<< 8 OODJJ 'vDJ $**%  #     +#'D  r@c\t|d|j}|jdddy)NQuittextcommandbottomrnonesideexpandfill)rr^pack)r;rootquits r>r_zChartMatrixView._init_quits&d> x 7r@ct|dd}|jddddd t|d d d |_|jjdd y)Nsunkenborderreliefrrrtop)rurvpadxpadyrtwhite)widthheight background)rurv)rrwr_canvas)r;rxcframes r>r`zChartMatrixView._init_matrixsOtAh7 16 FfCP  0r@cdt|d|_|jjdddy)Nz0 edgesrorrrr)rurvrt)rrcrwr;rxs r>rbzChartMatrixView._init_numedgess,$T : !!e!Dr@ct|gdd|_|jjdddd|fd }|jjd ||jj y) N)rrrrbothr)rtrurvrc(|jd|yNselect_fire_callbacks)edger;s r>cbz&ChartMatrixView._init_list..cb  4 0r@r)r*_listrw add_callbackfocus)r;rxrs r>razChartMatrixView._init_listsZdBb;  U16B 1 "- r@c~|jy |jjd|_y#Yd|_yxYwNr[r^r;es r>r^zChartMatrixView.destroy> ::    JJ       1<c^||jur||_d|_|jyyNr)rWrerfr;rhs r> set_chartzChartMatrixView.set_charts*  #DKDO IIK $r@c t|jyt|j}t|Dcgc]}t|Dcgc]}dc}}}}|jD].}||j |j xxdz cc<0t|D]}t||D]}|||dk(rd}nDdjtddd|||zdz ztddd|||zdz z }|j||}|jj|| ||f|jk(r:|jj|d d |jj||jj|d d t|jj|j}|j j#||jj%|_|j(d|j&z|j(d<yycc}wcc}}w)Nrrgray20z#00{:02x}{:02x}2 rvz#00ffffr)outlinerblack)spanz%d edgesro)r[lenrXrangerWstartendformatminmaxr itemconfigrZ tag_raiselistrrset num_edgesrerc) r;NjrK cell_edgesrcolorcell_tagedgess r>updatezChartMatrixView.updates ::    5:1X>%(+Qq+> >KK 6D tzz| $TXXZ 0A 5 0 6q PA1a[ Pa=#q($E-44CcJqM!,<&!, ''u'=q6T000LL++Hiq+QLL**84LL++HgQ+O P P$T[[''T-@-@'AB u++//1    ++5+GD  ( ,9,>sH4 H/ H4/H4c^|jjdd|jy)N inactiveboxhiddenstaterrrr;s r>activatezChartMatrixView.activate"  X> r@c^|jjdd|jy)Nrnormalrrrs r> inactivatezChartMatrixView.inactivaterr@cBd|jj|i|<yNrrd setdefaultr;eventfuncs r>rzChartMatrixView.add_callback67""5"-d3r@NcT||j|=y |j||=y#YyxYwrrdrs r>remove_callbackzChartMatrixView.remove_callback3 <& OOE*40 #'c||jvryt|j|jD]}|| yrrdrkeysr;rargscb_funcs r>rzChartMatrixView._fire_callbacks>  ' DOOE2779: G TN r@c|jy||f|jk(r(|jj|jk(ry||f|_|j |j d||y)N select_cell)r[rZrWrrerrr;rKrs r>rzChartMatrixView.select_cellsg ::   q6T(( (T[[-B-B-D-W  !f  ]Aq1r@c|jyd|_|jjg|j yr)r[rZrrrrs r> deselect_cellzChartMatrixView.deselect_cells0 ::  " r r@cl|j||fk(r|jy|j||yr)rZrrrs r> _click_cellzChartMatrixView._click_cells0   1a& (      Q "r@ct|j|j|jj|yr)rrrviewr;rs r> view_edgezChartMatrixView.view_edge s($))+& r@c|jy|j|j|jj |yr)r[rrrmarkrs r> mark_edgezChartMatrixView.mark_edge$s5 ::  $))+& r@cT|jy|jj|yr)r[runmarkrs r> unmark_edgezChartMatrixView.unmark_edge*s! ::   $r@c|jy|j|j|jj |yr)r[rrrmarkonlyrs r> markonly_edgezChartMatrixView.markonly_edge/s7 ::  $))+& D!r@c "|jydx}}d}|j}|jd|jj dz}t |d|z |z }t |d|z |z |z }|jdt |D]}|j|dz ||z|dz z|zt|d |j||z|dz z|z||z|zdzt|d |j|||dzz|z||z|z||dzz|zd |j||z|z|||z|z||z|zd |j|||||zz||z|zd t |D cgc]} t |Dcgc]}dc}c}} |_ t |D]s}t ||D]b} |j| |z|z||z|z| dz|z|z|dz|z|zd} | |j|| <||| fd} |j| d| dut |dt |d} } |jdd| dz| dzddd} |j| |jycc}wcc}} w)Nrallrrrr{r)roanchorn.)dashrrrc(|j||yr)r)rr;rKrs r>rz ChartMatrixView.draw..cbns$$Q*r@ idgray50rr)rvrtag)r[rdeleterW num_leavesintr create_textrepr create_linecreate_rectanglerXtag_bind tag_lowerr)r; LEFT_MARGIN BOT_MARGIN TOP_MARGINcrdxdyrKrtrxmaxymaxs r>rfzChartMatrixView.draw5s ::  #%% j LL  KK " " $q (!G*o +q 0!H++j8A = q A MMaR"q&:!=DGTW   MMBa+-B#a'!W   MMa!e z)Q$a!e z)   MMQ$Q$Q#  # 4  [26%926J;NVW  :?qBAeAh//B q 0A1a[ 0&&F[(FZ'UbL;.UbL:-! '%& Aq!#'1+ 1lB/ 0 0"7_c!H+&6d     3J 3J   A ?0BsJ  J J J c<|jj|i|yrr[rwr;rkwargss r>rwzChartMatrixView.pack ((r@)T Chart MatrixFr)rNrOrP__doc__rkr_r`rbrar^rrrrrrrrrrrrrrrfrwrRr@r>rTrT{s QV<81 E #HJ8 2#   " L\)r@rTcXeZdZddZddZdZdZdZdZdZ d Z d Z d Z d Z d Zy)ChartResultsViewc||_||_g|_d|_g|_d|_d|_|rRt||_|jjd|jjd|jnt||_|rt|j}|jdddt|d|j jd t|d |j jd t|d|j jd t#|jd|_|j$jddd|j'y)Nrz!Chart Parser Application: ResultsrVrqrxrsrmrnrightrtz Print AllleftzPrint Selectionr) closeenoughrrr)rW_grammar_trees_y _treewidgets _selection _selectboxr r[r\r]r^rrwr print_allprint_selectionr_cframer)r;rgrhgrammarributtonss r>rkzChartResultsView.__init__s8    !&)DJ JJ  @ A JJOOM4<< 8vDJ DJJ'G LLhqsL ; 7 > C C C Q 7dnn E J JPV J W 7!2D  uQV< r@Nc|jy|Z|j|jjk7ry|j d|j j fk7ry|j j|jjD]"}||jvs|j|$yr) r[rCr(rrrWrparsesr)_add)r;rparses r>rzChartResultsView.updates ::    xxzT]]0022yy{q$++"8"8":;;[['' (;(;(=> !EDKK' %  !r@cv|jj||jj}t ||}|j j||jj |d|j|j|j|jddz|_y)Nrr) r)rBr0canvasrr+ add_widgetr* bind_click_clickbbox)r;r6r treewidgets r>r5zChartResultsView._adds 5! LL   !(E2    ,  B8 dkk*//#A&+r@c|jj}|j|j|j||_|j \}}}}|j ||||dd|_y)Nr{z#088)rr)r0r8r,rr-r<r )r;widgetrx1y1x2y2s r>r;zChartResultsView._clicksh LL   ! ?? & HHT__ % !;;=RR,,RR1f,Ur@c||jd<|jD]*}t|tr|j ||&||d<,y)Nr)labelsubtreesrGr_color)r;r=rchilds r>rGzChartResultsView._colorsO&+ 7#((* 'E%!23 E5)!&g  'r@cR|jy|jjyr)r[r0 print_to_filers r>r.zChartResultsView.print_alls ::   ""$r@cx|jy|j tddy|jj }|j D],}||jus|jj |.|j|j|jj\}}}}|jjd|z d|z d||z dzd||z dz|d<|jj|jg|_|j|jy)Nz Print ErrorzNo tree selectedrz0 0 r scrollregion)r[r,rr0r8r+destroy_widgetrr-r<moverJclearr)r;rrr?r@rArBrCs r>r/z ChartResultsView.print_selections  ::   ?? " m%7 8 ##%A++ 80LL//7 8 HHT__ %#335 RR OO b"r' 2"&rBw|nAb2gl^ DAn  LL & & ("& 1D  JJL KKMr@c,|jy|jD]}|jj|g|_g|_|j 3|jj j|jd|_d|_ y)Nr) r[r+r0rNr)r,r8rr-r*)r;r=s r>rPzChartResultsView.clears| ::  ++ 4J LL ' ' 3 4  ?? & LL   ! ( ( 9r@cR|j||_|jyr)rPrWrrs r>rzChartResultsView.set_charts   r@cR|j||_|jyr)rPr(rr;r1s r> set_grammarzChartResultsView.set_grammar s   r@c~|jy |jjd|_y#Yd|_yxYwrrrs r>r^zChartResultsView.destroyrrc<|jj|i|yrrrs r>rwzChartResultsView.packrr@)Tr)rNrOrPrkrr5r;rGr.r/rPrrUr^rwrRr@r>r!r!s@B !,$V'% *   )r@r!ceZdZdZdej dej ddZdZdZdZ d Z d Z d Z d Z d ZdZdZdZddgZdZdZdZdZdZdZdZdZdZdZdZdZdZd Z y!)" ChartComparera :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. - intersectionunion)rZandorcdgdz}t||_d|_d|_|j|_|j|_d|ji|_|j|_d|_t|_ |jjd|jjd|j|jjd|j|j|j|j|j|j!|j|j#|j|j%|j|D]}|j'|y)NNonezChart ComparisonrV )r _emptychart _left_name _right_name _left_chart _right_chart_charts _out_chart _operatorr r[r\r]r^ _init_menubar_init_chartviews _init_divider _init_buttons_init_bindings load_chart)r;chart_filenamesfaketokfilenames r>rkzChartComparer.__init__Bs7$( >!!++ ,, 0 01 **T  +,  t||4  t||4 4::& djj) 4::& 4::& DJJ'( &H OOH % &r@c~|jy |jjd|_y#Yd|_yxYwrrrs r>r^zChartComparer.destroyhrrcyr)r[mainlooprs r>rwzChartComparer.mainloopqsr@ct|}t|d}|jddd|j|jddd|j|j |jdd |j d |j d d| t|d}|jd|jd|jd|jd|jd|jd|j |jd|j|j dd| |jj|y)Nrtearoff Load ChartCtrl-o)rE acceleratorr3rpz Save OutputCtrl-sExitrCtrl-xrEr3rpr}FilerEr3menu Intersection+)rErpr}Union* DifferencerZ Swap ChartsrErpComparer) r add_commandload_chart_dialogsave_chart_dialog add_separatorr^ add_cascade _intersection_union _difference _swapchartsr[config)r;rxmenubarfilemenuopmenus r>rlzChartComparer._init_menubarysft*+ **   **   At||   &AHEgq) $*<*<#   $++3O(8(8c   8H8HI)qvF w'r@cHt|dd}|jdddy)Nr{r|r}rr#)rtrvipady)rrw)r;rxdividers r>rnzChartComparer._init_dividers"Qx8 %c 3r@cvd}d}t|d}|jdddt|d d }|jd d dddt|t|jj |j |_|jjdddt||jdd|_ |jjddddd|jjd|j|jjd|j|jjt|dd d||_|j jd ddt|d d }|jd d dddt|t|jj |j"|_|j$jdddt||jdd|_|j&jddddd|j&jd|j|j&jd|j|j&jt|dd d|jd ddt|d d }|jd d dddt|d|_|j(jdd !t||jdd|_|j*jddddd|j*jd|j|j*jd|j|j*jy)"N)r0)r7rz#c0c0c0rrrrrsrgroover}r&ra)rtrrrurv)rprr#)rtrrvFT)rirjrqrrrL)rorrr2)rtrr=Outputr )rtr)rrwrrrir _select_left_left_selectorrTrd _left_matrixr select_edgerrr _op_label _select_right_right_selector _right_matrix _out_label _out_matrix)r;rxopfonteqfontframe cv1_frame cv2_frame out_frames r>rmzChartComparer._init_chartviewssF #dy1 af 5%(; F16J/ tDLL--/0$:K:K    e!# >+ t''%t  H11QVT &&x1A1AB &&}d6F6FG $$& 1  aa8%(; F16J0 tDLL--/0$:L:L  !!u13!?, t''%t  XAAafU ''$2B2BC '' t7G7GH %%' e#Q96JOOaa P %(; F16J 9 %a0* t''%t  8!!AFS %%h0@0@A %%mT5E5EF ##%r@ct|}|jddddt|d|jjd t|d |jjd t|d |j jd t|d jd t|d|j jd t|d|jjd y)Nrqrr#r)rtrrvrurrnr&r%rrrrrz Detach Outputr$)rrwrrrrr _detach_out)r;rxr2s r>rozChartComparer._init_buttonss+ (Q ?w^T5G5GHMM N  wWdkk:??V?Lw\43C3CDIIvIV gR %%6%2w]D4D4DEJJPVJWw_d6F6FGLL M r@c<|jd|jy)N )r]rrs r>rpzChartComparer._init_bindingss -!7!78r@c||_|j||_|jj |j|dk(r|jj |j yNrb)rerirgrrr _apply_opr;names r>rzChartComparer._select_leftsW<<- ##D$4$45 6>    ( ( * r@c||_|j||_|jj |j|dk(r|jj |j yr)rfrirhrrrrrs r>rzChartComparer._select_rightsX LL. $$T%6%67 6>    ) ) + r@c|jdk(r|jy|jdk(r|jy|jdk(r|jyy)NrZr^r])rkrrrrs r>rzChartComparer._apply_op sL >>S     ^^t # KKM ^^u $    %r@z Pickle file.picklez All filesrc t|jd}|sy t|d5}tj|j |dddy#1swYyxYw#t $r}tdd|d|Yd}~yd}~wwxYw)Nr filetypesdefaultextensionwbzError Saving ChartUnable to open file:  )rCHART_FILE_TYPESopenpickledumprj Exceptionrr;rrtoutfilers r>rzChartComparer.save_chart_dialogs$++i   Wh% 6 DOOW5 6 6 6 W *.CH>Bct|jd}|sy |j|y#t$r}t dd|d|Yd}~yd}~wwxYw)NrrzError Loading Chartrr)r rrqrr)r;rrtrs r>rzChartComparer.load_chart_dialog"sb"++i   X OOH % X +/DXLPRSTRU-V W W Xs. AAAcbt|d5}tj|}dddtjj |}|j dr|dd}|j dr|dd}|j|<|jj||jj||j|jur|jj|y|j|jur|jj|yy#1swYxYw)Nrbriz.charti)rrloadospathbasenameendswithriraddrrgrdrrh)r;rtinfilerhrs r>rqzChartComparer.load_chart-s (D ! (VKK'E (ww) == #9D == "9D" T %   &   t// /    # #D )   $"2"2 2  $ $T *3 ( (s D$$D.c|jj|jj|jjyr)rrrrrs r>_update_chartviewsz ChartComparer._update_chartviews@s8   " !!# !r@c||jvr|jj|n|jj||jvr|j j|n|j j||j vr|jj|y|jjyr)rgrrrrhrrjrrs r>rzChartComparer.select_edgeIs 4## #    + +D 1    ) ) + 4$$ $    , ,T 2    * * , 4?? "    * *4 0    ( ( *r@c|jj|||jj|||jj||yr)rrrrrs r>rzChartComparer.select_cellWsD %%a+ &&q!, $$Q*r@c|jsyt|jj}|jD]#}||jvs|j |g%|j d|y)NrZ _checkcompatrrgtokensrhinsert_updater; out_chartrs r>rzChartComparer._difference`sm  " $**1134 $$ +D4,,,  r* + S)$r@c|jsyt|jj}|jD]#}||jvs|j |g%|j d|y)Nr]rrs r>rzChartComparer._intersectionksm  " $**1134 $$ +Dt(((  r* + UI&r@c|jsyt|jj}|jD]}|j |g|j D]}|j |g|j d|y)Nr^)rrrgrrrhrrs r>rzChartComparer._unionvs  " $**1134 $$ 'D   T2 & '%% 'D   T2 & ' T9%r@c|j|j}}|jj||jj|yr)rerfrrr)r;r&r$s r>rzChartComparer._swapchartss=oot'7'7e &   &r@c|jj|jjk7sg|jj|jjk7s2|j|jk(s|j|jk(r`|j|_|j j|j |j jd|jd<yy)NrroFT) rgrrhproperty_namesrdrjrrrrrs r>rzChartComparer._checkcompats    # # %):):)A)A)C C..0D4E4E4T4T4VV4#3#33  D$4$44#..DO    & &t 7    ' ' )&.DOOF #r@c ||_|j||jd<||_|jj |dj |j|j|j|jd<y)Nroz{} {} {}) rk _OPSYMBOLrrjrrrrerfr)r;operatorrs r>rzChartComparer._updatesk!!%!9v# ""9-","3"3 OO NN   # r@c|j|_|jj|jd|jd<|jj y)NrLro)rdrjrrrrrs r>_clear_out_chartzChartComparer._clear_out_chartsG** ""4??3!$v ##%r@cbt|j|j|jdy)Nro)r\)rTr[rjrrs r>rzChartComparer._detach_outs DOO4??6;RSr@N)!rNrOrPrrrQrrkr^rwrlrnrmrorprrrrrrrqrrrrrrrrrrrrRr@r>rYrY s8##N3""7+I $&L-%(N4;&z  9!34FG W X+&" ++ % ' &' "  & Tr@rYceZdZdZdZdZdZdZd(dZdZ d)dZ d Z d Z d Z d Zd ZdZdZdZd(dZdZdZd*dZdZdZd+dZd,dZd(dZd,dZdZdZdZdZ dZ!d(d Z"d!Z#d"Z$d*d#Z%d$Z&d%Z'd(d&Z(d'Z)y)- ChartViewa 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 |jdd}|jdd}|jdd|_||_i|_g|_i|_i|_g|_d|_d|_ g|_ d|_ |_t}|jd|fd }|fd }|jd |t|d | } | j!d||_n||_|j%||j'|j"\|_|_d|j*d<d|j*d<|rit-|j"dd} | j!ddt/| d|_d|j0d<|j0j!dnd|_|r?|j'|j"dd\} } | | c|_|_d |j4d<nd|_|j7|j9|j;|j=|j*jd!|j>y)"z4 Construct a new ``Chart`` display. draw_treer draw_sentencerfontsizer8Nz Chart Viewc$|jyrr^)rrs r>destroy1z$ChartView.__init__..destroy1   r@c$|jyrr)rs r>destroy2z$ChartView.__init__..destroy2rr@qDonernrqr%i,rrr'sunkr{rr~r)rvrtrr#e0e0e0rrrr#rz ) get _fontsizerWrd _edgelevels _edgetagsrY _treetoks_treetoks_edge_treetoks_index _tree_tags_compactr r\r]rrwr[ _init_fonts _sb_canvas _chart_sb _chart_canvasrr_sentence_canvas_tree_sb _tree_canvas_analyzerf_resize_grow _configure) r;rhrxkwrrrrrbrsbr8s r>rkzChartView.__init__s2 FF;* 2  C0  "   <$C IIl # # !  HHS( #s:A FFF !DJDJ 04tzz/J,+'*8$,.=) 4::fQ?F KKV(K 3$*6"$=D !2;D ! !, /  ! ! & &F & 3%)D ! ??4::sC@LR24f .T]D-*-D  h ' $D       t?r@ctdd|j|_td|j|_tt d|_|j d|j y)Nr7r9familyweightsizer r"r2r2*Font)rr _boldfont_fontr_sysfont option_addrs r>rzChartView._init_fonts?sM[dnnU4>>B &(6"23  /r@c t|dd}|j|||t|d}t|d}|jd d |jd |d |j|d<|j |d<||fS)zK Helper for __init__: construct a canvas with a scrollbar. rr{r)rvrurtrrvertical)orientr$yrtrvr&yesrtrvrurpyscrollcommand)rrwrr yviewr)r;rxrurvrtrr8rs r>rzChartView._sb_canvasFstF15 f4 895vj 1 W3' d5 9 9 #%66 F|r@c>|jjdddy)Nscrollunitsrr2rs r> scroll_upzChartView.scroll_upY   2w7r@c>|jjdddy)Nr4rr6r7rs r> scroll_downzChartView.scroll_down\   1g6r@c>|jjdddy)Nr4r5pagesr7rs r>page_upzChartView.page_up_r9r@c>|jjdddy)Nr4rr>r7rs r> page_downzChartView.page_downbr<r@c|jj}tt|jd||j zt jdzz}|jj||jj|jd|dt jzz |z |_|j|j|jd<yy)z/ Grow the window, if necessary rr{rrrN) rWrrr r _unitsizer_MARGIN configurer_sentence_height)r;rrs r>rzChartView._growes KK " " $ ""7+ ,a$...@9CTCTWXCX.X  $$5$1 $$D,>,>x,H$I!i&7&7"771<  ,.2.C.CD ! !( + -r@cH|jjt| |jjt| |jjt| |j |j |jyN)r")r'rEabsr&r(rrrfr;r"s r> set_font_sizezChartView.set_font_sizezsn 3t9*-   s4yj 1 c$iZ0   r@c,t|jSr)rIrrs r> get_font_sizezChartView.get_font_sizes4>>""r@c|jj}|jdtjzz |z |_|j y)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)rWrrrrDrCrf)r;rrs r>rzChartView._configures? KK " " $''A (9(9$99Q> r@cV|f||_g|_i|_|j|j |j |j |jy|jD]"}||jvs|j|$|jy)au 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) rWr rYrrrf erase_treerr  _add_edge)r;rhrs r>rzChartView.updates  DK!D DK MMO JJL IIK OO  LLN  )t~~-NN4( ) LLNr@c|j\}}|j|D]F}|j\}}||cxkr|ks%n||cxkr|ksn||cxk(r |cxk(r|k(sByFyy)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)rr )r;rlvls1e1 otheredges2e2s r>_edge_conflictzChartView._edge_conflictst 99;R))#. I ~~'HRb 2 2=b=bB6J"6J6J7K r@c |j}t|tr|j}g}|j D]U}t|t r)|j t|j<|j t|Wdj|}n|j}d}||fD]}|jdd||jdd}|j|} |j|| d} t|j!d} t|j"| | z |_t|j$| d | dz |_y ) z Given a new edge, recalculate: - _text_height - _unitsize (if the edge text is too big for the current _unitsize, then increase _unitsize) rLr`rnwr&ror2rjustifyr{rrN)rrGr&rCrFrrBstrr0r joinr r&r<rrlengthrC _text_height) r;rrrCrhseltsrLrFsrr<redgelens r> _analyze_edgezChartView._analyze_edges5    dH %((*CGxxz .c;/NN3szz|#45NN49-  . ((7#C((*CCs JA--114>>$ C66#;D HHSMGE$++-+G ADN #D$5$5tAwa7H ID  Jr@ct|try||jvry|j||j |j sW|j j|gt|j dz }|j|||jyd} |t|j k\rD|j jg|j|t|j k\rD||k\r1|j||s|j |j|n|dz }|j||y)z Add a single edge to the ChartView: - Call analyze_edge to recalculate display parameters - Find an available level - Call _draw_edge Nrr) rGr r rerrr rBr _draw_edgerrY)r;rminlvlrSs r>rQzChartView._add_edges& dH %  4>> !  4  }}    # #TF +d&&'!+C OOD# & LLN T--..  ''+ T--.. f}T%8%8s%C  %,,T2 1HC c"r@cpd}tt|jD]}||j|vs|}n|y|dz|jz}|jdz}|j j dd|jdk7r-|j j d||z |jz yy)Nrrmovetog?r)rrr _chart_level_sizerarr2 _chart_height)r;rlevelrKr-rs r>rzChartView.view_edgess4++,- At''**  =  QY$00 0    #   3/    "    $ $XB$:L:L/L M #r@c|j}|j|jztjz}|j |jztjz}||k(r|t d|jdz z }|dz|jz}|j||||dd}t|tr{g}|jD]U} t| tr)|jt| j<|jt!| W|j#} ng}d} dj%|d | } dj%|| d } |j'|dz|| |j(d } |j+| d d z}|j+| d|j+| dzd z }|j-|d z |d z |d z|d z}|j'|d z|| |j(d }|j'||zd z |t|j/d|j0}|| |||f|j2|<||fd}|j5| d||j5|d||j5|d||j5|d||j5|d||j7|y )z6 Draw a single edge on the ChartView. rrlastr)r/rrrLNr[)ror2rr{rcrorr2c(|jd|yrr)rr;rs r>rz ChartView._draw_edge..cb<rr@r)rrrCrrDrrrkr rGr&rFrrBr^r0r r4r_r r'r< create_ovalrCr&r r _color_edge)r;rrSrr@rBr-linetagrFrLposrhs1rhs2rhstag1dotxdotydottagrhstag2lhstagrs r>rgzChartView._draw_edges   ZZ\DNN *Y->-> > XXZ$.. (9+<+< < 8 #a!+, ,B 1W.. .--Ar1F!-D dH %Cxxz *c;/JJs3::<01JJtCy)  * ((*CCCxxDS "xxCD "--Q4::d-Svvgq!A%w"QVVG_Q%771<tax4!8TAXF--q!$TZZPT-U "WM13txxz?3T^^  !(&'6Jt d 1 7L"- 7L"- 7L"- 6<, 6<, r@c||jvry|j}||||jvr|j|}|j|}|j|d||j|d||j|d|||j|d||j|d|y|jj }||jvr|j |j||jr)|jd|fk(r|j |d d yt|tr|j |d d y|j |d dy)z Color in an edge with the given colors. If no colors are specified, use intelligent defaults (dependent on selection, etc.) Nrrrr{rvrrro#084#042z#48cz#246z#00fz#008) r rrYrrWrrurHrrGr )r;r linecolor textcolorrtagsrs r>ruzChartView._color_edgeGsL t~~ %      Y%:t{{" KK- >>$'D LLayL 1 LLayL 1 LLay)L D LLayL 1 LLayL 1  &&(At{{"  T!23!diikaV&;  vv6D(+  vv6  vv6r@cD||j|<|j|y)z Mark an edge N)rYrur;rrs r>rzChartView.mark_edgefs ! D r@c|Ct|jj}i|_|D]}|j|y|j|=|j|y)z/ Unmark an edge (or all edges) N)rrYrru)r;rold_marked_edgess r>rzChartView.unmark_edgemsa <#DKK$4$4$67 DK( '  & ' D!   T "r@cH|j|j||yr)rrrs r>rzChartView.markonly_edgezs  tT"r@c d}d}|j}|jjD]}|jddt ||j dd}|j |}|j||dtjz}t||}t||d|dz }||_ ||_ |jdtjzz|_|jjD]}|j!||jdz|_dtj$|jzz|_|j)y ) z Analyze the sentence string, to figure out how big a unit needs to be, How big the tree should be, etc. Frr[r&r\r{rrN)rrWleavesr r r'r<rr _LEAF_SPACINGrrCrarDrFrrerk_TREE_LEVEL_SIZE _tree_heightr) r;unitsize text_heightrleafrr<rrs r>rzChartView._analyze~sM    KK&&( >D--14:DJJtV C66#;D HHSMGi555E5(+Hk47T!W+<=K >"' $ 1 1A 8I8I4I IKK%%' %D   t $ %"&!2!2Q!6!;!;d>O>O!OP r@cd|j}|jj|jztj dzz}t |j}|dz|jz|_ dd||jf|d<|jrdd||jf|jd<yy)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{rrMN) rrWrrCrrDrr rkrlrr)r;rrlevelss r>rzChartView._resizes    &&(4>>9IO>O0PD  n - r@czd}|j}|j}|j}tj}g|_t d|jjdzD]S}||jz|z}|r%|j|d||}|j||r/|j|d||j} |j| |j|d||} |j| |j|dzdt|d|j} |j| |dzdk(r?|r|j!d|r|j! d|j!| d|r|j!d |r|j! d |j!| d Vy ) zv Draw location lines. These are vertical gridlines used to show where each location unit is. iPrrr{r[rrgray60rgray80N)rrrrrD _loclinesrrWrrCr rrFr r r'r) r;BOTTOMc1c2c3marginrKr#t1t2t3t4s r>_draw_loclineszChartView._draw_locliness      " "   ""q$++002Q67 1ADNN"V+A^^Aq!V4 R ^^Aq!T-B-BC R 1a0B LL AqtAwt$**UB LL  1uzMM"8M4MM"8M4 bx 0MM"8M4MM"8M4 bx 09 1r@c n|jjdk(ry|j}tj}tj}t |jj D]\}}||jz|z}||jz}||zdz }|j||t||jdd} |j| } |j|dz| dtjdz z |dz | dtjdz zd d } |j| y) zDraw the sentence string.rNr{rr&r\rrz#f0f0f0r)rWrrrrDrErrCr r r'r<r rr) r;rrr-rKrr@rBr#rr<rts r>_draw_sentencezChartView._draw_sentences) ;; ! ! #q (   ! !""    !3!3!56 GAtT^^#f,Bdnn$BbA A--14:DJJsF C66#;D##QQ922Q67QQ922Q67! $B KKO! r@c|jD]}|jj|g|_d|_d|_yr)rrrr r r )r;rs r>rPzChartView.erase_trees@?? *C    $ $S ) *" r@c| |jy| |j}|j|k7rM|jj|Dcgc]}t|ts|c}|_||_d|_t|j dk(ry|jD]}|jj||j |j }|j||j|j|jj|jzdt j"zz}|j%t j&|j(zz}dd||f|jd<ycc}w)Nrr{rM)r rWtreesrGr'r r rrrr _draw_treetokr_draw_treecyclerrCrrDrrra)r;rrrtreewhs r>rzChartView.draw_treesU  cycle_treezChartView.cycle_tree"s5 $ 4 4q 8Crz%ChartView._draw_treecycle..cbAs'($ r@r)rr rrrDrWrrCr r&rrBr<rr create_polygonr) r;rErrr$r_r-rKr#rvrs r>rzChartView._draw_treecycle&sj t~~ ! # S00   "" &&(4>>9FBQFmmE1TDNNmS s#VVC[ 1as4>>*+ .Ac$..1A59::AD(((""1r61q5!QVQV$#C OO " "3 ' $q ! JJsL" -# .r@c |j}tj}g}|D]l}t|tr+|j |||dz\}}|j |>|j d|zdz|jzdz |z|dz }n|rt|t|z } n d|zdz|jzdz |z} |dz }|tj|jzz} |j| | ddt|jd|j} |j j | | tjz|jz} t#||D]\} }t|trA|r?|j%| | |jz| | dd} |j j | t|trB|s@|j%| | |jz| | dd d } |j j | t|tr|j%| | |jz| d dd} |j j | | |fS) z :param index: The index of the first leaf in the tree. :return: The index of the first leaf after the tree. rr{rcenterr)rr]rorvr2r)rrvz#048z2 3)rrvri')rrrDrGr'rrBrCsumrrrar r^rEr&rzipr )r;treetokindexdepthrrchild_xsrHchild_xnodexnodeyrchildychildxs r>rzChartView._draw_treetokGsr   "" E%&!%!3!3E5%!)!L(UQ$.. @1 Dv MN    MCM1EY]dnn4q86AE QJE33d6G6GGHmm  W]]_%  s#333d6G6GG 73" ,MFE%&5mmD--- $&&s+%&ummD---$&&s+eT*mmD--- $&&s+E" ,He|r@c|jr+|jjd|j|jr+|jjd|j |j jdi|_tt|jD](}|j|D]}|j||*|jD]}|j||jy)z1 Draw everything (from scratch). rN)rrrrrrr rrr rgrWrQr)r;rSrs r>rfzChartView.draws       $ $U + NN    ! ! ( ( /    ! !!%(T--./ +C((- +c* + +KK !D NN4  ! r@cBd|jj|i|<yrrrs r>rzChartView.add_callbackrr@cT||j|=y |j||=y#YyxYwrrrs r>rzChartView.remove_callbackrrc||jvryt|j|jD]}|| yrrrs r>rzChartView._fire_callbacksrr@r)r-rrq)r)NN)#0df)*rNrOrPrrrDr_CHART_LEVEL_SIZErkrrr8r;r?rArrKrMrrrYrerQrrgrurrrrrrrrPrrrrrfrrrrRr@r>rrs,\MGW@r0&8787D*# 2  JD)#V N3j7> ##$LQ('1R4!9@,.BN`28r@rc6eZdZdZfdZfdZfdZxZS)EdgeRulez 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!) cn|jjd}||_|jdz |_yr) __class__ __bases___edge NUM_EDGES)r;rsuperrs r>rkzEdgeRule.__init__s.((+ 1,r@c'K|jjd}||jfz }|j|||g|Ed{y7wr)rrrapply)r;rhr1rrrs r>rzEdgeRule.applysH((+ $**5;;tUGrzEdgeRule.__str__s&((+}}T""r@)rNrOrPrrkrr __classcell__)rs@r>rrs - = ##r@rc eZdZy)TopDownPredictEdgeRuleNrNrOrPrRr@r>rrr@rc eZdZy)BottomUpEdgeRuleNrrRr@r>rrrr@rc eZdZy)BottomUpLeftCornerEdgeRuleNrrRr@r>rrrr@rc eZdZy)FundamentalEdgeRuleNrrRr@r>rrrr@rceZdZd3dZdZdZdZdZdZdZ dZ d Z d Z d Z d Zd ZdZdZdZdZdZdZddgZgdZdZdZdZdZdZdZdZdZdZ d Z!d!Z"d"Z#d#Z$d$Z%d4d&Z&d'Z'd5d(Z(d)Z)d*Z*e+gZ,e-gZ.e/gZ0e1gZ2e3gZ4e,e.ze4zZ5e0e4zZ6e2e4zZ7d+Z8d,Z9d-Z:d.Z;d/Zd2Z?y%)6ChartParserAppc$|j||d|_ t|_|jj||jj d|j t |j}t |j}t |j}|jdd|jdd|jddd|j|j|j|j||j||j||jd|_d|_|j!y#t#d |j xYw) NrVrqrrr.r#rrr0zError creating Tree View) _init_parserr[r r\r]r^rrwr_init_animation_init_chartview_init_rulelabelrorl_matrix_resultsrpprint)r;r1rr\frame3frame2frame1s r>rkzChartParserApp.__init__s9 '6*  DJ JJ  U # JJOOM4<< 84::&F4::&F4::&F KKXFK 3 KKXCK 0 KKXF1K =   TZZ (  "   (   (   v &    DL DM    !  , - LLN s EE11Fc`|jy|jjd|_yrrr;rs r>r^zChartParserApp.destroys& ::    r@cRtry|jj|i|y)z Enter the Tkinter mainloop. This function must be called if this demo is created from a non-interactive program (e.g. from a secript); otherwise, the demo will close as soon as the script completes. N)r(r[rwrs r>rwzChartParserApp.mainloop!s% 9  T,V,r@c@||_||_|jyr)r(_tokens _reset_parser)r;r1rs r>rzChartParserApp._init_parser0s   r@crt|j|_|jj|j|jj |_tj|j |jD]}|jj|_ d|_ yr) r#r(_cp initializerrhrWr!rstep_cpstepr,)r; _new_edges r>rzChartParserApp._reset_parser5s&t}}5 DLL)hhnn& &--dkk4==I I  xx}} r@cttd|_|jd|jt ||_|j j |jjdtdd|j j|_ td|j j|_ y) Nr2r$r%r"r7r9rr#) rrr(r)r_sizercgetrr&r'rs r>rzChartParserApp._init_fontsDs&(6"23  /D\  t}}))&12[djjnnFVW4::>>3CD r@ct|j|_|jjdt|j|_|jjdd|_y)Nrrr)rr[_stepr_animate _animatingrs r>rzChartParserApp._init_animationPsODJJ'  qtzz*  !r@ct|j|dd|_|jjd|jy)Nr)rrr)rrW_cvr_click_cv_edge)r;rgs r>rzChartParserApp._init_chartview\s2T[[&AQO h(;(;rzChartParserApp._init_rulelabel`s+ gDNNK "Xc  6* 6*6DJJVD w r@ct|}t|}|jdd|jddt|ddd|j jd t|d dd|j jd t|ddd|j jd t|ddd|j jd t|ddd|j jd t|ddd|j jd t|djd t|ddd|j jd t|djd t|ddd|j jd t|djd t|ddd|j jd y)Nrqr#r.rrrz Reset Parserz#90c0d0r)rorr.rpr$r%zTop Down Strategyr&zBottom Up StrategyzBottom Up Left-Corner StrategyzTop Down Init Rulez#90f090zTop Down Predict RulerrzBottom Up Predict Rulez"Bottom Up Left-Corner Predict RulezFundamental Rule) rrwrresettop_down_strategybottom_up_strategybottom_up_leftcorner_strategy top_down_inittop_down_predict bottom_upbottom_up_leftcorner fundamental)r;rgrrs r>rozChartParserApp._init_buttonslsvv  , V ,   JJ  $G$   % **  $F$  & ++  $F$  2 66  $F$  & &&  $F$  ) ))  $F$  fB$$&$1 * NN  $F$  fB$$&$1 6 --  $F$  fB$$&$1 $ $$  $F$ r@c\|jjd|jj|jjd|jj|jjd|jj |jjd|jj |jjd|j|jjd|j|jjd|j|jjd|j|jjd |j|jjd |j|jjd |j|jjd |j|jjd |j|jjd|j|jjd|j |jjd|j"|jjd|j$fd|jjd|j$fd|jjd|j$fd|jjd|j&fdy)NzzzzrVrczz rz rrrzz z rZc$|jdSrrras r>z/ChartParserApp._init_bindings..ar@rc$|jdS)Nr{rrs r>rz/ChartParserApp._init_bindings..rr@rc$|jdS)Nrrrs r>rz/ChartParserApp._init_bindings..rr@rccB|j|j Sr)rr)rrcs r>rz/ChartParserApp._init_bindings..sQUUquuw;5Gr@)r[r]rr8r;r?rAr^help save_chartrqr r r r_stop_animation edit_grammar edit_sentencerrrs r>rpzChartParserApp._init_bindingss  2 23 $(("6"67  488#3#34 $(("4"45  t||4  t||4  *  t7  t7  tzz2 T334 T445 T??@  4#7#78  t'8'89  t'9'9: @A @A @A GHr@cJ t|j}t|d}|jdd|jd|jdd|jd|jdd|j d |j |jd |j |jd |j |j |jd d|jd|jdd|t|d}|jdd|jd|jdd|jd|jdd|t|d}|jdd|j|jdd|j|jdd|t|d}|jdd|jd|jdd|j d |jd!d|j"d"|j |jd#|j$ |jd$|j& |jd%|j( |jd&|j* |jd'|j, |jd(d|t|d}|j/d)d|j0d*+|j |j3d,d|j4d-|j3d.d|j4dd/0|j3d1d|j4d2d30|j3d4d|j4d5d60|jd7d|t|d}|j3d8|j6dd9|j8:|j3d;|j6dd<|j8:|j3d=|j6dd>|j8:|j3d?|j6dd@|j8:|j3dA|j6ddB|j8:|jdCd|t|d}|jdDd|j:|jdEd|j<dF|jdGd||jj?|Hy)INrryz Save Chartr~rr{r|z Reset ChartzCtrl-rz Save Grammarrz Load Grammarrrrrrz Edit GrammarrzCtrl-g Edit TextzCtrl-tEditrrq)rEr3rpResultsViewzTop Down StrategyrzBottom Up StrategyrzBottom Up Left-Corner StrategyrzBottom Up RulezBottom Up Left-Corner RulezTop Down Init RulezTop Down Predict RulezFundamental RuleApplyrrc)rEr3rr}z No Animation)rEr3rvaluezSlow AnimationrZ)rEr3rr)r}zNormal Animationr{rzFast AnimationrrAnimateTinyr)rErr3r)rpSmallrMediumLargeHugeZoomAbout InstructionsF1Helpr) r r[rrrqr r save_grammar load_grammarr^rr!r" view_matrix view_resultsr r rrrrrradd_checkbuttonradd_radiobuttonrrresizeaboutrr) r;rreditmenuviewmenurulemenu animatemenuzoommenuhelpmenus r>rlzChartParserApp._init_menubars[tzz"+OO  OO  1djjh    >4;L;LM>4;L;LM At||   &AHE+ %%  &&  &AHE+ At7G7G   94CTCTU&AHE+%**  &++  266   #3T^^L.8Q8Q   #7ASAST)43H3H   #5t?O?OP'QXF7A. ##A  $  !!### A Q $  ##"]] $  ##$]] $  ##"]] $  )q{K+  ZZKK !    ZZKK !    ZZKK !    ZZKK !    ZZKK !  &AHE+7aL Atyyd   &AHE w'r@cz||jk7r|j|y|jjyr)r, _select_edgerrrs r>rzChartParserApp._click_cv_edge}s. 4?? "   d # HH   !r@c\|j||jj|yr)rGrrrs r>_select_matrix_edgez"ChartParserApp._select_matrix_edges" $ 4 r@c||_|jj|d|jj||jr|jj||jr|jj |yy)Nz#f00)r,rrrrrrs r>rGzChartParserApp._select_edgesf tV, 4 << LL & &t , << LL " "4 ( r@cd|_|jj|jj|jr|jjyyr)r,rrrPrrs r>_deselect_edgezChartParserApp._deselect_edgesE   << LL $ $ & r@cf|j|jj|jj |jj ||jj |d|jj||jr|jj |jr|jj ||jr|jj||jr|jj |yy)Nr) _display_rulercurrent_chartrulerrrrrrrrs r>_show_new_edgezChartParserApp._show_new_edges 4885578  4  tV, 4 << LL   ! << LL & &t , << LL " "4 ( == MM  & r@cd|_ t|jdtxsdj ddy#t|jdtxsdj dYyxYw)NrzHelp: Chart Parser Applicationr`Kfixed)rr2r)rrr[rstriprs r>rzChartParserApp.helpsb   0B%%'     0B%%'  s 0:1A-c d}td|y)Nz5NLTK Chart Parser Application Written by Edward LoperzAbout: Chart Parser Application)r)r;rABOUTs r>r?zChartParserApp.aboutsM2E:r@rr))zPlaintext grammar file.cfgrrc,t|jd}|sy t|d5}tj|}ddd|_|j j||jr|jj||jr|jj|jr|jj||jj|y#1swYxYw#t$r}d}~wwxYw)zLoad a chart from a pickle filerrNr)r rrrrrWrrrrrrrrr)r;rrtrrhrs r>rqzChartParserApp.load_charts"++i   Sh% , F+ ,DK HHOOE "|| &&u-|| **,}} ''. HH  u % , , S  Ss. DC8B9D8D=D D DDct|jd}|sy t|d5}tj|j |dddy#1swYyxYw#t $r}d}~wwxYw)zSave a chart to a pickle filerrNr)rrrrrrWrrrs r>rzChartParserApp.save_chartsl$++i   Rh% 2 DKK1 2 2 2 R  Rs4 A!A AAAA A-'A((A-ct|jd}|sy |jdr+t|d5}t j |}dddn7t|5}t j|j}ddd|jy#1swYxYw#1swY'xYw#t$r}tdd|zYd}~yd}~wwxYw)z!Load a grammar from a pickle filerWrNrrzError Loading GrammarUnable to open file: %r) r GRAMMAR_FILE_TYPESrrrrr fromstringreadrUrr)r;rrtrr1rs r>r9zChartParserApp.load_grammars"--   U  +(D)2V$kk&1G22(^r8zChartParserApp.save_grammar sz$--   T  +(D)FWKKdll ;WEFF(C(5G MM557E(-R1DMMr zChartParserApp.reset$ s   $ << LL " "4;; / << LL & & ( == MM # #DKK 0 r@cZt|j|j|jyr)rr[r(rUrs r>r!zChartParserApp.edit_grammar3 s$**dmmT-=-=>r@c||_|jj||jr|jj|yyr)r(rrUrrTs r>rUzChartParserApp.set_grammar6 s9  W% == MM % %g . r@cdj|j}d}d}t|j|||j|y)NrLr$zEnter a new sentence to parse.)r_rrr[ set_sentence)r;rsentencer\instrs r>r"zChartParserApp.edit_sentence< s888DLL)0DJJ%1B1BEJr@c`t|j|_|jyr)rsplitrr )r;rks r>rjzChartParserApp.set_sentenceB sHNN,-  r@c|j|jjt|j|j|_|jj d|j yr)rr^rTr[rWrrIrs r>r:zChartParserApp.view_matrixJ sL << # LL "&tzz4;;?  !!(D,D,DEr@c|j|jjt|j|j|j |_yr)rr^r!r[rWr(rs r>r;zChartParserApp.view_resultsP s8 == $ MM ! ! #(T[[$--P r@cdd|_|j|jjyr)rrKrrrs r>r>zChartParserApp.resizeY s" 4::>>+,r@c|jj||jjt | |j jt | |j jt | yrH)rrKr'rErIr&r(rJs r>rKzChartParserApp.set_font_size] se t$ 3t9*-   s4yj 1 c$iZ0r@cHt|jjSr)rIrrrs r>rMzChartParserApp.get_font_sizec s4::>>#$$r@Nc:|jrd|_y|jd|jjr|j}|X|V|j j ||g|j}|N|jjd|_y|j j ||jyy|j j ||jjrd|_|jy|jD]}|n|jj|jr|jj|jr|jjyy)Nrr)rrNrrr,r set_strategy_apply_strategyrrr_animate_strategyrrrr)r;strategy edge_strategy selectionnewedgers r>apply_strategyzChartParserApp.apply_strategyj sE ??DO  4  ::>> I%M,E%%}Y'?&@A..0?HH((*&*DO%%h/$$& # HH ! !( +}}  ""#&&( LLD|!<<LL'')==MM((*!r@cd|_yr)rrs r>r zChartParserApp._stop_animation s r@c|jdk(ry|j|jjdk(s|jjdk(ry|jjdk(r'|j j d|jy|jjdk(r'|j j d|jy|j j d|jyy)Nrri r{ir)rrvrrrr[afterrw)r;speeds r>rwz ChartParserApp._animate_strategy s ??a      ! -}}  "a'4::>>+;q+@}}  "a'   t'='=>""$)   t'='=>   T%;%;< .r@cVt|j}||j||Sr)nextrrP)r;new_edges r>rvzChartParserApp._apply_strategy s) %      )r@c|d|jd<yt|}||jd<|jj}y)Nr`ro)rr^rrM)r;rulerr"s r>rNzChartParserApp._display_rule sB <')D  V $t9D'+D  V $88))+Dr@c<|j|jdyr)r|_TD_INITrs r>rzChartParserApp.top_down_init s DMM40r@cD|j|jtyr)r| _TD_PREDICTrrs r>rzChartParserApp.top_down_predict s D,,.DEr@cD|j|jtyr)r|_BU_RULErrs r>rzChartParserApp.bottom_up s DMM+;rz#ChartParserApp.bottom_up_leftcorner s D,,.HIr@cD|j|jtyr)r| _FUNDAMENTALrrs r>rzChartParserApp.fundamental s D--/BCr@cD|j|jtyr)r| _BU_STRATEGYrrs r>r z!ChartParserApp.bottom_up_strategy s D--/?@r@cD|j|jtyr)r|_BU_LC_STRATEGYrrs r>rz,ChartParserApp.bottom_up_leftcorner_strategy s D002LMr@cD|j|jtyr)r| _TD_STRATEGYrrs r>r z ChartParserApp.top_down_strategy s D--/EFr@)zChart Parser Applicationr)r)@rNrOrPrkr^rwrrrrrrrorprlrrIrGrLrPrr?rr\rqrr9r8r r!rUr"rjr:r;r>rKrMr|r rwrvrNr$rr%rrrrrr"rrrrrrrrrr rr rRr@r>rrsq#J -  E =  ObI:](F"! )'',&;34FG S. RU$T, 1?/ K F Q-1 %&+P =, !"H%'(K#%&H-/0K-/0Lk)L8Ll*L!L0O1F=JDANGr@rcVtjd}d}d}t|j}t d|j D]}t dt |dzft dt d|zt d t||jy) 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 forkzJohn ate the cake on the tablez grammar= (z ,)z tokens = %rz,Calling "ChartParserApp(grammar, tokens)"...) rr]rrnrr`r rrw)r1sentrrs r>appr snn G 8D +D $**, F ,##%* vtDzC'()* #J -& ! 897F#,,.r@__main__r)Bros.pathrrtkinterrrrrrrr r r r tkinter.filedialogr r tkinter.fontrtkinter.messageboxrr nltk.drawrrrnltk.draw.utilrrrrrr nltk.grammarrrnltk.parse.chartrrrr r!r"r#r$r%r& nltk.treer' nltk.utilr(r*rTr!rYrrrrrrrrrN__all__rRr@r>rs<    B2GG*   }@I)I)bL)L)hJTJTd N N p##, X'9  x!4  +E  ($= c Gc GL/< zE 'r@