def _selectprod_cb(self, production): canvas = self._treelet_canvas self._prodlist.highlight(production) if self._treelet is not None: self._treelet.destroy() # Convert the production to a tree. rhs = production.rhs() for (i, elt) in enumerate(rhs): if isinstance(elt, Nonterminal): elt = Tree(elt) tree = Tree(production.lhs().symbol(), *rhs) # Draw the tree in the treelet area. fontsize = int(self._size.get()) node_font = ('helvetica', -(fontsize+4), 'bold') leaf_font = ('helvetica', -(fontsize+2)) self._treelet = tree_to_treesegment(canvas, tree, node_font=node_font, leaf_font=leaf_font) self._treelet['draggable'] = 1 # Center the treelet. (x1, y1, x2, y2) = self._treelet.bbox() w, h = int(canvas['width']), int(canvas['height']) self._treelet.move((w-x1-x2)/2, (h-y1-y2)/2) # Mark the places where we can add it to the workspace. self._markproduction(production)
def _selectprod_cb(self, production): canvas = self._treelet_canvas self._prodlist.highlight(production) if self._treelet is not None: self._treelet.destroy() # Convert the production to a tree. rhs = production.rhs() for (i, elt) in enumerate(rhs): if isinstance(elt, Nonterminal): elt = Tree(elt) tree = Tree(production.lhs().symbol(), *rhs) # Draw the tree in the treelet area. fontsize = int(self._size.get()) node_font = ("helvetica", -(fontsize + 4), "bold") leaf_font = ("helvetica", -(fontsize + 2)) self._treelet = tree_to_treesegment(canvas, tree, node_font=node_font, leaf_font=leaf_font) self._treelet["draggable"] = 1 # Center the treelet. (x1, y1, x2, y2) = self._treelet.bbox() w, h = int(canvas["width"]), int(canvas["height"]) self._treelet.move((w - x1 - x2) / 2, (h - y1 - y2) / 2) # Mark the places where we can add it to the workspace. self._markproduction(production)
def write_tree_stream(self, nltk_tree): """ Draws and outputs in PNG for ipython. PNG is used instead of PDF, since it can be displayed in the qt console and has wider browser support. """ import os import base64 import subprocess import tempfile from nltk.draw.tree import tree_to_treesegment from nltk.draw.util import CanvasFrame from nltk.internals import find_binary _canvas_frame = CanvasFrame() # widget = tree_to_treesegment(_canvas_frame.canvas(), self) widget = tree_to_treesegment(_canvas_frame.canvas(), nltk_tree) _canvas_frame.add_widget(widget) x, y, w, h = widget.bbox() # print_to_file uses scrollregion to set the width and height of the pdf. _canvas_frame.canvas()["scrollregion"] = (0, 0, w, h) with tempfile.NamedTemporaryFile() as file: in_path = "{0:}.ps".format(file.name) out_path = "{0:}.png".format(file.name) _canvas_frame.print_to_file(in_path) _canvas_frame.destroy_widget(widget) try: subprocess.call([ find_binary( "gs", binary_names=["gswin32c.exe", "gswin64c.exe"], env_vars=["PATH"], verbose=False, ) ] + "-q -dEPSCrop -sDEVICE=png16m -r90 -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dSAFER -dBATCH -dNOPAUSE -sOutputFile={0:} {1:}" .format(out_path, in_path).split()) except LookupError: pre_error_message = str( "The Ghostscript executable isn't found.\n" "See http://web.mit.edu/ghostscript/www/Install.htm\n" "If you're using a Mac, you can try installing\n" "https://docs.brew.sh/Installation then `brew install ghostscript`" ) print(pre_error_message, file=sys.stderr) raise LookupError with open(out_path, "rb") as sr: res = sr.read() b = bytearray(res) # b0 = b[0] os.remove(in_path) os.remove(out_path) # return base64.b64encode(res).decode() # return base64.b64encode(res) # return b0 return b
def to_ps(self, filename): """Export as a PostScript image. This function is used by `_repr_png_`. """ _canvas_frame = CanvasFrame() # WIP customization of visual appearance # NB: conda-provided python and tk cannot access most fonts on the # system, thus it currently falls back on the default font widget = tree_to_treesegment(_canvas_frame.canvas(), self, tree_yspace=35, node_font=('Verdana', -18, 'bold'), leaf_font=('Verdana', -18)) _canvas_frame.add_widget(widget) x, y, w, h = widget.bbox() # print_to_file uses scrollregion to set the width and height of the # pdf _canvas_frame.canvas()['scrollregion'] = (0, 0, w, h) # print to file _canvas_frame.print_to_file(filename) _canvas_frame.destroy_widget(widget)
def to_ps(self, filename): """Export as a PostScript image. This function is used by `_repr_png_`. """ _canvas_frame = CanvasFrame() # WIP customization of visual appearance # NB: conda-provided python and tk cannot access most fonts on the # system, thus it currently falls back on the default font widget = tree_to_treesegment(_canvas_frame.canvas(), self, tree_yspace=35, node_font=('Verdana', -18, 'bold'), leaf_font=('Verdana', -18)) _canvas_frame.add_widget(widget) x, y, w, h = widget.bbox() # print_to_file uses scrollregion to set the width and height of the # pdf _canvas_frame.canvas()['scrollregion'] = (0, 0, w, h) # print to file _canvas_frame.print_to_file(filename) _canvas_frame.destroy_widget(widget)