def __init__(self, canvas, t, fs, show, parent, make_node=TextWidget, make_leaf=TextWidget, **attribs): self._all_fs = fs self._show_fs = show self._attr = {} for (i, j) in attribs.items(): if i in ['keep', 'remove', 'highlight', 'reg']: self._attr[i] = j del attribs[i] TreeWidget.__init__(self, canvas, t, make_node, make_leaf, **attribs) self._top = parent
def redraw(self): for w in self._widgets: self._cframe.destroy_widget(w) del self._widgets[:] bold = ('helvetica', -self._size.get(), 'bold') helv = ('helvetica', -self._size.get()) widget = TreeWidget(self._cframe.canvas(), BP_tree_to_nltk_tree(self.simulation.tree.root), node_font=bold, leaf_color='#008040', node_color='#004080', roof_color='#004040', roof_fill='white', line_color='#004040', draggable=1, leaf_font=helv) widget.bind_click_trees(widget.toggle_collapsed) self._widgets.append(widget) self._cframe.add_widget(widget) self._layout() self.display_label()
def __init__(self, tree, settings): self._trees = (tree, ) self._top = Tk() cf = self._cframe = CanvasFrame(self._top) # Size is variable. self._size = IntVar(self._top) self._size.set(12) bold = (settings['font'], -int(12 * settings['scale']), 'bold') norm = (settings['font'], -int(12 * settings['scale'])) self._width = 1 self._widgets = [] widget = TreeWidget( cf.canvas(), tree, node_font=bold, leaf_color=settings['terminal_color'], node_color=settings['nonterminal_color'], roof_color='#004040', roof_fill='white', line_color='#004040', leaf_font=norm, ) widget['xspace'] = int(settings['scale'] * widget['xspace']) widget['yspace'] = int(settings['scale'] * widget['yspace']) self._widgets.append(widget) cf.add_widget(widget, 0, 0) self._layout() self._cframe.pack(expand=1, fill='both', side='left')
def __init__(self, tree, settings): self._trees = (tree,) self._top = Tk() cf = self._cframe = CanvasFrame(self._top) # Size is variable. self._size = IntVar(self._top) self._size.set(12) bold = (settings["font"], -int(12 * settings["scale"]), "bold") norm = (settings["font"], -int(12 * settings["scale"])) self._width = 1 self._widgets = [] widget = TreeWidget( cf.canvas(), tree, node_font=bold, leaf_color=settings["terminal_color"], node_color=settings["nonterminal_color"], roof_color="#004040", roof_fill="white", line_color="#004040", leaf_font=norm, ) widget["xspace"] = int(settings["scale"] * widget["xspace"]) widget["yspace"] = int(settings["scale"] * widget["yspace"]) self._widgets.append(widget) cf.add_widget(widget, 0, 0) self._layout() self._cframe.pack(expand=1, fill="both", side="left")
def save_tree(tree): cf = CanvasFrame() tc = TreeWidget(cf.canvas(), tree) cf.add_widget(tc, 10, 10) # (10,10) offsets cf.print_to_file('tree.ps') cf.destroy() os.system('convert tree.ps output.png')
def __init__(self, sentence): self._tree = self.generate_tree(sentence) self._top = Tk() self._top.title(sentence) self._top.geometry('756x512') self._top.bind('<Control-x>', self.destroy) self._top.bind('<Control-q>', self.destroy) cf = self._cframe = CanvasFrame(self._top) self._top.bind('<Control-p>', self._cframe.print_to_file) self._size = IntVar(self._top) self._size.set(12) bold = ('helvetica', -self._size.get(), 'bold') helv = ('helvetica', -self._size.get()) # Lay the trees out in a square. self._width = 1 self._widgets = [] widget = TreeWidget( cf.canvas(), self._tree, node_font=bold, leaf_color='#008040', node_color='#004080', roof_color='#004040', roof_fill='white', line_color='#004040', draggable=1, leaf_font=helv, ) widget.bind_click_trees(widget.toggle_collapsed) self._widgets.append(widget) cf.add_widget(widget, 0, 0) self._layout() self._cframe.pack(expand=1, fill='both') self._init_menubar()
def submitButton(*s): global ts, prev if E1.get() is "": return if ts.hidden(): ts.show() cf.remove_widget(ts) ts.destroy() tree = next(parser.raw_parse(E1.get())) ts = TreeWidget(cf.canvas(), tree, draggable=1, shapeable=1, node_font=('helvetica', -18, 'bold'), leaf_font=('helvetica', -14, 'italic'), roof_fill='white', roof_color='black', leaf_color='green4', node_color='blue2') prev = ts._nodes[0] ts.bind_click_trees(treeClicked) cf.add_widget(ts) possible.clear() load_nouns(ts._treeseg, possible) print("Possible variants: ") for p in possible: p._label['color'] = 'green3' s = [] get_subsentence(p, s) print(" - " + " ".join(s)) for p in possible: if treeClicked(p): break
def show_tree(treetok): cf = CanvasFrame(width=1000, height=450, closeenough=2) tc = TreeWidget(cf.canvas(), treetok, draggable=1, node_font=('helvetica', -14, 'bold'), leaf_font=('helvetica', -12, 'italic'), roof_fill='white', roof_color='black', leaf_color='green4', node_color='blue2') cf.add_widget(tc, 10, 10) cf.mainloop()
def create_tree_ps(parse_tree): cf = CanvasFrame(width=550, height=450, closeenough=2) t = Tree.fromstring(parse_tree) tc = TreeWidget( cf.canvas(), t, draggable=1, node_font=("helvetica", -14, "bold"), leaf_font=("helvetica", -12, "italic"), roof_fill="white", roof_color="black", leaf_color="green4", node_color="blue2", ) cf.add_widget(tc, 10, 10) cf.print_to_file(constants.QIK_DATA_DIR + "query_parse_tree.ps")
global ts if __name__ == '__main__': top = Tk() top.title("TREE!") top.geometry("800x500") parser = CoreNLP.CoreNLPParser() cf = CanvasFrame(width=400, height=400, parent=top) ts = TreeWidget( cf.canvas(), next(parser.raw_parse("What is Faculty of Electrical Engineering?")), draggable=1, node_font=('helvetica', -17, 'bold'), leaf_font=('helvetica', -14, 'italic'), roof_fill='white', roof_color='black', leaf_color='green4', node_color='blue2') E1 = Entry(top, bd=1, width=50, bg="white") E1.insert(0, "What is Faculty of Electrical Engineering?") cf.add_widget(ts, 0, 60) #Label for possible Concept Graph information ConceptText = StringVar() info = Label(top, textvariable=ConceptText, font="Courier 16 bold",
{<NOUN><IN><ADJ><NOUN>} {<ADJ><NOUN><IN><NOUN>} {<NOUN><IN><NOUN>} # 名词+介词+名词 ADV_ADJ:{<RB><ADJ>} # 副词+形容词 ADJ_PREP_NOUN:{<ADJ><IN><NOUN>} # 形容词+介词+名词 """ cp = nltk.RegexpParser(grammar) if len(sys.argv) < 2: sys.exit(0) for s in sys.argv[1:]: print('*****************************') print(s) tags = nltk.pos_tag(nltk.word_tokenize(s)) tree = nltk.chunk.ne_chunk(tags) print(str(tree)) cf = CanvasFrame() tc = TreeWidget(cf.canvas(), tree) cf.add_widget(tc, 10, 10) cf.print_to_file(s + '.1.ps') cf.destroy() tree = cp.parse(tags) print(str(tree)) cf = CanvasFrame() tc = TreeWidget(cf.canvas(), tree) cf.add_widget(tc, 10, 10) cf.print_to_file(s + '.2.ps')