<s> """<STR_LIT>""" <EOL> from __future__ import division <EOL> import nltk . compat <EOL> import pickle <EOL> from tkinter . filedialog import asksaveasfilename , askopenfilename <EOL> import tkinter <EOL> import math <EOL> import os . path <EOL> import tkinter . font , tkinter . messagebox <EOL> from nltk . parse . chart import ( BottomUpPredictCombineRule , BottomUpPredictRule , <EOL> Chart , LeafEdge , LeafInitRule , SingleEdgeFundamentalRule , <EOL> SteppingChartParser , TopDownInitRule , TopDownPredictRule , <EOL> TreeEdge ) <EOL> from nltk . tree import Tree <EOL> from nltk . grammar import Nonterminal , CFG <EOL> from nltk . util import in_idle <EOL> from nltk . draw . util import ( CanvasFrame , ColorizedList , <EOL> EntryDialog , MutableOptionMenu , <EOL> ShowText , SymbolWidget ) <EOL> from nltk . draw import CFGEditor , tree_to_treesegment , TreeSegmentWidget <EOL> class EdgeList ( ColorizedList ) : <EOL> ARROW = SymbolWidget . SYMBOLS [ '<STR_LIT>' ] <EOL> def _init_colortags ( self , textwidget , options ) : <EOL> textwidget . tag_config ( '<STR_LIT>' , foreground = '<STR_LIT>' ) <EOL> textwidget . tag_config ( '<STR_LIT>' , font = '<STR_LIT>' , underline = '<STR_LIT:0>' ) <EOL> textwidget . tag_config ( '<STR_LIT>' , foreground = '<STR_LIT>' ) <EOL> textwidget . tag_config ( '<STR_LIT>' , foreground = '<STR_LIT>' , <EOL> font = ( '<STR_LIT>' , - <NUM_LIT:12> , '<STR_LIT>' ) ) <EOL> def _item_repr ( self , item ) : <EOL> contents = [ ] <EOL> contents . append ( ( '<STR_LIT>' % item . lhs ( ) , '<STR_LIT>' ) ) <EOL> contents . append ( ( self . ARROW , '<STR_LIT>' ) ) <EOL> for i , elt in enumerate ( item . rhs ( ) ) : <EOL> if i == item . dot ( ) : <EOL> contents . append ( ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if isinstance ( elt , Nonterminal ) : <EOL> contents . append ( ( '<STR_LIT>' % elt . symbol ( ) , '<STR_LIT>' ) ) <EOL> else : <EOL> contents . append ( ( '<STR_LIT>' % elt , '<STR_LIT>' ) ) <EOL> if item . is_complete ( ) : <EOL> contents . append ( ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> return contents <EOL> class ChartMatrixView ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , parent , chart , toplevel = True , title = '<STR_LIT>' , <EOL> show_numedges = False ) : <EOL> self . _chart = chart <EOL> self . _cells = [ ] <EOL> self . _marks = [ ] <EOL> self . _selected_cell = None <EOL> if toplevel : <EOL> self . _root = tkinter . Toplevel ( parent ) <EOL> self . _root . title ( title ) <EOL> self . _root . bind ( '<STR_LIT>' , self . destroy ) <EOL> self . _init_quit ( self . _root ) <EOL> else : <EOL> self . _root = tkinter . Frame ( parent ) <EOL> self . _init_matrix ( self . _root ) <EOL> self . _init_list ( self . _root ) <EOL> if show_numedges : <EOL> self . _init_numedges ( self . _root ) <EOL> else : <EOL> self . _numedges_label = None <EOL> self . _callbacks = { } <EOL> self . _num_edges = <NUM_LIT:0> <EOL> self . draw ( ) <EOL> def _init_quit ( self , root ) : <EOL> quit = tkinter . Button ( root , text = '<STR_LIT>' , command = self . destroy ) <EOL> quit . pack ( side = '<STR_LIT>' , expand = <NUM_LIT:0> , fill = '<STR_LIT:none>' ) <EOL> def _init_matrix ( self , root ) : <EOL> cframe = tkinter . Frame ( root , border = <NUM_LIT:2> , relief = '<STR_LIT>' ) <EOL> cframe . pack ( expand = <NUM_LIT:0> , fill = '<STR_LIT:none>' , padx = <NUM_LIT:1> , pady = <NUM_LIT:3> , side = '<STR_LIT>' ) <EOL> self . _canvas = tkinter . Canvas ( cframe , width = <NUM_LIT:200> , height = <NUM_LIT:200> , <EOL> background = '<STR_LIT>' ) <EOL> self . _canvas . pack ( expand = <NUM_LIT:0> , fill = '<STR_LIT:none>' ) <EOL> def _init_numedges ( self , root ) : <EOL> self . _numedges_label = tkinter . Label ( root , text = '<STR_LIT>' ) <EOL> self . _numedges_label . pack ( expand = <NUM_LIT:0> , fill = '<STR_LIT:none>' , side = '<STR_LIT>' ) <EOL> def _init_list ( self , root ) : <EOL> self . _list = EdgeList ( root , [ ] , width = <NUM_LIT:20> , height = <NUM_LIT:5> ) <EOL> self . _list . pack ( side = '<STR_LIT>' , expand = <NUM_LIT:1> , fill = '<STR_LIT>' , pady = <NUM_LIT:3> ) <EOL> def cb ( edge , self = self ) : self . _fire_callbacks ( '<STR_LIT>' , edge ) <EOL> self . _list . add_callback ( '<STR_LIT>' , cb ) <EOL> self . _list . focus ( ) <EOL> def destroy ( self , * e ) : <EOL> if self . _root is None : return <EOL> try : self . _root . destroy ( ) <EOL> except : pass <EOL> self . _root = None <EOL> def set_chart ( self , chart ) : <EOL> if chart is not self . _chart : <EOL> self . _chart = chart <EOL> self . _num_edges = <NUM_LIT:0> <EOL> self . draw ( ) <EOL> def update ( self ) : <EOL> if self . _root is None : return <EOL> N = len ( self . _cells ) <EOL> cell_edges = [ [ <NUM_LIT:0> for i in range ( N ) ] for j in range ( N ) ] <EOL> for edge in self . _chart : <EOL> cell_edges [ edge . start ( ) ] [ edge . end ( ) ] += <NUM_LIT:1> <EOL> for i in range ( N ) : <EOL> for j in range ( i , N ) : <EOL> if cell_edges [ i ] [ j ] == <NUM_LIT:0> : <EOL> color = '<STR_LIT>' <EOL> else : <EOL> color = ( '<STR_LIT>' % <EOL> ( min ( <NUM_LIT:255> , <NUM_LIT:50> + <NUM_LIT> * cell_edges [ i ] [ j ] / <NUM_LIT:10> ) , <EOL> max ( <NUM_LIT:0> , <NUM_LIT> - <NUM_LIT> * cell_edges [ i ] [ j ] / <NUM_LIT:10> ) ) ) <EOL> cell_tag = self . _cells [ i ] [ j ] <EOL> self . _canvas . itemconfig ( cell_tag , fill = color ) <EOL> if ( i , j ) == self . _selected_cell : <EOL> self . _canvas . itemconfig ( cell_tag , outline = '<STR_LIT>' , <EOL> width = <NUM_LIT:3> ) <EOL> self . _canvas . tag_raise ( cell_tag ) <EOL> else : <EOL> self . _canvas . itemconfig ( cell_tag , outline = '<STR_LIT>' , <EOL> width = <NUM_LIT:1> ) <EOL> edges = list ( self . _chart . select ( span = self . _selected_cell ) ) <EOL> self . _list . set ( edges ) <EOL> self . _num_edges = self . _chart . num_edges ( ) <EOL> if self . _numedges_label is not None : <EOL> self . _numedges_label [ '<STR_LIT:text>' ] = '<STR_LIT>' % self . _num_edges <EOL> def activate ( self ) : <EOL> self . _canvas . itemconfig ( '<STR_LIT>' , state = '<STR_LIT>' ) <EOL> self . update ( ) <EOL> def inactivate ( self ) : <EOL> self . _canvas . itemconfig ( '<STR_LIT>' , state = '<STR_LIT>' ) <EOL> self . update ( ) <EOL> def add_callback ( self , event , func ) : <EOL> self . _callbacks . setdefault ( event , { } ) [ func ] = <NUM_LIT:1> <EOL> def remove_callback ( self , event , func = None ) : <EOL> if func is None : del self . _callbacks [ event ] <EOL> else : <EOL> try : del self . _callbacks [ event ] [ func ] <EOL> except : pass <EOL> def _fire_callbacks ( self , event , * args ) : <EOL> if event not in self . _callbacks : return <EOL> for cb_func in list ( self . _callbacks [ event ] . keys ( ) ) : cb_func ( * args ) <EOL> def select_cell ( self , i , j ) : <EOL> if self . _root is None : return <EOL> if ( ( i , j ) == self . _selected_cell and <EOL> self . _chart . num_edges ( ) == self . _num_edges ) : return <EOL> self . _selected_cell = ( i , j ) <EOL> self . update ( ) <EOL> self . _fire_callbacks ( '<STR_LIT>' , i , j ) <EOL> def deselect_cell ( self ) : <EOL> if self . _root is None : return <EOL> self . _selected_cell = None <EOL> self . _list . set ( [ ] ) <EOL> self . update ( ) <EOL> def _click_cell ( self , i , j ) : <EOL> if self . _selected_cell == ( i , j ) : <EOL> self . deselect_cell ( ) <EOL> else : <EOL> self . select_cell ( i , j ) <EOL> def view_edge ( self , edge ) : <EOL> self . select_cell ( * edge . span ( ) ) <EOL> self . _list . view ( edge ) <EOL> def mark_edge ( self , edge ) : <EOL> if self . _root is None : return <EOL> self . select_cell ( * edge . span ( ) ) <EOL> self . _list . mark ( edge ) <EOL> def unmark_edge ( self , edge = None ) : <EOL> if self . _root is None : return <EOL> self . _list . unmark ( edge ) <EOL> def markonly_edge ( self , edge ) : <EOL> if self . _root is None : return <EOL> self . select_cell ( * edge . span ( ) ) <EOL> self . _list . markonly ( edge ) <EOL> def draw ( self ) : <EOL> if self . _root is None : return <EOL> LEFT_MARGIN = BOT_MARGIN = <NUM_LIT:15> <EOL> TOP_MARGIN = <NUM_LIT:5> <EOL> c = self . _canvas <EOL> c . delete ( '<STR_LIT:all>' ) <EOL> N = self . _chart . num_leaves ( ) + <NUM_LIT:1> <EOL> dx = ( int ( c [ '<STR_LIT:width>' ] ) - LEFT_MARGIN ) / N <EOL> dy = ( int ( c [ '<STR_LIT>' ] ) - TOP_MARGIN - BOT_MARGIN ) / N <EOL> c . delete ( '<STR_LIT:all>' ) <EOL> for i in range ( N ) : <EOL> c . create_text ( LEFT_MARGIN - <NUM_LIT:2> , i * dy + dy / <NUM_LIT:2> + TOP_MARGIN , <EOL> text = repr ( i ) , anchor = '<STR_LIT:e>' ) <EOL> c . create_text ( i * dx + dx / <NUM_LIT:2> + LEFT_MARGIN , N * dy + TOP_MARGIN + <NUM_LIT:1> , <EOL> text = repr ( i ) , anchor = '<STR_LIT:n>' ) <EOL> c . create_line ( LEFT_MARGIN , dy * ( i + <NUM_LIT:1> ) + TOP_MARGIN , <EOL> dx * N + LEFT_MARGIN , dy * ( i + <NUM_LIT:1> ) + TOP_MARGIN , dash = '<STR_LIT:.>' ) <EOL> c . create_line ( dx * i + LEFT_MARGIN , TOP_MARGIN , <EOL> dx * i + LEFT_MARGIN , dy * N + TOP_MARGIN , dash = '<STR_LIT:.>' ) <EOL> c . create_rectangle ( LEFT_MARGIN , TOP_MARGIN , <EOL> LEFT_MARGIN + dx * N , dy * N + TOP_MARGIN , <EOL> width = <NUM_LIT:2> ) <EOL> self . _cells = [ [ None for i in range ( N ) ] for j in range ( N ) ] <EOL> for i in range ( N ) : <EOL> for j in range ( i , N ) : <EOL> t = c . create_rectangle ( j * dx + LEFT_MARGIN , i * dy + TOP_MARGIN , <EOL> ( j + <NUM_LIT:1> ) * dx + LEFT_MARGIN , <EOL> ( i + <NUM_LIT:1> ) * dy + TOP_MARGIN , <EOL> fill = '<STR_LIT>' ) <EOL> self . _cells [ i ] [ j ] = t <EOL> def cb ( event , self = self , i = i , j = j ) : self . _click_cell ( i , j ) <EOL> c . tag_bind ( t , '<STR_LIT>' , cb ) <EOL> xmax , ymax = int ( c [ '<STR_LIT:width>' ] ) , int ( c [ '<STR_LIT>' ] ) <EOL> t = c . create_rectangle ( - <NUM_LIT:100> , - <NUM_LIT:100> , xmax + <NUM_LIT:100> , ymax + <NUM_LIT:100> , <EOL> fill = '<STR_LIT>' , state = '<STR_LIT>' , <EOL> tag = '<STR_LIT>' ) <EOL> c . tag_lower ( t ) <EOL> self . update ( ) <EOL> def pack ( self , * args , ** kwargs ) : <EOL> self . _root . pack ( * args , ** kwargs ) <EOL> class ChartResultsView ( object ) : <EOL> def __init__ ( self , parent , chart , grammar , toplevel = True ) : <EOL> self . _chart = chart <EOL> self . _grammar = grammar <EOL> self . _trees = [ ] <EOL> self . _y = <NUM_LIT:10> <EOL> self . _treewidgets = [ ] <EOL> self . _selection = None <EOL> self . _selectbox = None <EOL> if toplevel : <EOL> self . _root = tkinter . Toplevel ( parent ) <EOL> self . _root . title ( '<STR_LIT>' ) <EOL> self . _root . bind ( '<STR_LIT>' , self . destroy ) <EOL> else : <EOL> self . _root = tkinter . Frame ( parent ) <EOL> if toplevel : <EOL> buttons = tkinter . Frame ( self . _root ) <EOL> buttons . pack ( side = '<STR_LIT>' , expand = <NUM_LIT:0> , fill = '<STR_LIT:x>' ) <EOL> tkinter . Button ( buttons , text = '<STR_LIT>' , <EOL> command = self . destroy ) . pack ( side = '<STR_LIT:right>' ) <EOL> tkinter . Button ( buttons , text = '<STR_LIT>' , <EOL> command = self . print_all ) . pack ( side = '<STR_LIT:left>' ) <EOL> tkinter . Button ( buttons , text = '<STR_LIT>' , <EOL> command = self . print_selection ) . pack ( side = '<STR_LIT:left>' ) <EOL> self . _cframe = CanvasFrame ( self . _root , closeenough = <NUM_LIT:20> ) <EOL> self . _cframe . pack ( side = '<STR_LIT>' , expand = <NUM_LIT:1> , fill = '<STR_LIT>' ) <EOL> self . update ( ) <EOL> def update ( self , edge = None ) : <EOL> if self . _root is None : return <EOL> if edge is not None : <EOL> if edge . lhs ( ) != self . _grammar . start ( ) : return <EOL> if edge . span ( ) != ( <NUM_LIT:0> , self . _chart . num_leaves ( ) ) : return <EOL> for parse in self . _chart . parses ( self . _grammar . start ( ) ) : <EOL> if parse not in self . _trees : <EOL> self . _add ( parse ) <EOL> def _add ( self , parse ) : <EOL> self . _trees . append ( parse ) <EOL> c = self . _cframe . canvas ( ) <EOL> treewidget = tree_to_treesegment ( c , parse ) <EOL> self . _treewidgets . append ( treewidget ) <EOL> self . _cframe . add_widget ( treewidget , <NUM_LIT:10> , self . _y ) <EOL> treewidget . bind_click ( self . _click ) <EOL> self . _y = treewidget . bbox ( ) [ <NUM_LIT:3> ] + <NUM_LIT:10> <EOL> def _click ( self , widget ) : <EOL> c = self . _cframe . canvas ( ) <EOL> if self . _selection is not None : <EOL> c . delete ( self . _selectbox ) <EOL> self . _selection = widget <EOL> ( x1 , y1 , x2 , y2 ) = widget . bbox ( ) <EOL> self . _selectbox = c . create_rectangle ( x1 , y1 , x2 , y2 , <EOL> width = <NUM_LIT:2> , outline = '<STR_LIT>' ) <EOL> def _color ( self , treewidget , color ) : <EOL> treewidget . label ( ) [ '<STR_LIT>' ] = color <EOL> for child in treewidget . subtrees ( ) : <EOL> if isinstance ( child , TreeSegmentWidget ) : <EOL> self . _color ( child , color ) <EOL> else : <EOL> child [ '<STR_LIT>' ] = color <EOL> def print_all ( self , * e ) : <EOL> if self . _root is None : return <EOL> self . _cframe . print_to_file ( ) <EOL> def print_selection ( self , * e ) : <EOL> if self . _root is None : return <EOL> if self . _selection is None : <EOL> tkinter . messagebox . showerror ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> else : <EOL> c = self . _cframe . canvas ( ) <EOL> for widget in self . _treewidgets : <EOL> if widget is not self . _selection : <EOL> self . _cframe . destroy_widget ( widget ) <EOL> c . delete ( self . _selectbox ) <EOL> ( x1 , y1 , x2 , y2 ) = self . _selection . bbox ( ) <EOL> self . _selection . move ( <NUM_LIT:10> - x1 , <NUM_LIT:10> - y1 ) <EOL> c [ '<STR_LIT>' ] = '<STR_LIT>' % ( x2 - x1 + <NUM_LIT:20> , y2 - y1 + <NUM_LIT:20> ) <EOL> self . _cframe . print_to_file ( ) <EOL> self . _treewidgets = [ self . _selection ] <EOL> self . clear ( ) <EOL> self . update ( ) <EOL> def clear ( self ) : <EOL> if self . _root is None : return <EOL> for treewidget in self . _treewidgets : <EOL> self . _cframe . destroy_widget ( treewidget ) <EOL> self . _trees = [ ] <EOL> self . _treewidgets = [ ] <EOL> if self . _selection is not None : <EOL> self . _cframe . canvas ( ) . delete ( self . _selectbox ) <EOL> self . _selection = None <EOL> self . _y = <NUM_LIT:10> <EOL> def set_chart ( self , chart ) : <EOL> self . clear ( ) <EOL> self . _chart = chart <EOL> self . update ( ) <EOL> def set_grammar ( self , grammar ) : <EOL> self . clear ( ) <EOL> self . _grammar = grammar <EOL> self . update ( ) <EOL> def destroy ( self , * e ) : <EOL> if self . _root is None : return <EOL> try : self . _root . destroy ( ) <EOL> except : pass <EOL> self . _root = None <EOL> def pack ( self , * args , ** kwargs ) : <EOL> self . _root . pack ( * args , ** kwargs ) <EOL> class ChartComparer ( object ) : <EOL> """<STR_LIT>""" <EOL> _OPSYMBOL = { '<STR_LIT:->' : '<STR_LIT:->' , <EOL> '<STR_LIT>' : SymbolWidget . SYMBOLS [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : SymbolWidget . SYMBOLS [ '<STR_LIT>' ] } <EOL> def __init__ ( self , * chart_filenames ) : <EOL> faketok = [ '<STR_LIT>' ] * <NUM_LIT:8> <EOL> self . _emptychart = Chart ( faketok ) <EOL> self . _left_name = '<STR_LIT:None>' <EOL> self . _right_name = '<STR_LIT:None>' <EOL> self . _left_chart = self . _emptychart <EOL> self . _right_chart = self . _emptychart <EOL> self . _charts = { '<STR_LIT:None>' : self . _emptychart } <EOL> self . _out_chart = self . _emptychart <EOL> self . _operator = None <EOL> self . _root = tkinter . Tk ( ) <EOL> self . _root . title ( '<STR_LIT>' ) <EOL> self . _root . bind ( '<STR_LIT>' , self . destroy ) <EOL> self . _root . bind ( '<STR_LIT>' , self . destroy ) <EOL> self . _init_menubar ( self . _root ) <EOL> self . _init_chartviews ( self . _root ) <EOL> self . _init_divider ( self . _root ) <EOL> self . _init_buttons ( self . _root ) <EOL> self . _init_bindings ( self . _root ) <EOL> for filename in chart_filenames : <EOL> self . load_chart ( filename ) <EOL> def destroy ( self , * e ) : <EOL> if self . _root is None : return <EOL> try : self . _root . destroy ( ) <EOL> except : pass <EOL> self . _root = None <EOL> def mainloop ( self , * args , ** kwargs ) : <EOL> return <EOL> self . _root . mainloop ( * args , ** kwargs ) <EOL> def _init_menubar ( self , root ) : <EOL> menubar = tkinter . Menu ( root ) <EOL> filemenu = tkinter . Menu ( menubar , tearoff = <NUM_LIT:0> ) <EOL> filemenu . add_command ( label = '<STR_LIT>' , accelerator = '<STR_LIT>' , <EOL> underline = <NUM_LIT:0> , command = self . load_chart_dialog ) <EOL> filemenu . add_command ( label = '<STR_LIT>' , accelerator = '<STR_LIT>' , <EOL> underline = <NUM_LIT:0> , command = self . save_chart_dialog ) <EOL> filemenu . add_separator ( ) <EOL> filemenu . add_command ( label = '<STR_LIT>' , underline = <NUM_LIT:1> , <EOL> command = self . destroy , accelerator = '<STR_LIT>' ) <EOL> menubar . add_cascade ( label = '<STR_LIT>' , underline = <NUM_LIT:0> , menu = filemenu ) <EOL> opmenu = tkinter . Menu ( menubar , tearoff = <NUM_LIT:0> ) <EOL> opmenu . add_command ( label = '<STR_LIT>' , <EOL> command = self . _intersection , <EOL> accelerator = '<STR_LIT:+>' ) <EOL> opmenu . add_command ( label = '<STR_LIT>' , <EOL> command = self . _union , <EOL> accelerator = '<STR_LIT:*>' ) <EOL> opmenu . add_command ( label = '<STR_LIT>' , <EOL> command = self . _difference , <EOL> accelerator = '<STR_LIT:->' ) <EOL> opmenu . add_separator ( ) <EOL> opmenu . add_command ( label = '<STR_LIT>' , <EOL> command = self . _swapcharts ) <EOL> menubar . add_cascade ( label = '<STR_LIT>' , underline = <NUM_LIT:0> , menu = opmenu ) <EOL> self . _root . config ( menu = menubar ) <EOL> def _init_divider ( self , root ) : <EOL> divider = tkinter . Frame ( root , border = <NUM_LIT:2> , relief = '<STR_LIT>' ) <EOL> divider . pack ( side = '<STR_LIT>' , fill = '<STR_LIT:x>' , ipady = <NUM_LIT:2> ) <EOL> def _init_chartviews ( self , root ) : <EOL> opfont = ( '<STR_LIT>' , - <NUM_LIT> ) <EOL> eqfont = ( '<STR_LIT>' , - <NUM_LIT> ) <EOL> frame = tkinter . Frame ( root , background = '<STR_LIT>' ) <EOL> frame . pack ( side = '<STR_LIT>' , expand = <NUM_LIT:1> , fill = '<STR_LIT>' ) <EOL> cv1_frame = tkinter . Frame ( frame , border = <NUM_LIT:3> , relief = '<STR_LIT>' ) <EOL> cv1_frame . pack ( side = '<STR_LIT:left>' , padx = <NUM_LIT:8> , pady = <NUM_LIT:7> , expand = <NUM_LIT:1> , fill = '<STR_LIT>' ) <EOL> self . _left_selector = MutableOptionMenu ( <EOL> cv1_frame , list ( self . _charts . keys ( ) ) , command = self . _select_left ) <EOL> self . _left_selector . pack ( side = '<STR_LIT>' , pady = <NUM_LIT:5> , fill = '<STR_LIT:x>' ) <EOL> self . _left_matrix = ChartMatrixView ( cv1_frame , self . _emptychart , <EOL> toplevel = False , <EOL> show_numedges = True ) <EOL> self . _left_matrix . pack ( side = '<STR_LIT>' , padx = <NUM_LIT:5> , pady = <NUM_LIT:5> , <EOL> expand = <NUM_LIT:1> , fill = '<STR_LIT>' ) <EOL> self . _left_matrix . add_callback ( '<STR_LIT>' , self . select_edge ) <EOL> self . _left_matrix . add_callback ( '<STR_LIT>' , self . select_cell ) <EOL> self . _left_matrix . inactivate ( ) <EOL> self . _op_label = tkinter . Label ( frame , text = '<STR_LIT:U+0020>' , width = <NUM_LIT:3> , <EOL> background = '<STR_LIT>' , font = opfont ) <EOL> self . _op_label . pack ( side = '<STR_LIT:left>' , padx = <NUM_LIT:5> , pady = <NUM_LIT:5> ) <EOL> cv2_frame = tkinter . Frame ( frame , border = <NUM_LIT:3> , relief = '<STR_LIT>' ) <EOL> cv2_frame . pack ( side = '<STR_LIT:left>' , padx = <NUM_LIT:8> , pady = <NUM_LIT:7> , expand = <NUM_LIT:1> , fill = '<STR_LIT>' ) <EOL> self . _right_selector = MutableOptionMenu ( <EOL> cv2_frame , list ( self . _charts . keys ( ) ) , command = self . _select_right ) <EOL> self . _right_selector . pack ( side = '<STR_LIT>' , pady = <NUM_LIT:5> , fill = '<STR_LIT:x>' ) <EOL> self . _right_matrix = ChartMatrixView ( cv2_frame , self . _emptychart , <EOL> toplevel = False , <EOL> show_numedges = True ) <EOL> self . _right_matrix . pack ( side = '<STR_LIT>' , padx = <NUM_LIT:5> , pady = <NUM_LIT:5> , <EOL> expand = <NUM_LIT:1> , fill = '<STR_LIT>' ) <EOL> self . _right_matrix . add_callback ( '<STR_LIT>' , self . select_edge ) <EOL> self . _right_matrix . add_callback ( '<STR_LIT>' , self . select_cell ) <EOL> self . _right_matrix . inactivate ( ) <EOL> tkinter . Label ( frame , text = '<STR_LIT:=>' , width = <NUM_LIT:3> , background = '<STR_LIT>' , <EOL> font = eqfont ) . pack ( side = '<STR_LIT:left>' , padx = <NUM_LIT:5> , pady = <NUM_LIT:5> ) <EOL> out_frame = tkinter . Frame ( frame , border = <NUM_LIT:3> , relief = '<STR_LIT>' ) <EOL> out_frame . pack ( side = '<STR_LIT:left>' , padx = <NUM_LIT:8> , pady = <NUM_LIT:7> , expand = <NUM_LIT:1> , fill = '<STR_LIT>' ) <EOL> self . _out_label = tkinter . Label ( out_frame , text = '<STR_LIT>' ) <EOL> self . _out_label . pack ( side = '<STR_LIT>' , pady = <NUM_LIT:9> ) <EOL> self . _out_matrix = ChartMatrixView ( out_frame , self . _emptychart , <EOL> toplevel = False , <EOL> show_numedges = True ) <EOL> self . _out_matrix . pack ( side = '<STR_LIT>' , padx = <NUM_LIT:5> , pady = <NUM_LIT:5> , <EOL> expand = <NUM_LIT:1> , fill = '<STR_LIT>' ) <EOL> self . _out_matrix . add_callback ( '<STR_LIT>' , self . select_edge ) <EOL> self . _out_matrix . add_callback ( '<STR_LIT>' , self . select_cell ) <EOL> self . _out_matrix . inactivate ( ) <EOL> def _init_buttons ( self , root ) : <EOL> buttons = tkinter . Frame ( root ) <EOL> buttons . pack ( side = '<STR_LIT>' , pady = <NUM_LIT:5> , fill = '<STR_LIT:x>' , expand = <NUM_LIT:0> ) <EOL> tkinter . Button ( buttons , text = '<STR_LIT>' , <EOL> command = self . _intersection ) . pack ( side = '<STR_LIT:left>' ) <EOL> tkinter . Button ( buttons , text = '<STR_LIT>' , <EOL> command = self . _union ) . pack ( side = '<STR_LIT:left>' ) <EOL> tkinter . Button ( buttons , text = '<STR_LIT>' , <EOL> command = self . _difference ) . pack ( side = '<STR_LIT:left>' ) <EOL> tkinter . Frame ( buttons , width = <NUM_LIT:20> ) . pack ( side = '<STR_LIT:left>' ) <EOL> tkinter . Button ( buttons , text = '<STR_LIT>' , <EOL> command = self . _swapcharts ) . pack ( side = '<STR_LIT:left>' ) <EOL> tkinter . Button ( buttons , text = '<STR_LIT>' , <EOL> command = self . _detatch_out ) . pack ( side = '<STR_LIT:right>' ) <EOL> def _init_bindings ( self , root ) : <EOL> root . bind ( '<STR_LIT>' , self . load_chart_dialog ) <EOL> def _select_left ( self , name ) : <EOL> self . _left_name = name <EOL> self . _left_chart = self . _charts [ name ] <EOL> self . _left_matrix . set_chart ( self . _left_chart ) <EOL> if name == '<STR_LIT:None>' : self . _left_matrix . inactivate ( ) <EOL> self . _apply_op ( ) <EOL> def _select_right ( self , name ) : <EOL> self . _right_name = name <EOL> self . _right_chart = self . _charts [ name ] <EOL> self . _right_matrix . set_chart ( self . _right_chart ) <EOL> if name == '<STR_LIT:None>' : self . _right_matrix . inactivate ( ) <EOL> self . _apply_op ( ) <EOL> def _apply_op ( self ) : <EOL> if self . _operator == '<STR_LIT:->' : self . _difference ( ) <EOL> elif self . _operator == '<STR_LIT>' : self . _union ( ) <EOL> elif self . _operator == '<STR_LIT>' : self . _intersection ( ) <EOL> CHART_FILE_TYPES = [ ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:*>' ) ] <EOL> def save_chart_dialog ( self , * args ) : <EOL> filename = asksaveasfilename ( filetypes = self . CHART_FILE_TYPES , <EOL> defaultextension = '<STR_LIT>' ) <EOL> if not filename : return <EOL> try : <EOL> with open ( filename , '<STR_LIT:wb>' ) as outfile : <EOL> pickle . dump ( self . _out_chart , outfile ) <EOL> except Exception as e : <EOL> tkinter . messagebox . showerror ( '<STR_LIT>' , <EOL> '<STR_LIT>' % <EOL> ( filename , e ) ) <EOL> def load_chart_dialog ( self , * args ) : <EOL> filename = askopenfilename ( filetypes = self . CHART_FILE_TYPES , <EOL> defaultextension = '<STR_LIT>' ) <EOL> if not filename : return <EOL> try : self . load_chart ( filename ) <EOL> except Exception as e : <EOL> tkinter . messagebox . showerror ( '<STR_LIT>' , <EOL> '<STR_LIT>' % <EOL> ( filename , e ) ) <EOL> def load_chart ( self , filename ) : <EOL> with open ( filename , '<STR_LIT:rb>' ) as infile : <EOL> chart = pickle . load ( infile ) <EOL> name = os . path . basename ( filename ) <EOL> if name . endswith ( '<STR_LIT>' ) : name = name [ : - <NUM_LIT:7> ] <EOL> if name . endswith ( '<STR_LIT>' ) : name = name [ : - <NUM_LIT:6> ] <EOL> self . _charts [ name ] = chart <EOL> self . _left_selector . add ( name ) <EOL> self . _right_selector . add ( name ) <EOL> if self . _left_chart is self . _emptychart : <EOL> self . _left_selector . set ( name ) <EOL> elif self . _right_chart is self . _emptychart : <EOL> self . _right_selector . set ( name ) <EOL> def _update_chartviews ( self ) : <EOL> self . _left_matrix . update ( ) <EOL> self . _right_matrix . update ( ) <EOL> self . _out_matrix . update ( ) <EOL> def select_edge ( self , edge ) : <EOL> if edge in self . _left_chart : <EOL> self . _left_matrix . markonly_edge ( edge ) <EOL> else : <EOL> self . _left_matrix . unmark_edge ( ) <EOL> if edge in self . _right_chart : <EOL> self . _right_matrix . markonly_edge ( edge ) <EOL> else : <EOL> self . _right_matrix . unmark_edge ( ) <EOL> if edge in self . _out_chart : <EOL> self . _out_matrix . markonly_edge ( edge ) <EOL> else : <EOL> self . _out_matrix . unmark_edge ( ) <EOL> def select_cell ( self , i , j ) : <EOL> self . _left_matrix . select_cell ( i , j ) <EOL> self . _right_matrix . select_cell ( i , j ) <EOL> self . _out_matrix . select_cell ( i , j ) <EOL> def _difference ( self ) : <EOL> if not self . _checkcompat ( ) : return <EOL> out_chart = Chart ( self . _left_chart . tokens ( ) ) <EOL> for edge in self . _left_chart : <EOL> if edge not in self . _right_chart : <EOL> out_chart . insert ( edge , [ ] ) <EOL> self . _update ( '<STR_LIT:->' , out_chart ) <EOL> def _intersection ( self ) : <EOL> if not self . _checkcompat ( ) : return <EOL> out_chart = Chart ( self . _left_chart . tokens ( ) ) <EOL> for edge in self . _left_chart : <EOL> if edge in self . _right_chart : <EOL> out_chart . insert ( edge , [ ] ) <EOL> self . _update ( '<STR_LIT>' , out_chart ) <EOL> def _union ( self ) : <EOL> if not self . _checkcompat ( ) : return <EOL> out_chart = Chart ( self . _left_chart . tokens ( ) ) <EOL> for edge in self . _left_chart : <EOL> out_chart . insert ( edge , [ ] ) <EOL> for edge in self . _right_chart : <EOL> out_chart . insert ( edge , [ ] ) <EOL> self . _update ( '<STR_LIT>' , out_chart ) <EOL> def _swapcharts ( self ) : <EOL> left , right = self . _left_name , self . _right_name <EOL> self . _left_selector . set ( right ) <EOL> self . _right_selector . set ( left ) <EOL> def _checkcompat ( self ) : <EOL> if ( self . _left_chart . tokens ( ) != self . _right_chart . tokens ( ) or <EOL> self . _left_chart . property_names ( ) != <EOL> self . _right_chart . property_names ( ) or <EOL> self . _left_chart == self . _emptychart or <EOL> self . _right_chart == self . _emptychart ) : <EOL> self . _out_chart = self . _emptychart <EOL> self . _out_matrix . set_chart ( self . _out_chart ) <EOL> self . _out_matrix . inactivate ( ) <EOL> self . _out_label [ '<STR_LIT:text>' ] = '<STR_LIT>' <EOL> return False <EOL> else : <EOL> return True <EOL> def _update ( self , operator , out_chart ) : <EOL> self . _operator = operator <EOL> self . _op_label [ '<STR_LIT:text>' ] = self . _OPSYMBOL [ operator ] <EOL> self . _out_chart = out_chart <EOL> self . _out_matrix . set_chart ( out_chart ) <EOL> self . _out_label [ '<STR_LIT:text>' ] = '<STR_LIT>' % ( self . _left_name , <EOL> self . _operator , <EOL> self . _right_name ) <EOL> def _clear_out_chart ( self ) : <EOL> self . _out_chart = self . _emptychart <EOL> self . _out_matrix . set_chart ( self . _out_chart ) <EOL> self . _op_label [ '<STR_LIT:text>' ] = '<STR_LIT:U+0020>' <EOL> self . _out_matrix . inactivate ( ) <EOL> def _detatch_out ( self ) : <EOL> ChartMatrixView ( self . _root , self . _out_chart , <EOL> title = self . _out_label [ '<STR_LIT:text>' ] ) <EOL> class ChartView ( object ) : <EOL> """<STR_LIT>""" <EOL> _LEAF_SPACING = <NUM_LIT:10> <EOL> _MARGIN = <NUM_LIT:10> <EOL> _TREE_LEVEL_SIZE = <NUM_LIT:12> <EOL> _CHART_LEVEL_SIZE = <NUM_LIT> <EOL> def __init__ ( self , chart , root = None , ** kw ) : <EOL> """<STR_LIT>""" <EOL> draw_tree = kw . get ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> draw_sentence = kw . get ( '<STR_LIT>' , <NUM_LIT:1> ) <EOL> self . _fontsize = kw . get ( '<STR_LIT>' , - <NUM_LIT:12> ) <EOL> self . _chart = chart <EOL> self . _callbacks = { } <EOL> self . _edgelevels = [ ] <EOL> self . _edgetags = { } <EOL> self . _marks = { } <EOL> self . _treetoks = [ ] <EOL> self . _treetoks_edge = None <EOL> self . _treetoks_index = <NUM_LIT:0> <EOL> self . _tree_tags = [ ] <EOL> self . _compact = <NUM_LIT:0> <EOL> if root is None : <EOL> top = tkinter . Tk ( ) <EOL> top . title ( '<STR_LIT>' ) <EOL> def destroy1 ( e , top = top ) : top . destroy ( ) <EOL> def destroy2 ( top = top ) : top . destroy ( ) <EOL> top . bind ( '<STR_LIT:q>' , destroy1 ) <EOL> b = tkinter . Button ( top , text = '<STR_LIT>' , command = destroy2 ) <EOL> b . pack ( side = '<STR_LIT>' ) <EOL> self . _root = top <EOL> else : <EOL> self . _root = root <EOL> self . _init_fonts ( root ) <EOL> ( self . _chart_sb , self . _chart_canvas ) = self . _sb_canvas ( self . _root ) <EOL> self . _chart_canvas [ '<STR_LIT>' ] = <NUM_LIT> <EOL> self . _chart_canvas [ '<STR_LIT>' ] = <NUM_LIT:15> <EOL> if draw_sentence : <EOL> cframe = tkinter . Frame ( self . _root , relief = '<STR_LIT>' , border = <NUM_LIT:2> ) <EOL> cframe . pack ( fill = '<STR_LIT>' , side = '<STR_LIT>' ) <EOL> self . _sentence_canvas = tkinter . Canvas ( cframe , height = <NUM_LIT:50> ) <EOL> self . _sentence_canvas [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . _sentence_canvas . pack ( fill = '<STR_LIT>' ) <EOL> else : <EOL> self . _sentence_canvas = None <EOL> if draw_tree : <EOL> ( sb , canvas ) = self . _sb_canvas ( self . _root , '<STR_LIT:n>' , '<STR_LIT:x>' ) <EOL> ( self . _tree_sb , self . _tree_canvas ) = ( sb , canvas ) <EOL> self . _tree_canvas [ '<STR_LIT>' ] = <NUM_LIT:200> <EOL> else : <EOL> self . _tree_canvas = None <EOL> self . _analyze ( ) <EOL> self . draw ( ) <EOL> self . _resize ( ) <EOL> self . _grow ( ) <EOL> self . _chart_canvas . bind ( '<STR_LIT>' , self . _configure ) <EOL> def _init_fonts ( self , root ) : <EOL> self . _boldfont = tkinter . font . Font ( family = '<STR_LIT>' , weight = '<STR_LIT>' , <EOL> size = self . _fontsize ) <EOL> self . _font = tkinter . font . Font ( family = '<STR_LIT>' , <EOL> size = self . _fontsize ) <EOL> self . _sysfont = tkinter . font . Font ( font = tkinter . Button ( ) [ "<STR_LIT>" ] ) <EOL> root . option_add ( "<STR_LIT>" , self . _sysfont ) <EOL> def _sb_canvas ( self , root , expand = '<STR_LIT:y>' , <EOL> fill = '<STR_LIT>' , side = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> cframe = tkinter . Frame ( root , relief = '<STR_LIT>' , border = <NUM_LIT:2> ) <EOL> cframe . pack ( fill = fill , expand = expand , side = side ) <EOL> canvas = tkinter . Canvas ( cframe , background = '<STR_LIT>' ) <EOL> sb = tkinter . Scrollbar ( cframe , orient = '<STR_LIT>' ) <EOL> sb . pack ( side = '<STR_LIT:right>' , fill = '<STR_LIT:y>' ) <EOL> canvas . pack ( side = '<STR_LIT:left>' , fill = fill , expand = '<STR_LIT:yes>' ) <EOL> sb [ '<STR_LIT>' ] = canvas . yview <EOL> canvas [ '<STR_LIT>' ] = sb . set <EOL> return ( sb , canvas ) <EOL> def scroll_up ( self , * e ) : <EOL> self . _chart_canvas . yview ( '<STR_LIT>' , - <NUM_LIT:1> , '<STR_LIT>' ) <EOL> def scroll_down ( self , * e ) : <EOL> self . _chart_canvas . yview ( '<STR_LIT>' , <NUM_LIT:1> , '<STR_LIT>' ) <EOL> def page_up ( self , * e ) : <EOL> self . _chart_canvas . yview ( '<STR_LIT>' , - <NUM_LIT:1> , '<STR_LIT>' ) <EOL> def page_down ( self , * e ) : <EOL> self . _chart_canvas . yview ( '<STR_LIT>' , <NUM_LIT:1> , '<STR_LIT>' ) <EOL> def _grow ( self ) : <EOL> """<STR_LIT>""" <EOL> N = self . _chart . num_leaves ( ) <EOL> width = max ( int ( self . _chart_canvas [ '<STR_LIT:width>' ] ) , <EOL> N * self . _unitsize + ChartView . _MARGIN * <NUM_LIT:2> ) <EOL> self . _chart_canvas . configure ( width = width ) <EOL> self . _chart_canvas . configure ( height = self . _chart_canvas [ '<STR_LIT>' ] ) <EOL> self . _unitsize = ( width - <NUM_LIT:2> * ChartView . _MARGIN ) / N <EOL> if self . _sentence_canvas is not None : <EOL> self . _sentence_canvas [ '<STR_LIT>' ] = self . _sentence_height <EOL> def set_font_size ( self , size ) : <EOL> self . _font . configure ( size = - abs ( size ) ) <EOL> self . _boldfont . configure ( size = - abs ( size ) ) <EOL> self . _sysfont . configure ( size = - abs ( size ) ) <EOL> self . _analyze ( ) <EOL> self . _grow ( ) <EOL> self . draw ( ) <EOL> def get_font_size ( self ) : <EOL> return abs ( self . _fontsize ) <EOL> def _configure ( self , e ) : <EOL> """<STR_LIT>""" <EOL> N = self . _chart . num_leaves ( ) <EOL> self . _unitsize = ( e . width - <NUM_LIT:2> * ChartView . _MARGIN ) / N <EOL> self . draw ( ) <EOL> def update ( self , chart = None ) : <EOL> """<STR_LIT>""" <EOL> if chart is not None : <EOL> self . _chart = chart <EOL> self . _edgelevels = [ ] <EOL> self . _marks = { } <EOL> self . _analyze ( ) <EOL> self . _grow ( ) <EOL> self . draw ( ) <EOL> self . erase_tree ( ) <EOL> self . _resize ( ) <EOL> else : <EOL> for edge in self . _chart : <EOL> if edge not in self . _edgetags : <EOL> self . _add_edge ( edge ) <EOL> self . _resize ( ) <EOL> def _edge_conflict ( self , edge , lvl ) : <EOL> """<STR_LIT>""" <EOL> ( s1 , e1 ) = edge . span ( ) <EOL> for otheredge in self . _edgelevels [ lvl ] : <EOL> ( s2 , e2 ) = otheredge . span ( ) <EOL> if ( s1 <= s2 < e1 ) or ( s2 <= s1 < e2 ) or ( s1 == s2 == e1 == e2 ) : <EOL> return True <EOL> return False <EOL> def _analyze_edge ( self , edge ) : <EOL> """<STR_LIT>""" <EOL> c = self . _chart_canvas <EOL> if isinstance ( edge , TreeEdge ) : <EOL> lhs = edge . lhs ( ) <EOL> rhselts = [ ] <EOL> for elt in edge . rhs ( ) : <EOL> if isinstance ( elt , Nonterminal ) : <EOL> rhselts . append ( str ( elt . symbol ( ) ) ) <EOL> else : <EOL> rhselts . append ( repr ( elt ) ) <EOL> rhs = "<STR_LIT:U+0020>" . join ( rhselts ) <EOL> else : <EOL> lhs = edge . lhs ( ) <EOL> rhs = '<STR_LIT>' <EOL> for s in ( lhs , rhs ) : <EOL> tag = c . create_text ( <NUM_LIT:0> , <NUM_LIT:0> , text = s , <EOL> font = self . _boldfont , <EOL> anchor = '<STR_LIT>' , justify = '<STR_LIT:left>' ) <EOL> bbox = c . bbox ( tag ) <EOL> c . delete ( tag ) <EOL> width = bbox [ <NUM_LIT:2> ] <EOL> edgelen = max ( edge . length ( ) , <NUM_LIT:1> ) <EOL> self . _unitsize = max ( self . _unitsize , width / edgelen ) <EOL> self . _text_height = max ( self . _text_height , bbox [ <NUM_LIT:3> ] - bbox [ <NUM_LIT:1> ] ) <EOL> def _add_edge ( self , edge , minlvl = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( edge , LeafEdge ) : return <EOL> if edge in self . _edgetags : return <EOL> self . _analyze_edge ( edge ) <EOL> self . _grow ( ) <EOL> if not self . _compact : <EOL> self . _edgelevels . append ( [ edge ] ) <EOL> lvl = len ( self . _edgelevels ) - <NUM_LIT:1> <EOL> self . _draw_edge ( edge , lvl ) <EOL> self . _resize ( ) <EOL> return <EOL> lvl = <NUM_LIT:0> <EOL> while True : <EOL> while lvl >= len ( self . _edgelevels ) : <EOL> self . _edgelevels . append ( [ ] ) <EOL> self . _resize ( ) <EOL> if lvl >= minlvl and not self . _edge_conflict ( edge , lvl ) : <EOL> self . _edgelevels [ lvl ] . append ( edge ) <EOL> break <EOL> lvl += <NUM_LIT:1> <EOL> self . _draw_edge ( edge , lvl ) <EOL> def view_edge ( self , edge ) : <EOL> level = None <EOL> for i in range ( len ( self . _edgelevels ) ) : <EOL> if edge in self . _edgelevels [ i ] : <EOL> level = i <EOL> break <EOL> if level is None : return <EOL> y = ( level + <NUM_LIT:1> ) * self . _chart_level_size <EOL> dy = self . _text_height + <NUM_LIT:10> <EOL> self . _chart_canvas . yview ( '<STR_LIT>' , <NUM_LIT:1.0> ) <EOL> if self . _chart_height != <NUM_LIT:0> : <EOL> self . _chart_canvas . yview ( '<STR_LIT>' , <EOL> ( y - dy ) / self . _chart_height ) <EOL> def _draw_edge ( self , edge , lvl ) : <EOL> """<STR_LIT>""" <EOL> c = self . _chart_canvas <EOL> x1 = ( edge . start ( ) * self . _unitsize + ChartView . _MARGIN ) <EOL> x2 = ( edge . end ( ) * self . _unitsize + ChartView . _MARGIN ) <EOL> if x2 == x1 : x2 += max ( <NUM_LIT:4> , self . _unitsize / <NUM_LIT:5> ) <EOL> y = ( lvl + <NUM_LIT:1> ) * self . _chart_level_size <EOL> linetag = c . create_line ( x1 , y , x2 , y , arrow = '<STR_LIT>' , width = <NUM_LIT:3> ) <EOL> if isinstance ( edge , TreeEdge ) : <EOL> rhs = [ ] <EOL> for elt in edge . rhs ( ) : <EOL> if isinstance ( elt , Nonterminal ) : <EOL> rhs . append ( str ( elt . symbol ( ) ) ) <EOL> else : <EOL> rhs . append ( repr ( elt ) ) <EOL> pos = edge . dot ( ) <EOL> else : <EOL> rhs = [ ] <EOL> pos = <NUM_LIT:0> <EOL> rhs1 = "<STR_LIT:U+0020>" . join ( rhs [ : pos ] ) <EOL> rhs2 = "<STR_LIT:U+0020>" . join ( rhs [ pos : ] ) <EOL> rhstag1 = c . create_text ( x1 + <NUM_LIT:3> , y , text = rhs1 , <EOL> font = self . _font , <EOL> anchor = '<STR_LIT>' ) <EOL> dotx = c . bbox ( rhstag1 ) [ <NUM_LIT:2> ] + <NUM_LIT:6> <EOL> doty = ( c . bbox ( rhstag1 ) [ <NUM_LIT:1> ] + c . bbox ( rhstag1 ) [ <NUM_LIT:3> ] ) / <NUM_LIT:2> <EOL> dottag = c . create_oval ( dotx - <NUM_LIT:2> , doty - <NUM_LIT:2> , dotx + <NUM_LIT:2> , doty + <NUM_LIT:2> ) <EOL> rhstag2 = c . create_text ( dotx + <NUM_LIT:6> , y , text = rhs2 , <EOL> font = self . _font , <EOL> anchor = '<STR_LIT>' ) <EOL> lhstag = c . create_text ( ( x1 + x2 ) / <NUM_LIT:2> , y , text = str ( edge . lhs ( ) ) , <EOL> anchor = '<STR_LIT:s>' , <EOL> font = self . _boldfont ) <EOL> self . _edgetags [ edge ] = ( linetag , rhstag1 , <EOL> dottag , rhstag2 , lhstag ) <EOL> def cb ( event , self = self , edge = edge ) : <EOL> self . _fire_callbacks ( '<STR_LIT>' , edge ) <EOL> c . tag_bind ( rhstag1 , '<STR_LIT>' , cb ) <EOL> c . tag_bind ( rhstag2 , '<STR_LIT>' , cb ) <EOL> c . tag_bind ( linetag , '<STR_LIT>' , cb ) <EOL> c . tag_bind ( dottag , '<STR_LIT>' , cb ) <EOL> c . tag_bind ( lhstag , '<STR_LIT>' , cb ) <EOL> self . _color_edge ( edge ) <EOL> def _color_edge ( self , edge , linecolor = None , textcolor = None ) : <EOL> """<STR_LIT>""" <EOL> if edge not in self . _edgetags : return <EOL> c = self . _chart_canvas <EOL> if linecolor is not None and textcolor is not None : <EOL> if edge in self . _marks : <EOL> linecolor = self . _marks [ edge ] <EOL> tags = self . _edgetags [ edge ] <EOL> c . itemconfig ( tags [ <NUM_LIT:0> ] , fill = linecolor ) <EOL> c . itemconfig ( tags [ <NUM_LIT:1> ] , fill = textcolor ) <EOL> c . itemconfig ( tags [ <NUM_LIT:2> ] , fill = textcolor , <EOL> outline = textcolor ) <EOL> c . itemconfig ( tags [ <NUM_LIT:3> ] , fill = textcolor ) <EOL> c . itemconfig ( tags [ <NUM_LIT:4> ] , fill = textcolor ) <EOL> return <EOL> else : <EOL> N = self . _chart . num_leaves ( ) <EOL> if edge in self . _marks : <EOL> self . _color_edge ( self . _marks [ edge ] ) <EOL> if ( edge . is_complete ( ) and edge . span ( ) == ( <NUM_LIT:0> , N ) ) : <EOL> self . _color_edge ( edge , '<STR_LIT>' , '<STR_LIT>' ) <EOL> elif isinstance ( edge , LeafEdge ) : <EOL> self . _color_edge ( edge , '<STR_LIT>' , '<STR_LIT>' ) <EOL> else : <EOL> self . _color_edge ( edge , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def mark_edge ( self , edge , mark = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> self . _marks [ edge ] = mark <EOL> self . _color_edge ( edge ) <EOL> def unmark_edge ( self , edge = None ) : <EOL> """<STR_LIT>""" <EOL> if edge is None : <EOL> old_marked_edges = list ( self . _marks . keys ( ) ) <EOL> self . _marks = { } <EOL> for edge in old_marked_edges : <EOL> self . _color_edge ( edge ) <EOL> else : <EOL> del self . _marks [ edge ] <EOL> self . _color_edge ( edge ) <EOL> def markonly_edge ( self , edge , mark = '<STR_LIT>' ) : <EOL> self . unmark_edge ( ) <EOL> self . mark_edge ( edge , mark ) <EOL> def _analyze ( self ) : <EOL> """<STR_LIT>""" <EOL> unitsize = <NUM_LIT> <EOL> text_height = <NUM_LIT:0> <EOL> c = self . _chart_canvas <EOL> for leaf in self . _chart . leaves ( ) : <EOL> tag = c . create_text ( <NUM_LIT:0> , <NUM_LIT:0> , text = repr ( leaf ) , <EOL> font = self . _font , <EOL> anchor = '<STR_LIT>' , justify = '<STR_LIT:left>' ) <EOL> bbox = c . bbox ( tag ) <EOL> c . delete ( tag ) <EOL> width = bbox [ <NUM_LIT:2> ] + ChartView . _LEAF_SPACING <EOL> unitsize = max ( width , unitsize ) <EOL> text_height = max ( text_height , bbox [ <NUM_LIT:3> ] - bbox [ <NUM_LIT:1> ] ) <EOL> self . _unitsize = unitsize <EOL> self . _text_height = text_height <EOL> self . _sentence_height = ( self . _text_height + <EOL> <NUM_LIT:2> * ChartView . _MARGIN ) <EOL> for edge in self . _chart . edges ( ) : <EOL> self . _analyze_edge ( edge ) <EOL> self . _chart_level_size = self . _text_height * <NUM_LIT:2> <EOL> self . _tree_height = ( <NUM_LIT:3> * ( ChartView . _TREE_LEVEL_SIZE + <EOL> self . _text_height ) ) <EOL> self . _resize ( ) <EOL> def _resize ( self ) : <EOL> """<STR_LIT>""" <EOL> c = self . _chart_canvas <EOL> width = ( self . _chart . num_leaves ( ) * self . _unitsize + <EOL> ChartView . _MARGIN * <NUM_LIT:2> ) <EOL> levels = len ( self . _edgelevels ) <EOL> self . _chart_height = ( levels + <NUM_LIT:2> ) * self . _chart_level_size <EOL> c [ '<STR_LIT>' ] = ( <NUM_LIT:0> , <NUM_LIT:0> , width , self . _chart_height ) <EOL> if self . _tree_canvas : <EOL> self . _tree_canvas [ '<STR_LIT>' ] = ( <NUM_LIT:0> , <NUM_LIT:0> , width , <EOL> self . _tree_height ) <EOL> def _draw_loclines ( self ) : <EOL> """<STR_LIT>""" <EOL> BOTTOM = <NUM_LIT> <EOL> c1 = self . _tree_canvas <EOL> c2 = self . _sentence_canvas <EOL> c3 = self . _chart_canvas <EOL> margin = ChartView . _MARGIN <EOL> self . _loclines = [ ] <EOL> for i in range ( <NUM_LIT:0> , self . _chart . num_leaves ( ) + <NUM_LIT:1> ) : <EOL> x = i * self . _unitsize + margin <EOL> if c1 : <EOL> t1 = c1 . create_line ( x , <NUM_LIT:0> , x , BOTTOM ) <EOL> c1 . tag_lower ( t1 ) <EOL> if c2 : <EOL> t2 = c2 . create_line ( x , <NUM_LIT:0> , x , self . _sentence_height ) <EOL> c2 . tag_lower ( t2 ) <EOL> t3 = c3 . create_line ( x , <NUM_LIT:0> , x , BOTTOM ) <EOL> c3 . tag_lower ( t3 ) <EOL> t4 = c3 . create_text ( x + <NUM_LIT:2> , <NUM_LIT:0> , text = repr ( i ) , anchor = '<STR_LIT>' , <EOL> font = self . _font ) <EOL> c3 . tag_lower ( t4 ) <EOL> if i % <NUM_LIT:2> == <NUM_LIT:0> : <EOL> if c1 : c1 . itemconfig ( t1 , fill = '<STR_LIT>' ) <EOL> if c2 : c2 . itemconfig ( t2 , fill = '<STR_LIT>' ) <EOL> c3 . itemconfig ( t3 , fill = '<STR_LIT>' ) <EOL> else : <EOL> if c1 : c1 . itemconfig ( t1 , fill = '<STR_LIT>' ) <EOL> if c2 : c2 . itemconfig ( t2 , fill = '<STR_LIT>' ) <EOL> c3 . itemconfig ( t3 , fill = '<STR_LIT>' ) <EOL> def _draw_sentence ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _chart . num_leaves ( ) == <NUM_LIT:0> : return <EOL> c = self . _sentence_canvas <EOL> margin = ChartView . _MARGIN <EOL> y = ChartView . _MARGIN <EOL> for i , leaf in enumerate ( self . _chart . leaves ( ) ) : <EOL> x1 = i * self . _unitsize + margin <EOL> x2 = x1 + self . _unitsize <EOL> x = ( x1 + x2 ) / <NUM_LIT:2> <EOL> tag = c . create_text ( x , y , text = repr ( leaf ) , <EOL> font = self . _font , <EOL> anchor = '<STR_LIT:n>' , justify = '<STR_LIT:left>' ) <EOL> bbox = c . bbox ( tag ) <EOL> rt = c . create_rectangle ( x1 + <NUM_LIT:2> , bbox [ <NUM_LIT:1> ] - ( ChartView . _LEAF_SPACING / <NUM_LIT:2> ) , <EOL> x2 - <NUM_LIT:2> , bbox [ <NUM_LIT:3> ] + ( ChartView . _LEAF_SPACING / <NUM_LIT:2> ) , <EOL> fill = '<STR_LIT>' , outline = '<STR_LIT>' ) <EOL> c . tag_lower ( rt ) <EOL> def erase_tree ( self ) : <EOL> for tag in self . _tree_tags : self . _tree_canvas . delete ( tag ) <EOL> self . _treetoks = [ ] <EOL> self . _treetoks_edge = None <EOL> self . _treetoks_index = <NUM_LIT:0> <EOL> def draw_tree ( self , edge = None ) : <EOL> if edge is None and self . _treetoks_edge is None : return <EOL> if edge is None : edge = self . _treetoks_edge <EOL> if self . _treetoks_edge != edge : <EOL> self . _treetoks = [ t for t in self . _chart . trees ( edge ) <EOL> if isinstance ( t , Tree ) ] <EOL> self . _treetoks_edge = edge <EOL> self . _treetoks_index = <NUM_LIT:0> <EOL> if len ( self . _treetoks ) == <NUM_LIT:0> : return <EOL> for tag in self . _tree_tags : self . _tree_canvas . delete ( tag ) <EOL> tree = self . _treetoks [ self . _treetoks_index ] <EOL> self . _draw_treetok ( tree , edge . start ( ) ) <EOL> self . _draw_treecycle ( ) <EOL> w = self . _chart . num_leaves ( ) * self . _unitsize + <NUM_LIT:2> * ChartView . _MARGIN <EOL> h = tree . height ( ) * ( ChartView . _TREE_LEVEL_SIZE + self . _text_height ) <EOL> self . _tree_canvas [ '<STR_LIT>' ] = ( <NUM_LIT:0> , <NUM_LIT:0> , w , h ) <EOL> def cycle_tree ( self ) : <EOL> self . _treetoks_index = ( self . _treetoks_index + <NUM_LIT:1> ) % len ( self . _treetoks ) <EOL> self . draw_tree ( self . _treetoks_edge ) <EOL> def _draw_treecycle ( self ) : <EOL> if len ( self . _treetoks ) <= <NUM_LIT:1> : return <EOL> label = '<STR_LIT>' % len ( self . _treetoks ) <EOL> c = self . _tree_canvas <EOL> margin = ChartView . _MARGIN <EOL> right = self . _chart . num_leaves ( ) * self . _unitsize + margin - <NUM_LIT:2> <EOL> tag = c . create_text ( right , <NUM_LIT:2> , anchor = '<STR_LIT>' , text = label , <EOL> font = self . _boldfont ) <EOL> self . _tree_tags . append ( tag ) <EOL> _ , _ , _ , y = c . bbox ( tag ) <EOL> for i in range ( len ( self . _treetoks ) ) : <EOL> x = right - <NUM_LIT:20> * ( len ( self . _treetoks ) - i - <NUM_LIT:1> ) <EOL> if i == self . _treetoks_index : fill = '<STR_LIT>' <EOL> else : fill = '<STR_LIT>' <EOL> tag = c . create_polygon ( x , y + <NUM_LIT:10> , x - <NUM_LIT:5> , y , x - <NUM_LIT:10> , y + <NUM_LIT:10> , <EOL> fill = fill , outline = '<STR_LIT>' ) <EOL> self . _tree_tags . append ( tag ) <EOL> def cb ( event , self = self , i = i ) : <EOL> self . _treetoks_index = i <EOL> self . draw_tree ( ) <EOL> c . tag_bind ( tag , '<STR_LIT>' , cb ) <EOL> def _draw_treetok ( self , treetok , index , depth = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> c = self . _tree_canvas <EOL> margin = ChartView . _MARGIN <EOL> child_xs = [ ] <EOL> for child in treetok : <EOL> if isinstance ( child , Tree ) : <EOL> child_x , index = self . _draw_treetok ( child , index , depth + <NUM_LIT:1> ) <EOL> child_xs . append ( child_x ) <EOL> else : <EOL> child_xs . append ( ( <NUM_LIT:2> * index + <NUM_LIT:1> ) * self . _unitsize / <NUM_LIT:2> + margin ) <EOL> index += <NUM_LIT:1> <EOL> if child_xs : <EOL> nodex = sum ( child_xs ) / len ( child_xs ) <EOL> else : <EOL> nodex = ( <NUM_LIT:2> * index + <NUM_LIT:1> ) * self . _unitsize / <NUM_LIT:2> + margin <EOL> index += <NUM_LIT:1> <EOL> nodey = depth * ( ChartView . _TREE_LEVEL_SIZE + self . _text_height ) <EOL> tag = c . create_text ( nodex , nodey , anchor = '<STR_LIT:n>' , justify = '<STR_LIT>' , <EOL> text = str ( treetok . label ( ) ) , fill = '<STR_LIT>' , <EOL> font = self . _boldfont ) <EOL> self . _tree_tags . append ( tag ) <EOL> childy = nodey + ChartView . _TREE_LEVEL_SIZE + self . _text_height <EOL> for childx , child in zip ( child_xs , treetok ) : <EOL> if isinstance ( child , Tree ) and child : <EOL> tag = c . create_line ( nodex , nodey + self . _text_height , <EOL> childx , childy , width = <NUM_LIT:2> , fill = '<STR_LIT>' ) <EOL> self . _tree_tags . append ( tag ) <EOL> if isinstance ( child , Tree ) and not child : <EOL> tag = c . create_line ( nodex , nodey + self . _text_height , <EOL> childx , childy , width = <NUM_LIT:2> , <EOL> fill = '<STR_LIT>' , dash = '<STR_LIT>' ) <EOL> self . _tree_tags . append ( tag ) <EOL> if not isinstance ( child , Tree ) : <EOL> tag = c . create_line ( nodex , nodey + self . _text_height , <EOL> childx , <NUM_LIT> , width = <NUM_LIT:2> , fill = '<STR_LIT>' ) <EOL> self . _tree_tags . append ( tag ) <EOL> return nodex , index <EOL> def draw ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _tree_canvas : <EOL> self . _tree_canvas . delete ( '<STR_LIT:all>' ) <EOL> self . draw_tree ( ) <EOL> if self . _sentence_canvas : <EOL> self . _sentence_canvas . delete ( '<STR_LIT:all>' ) <EOL> self . _draw_sentence ( ) <EOL> self . _chart_canvas . delete ( '<STR_LIT:all>' ) <EOL> self . _edgetags = { } <EOL> for lvl in range ( len ( self . _edgelevels ) ) : <EOL> for edge in self . _edgelevels [ lvl ] : <EOL> self . _draw_edge ( edge , lvl ) <EOL> for edge in self . _chart : <EOL> self . _add_edge ( edge ) <EOL> self . _draw_loclines ( ) <EOL> def add_callback ( self , event , func ) : <EOL> self . _callbacks . setdefault ( event , { } ) [ func ] = <NUM_LIT:1> <EOL> def remove_callback ( self , event , func = None ) : <EOL> if func is None : del self . _callbacks [ event ] <EOL> else : <EOL> try : del self . _callbacks [ event ] [ func ] <EOL> except : pass <EOL> def _fire_callbacks ( self , event , * args ) : <EOL> if event not in self . _callbacks : return <EOL> for cb_func in list ( self . _callbacks [ event ] . keys ( ) ) : cb_func ( * args ) <EOL> class EdgeRule ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , edge ) : <EOL> super = self . __class__ . __bases__ [ <NUM_LIT:1> ] <EOL> self . _edge = edge <EOL> self . NUM_EDGES = super . NUM_EDGES - <NUM_LIT:1> <EOL> def apply ( self , chart , grammar , * edges ) : <EOL> super = self . __class__ . __bases__ [ <NUM_LIT:1> ] <EOL> edges += ( self . _edge , ) <EOL> for e in super . apply ( self , chart , grammar , * edges ) : yield e <EOL> def __str__ ( self ) : <EOL> super = self . __class__ . __bases__ [ <NUM_LIT:1> ] <EOL> return super . __str__ ( self ) <EOL> class TopDownPredictEdgeRule ( EdgeRule , TopDownPredictRule ) : <EOL> pass <EOL> class BottomUpEdgeRule ( EdgeRule , BottomUpPredictRule ) : <EOL> pass <EOL> class BottomUpLeftCornerEdgeRule ( EdgeRule , BottomUpPredictCombineRule ) : <EOL> pass <EOL> class FundamentalEdgeRule ( EdgeRule , SingleEdgeFundamentalRule ) : <EOL> pass <EOL> class ChartParserApp ( object ) : <EOL> def __init__ ( self , grammar , tokens , title = '<STR_LIT>' ) : <EOL> self . _init_parser ( grammar , tokens ) <EOL> self . _root = None <EOL> try : <EOL> self . _root = tkinter . Tk ( ) <EOL> self . _root . title ( title ) <EOL> self . _root . bind ( '<STR_LIT>' , self . destroy ) <EOL> frame3 = tkinter . Frame ( self . _root ) <EOL> frame2 = tkinter . Frame ( self . _root ) <EOL> frame1 = tkinter . Frame ( self . _root ) <EOL> frame3 . pack ( side = '<STR_LIT>' , fill = '<STR_LIT:none>' ) <EOL> frame2 . pack ( side = '<STR_LIT>' , fill = '<STR_LIT:x>' ) <EOL> frame1 . pack ( side = '<STR_LIT>' , fill = '<STR_LIT>' , expand = <NUM_LIT:1> ) <EOL> self . _init_fonts ( self . _root ) <EOL> self . _init_animation ( ) <EOL> self . _init_chartview ( frame1 ) <EOL> self . _init_rulelabel ( frame2 ) <EOL> self . _init_buttons ( frame3 ) <EOL> self . _init_menubar ( ) <EOL> self . _matrix = None <EOL> self . _results = None <EOL> self . _init_bindings ( ) <EOL> except : <EOL> print ( '<STR_LIT>' ) <EOL> self . destroy ( ) <EOL> raise <EOL> def destroy ( self , * args ) : <EOL> if self . _root is None : return <EOL> self . _root . destroy ( ) <EOL> self . _root = None <EOL> def mainloop ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> if in_idle ( ) : return <EOL> self . _root . mainloop ( * args , ** kwargs ) <EOL> def _init_parser ( self , grammar , tokens ) : <EOL> self . _grammar = grammar <EOL> self . _tokens = tokens <EOL> self . _reset_parser ( ) <EOL> def _reset_parser ( self ) : <EOL> self . _cp = SteppingChartParser ( self . _grammar ) <EOL> self . _cp . initialize ( self . _tokens ) <EOL> self . _chart = self . _cp . chart ( ) <EOL> for _new_edge in LeafInitRule ( ) . apply ( self . _chart , self . _grammar ) : <EOL> pass <EOL> self . _cpstep = self . _cp . step ( ) <EOL> self . _selection = None <EOL> def _init_fonts ( self , root ) : <EOL> self . _sysfont = tkinter . font . Font ( font = tkinter . Button ( ) [ "<STR_LIT>" ] ) <EOL> root . option_add ( "<STR_LIT>" , self . _sysfont ) <EOL> self . _size = tkinter . IntVar ( root ) <EOL> self . _size . set ( self . _sysfont . cget ( '<STR_LIT:size>' ) ) <EOL> self . _boldfont = tkinter . font . Font ( family = '<STR_LIT>' , weight = '<STR_LIT>' , <EOL> size = self . _size . get ( ) ) <EOL> self . _font = tkinter . font . Font ( family = '<STR_LIT>' , <EOL> size = self . _size . get ( ) ) <EOL> def _init_animation ( self ) : <EOL> self . _step = tkinter . IntVar ( self . _root ) <EOL> self . _step . set ( <NUM_LIT:1> ) <EOL> self . _animate = tkinter . IntVar ( self . _root ) <EOL> self . _animate . set ( <NUM_LIT:3> ) <EOL> self . _animating = <NUM_LIT:0> <EOL> def _init_chartview ( self , parent ) : <EOL> self . _cv = ChartView ( self . _chart , parent , <EOL> draw_tree = <NUM_LIT:1> , draw_sentence = <NUM_LIT:1> ) <EOL> self . _cv . add_callback ( '<STR_LIT>' , self . _click_cv_edge ) <EOL> def _init_rulelabel ( self , parent ) : <EOL> ruletxt = '<STR_LIT>' <EOL> self . _rulelabel1 = tkinter . Label ( parent , text = ruletxt , <EOL> font = self . _boldfont ) <EOL> self . _rulelabel2 = tkinter . Label ( parent , width = <NUM_LIT> , <EOL> relief = '<STR_LIT>' , anchor = '<STR_LIT:w>' , <EOL> font = self . _boldfont ) <EOL> self . _rulelabel1 . pack ( side = '<STR_LIT:left>' ) <EOL> self . _rulelabel2 . pack ( side = '<STR_LIT:left>' ) <EOL> step = tkinter . Checkbutton ( parent , variable = self . _step , <EOL> text = '<STR_LIT>' ) <EOL> step . pack ( side = '<STR_LIT:right>' ) <EOL> def _init_buttons ( self , parent ) : <EOL> frame1 = tkinter . Frame ( parent ) <EOL> frame2 = tkinter . Frame ( parent ) <EOL> frame1 . pack ( side = '<STR_LIT>' , fill = '<STR_LIT:x>' ) <EOL> frame2 . pack ( side = '<STR_LIT>' , fill = '<STR_LIT:none>' ) <EOL> tkinter . Button ( frame1 , text = '<STR_LIT>' , <EOL> background = '<STR_LIT>' , foreground = '<STR_LIT>' , <EOL> command = self . reset ) . pack ( side = '<STR_LIT:right>' ) <EOL> tkinter . Button ( frame1 , text = '<STR_LIT>' , <EOL> background = '<STR_LIT>' , foreground = '<STR_LIT>' , <EOL> command = self . top_down_strategy ) . pack ( side = '<STR_LIT:left>' ) <EOL> tkinter . Button ( frame1 , text = '<STR_LIT>' , <EOL> background = '<STR_LIT>' , foreground = '<STR_LIT>' , <EOL> command = self . bottom_up_strategy ) . pack ( side = '<STR_LIT:left>' ) <EOL> tkinter . Button ( frame1 , text = '<STR_LIT>' , <EOL> background = '<STR_LIT>' , foreground = '<STR_LIT>' , <EOL> command = self . bottom_up_leftcorner_strategy ) . pack ( side = '<STR_LIT:left>' ) <EOL> tkinter . Button ( frame2 , text = '<STR_LIT>' , <EOL> background = '<STR_LIT>' , foreground = '<STR_LIT>' , <EOL> command = self . top_down_init ) . pack ( side = '<STR_LIT:left>' ) <EOL> tkinter . Button ( frame2 , text = '<STR_LIT>' , <EOL> background = '<STR_LIT>' , foreground = '<STR_LIT>' , <EOL> command = self . top_down_predict ) . pack ( side = '<STR_LIT:left>' ) <EOL> tkinter . Frame ( frame2 , width = <NUM_LIT:20> ) . pack ( side = '<STR_LIT:left>' ) <EOL> tkinter . Button ( frame2 , text = '<STR_LIT>' , <EOL> background = '<STR_LIT>' , foreground = '<STR_LIT>' , <EOL> command = self . bottom_up ) . pack ( side = '<STR_LIT:left>' ) <EOL> tkinter . Frame ( frame2 , width = <NUM_LIT:20> ) . pack ( side = '<STR_LIT:left>' ) <EOL> tkinter . Button ( frame2 , text = '<STR_LIT>' , <EOL> background = '<STR_LIT>' , foreground = '<STR_LIT>' , <EOL> command = self . bottom_up_leftcorner ) . pack ( side = '<STR_LIT:left>' ) <EOL> tkinter . Frame ( frame2 , width = <NUM_LIT:20> ) . pack ( side = '<STR_LIT:left>' ) <EOL> tkinter . Button ( frame2 , text = '<STR_LIT>' , <EOL> background = '<STR_LIT>' , foreground = '<STR_LIT>' , <EOL> command = self . fundamental ) . pack ( side = '<STR_LIT:left>' ) <EOL> def _init_bindings ( self ) : <EOL> self . _root . bind ( '<STR_LIT>' , self . _cv . scroll_up ) <EOL> self . _root . bind ( '<STR_LIT>' , self . _cv . scroll_down ) <EOL> self . _root . bind ( '<STR_LIT>' , self . _cv . page_up ) <EOL> self . _root . bind ( '<STR_LIT>' , self . _cv . page_down ) <EOL> self . _root . bind ( '<STR_LIT>' , self . destroy ) <EOL> self . _root . bind ( '<STR_LIT>' , self . destroy ) <EOL> self . _root . bind ( '<STR_LIT>' , self . help ) <EOL> self . _root . bind ( '<STR_LIT>' , self . save_chart ) <EOL> self . _root . bind ( '<STR_LIT>' , self . load_chart ) <EOL> self . _root . bind ( '<STR_LIT>' , self . reset ) <EOL> self . _root . bind ( '<STR_LIT:t>' , self . top_down_strategy ) <EOL> self . _root . bind ( '<STR_LIT:b>' , self . bottom_up_strategy ) <EOL> self . _root . bind ( '<STR_LIT:c>' , self . bottom_up_leftcorner_strategy ) <EOL> self . _root . bind ( '<STR_LIT>' , self . _stop_animation ) <EOL> self . _root . bind ( '<STR_LIT>' , self . edit_grammar ) <EOL> self . _root . bind ( '<STR_LIT>' , self . edit_sentence ) <EOL> self . _root . bind ( '<STR_LIT:->' , lambda e , a = self . _animate : a . set ( <NUM_LIT:1> ) ) <EOL> self . _root . bind ( '<STR_LIT:=>' , lambda e , a = self . _animate : a . set ( <NUM_LIT:2> ) ) <EOL> self . _root . bind ( '<STR_LIT:+>' , lambda e , a = self . _animate : a . set ( <NUM_LIT:3> ) ) <EOL> self . _root . bind ( '<STR_LIT:s>' , lambda e , s = self . _step : s . set ( not s . get ( ) ) ) <EOL> def _init_menubar ( self ) : <EOL> menubar = tkinter . Menu ( self . _root ) <EOL> filemenu = tkinter . Menu ( menubar , tearoff = <NUM_LIT:0> ) <EOL> filemenu . add_command ( label = '<STR_LIT>' , underline = <NUM_LIT:0> , <EOL> command = self . save_chart , accelerator = '<STR_LIT>' ) <EOL> filemenu . add_command ( label = '<STR_LIT>' , underline = <NUM_LIT:0> , <EOL> command = self . load_chart , accelerator = '<STR_LIT>' ) <EOL> filemenu . add_command ( label = '<STR_LIT>' , underline = <NUM_LIT:0> , <EOL> command = self . reset , accelerator = '<STR_LIT>' ) <EOL> filemenu . add_separator ( ) <EOL> filemenu . add_command ( label = '<STR_LIT>' , <EOL> command = self . save_grammar ) <EOL> filemenu . add_command ( label = '<STR_LIT>' , <EOL> command = self . load_grammar ) <EOL> filemenu . add_separator ( ) <EOL> filemenu . add_command ( label = '<STR_LIT>' , underline = <NUM_LIT:1> , <EOL> command = self . destroy , accelerator = '<STR_LIT>' ) <EOL> menubar . add_cascade ( label = '<STR_LIT>' , underline = <NUM_LIT:0> , menu = filemenu ) <EOL> editmenu = tkinter . Menu ( menubar , tearoff = <NUM_LIT:0> ) <EOL> editmenu . add_command ( label = '<STR_LIT>' , underline = <NUM_LIT:5> , <EOL> command = self . edit_grammar , <EOL> accelerator = '<STR_LIT>' ) <EOL> editmenu . add_command ( label = '<STR_LIT>' , underline = <NUM_LIT:5> , <EOL> command = self . edit_sentence , <EOL> accelerator = '<STR_LIT>' ) <EOL> menubar . add_cascade ( label = '<STR_LIT>' , underline = <NUM_LIT:0> , menu = editmenu ) <EOL> viewmenu = tkinter . Menu ( menubar , tearoff = <NUM_LIT:0> ) <EOL> viewmenu . add_command ( label = '<STR_LIT>' , underline = <NUM_LIT:6> , <EOL> command = self . view_matrix ) <EOL> viewmenu . add_command ( label = '<STR_LIT>' , underline = <NUM_LIT:0> , <EOL> command = self . view_results ) <EOL> menubar . add_cascade ( label = '<STR_LIT>' , underline = <NUM_LIT:0> , menu = viewmenu ) <EOL> rulemenu = tkinter . Menu ( menubar , tearoff = <NUM_LIT:0> ) <EOL> rulemenu . add_command ( label = '<STR_LIT>' , underline = <NUM_LIT:0> , <EOL> command = self . top_down_strategy , <EOL> accelerator = '<STR_LIT:t>' ) <EOL> rulemenu . add_command ( label = '<STR_LIT>' , underline = <NUM_LIT:0> , <EOL> command = self . bottom_up_strategy , <EOL> accelerator = '<STR_LIT:b>' ) <EOL> rulemenu . add_command ( label = '<STR_LIT>' , underline = <NUM_LIT:0> , <EOL> command = self . bottom_up_leftcorner_strategy , <EOL> accelerator = '<STR_LIT:c>' ) <EOL> rulemenu . add_separator ( ) <EOL> rulemenu . add_command ( label = '<STR_LIT>' , <EOL> command = self . bottom_up ) <EOL> rulemenu . add_command ( label = '<STR_LIT>' , <EOL> command = self . bottom_up_leftcorner ) <EOL> rulemenu . add_command ( label = '<STR_LIT>' , <EOL> command = self . top_down_init ) <EOL> rulemenu . add_command ( label = '<STR_LIT>' , <EOL> command = self . top_down_predict ) <EOL> rulemenu . add_command ( label = '<STR_LIT>' , <EOL> command = self . fundamental ) <EOL> menubar . add_cascade ( label = '<STR_LIT>' , underline = <NUM_LIT:0> , menu = rulemenu ) <EOL> animatemenu = tkinter . Menu ( menubar , tearoff = <NUM_LIT:0> ) <EOL> animatemenu . add_checkbutton ( label = "<STR_LIT>" , underline = <NUM_LIT:0> , <EOL> variable = self . _step , <EOL> accelerator = '<STR_LIT:s>' ) <EOL> animatemenu . add_separator ( ) <EOL> animatemenu . add_radiobutton ( label = "<STR_LIT>" , underline = <NUM_LIT:0> , <EOL> variable = self . _animate , value = <NUM_LIT:0> ) <EOL> animatemenu . add_radiobutton ( label = "<STR_LIT>" , underline = <NUM_LIT:0> , <EOL> variable = self . _animate , value = <NUM_LIT:1> , <EOL> accelerator = '<STR_LIT:->' ) <EOL> animatemenu . add_radiobutton ( label = "<STR_LIT>" , underline = <NUM_LIT:0> , <EOL> variable = self . _animate , value = <NUM_LIT:2> , <EOL> accelerator = '<STR_LIT:=>' ) <EOL> animatemenu . add_radiobutton ( label = "<STR_LIT>" , underline = <NUM_LIT:0> , <EOL> variable = self . _animate , value = <NUM_LIT:3> , <EOL> accelerator = '<STR_LIT:+>' ) <EOL> menubar . add_cascade ( label = "<STR_LIT>" , underline = <NUM_LIT:1> , menu = animatemenu ) <EOL> zoommenu = tkinter . Menu ( menubar , tearoff = <NUM_LIT:0> ) <EOL> zoommenu . add_radiobutton ( label = '<STR_LIT>' , variable = self . _size , <EOL> underline = <NUM_LIT:0> , value = <NUM_LIT:10> , command = self . resize ) <EOL> zoommenu . add_radiobutton ( label = '<STR_LIT>' , variable = self . _size , <EOL> underline = <NUM_LIT:0> , value = <NUM_LIT:12> , command = self . resize ) <EOL> zoommenu . add_radiobutton ( label = '<STR_LIT>' , variable = self . _size , <EOL> underline = <NUM_LIT:0> , value = <NUM_LIT> , command = self . resize ) <EOL> zoommenu . add_radiobutton ( label = '<STR_LIT>' , variable = self . _size , <EOL> underline = <NUM_LIT:0> , value = <NUM_LIT> , command = self . resize ) <EOL> zoommenu . add_radiobutton ( label = '<STR_LIT>' , variable = self . _size , <EOL> underline = <NUM_LIT:0> , value = <NUM_LIT> , command = self . resize ) <EOL> menubar . add_cascade ( label = '<STR_LIT>' , underline = <NUM_LIT:0> , menu = zoommenu ) <EOL> helpmenu = tkinter . Menu ( menubar , tearoff = <NUM_LIT:0> ) <EOL> helpmenu . add_command ( label = '<STR_LIT>' , underline = <NUM_LIT:0> , <EOL> command = self . about ) <EOL> helpmenu . add_command ( label = '<STR_LIT>' , underline = <NUM_LIT:0> , <EOL> command = self . help , accelerator = '<STR_LIT>' ) <EOL> menubar . add_cascade ( label = '<STR_LIT>' , underline = <NUM_LIT:0> , menu = helpmenu ) <EOL> self . _root . config ( menu = menubar ) <EOL> def _click_cv_edge ( self , edge ) : <EOL> if edge != self . _selection : <EOL> self . _select_edge ( edge ) <EOL> else : <EOL> self . _cv . cycle_tree ( ) <EOL> def _select_matrix_edge ( self , edge ) : <EOL> self . _select_edge ( edge ) <EOL> self . _cv . view_edge ( edge ) <EOL> def _select_edge ( self , edge ) : <EOL> self . _selection = edge <EOL> self . _cv . markonly_edge ( edge , '<STR_LIT>' ) <EOL> self . _cv . draw_tree ( edge ) <EOL> if self . _matrix : self . _matrix . markonly_edge ( edge ) <EOL> if self . _matrix : self . _matrix . view_edge ( edge ) <EOL> def _deselect_edge ( self ) : <EOL> self . _selection = None <EOL> self . _cv . unmark_edge ( ) <EOL> self . _cv . erase_tree ( ) <EOL> if self . _matrix : self . _matrix . unmark_edge ( ) <EOL> def _show_new_edge ( self , edge ) : <EOL> self . _display_rule ( self . _cp . current_chartrule ( ) ) <EOL> self . _cv . update ( ) <EOL> self . _cv . draw_tree ( edge ) <EOL> self . _cv . markonly_edge ( edge , '<STR_LIT>' ) <EOL> self . _cv . view_edge ( edge ) <EOL> if self . _matrix : self . _matrix . update ( ) <EOL> if self . _matrix : self . _matrix . markonly_edge ( edge ) <EOL> if self . _matrix : self . _matrix . view_edge ( edge ) <EOL> if self . _results : self . _results . update ( edge ) <EOL> def help ( self , * e ) : <EOL> self . _animating = <NUM_LIT:0> <EOL> try : <EOL> ShowText ( self . _root , '<STR_LIT>' , <EOL> ( __doc__ or '<STR_LIT>' ) . strip ( ) , width = <NUM_LIT> , font = '<STR_LIT>' ) <EOL> except : <EOL> ShowText ( self . _root , '<STR_LIT>' , <EOL> ( __doc__ or '<STR_LIT>' ) . strip ( ) , width = <NUM_LIT> ) <EOL> def about ( self , * e ) : <EOL> ABOUT = ( "<STR_LIT>" + <EOL> "<STR_LIT>" ) <EOL> tkinter . messagebox . showinfo ( '<STR_LIT>' , ABOUT ) <EOL> CHART_FILE_TYPES = [ ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:*>' ) ] <EOL> GRAMMAR_FILE_TYPES = [ ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT:*>' ) ] <EOL> def load_chart ( self , * args ) : <EOL> "<STR_LIT>" <EOL> filename = askopenfilename ( filetypes = self . CHART_FILE_TYPES , <EOL> defaultextension = '<STR_LIT>' ) <EOL> if not filename : return <EOL> try : <EOL> with open ( filename , '<STR_LIT:rb>' ) as infile : <EOL> chart = pickle . load ( infile ) <EOL> self . _chart = chart <EOL> self . _cv . update ( chart ) <EOL> if self . _matrix : self . _matrix . set_chart ( chart ) <EOL> if self . _matrix : self . _matrix . deselect_cell ( ) <EOL> if self . _results : self . _results . set_chart ( chart ) <EOL> self . _cp . set_chart ( chart ) <EOL> except Exception as e : <EOL> raise <EOL> tkinter . messagebox . showerror ( '<STR_LIT>' , <EOL> '<STR_LIT>' % filename ) <EOL> def save_chart ( self , * args ) : <EOL> "<STR_LIT>" <EOL> filename = asksaveasfilename ( filetypes = self . CHART_FILE_TYPES , <EOL> defaultextension = '<STR_LIT>' ) <EOL> if not filename : return <EOL> try : <EOL> with open ( filename , '<STR_LIT:wb>' ) as outfile : <EOL> pickle . dump ( self . _chart , outfile ) <EOL> except Exception as e : <EOL> raise <EOL> tkinter . messagebox . showerror ( '<STR_LIT>' , <EOL> '<STR_LIT>' % filename ) <EOL> def load_grammar ( self , * args ) : <EOL> "<STR_LIT>" <EOL> filename = askopenfilename ( filetypes = self . GRAMMAR_FILE_TYPES , <EOL> defaultextension = '<STR_LIT>' ) <EOL> if not filename : return <EOL> try : <EOL> if filename . endswith ( '<STR_LIT>' ) : <EOL> with open ( filename , '<STR_LIT:rb>' ) as infile : <EOL> grammar = pickle . load ( infile ) <EOL> else : <EOL> with open ( filename , '<STR_LIT:r>' ) as infile : <EOL> grammar = CFG . fromstring ( infile . read ( ) ) <EOL> self . set_grammar ( grammar ) <EOL> except Exception as e : <EOL> tkinter . messagebox . showerror ( '<STR_LIT>' , <EOL> '<STR_LIT>' % filename ) <EOL> def save_grammar ( self , * args ) : <EOL> filename = asksaveasfilename ( filetypes = self . GRAMMAR_FILE_TYPES , <EOL> defaultextension = '<STR_LIT>' ) <EOL> if not filename : return <EOL> try : <EOL> if filename . endswith ( '<STR_LIT>' ) : <EOL> with open ( filename , '<STR_LIT:wb>' ) as outfile : <EOL> pickle . dump ( ( self . _chart , self . _tokens ) , outfile ) <EOL> else : <EOL> with open ( filename , '<STR_LIT:w>' ) as outfile : <EOL> prods = self . _grammar . productions ( ) <EOL> start = [ p for p in prods if p . lhs ( ) == self . _grammar . start ( ) ] <EOL> rest = [ p for p in prods if p . lhs ( ) != self . _grammar . start ( ) ] <EOL> for prod in start : outfile . write ( '<STR_LIT>' % prod ) <EOL> for prod in rest : outfile . write ( '<STR_LIT>' % prod ) <EOL> except Exception as e : <EOL> tkinter . messagebox . showerror ( '<STR_LIT>' , <EOL> '<STR_LIT>' % filename ) <EOL> def reset ( self , * args ) : <EOL> self . _animating = <NUM_LIT:0> <EOL> self . _reset_parser ( ) <EOL> self . _cv . update ( self . _chart ) <EOL> if self . _matrix : self . _matrix . set_chart ( self . _chart ) <EOL> if self . _matrix : self . _matrix . deselect_cell ( ) <EOL> if self . _results : self . _results . set_chart ( self . _chart ) <EOL> def edit_grammar ( self , * e ) : <EOL> CFGEditor ( self . _root , self . _grammar , self . set_grammar ) <EOL> def set_grammar ( self , grammar ) : <EOL> self . _grammar = grammar <EOL> self . _cp . set_grammar ( grammar ) <EOL> if self . _results : self . _results . set_grammar ( grammar ) <EOL> def edit_sentence ( self , * e ) : <EOL> sentence = "<STR_LIT:U+0020>" . join ( self . _tokens ) <EOL> title = '<STR_LIT>' <EOL> instr = '<STR_LIT>' <EOL> EntryDialog ( self . _root , sentence , instr , self . set_sentence , title ) <EOL> def set_sentence ( self , sentence ) : <EOL> self . _tokens = list ( sentence . split ( ) ) <EOL> self . reset ( ) <EOL> def view_matrix ( self , * e ) : <EOL> if self . _matrix is not None : self . _matrix . destroy ( ) <EOL> self . _matrix = ChartMatrixView ( self . _root , self . _chart ) <EOL> self . _matrix . add_callback ( '<STR_LIT>' , self . _select_matrix_edge ) <EOL> def view_results ( self , * e ) : <EOL> if self . _results is not None : self . _results . destroy ( ) <EOL> self . _results = ChartResultsView ( self . _root , self . _chart , <EOL> self . _grammar ) <EOL> def resize ( self ) : <EOL> self . _animating = <NUM_LIT:0> <EOL> self . set_font_size ( self . _size . get ( ) ) <EOL> def set_font_size ( self , size ) : <EOL> self . _cv . set_font_size ( size ) <EOL> self . _font . configure ( size = - abs ( size ) ) <EOL> self . _boldfont . configure ( size = - abs ( size ) ) <EOL> self . _sysfont . configure ( size = - abs ( size ) ) <EOL> def get_font_size ( self ) : <EOL> return abs ( self . _size . get ( ) ) <EOL> def apply_strategy ( self , strategy , edge_strategy = None ) : <EOL> if self . _animating : <EOL> self . _animating = <NUM_LIT:0> <EOL> return <EOL> self . _display_rule ( None ) <EOL> if self . _step . get ( ) : <EOL> selection = self . _selection <EOL> if ( selection is not None ) and ( edge_strategy is not None ) : <EOL> self . _cp . set_strategy ( [ edge_strategy ( selection ) ] ) <EOL> newedge = self . _apply_strategy ( ) <EOL> if newedge is None : <EOL> self . _cv . unmark_edge ( ) <EOL> self . _selection = None <EOL> else : <EOL> self . _cp . set_strategy ( strategy ) <EOL> self . _apply_strategy ( ) <EOL> else : <EOL> self . _cp . set_strategy ( strategy ) <EOL> if self . _animate . get ( ) : <EOL> self . _animating = <NUM_LIT:1> <EOL> self . _animate_strategy ( ) <EOL> else : <EOL> for edge in self . _cpstep : <EOL> if edge is None : break <EOL> self . _cv . update ( ) <EOL> if self . _matrix : self . _matrix . update ( ) <EOL> if self . _results : self . _results . update ( ) <EOL> def _stop_animation ( self , * e ) : <EOL> self . _animating = <NUM_LIT:0> <EOL> def _animate_strategy ( self , speed = <NUM_LIT:1> ) : <EOL> if self . _animating == <NUM_LIT:0> : return <EOL> if self . _apply_strategy ( ) is not None : <EOL> if self . _animate . get ( ) == <NUM_LIT:0> or self . _step . get ( ) == <NUM_LIT:1> : <EOL> return <EOL> if self . _animate . get ( ) == <NUM_LIT:1> : <EOL> self . _root . after ( <NUM_LIT> , self . _animate_strategy ) <EOL> elif self . _animate . get ( ) == <NUM_LIT:2> : <EOL> self . _root . after ( <NUM_LIT:1000> , self . _animate_strategy ) <EOL> else : <EOL> self . _root . after ( <NUM_LIT:20> , self . _animate_strategy ) <EOL> def _apply_strategy ( self ) : <EOL> new_edge = next ( self . _cpstep ) <EOL> if new_edge is not None : <EOL> self . _show_new_edge ( new_edge ) <EOL> return new_edge <EOL> def _display_rule ( self , rule ) : <EOL> if rule is None : <EOL> self . _rulelabel2 [ '<STR_LIT:text>' ] = '<STR_LIT>' <EOL> else : <EOL> name = str ( rule ) <EOL> self . _rulelabel2 [ '<STR_LIT:text>' ] = name <EOL> size = self . _cv . get_font_size ( ) <EOL> _TD_INIT = [ TopDownInitRule ( ) ] <EOL> _TD_PREDICT = [ TopDownPredictRule ( ) ] <EOL> _BU_RULE = [ BottomUpPredictRule ( ) ] <EOL> _BU_LC_RULE = [ BottomUpPredictCombineRule ( ) ] <EOL> _FUNDAMENTAL = [ SingleEdgeFundamentalRule ( ) ] <EOL> _TD_STRATEGY = _TD_INIT + _TD_PREDICT + _FUNDAMENTAL <EOL> _BU_STRATEGY = _BU_RULE + _FUNDAMENTAL <EOL> _BU_LC_STRATEGY = _BU_LC_RULE + _FUNDAMENTAL <EOL> def top_down_init ( self , * e ) : <EOL> self . apply_strategy ( self . _TD_INIT , None ) <EOL> def top_down_predict ( self , * e ) : <EOL> self . apply_strategy ( self . _TD_PREDICT , TopDownPredictEdgeRule ) <EOL> def bottom_up ( self , * e ) : <EOL> self . apply_strategy ( self . _BU_RULE , BottomUpEdgeRule ) <EOL> def bottom_up_leftcorner ( self , * e ) : <EOL> self . apply_strategy ( self . _BU_LC_RULE , BottomUpLeftCornerEdgeRule ) <EOL> def fundamental ( self , * e ) : <EOL> self . apply_strategy ( self . _FUNDAMENTAL , FundamentalEdgeRule ) <EOL> def bottom_up_strategy ( self , * e ) : <EOL> self . apply_strategy ( self . _BU_STRATEGY , BottomUpEdgeRule ) <EOL> def bottom_up_leftcorner_strategy ( self , * e ) : <EOL> self . apply_strategy ( self . _BU_LC_STRATEGY , BottomUpLeftCornerEdgeRule ) <EOL> def top_down_strategy ( self , * e ) : <EOL> self . apply_strategy ( self . _TD_STRATEGY , TopDownPredictEdgeRule ) <EOL> def app ( ) : <EOL> grammar = CFG . fromstring ( """<STR_LIT>""" ) <EOL> sent = '<STR_LIT>' <EOL> sent = '<STR_LIT>' <EOL> tokens = list ( sent . split ( ) ) <EOL> print ( '<STR_LIT>' ) <EOL> for rule in grammar . productions ( ) : <EOL> print ( ( '<STR_LIT:U+0020>' , repr ( rule ) + '<STR_LIT:U+002C>' ) ) <EOL> print ( '<STR_LIT:)>' ) <EOL> print ( ( '<STR_LIT>' % tokens ) ) <EOL> print ( '<STR_LIT>' ) <EOL> ChartParserApp ( grammar , tokens ) . mainloop ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> app ( ) <EOL> __all__ = [ '<STR_LIT>' ] </s>
