本文整理汇总了Python中nltk.draw.util.CanvasFrame.pack方法的典型用法代码示例。如果您正苦于以下问题:Python CanvasFrame.pack方法的具体用法?Python CanvasFrame.pack怎么用?Python CanvasFrame.pack使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类nltk.draw.util.CanvasFrame
的用法示例。
在下文中一共展示了CanvasFrame.pack方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from nltk.draw.util import CanvasFrame [as 别名]
# 或者: from nltk.draw.util.CanvasFrame import pack [as 别名]
class TreeView:
def __init__(self, trees, root=None):
if len(trees) == 0:
print "No trees to display."
return
newroot = False
if root is None:
root = Tkinter.Tk()
window = root
newroot = True
else:
window = Tkinter.Toplevel(root)
window.title("Parse Tree")
window.geometry("600x400")
self.cf = CanvasFrame(window)
self.cf.pack(side='top', expand=1, fill='both')
buttons = Tkinter.Frame(window)
buttons.pack(side='bottom', fill='x')
self.spin = Tkinter.Spinbox(buttons, from_=1, to=len(trees),
command=self.showtree, width=3)
if len(trees) > 1: self.spin.pack(side='left')
self.label = Tkinter.Label(buttons, text="of %d" % len(trees))
if len(trees) > 1: self.label.pack(side='left')
self.done = Tkinter.Button(buttons, text="Done", command=window.destroy)
self.done.pack(side='right')
self.printps = Tkinter.Button(buttons, text="Print to Postscript", command=self.cf.print_to_file)
self.printps.pack(side='right')
self.trees = trees
self.treeWidget = None
self.showtree()
if newroot: root.mainloop()
def showtree(self):
try: n = int(self.spin.get())
except ValueError: n=1
if self.treeWidget is not None: self.cf.destroy_widget(self.treeWidget)
self.treeWidget = TreeWidget(self.cf.canvas(),
self.trees[n-1], draggable=1, shapeable=1)
self.cf.add_widget(self.treeWidget, 0, 0)
示例2: CFGDemo
# 需要导入模块: from nltk.draw.util import CanvasFrame [as 别名]
# 或者: from nltk.draw.util.CanvasFrame import pack [as 别名]
class CFGDemo(object):
def __init__(self, grammar, text):
self._grammar = grammar
self._text = text
# Set up the main window.
self._top = Tk()
self._top.title('Context Free Grammar Demo')
# Base font size
self._size = IntVar(self._top)
self._size.set(12) # = medium
# Set up the key bindings
self._init_bindings(self._top)
# Create the basic frames
frame1 = Frame(self._top)
frame1.pack(side='left', fill='y', expand=0)
self._init_menubar(self._top)
self._init_buttons(self._top)
self._init_grammar(frame1)
self._init_treelet(frame1)
self._init_workspace(self._top)
#//////////////////////////////////////////////////
# Initialization
#//////////////////////////////////////////////////
def _init_bindings(self, top):
top.bind('<Control-q>', self.destroy)
def _init_menubar(self, parent): pass
def _init_buttons(self, parent): pass
def _init_grammar(self, parent):
self._prodlist = ProductionList(parent, self._grammar, width=20)
self._prodlist.pack(side='top', fill='both', expand=1)
self._prodlist.focus()
self._prodlist.add_callback('select', self._selectprod_cb)
self._prodlist.add_callback('move', self._selectprod_cb)
def _init_treelet(self, parent):
self._treelet_canvas = Canvas(parent, background='white')
self._treelet_canvas.pack(side='bottom', fill='x')
self._treelet = None
def _init_workspace(self, parent):
self._workspace = CanvasFrame(parent, background='white')
self._workspace.pack(side='right', fill='both', expand=1)
self._tree = None
self.reset_workspace()
#//////////////////////////////////////////////////
# Workspace
#//////////////////////////////////////////////////
def reset_workspace(self):
c = self._workspace.canvas()
fontsize = int(self._size.get())
node_font = ('helvetica', -(fontsize+4), 'bold')
leaf_font = ('helvetica', -(fontsize+2))
# Remove the old tree
if self._tree is not None:
self._workspace.remove_widget(self._tree)
# The root of the tree.
start = self._grammar.start().symbol()
rootnode = TextWidget(c, start, font=node_font, draggable=1)
# The leaves of the tree.
leaves = []
for word in self._text:
leaves.append(TextWidget(c, word, font=leaf_font, draggable=1))
# Put it all together into one tree
self._tree = TreeSegmentWidget(c, rootnode, leaves,
color='white')
# Add it to the workspace.
self._workspace.add_widget(self._tree)
# Move the leaves to the bottom of the workspace.
for leaf in leaves: leaf.move(0,100)
#self._nodes = {start:1}
#self._leaves = dict([(l,1) for l in leaves])
def workspace_markprod(self, production):
pass
def _markproduction(self, prod, tree=None):
if tree is None: tree = self._tree
for i in range(len(tree.subtrees())-len(prod.rhs())):
if tree['color', i] == 'white':
self._markproduction
for j, node in enumerate(prod.rhs()):
#.........这里部分代码省略.........
示例3: RecursiveDescentApp
# 需要导入模块: from nltk.draw.util import CanvasFrame [as 别名]
# 或者: from nltk.draw.util.CanvasFrame import pack [as 别名]
class RecursiveDescentApp(object):
"""
A graphical tool for exploring the recursive descent parser. The tool
displays the parser's tree and the remaining text, and allows the
user to control the parser's operation. In particular, the user
can expand subtrees on the frontier, match tokens on the frontier
against the text, and backtrack. A "step" button simply steps
through the parsing process, performing the operations that
``RecursiveDescentParser`` would use.
"""
def __init__(self, grammar, sent, trace=0):
self._sent = sent
self._parser = SteppingRecursiveDescentParser(grammar, trace)
# Set up the main window.
self._top = Tk()
self._top.title('Recursive Descent Parser Application')
# Set up key bindings.
self._init_bindings()
# Initialize the fonts.
self._init_fonts(self._top)
# Animations. animating_lock is a lock to prevent the demo
# from performing new operations while it's animating.
self._animation_frames = IntVar(self._top)
self._animation_frames.set(5)
self._animating_lock = 0
self._autostep = 0
# The user can hide the grammar.
self._show_grammar = IntVar(self._top)
self._show_grammar.set(1)
# Create the basic frames.
self._init_menubar(self._top)
self._init_buttons(self._top)
self._init_feedback(self._top)
self._init_grammar(self._top)
self._init_canvas(self._top)
# Initialize the parser.
self._parser.initialize(self._sent)
# Resize callback
self._canvas.bind('<Configure>', self._configure)
#########################################
## Initialization Helpers
#########################################
def _init_fonts(self, root):
# See: <http://www.astro.washington.edu/owen/ROTKFolklore.html>
self._sysfont = tkFont.Font(font=Button()["font"])
root.option_add("*Font", self._sysfont)
# TWhat's our font size (default=same as sysfont)
self._size = IntVar(root)
self._size.set(self._sysfont.cget('size'))
self._boldfont = tkFont.Font(family='helvetica', weight='bold',
size=self._size.get())
self._font = tkFont.Font(family='helvetica',
size=self._size.get())
if self._size.get() < 0: big = self._size.get()-2
else: big = self._size.get()+2
self._bigfont = tkFont.Font(family='helvetica', weight='bold',
size=big)
def _init_grammar(self, parent):
# Grammar view.
self._prodframe = listframe = Frame(parent)
self._prodframe.pack(fill='both', side='left', padx=2)
self._prodlist_label = Label(self._prodframe, font=self._boldfont,
text='Available Expansions')
self._prodlist_label.pack()
self._prodlist = Listbox(self._prodframe, selectmode='single',
relief='groove', background='white',
foreground='#909090', font=self._font,
selectforeground='#004040',
selectbackground='#c0f0c0')
self._prodlist.pack(side='right', fill='both', expand=1)
self._productions = list(self._parser.grammar().productions())
for production in self._productions:
self._prodlist.insert('end', (' %s' % production))
self._prodlist.config(height=min(len(self._productions), 25))
# Add a scrollbar if there are more than 25 productions.
if len(self._productions) > 25:
listscroll = Scrollbar(self._prodframe,
orient='vertical')
self._prodlist.config(yscrollcommand = listscroll.set)
listscroll.config(command=self._prodlist.yview)
listscroll.pack(side='left', fill='y')
# If they select a production, apply it.
self._prodlist.bind('<<ListboxSelect>>', self._prodlist_select)
#.........这里部分代码省略.........
示例4: ShiftReduceApp
# 需要导入模块: from nltk.draw.util import CanvasFrame [as 别名]
# 或者: from nltk.draw.util.CanvasFrame import pack [as 别名]
class ShiftReduceApp(object):
"""
A graphical tool for exploring the shift-reduce parser. The tool
displays the parser's stack and the remaining text, and allows the
user to control the parser's operation. In particular, the user
can shift tokens onto the stack, and can perform reductions on the
top elements of the stack. A "step" button simply steps through
the parsing process, performing the operations that
``nltk.parse.ShiftReduceParser`` would use.
"""
def __init__(self, grammar, sent, trace=0):
self._sent = sent
self._parser = SteppingShiftReduceParser(grammar, trace)
# Set up the main window.
self._top = Tk()
self._top.title('Shift Reduce Parser Application')
# Animations. animating_lock is a lock to prevent the demo
# from performing new operations while it's animating.
self._animating_lock = 0
self._animate = IntVar(self._top)
self._animate.set(10) # = medium
# The user can hide the grammar.
self._show_grammar = IntVar(self._top)
self._show_grammar.set(1)
# Initialize fonts.
self._init_fonts(self._top)
# Set up key bindings.
self._init_bindings()
# Create the basic frames.
self._init_menubar(self._top)
self._init_buttons(self._top)
self._init_feedback(self._top)
self._init_grammar(self._top)
self._init_canvas(self._top)
# A popup menu for reducing.
self._reduce_menu = Menu(self._canvas, tearoff=0)
# Reset the demo, and set the feedback frame to empty.
self.reset()
self._lastoper1['text'] = ''
#########################################
## Initialization Helpers
#########################################
def _init_fonts(self, root):
# See: <http://www.astro.washington.edu/owen/ROTKFolklore.html>
self._sysfont = tkinter.font.Font(font=Button()["font"])
root.option_add("*Font", self._sysfont)
# TWhat's our font size (default=same as sysfont)
self._size = IntVar(root)
self._size.set(self._sysfont.cget('size'))
self._boldfont = tkinter.font.Font(family='helvetica', weight='bold',
size=self._size.get())
self._font = tkinter.font.Font(family='helvetica',
size=self._size.get())
def _init_grammar(self, parent):
# Grammar view.
self._prodframe = listframe = Frame(parent)
self._prodframe.pack(fill='both', side='left', padx=2)
self._prodlist_label = Label(self._prodframe,
font=self._boldfont,
text='Available Reductions')
self._prodlist_label.pack()
self._prodlist = Listbox(self._prodframe, selectmode='single',
relief='groove', background='white',
foreground='#909090',
font=self._font,
selectforeground='#004040',
selectbackground='#c0f0c0')
self._prodlist.pack(side='right', fill='both', expand=1)
self._productions = list(self._parser.grammar().productions())
for production in self._productions:
self._prodlist.insert('end', (' %s' % production))
self._prodlist.config(height=min(len(self._productions), 25))
# Add a scrollbar if there are more than 25 productions.
if 1:#len(self._productions) > 25:
listscroll = Scrollbar(self._prodframe,
orient='vertical')
self._prodlist.config(yscrollcommand = listscroll.set)
listscroll.config(command=self._prodlist.yview)
listscroll.pack(side='left', fill='y')
# If they select a production, apply it.
self._prodlist.bind('<<ListboxSelect>>', self._prodlist_select)
# When they hover over a production, highlight it.
#.........这里部分代码省略.........
示例5: DrtGlueDemo
# 需要导入模块: from nltk.draw.util import CanvasFrame [as 别名]
# 或者: from nltk.draw.util.CanvasFrame import pack [as 别名]
class DrtGlueDemo(object):
def __init__(self, examples):
# Set up the main window.
self._top = Tk()
self._top.title('DRT Glue Demo')
# Set up key bindings.
self._init_bindings()
# Initialize the fonts.self._error = None
self._init_fonts(self._top)
self._examples = examples
self._readingCache = [None for example in examples]
# The user can hide the grammar.
self._show_grammar = IntVar(self._top)
self._show_grammar.set(1)
# Set the data to None
self._curExample = -1
self._readings = []
self._drs = None
self._drsWidget = None
self._error = None
self._init_glue()
# Create the basic frames.
self._init_menubar(self._top)
self._init_buttons(self._top)
self._init_exampleListbox(self._top)
self._init_readingListbox(self._top)
self._init_canvas(self._top)
# Resize callback
self._canvas.bind('<Configure>', self._configure)
#########################################
## Initialization Helpers
#########################################
def _init_glue(self):
tagger = RegexpTagger(
[('^(David|Mary|John)$', 'NNP'),
('^(walks|sees|eats|chases|believes|gives|sleeps|chases|persuades|tries|seems|leaves)$', 'VB'),
('^(go|order|vanish|find|approach)$', 'VB'),
('^(a)$', 'ex_quant'),
('^(every)$', 'univ_quant'),
('^(sandwich|man|dog|pizza|unicorn|cat|senator)$', 'NN'),
('^(big|gray|former)$', 'JJ'),
('^(him|himself)$', 'PRP')
])
depparser = MaltParser(tagger=tagger)
self._glue = DrtGlue(depparser=depparser, remove_duplicates=False)
def _init_fonts(self, root):
# See: <http://www.astro.washington.edu/owen/ROTKFolklore.html>
self._sysfont = Font(font=Button()["font"])
root.option_add("*Font", self._sysfont)
# TWhat's our font size (default=same as sysfont)
self._size = IntVar(root)
self._size.set(self._sysfont.cget('size'))
self._boldfont = Font(family='helvetica', weight='bold',
size=self._size.get())
self._font = Font(family='helvetica',
size=self._size.get())
if self._size.get() < 0: big = self._size.get()-2
else: big = self._size.get()+2
self._bigfont = Font(family='helvetica', weight='bold',
size=big)
def _init_exampleListbox(self, parent):
self._exampleFrame = listframe = Frame(parent)
self._exampleFrame.pack(fill='both', side='left', padx=2)
self._exampleList_label = Label(self._exampleFrame, font=self._boldfont,
text='Examples')
self._exampleList_label.pack()
self._exampleList = Listbox(self._exampleFrame, selectmode='single',
relief='groove', background='white',
foreground='#909090', font=self._font,
selectforeground='#004040',
selectbackground='#c0f0c0')
self._exampleList.pack(side='right', fill='both', expand=1)
for example in self._examples:
self._exampleList.insert('end', (' %s' % example))
self._exampleList.config(height=min(len(self._examples), 25), width=40)
# Add a scrollbar if there are more than 25 examples.
if len(self._examples) > 25:
listscroll = Scrollbar(self._exampleFrame,
orient='vertical')
self._exampleList.config(yscrollcommand = listscroll.set)
listscroll.config(command=self._exampleList.yview)
listscroll.pack(side='left', fill='y')
#.........这里部分代码省略.........
示例6: TTFrame
# 需要导入模块: from nltk.draw.util import CanvasFrame [as 别名]
# 或者: from nltk.draw.util.CanvasFrame import pack [as 别名]
class TTFrame(): # Tree Transformation Frame
def __init__(self,parent=None,index=0,trees=list(),
readonly=False):
self._trees = trees
self._i_range = len(self._trees)
self._bold = ('helvetica', -12, 'bold')
self._helv = ('helvetica', -12)
if parent is None:
self._parent = Tk()
else:
self._parent = parent
t1 = 'Syntagrus - Brandeis - Tree Transformation'
self._parent.title(t1)
self._parent.columnconfigure(0,weight=1)
self._parent.rowconfigure(0,weight=1)
self._m_f = ttk.Frame(self._parent)
self._m_f.grid(column=0,row=0,sticky=(N,W,E,S))
self._m_f.columnconfigure(0,weight=1)
self._m_f.rowconfigure(1,weight=1)
self._i_f = ttk.Frame(self._m_f)
self._i_f.grid(column=0,row=0,sticky=(N,W,E,S))
self._i_f.columnconfigure(0,weight=1)
self._i_f.columnconfigure(7,weight=1)
self._o_p = ttk.Panedwindow(self._m_f,
orient=VERTICAL)
self._o_p.grid(column=0,row=1,sticky=(N,W,E,S))
t2 = 'Please enter the index (0-' + \
str(self._i_range-1) + ')'
self._i_l = ttk.Label(self._i_f,text=t2)
self._i_l.grid(column=1,row=0,columnspan=3,
sticky=(N,W,E,S))
self._i_l['anchor'] = 'center'
self._index = StringVar()
self._index.set(index)
self._i_e = ttk.Entry(self._i_f,width=5,
textvariable=self._index)
self._i_e.grid(column=1,row=1,sticky=(N,W,E,S))
if readonly:
self._i_e['state']='readonly'
self._i_b = ttk.Button(self._i_f,width=5,
text='Draw',command=self._draw)
self._i_b.grid(column=3,row=1,sticky=(N,W,E,S))
self._extra_label = ttk.Label(self._i_f,text=' ')
self._extra_label.grid(column=4,row=0,rowspan=2,
sticky=(N,W,E,S))
self._var_1 = IntVar()
self._i_cb_1 = ttk.Checkbutton(self._i_f,
text='Transformation 1',
variable=self._var_1)
self._i_cb_1.grid(column=5,row=0,sticky=(N,W,E,S))
self._var_2 = IntVar()
self._i_cb_2 = ttk.Checkbutton(self._i_f,
text='Transformation 2',
variable=self._var_2)
self._i_cb_2.grid(column=5,row=1,sticky=(N,W,E,S))
self._var_3 = IntVar()
self._i_cb_3 = ttk.Checkbutton(self._i_f,
text='Transformation 3',
variable=self._var_3)
self._i_cb_3.grid(column=6,row=0,sticky=(N,W,E,S))
self._var_4 = IntVar()
self._i_cb_4 = ttk.Checkbutton(self._i_f,
text='Transformation 4',
variable=self._var_4)
self._i_cb_4.grid(column=6,row=1,sticky=(N,W,E,S))
n = 250
self._o_f_1 = ttk.Labelframe(self._o_p,
text='Original',width=n,height=n)
self._o_f_2 = ttk.Labelframe(self._o_p,
text='Transformed',width=n,height=n)
self._o_p.add(self._o_f_1)
self._o_p.add(self._o_f_2)
self._o_cf_1 = CanvasFrame(parent=self._o_f_1,
width=n*2.5,height=n)
self._o_cf_1.pack(expand=1,fill='both')
self._o_cf_2 = CanvasFrame(parent=self._o_f_2,
width=n*2.5,height=n)
self._o_cf_2.pack(expand=1,fill='both')
self._i_e.focus()
self._parent.bind('<Return>',self._draw)
self._parent.mainloop()
#.........这里部分代码省略.........
示例7: CanvasFrame
# 需要导入模块: from nltk.draw.util import CanvasFrame [as 别名]
# 或者: from nltk.draw.util.CanvasFrame import pack [as 别名]
i_rb_p2 = ttk.Radiobutton(i_f,text=str(p_max),
variable=max_index,value=p_max)
i_rb_n2 = ttk.Radiobutton(i_f,text=str(n_max),
variable=max_index,value=n_max)
i_rb_p2.grid(column=7,row=0,sticky=(N,W,E,S))
i_rb_n2.grid(column=7,row=1,sticky=(N,W,E,S))
i_b_2 = ttk.Button(i_f,width=5,text='Tree tranformation',
command=open_window)
i_b_2.grid(column=3,row=2,columnspan=3,sticky=(N,W,E,S))
n = 300
d_f = ttk.Labelframe(o_p,text='DS',width=n,height=n)
p_f = ttk.Labelframe(o_p,text='PS',width=n,height=n)
o_p.add(d_f)
o_p.add(p_f)
d_cf = CanvasFrame(parent=d_f,width=n,height=n)
d_cf.pack(expand=1,fill='both')
p_cf = CanvasFrame(parent=p_f,width=n,height=n)
p_cf.pack(expand=1,fill='both')
i_e.focus()
i_rb_p2['state']='disabled'
i_rb_n2['state']='disabled'
root.bind('<Return>',draw)
root.mainloop()