示例#1
0
    def _init_canvas(self, parent):
        self._cframe = CanvasFrame(parent,
                                   background='white',
                                   width=525,
                                   closeenough=10,
                                   border=2,
                                   relief='sunken')
        self._cframe.pack(expand=1, fill='both', side='top', pady=2)
        canvas = self._canvas = self._cframe.canvas()

        self._stackwidgets = []
        self._rtextwidgets = []
        self._titlebar = canvas.create_rectangle(0,
                                                 0,
                                                 0,
                                                 0,
                                                 fill='#c0f0f0',
                                                 outline='black')
        self._exprline = canvas.create_line(0, 0, 0, 0, dash='.')
        self._stacktop = canvas.create_line(0, 0, 0, 0, fill='#408080')
        size = self._size.get() + 4
        self._stacklabel = TextWidget(canvas,
                                      'Stack',
                                      color='#004040',
                                      font=self._boldfont)
        self._rtextlabel = TextWidget(canvas,
                                      'Remaining Text',
                                      color='#004040',
                                      font=self._boldfont)
        self._cframe.add_widget(self._stacklabel)
        self._cframe.add_widget(self._rtextlabel)
def jupyter_draw_nltk_tree(tree: Tree = None,
                           directory: str = '/tmp',
                           f_name: str = 'tmp',
                           show_tree: bool = False):
    f_name = Path(directory) / f_name
    f_name.parent.mkdir(exist_ok=True, parents=True)

    cf = CanvasFrame()
    tc = TreeWidget(cf.canvas(), tree)
    tc['node_font'] = 'arial 13 bold'
    tc['leaf_font'] = 'arial 14'
    tc['node_color'] = '#005990'
    tc['leaf_color'] = '#3F8F57'
    tc['line_color'] = '#175252'

    cf.add_widget(tc, 20, 20)
    ps_file_name = f_name.with_suffix('.ps').as_posix()
    cf.print_to_file(ps_file_name)
    cf.destroy()

    png_file_name = f_name.with_suffix(".png").as_posix()
    system(f'convert {ps_file_name} {png_file_name}')

    if show_tree:
        display((Image(filename=png_file_name), ))

    system(f'rm {f_name}.ps')

    return png_file_name
示例#3
0
def quicktree(sentence):
    """Parse a sentence and return a visual representation in IPython"""
    import os
    from nltk import Tree
    from nltk.draw.util import CanvasFrame
    from nltk.draw import TreeWidget
    from stat_parser import Parser
    try:
        from IPython.display import display
        from IPython.display import Image
    except:
        pass
    try:
        get_ipython().getoutput()
    except TypeError:
        have_ipython = True
    except NameError:
        import subprocess
        have_ipython = False
    parser = Parser()
    parsed = parser.parse(sentence)
    cf = CanvasFrame()
    tc = TreeWidget(cf.canvas(), parsed)
    cf.add_widget(tc, 10, 10)  # (10,10) offsets
    cf.print_to_file('tree.ps')
    cf.destroy()
    if have_ipython:
        tregex_command = 'convert tree.ps tree.png'
        result = get_ipython().getoutput(tregex_command)
    else:
        tregex_command = ["convert", "tree.ps", "tree.png"]
        result = subprocess.check_output(tregex_command)
    os.remove("tree.ps")
    return Image(filename='tree.png')
    os.remove("tree.png")
示例#4
0
def save_tree(tree, d, name, postscript=False, **kwargs):
    drawable_tree = tree if d is None else get_drawable_tree(tree[0], d)
    extension = 'ps' if postscript else 'png'
    cf = CanvasFrame()
    tc = TreeWidget(cf.canvas(), drawable_tree)
    tc['node_font'] = 'arial 22 bold'
    tc['leaf_font'] = 'arial 22'
    tc['node_color'] = '#005990'
    tc['leaf_color'] = '#3F8F57'
    tc['line_color'] = '#175252'
    tc['xspace'] = 20
    tc['yspace'] = 20
    curr_y = 40 * len(kwargs)
    cf.add_widget(tc, 0, curr_y)
    cf.print_to_file('{0}.{1}'.format(name, extension))
    if not postscript:
        im1 = Image.open('{0}.{1}'.format(name, extension))
        im1 = im1.convert('RGB')
        curr_y = 10
        for key in kwargs:
            font = ImageFont.truetype("/fonts/Ubuntu-L.ttf", 24)
            draw = ImageDraw.Draw(im1)
            val = kwargs[key]
            format_str = '{0}'
            if isinstance(val, float):
                format_str = format_str + '={1:.4f}'
            else:
                format_str = format_str + '=  {1}'
            draw.text((10, curr_y), format_str.format(key, val), (0, 0, 0),
                      font)
            curr_y = curr_y + 40
        im1.save('{}.pdf'.format(name))
        cf.destroy()
        os.remove('{0}.{1}'.format(name, extension))
    return '{0}.{1}'.format(name, 'ps' if postscript else 'pdf')
示例#5
0
def draw_tree_to_file(tree, path):
    cf = CanvasFrame()
    tc = TreeWidget(cf.canvas(), tree)
    x, y = 0, 0
    cf.add_widget(tc, x, y)
    cf.print_to_file(path)
    cf.destroy()
示例#6
0
    def _init_canvas(self, parent):
        self._cframe = CanvasFrame(
            parent,
            background="white",
            width=525,
            closeenough=10,
            border=2,
            relief="sunken",
        )
        self._cframe.pack(expand=1, fill="both", side="top", pady=2)
        canvas = self._canvas = self._cframe.canvas()

        self._stackwidgets = []
        self._rtextwidgets = []
        self._titlebar = canvas.create_rectangle(
            0, 0, 0, 0, fill="#c0f0f0", outline="black"
        )
        self._exprline = canvas.create_line(0, 0, 0, 0, dash=".")
        self._stacktop = canvas.create_line(0, 0, 0, 0, fill="#408080")
        size = self._size.get() + 4
        self._stacklabel = TextWidget(
            canvas, "Stack", color="#004040", font=self._boldfont
        )
        self._rtextlabel = TextWidget(
            canvas, "Remaining Text", color="#004040", font=self._boldfont
        )
        self._cframe.add_widget(self._stacklabel)
        self._cframe.add_widget(self._rtextlabel)
示例#7
0
    def add_trees_list(self, frame, row_ind=0, col_ind=0):
        def show_tree(tree, prob, cf):
            cf.canvas().delete('all')
            tc = TreeWidget(cf.canvas(),
                            tree,
                            xspace=50,
                            yspace=50,
                            line_width=2,
                            node_font=('helvetica', -28))
            cf.add_widget(tc, 400, 0)
            tp = TreeWidget(cf.canvas(), 'p={}'.format(prob))
            cf.add_widget(tp, 200, 200)

        canvasFrameSuper = Frame(frame)
        canvasFrameSuper.grid(row=1, column=1)
        cf = CanvasFrame(canvasFrameSuper)
        cf.canvas()['width'] = 1000
        cf.canvas()['height'] = 400
        cf.pack()
        tree_frame = Frame(frame)
        tree_frame.grid(column=col_ind, row=row_ind, rowspan=2)
        lst = Checklist([], [],
                        tree_frame,
                        command=lambda a: show_tree(*lst.elements[a], cf))
        lst.grid(row=1, column=0, rowspan=1, columnspan=2)
        save_trees_button = Button(
            tree_frame,
            text='Save trees',
            command=lambda: self.save_trees(lst, save_trees_button))
        save_trees_button.grid(row=2, column=0)
        load_trees_button = Button(tree_frame,
                                   text='Load trees',
                                   command=lambda: self.load_trees(lst))
        load_trees_button.grid(row=2, column=1)
        return lst
示例#8
0
    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')
示例#9
0
文件: nltk.py 项目: code-gen/show_ast
    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")
示例#10
0
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')
示例#11
0
    def __init__(self, *trees):
        from math import sqrt, ceil

        self._trees = trees

        self._top = Tk()
        self._top.title('NLTK')
        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)

        # Size is variable.
        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 = int(ceil(sqrt(len(trees))))
        self._widgets = []
        for i in range(len(trees)):
            widget = TreeWidget(cf.canvas(), trees[i], 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()
示例#12
0
def format(sentence):
    filename = 'stanford-parser.jar'
    command = ['locate', filename]
    output = subprocess.Popen(command, stdout=subprocess.PIPE).communicate()[0]
    path_to_jar = output.decode().strip()

    filename = 'models.jar'
    command = ['locate', filename]
    output = subprocess.Popen(
        command, stdout=subprocess.PIPE).communicate()[0].decode().strip()
    output = output.split('\n')
    for op in output:
        if 'parse' in op:
            path_to_models_jar = op

    dependency_parser = StanfordDependencyParser(
        path_to_jar=path_to_jar, path_to_models_jar=path_to_models_jar)
    tokens = word_tokenize(sentence)
    result = dependency_parser.raw_parse(sentence)

    for dep in result:
        # print(dep.tree())
        cf = CanvasFrame()
        t = dep.tree()
        tc = TreeWidget(cf.canvas(), t)
        cf.add_widget(tc, 10, 10)  # (10,10) offsets
        cf.print_to_file('tree.ps')
        cf.destroy()
        return (dep, tokens)
示例#13
0
def draw_to_file(tree):
    canvas = CanvasFrame()
    tree_canvas = TreeWidget(canvas.canvas(), tree)
    canvas.add_widget(tree_canvas, 10, 10)

    file_name = 'tree_plot.ps'

    canvas.print_to_file(file_name)
    canvas.destroy()
示例#14
0
文件: tree.py 项目: steve3p0/LING511
    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
示例#15
0
def drawrst(strtree, fname):
    """ Draw RST tree into a file
    """
    if not fname.endswith(".ps"):
        fname += ".ps"
    cf = CanvasFrame()
    t = Tree.fromstring(strtree)
    tc = TreeWidget(cf.canvas(), t)
    cf.add_widget(tc,10,10) # (10,10) offsets
    cf.print_to_file(fname)
    cf.destroy()
def draw_nltk_tree(tree):
    cf = CanvasFrame()
    tc = TreeWidget(cf.canvas(), tree)
    tc['node_font'] = 'arial 15 bold'
    tc['leaf_font'] = 'arial 15'
    tc['node_color'] = '#005990'
    tc['leaf_color'] = '#3F8F57'
    tc['line_color'] = '#175252'
    cf.add_widget(tc, 50, 50)
    cf.print_to_file('tmp_tree_output.ps')
    cf.destroy()
示例#17
0
    def _init_canvas(self, parent):
        self._cframe = CanvasFrame(parent, background='white',
                                   #width=525, height=250,
                                   closeenough=10,
                                   border=2, relief='sunken')
        self._cframe.pack(expand=1, fill='both', side='top', pady=2)
        canvas = self._canvas = self._cframe.canvas()

        # Initially, there's no tree or text
        self._tree = None
        self._textwidgets = []
        self._textline = None
示例#18
0
 def draw_rst(self, fname):
     """ Draw RST tree into a file
     """
     tree_str = self.get_parse()
     if not fname.endswith(".ps"):
         fname += ".ps"
     cf = CanvasFrame()
     t = Tree.fromstring(tree_str)
     tc = TreeWidget(cf.canvas(), t)
     cf.add_widget(tc, 10, 10)  # (10,10) offsets
     cf.print_to_file(fname)
     cf.destroy()
示例#19
0
def save_tree_with_ps(str_tree, save_path):
	# 输出可视化结果
	cf = CanvasFrame()
	tc = TreeWidget(cf.canvas(), str_tree)
	tc['node_font'] = 'arial 14 bold'
	tc['leaf_font'] = 'arial 14'
	tc['node_color'] = '#005990'
	tc['leaf_color'] = '#3F8F57'
	tc['line_color'] = '#175252'
	cf.add_widget(tc, 10, 10) # (10,10) offsets
	cf.print_to_file('{}.ps'.format(save_path))
	cf.destroy()
示例#20
0
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 draw_nltk_tree(tree):
     cf = CanvasFrame()
     tc = TreeWidget(cf.canvas(), tree)
     tc['node_font'] = 'arial 15 bold'
     tc['leaf_font'] = 'arial 15'
     tc['node_color'] = '#005990'
     tc['leaf_color'] = '#3F8F57'
     tc['line_color'] = '#175252'
     cf.add_widget(tc, 50, 50)
     cf.print_to_file('tmp_tree_output.ps')
     cf.destroy()
     os.system('convert tmp_tree_output.ps tmp_tree_output.png')
     display(Image(filename='tmp_tree_output.png'))
示例#22
0
def drawTreeAndSaveImage(s, i, dir):
    parsed_sent = parser.raw_parse(s)
    for line in parsed_sent:
        cf = CanvasFrame()
        t = Tree.fromstring(str(line))
        tc = TreeWidget(cf.canvas(), t)
        cf.add_widget(tc, 10, 10)
        i += 1
        cf.print_to_file(dir + str(i) + '.ps')
        tree_name = dir + str(i) + '.ps'
        tree_new_name = dir + str(i) + '.png'
        os.system('convert ' + tree_name + ' ' + tree_new_name)
        cf.destroy()
def quicktree(sentence):
    """Parse a sentence and return a visual representation"""
    from nltk import Tree
    from nltk.draw.util import CanvasFrame
    from nltk.draw import TreeWidget
    from stat_parser import Parser
    from IPython.display import display
    from IPython.display import Image
    parser = Parser()
    parsed = parser.parse(sentence)
    cf = CanvasFrame()
    tc = TreeWidget(cf.canvas(), parsed)
    cf.add_widget(tc, 10, 10)  # (10,10) offsets
    cf.print_to_file('tree.ps')
    cf.destroy()
示例#24
0
def save_tree(str_tree, save_path):
	# 输出可视化结果
	cf = CanvasFrame()
	tc = TreeWidget(cf.canvas(), str_tree)
	tc['node_font'] = 'arial 14 bold'
	tc['leaf_font'] = 'arial 14'
	tc['node_color'] = '#005990'
	tc['leaf_color'] = '#3F8F57'
	tc['line_color'] = '#175252'
	cf.add_widget(tc, 10, 10) # (10,10) offsets
	cf.print_to_file('tmp.ps')
	cf.destroy()

	# 使用ImageMagick工具进行转换
	lake.shell.run('convert tmp.ps %s' % save_path)
	lake.shell.run('rm tmp.ps')
示例#25
0
    def __init__(self, gold_treebank: str, parsed_treebank: str):
        with open(gold_treebank) as gold:
            self.GOLD = gold.readlines()
        with open(parsed_treebank) as parsed:
            self.PARSED = parsed.readlines()

        self.GOLD = [x.strip() for x in self.GOLD]
        self.PARSED = [x.strip() for x in self.PARSED]
        self.IDX = 0

        self.MASTER = Tk()
        self.MASTER.bind('<Left>', self.prev)
        self.MASTER.bind('<Right>', self.next)
        self.CANVAS = CanvasFrame(self.MASTER, width=1200, height=600)
        self.SCORE = Label(self.MASTER, text='')
        self.GOLD_tc = None
示例#26
0
def main(args):
    """
    Subcommand main.

    You shouldn't need to call this yourself if you're using
    `config_argparser`
    """
    corpus = read_corpus(args)
    odir = get_output_dir(args)
    for key in corpus:
        cframe = CanvasFrame()
        widget = TreeWidget(cframe.canvas(), corpus[key])
        cframe.add_widget(widget, 10, 10)
        ofilename = fp.join(odir, key.doc) + '.ps'
        cframe.print_to_file(ofilename)
        cframe.destroy()
    announce_output_dir(odir)
def draw_nltk_tree(tree):
    # Borrowed from
    # https://stackoverflow.com/questions/31779707/how-do-you-make-nltk-draw-trees-that-are-inline-in-ipython-jupyter

    cf = CanvasFrame()
    tc = TreeWidget(cf.canvas(), tree)
    tc['node_font'] = 'arial 15 bold'
    tc['leaf_font'] = 'arial 15'
    tc['node_color'] = '#005990'
    tc['leaf_color'] = '#3F8F57'
    tc['line_color'] = '#175252'
    cf.add_widget(tc, 50, 50)
    cf.print_to_file('tmp_tree_output.ps')
    cf.destroy()
    os.system('convert tmp_tree_output.ps tmp_tree_output.png')
    display(Image(filename='tmp_tree_output.png'))
    os.system('rm tmp_tree_output.ps tmp_tree_output.png')
示例#28
0
def format(sentence, jar_location):
    path_to_jar = jar_location + '/stanford-parser.jar'
    path_to_models_jar = jar_location + '/stanford-parser-3.9.2-models.jar'

    dependency_parser = StanfordDependencyParser(
        path_to_jar=path_to_jar, path_to_models_jar=path_to_models_jar)
    tokens = word_tokenize(sentence)
    result = dependency_parser.raw_parse(sentence)

    for dep in result:
        # print(dep.tree())
        cf = CanvasFrame()
        t = dep.tree()
        tc = TreeWidget(cf.canvas(), t)
        cf.add_widget(tc, 10, 10)
        cf.print_to_file('tree.ps')
        cf.destroy()
        return (dep, tokens)
示例#29
0
def draw_trees(trees, name, reverse_dict=None, print_prob=False):
    for ind, tree in enumerate(trees):
        if reverse_dict:
            change_leaves(tree[0], reverse_dict)
        in_row = 1
        len_one = 140
        height = 200
        i = 0
        cf = CanvasFrame()
        tc = TreeWidget(cf.canvas(), tree[0])
        x, y = (i % in_row) * len_one, int(i / in_row) * height
        cf.add_widget(tc, x, y)
        if print_prob:
            tp = TreeWidget(cf.canvas(), tree[1])
            cf.add_widget(tp, x + len_one, y)
        i = i + 1
        cf.print_to_file('{0}{1}.ps'.format(name, ind))
        cf.destroy()
示例#30
0
    def __init__(self, trees, root=None, make_node=None):
        if root is None:
            window = Tkinter.Tk()
        else:
            window = Tkinter.Toplevel(root)

        window.title("Semantic Tree")
        window.geometry("800x600")

        self.cf = CanvasFrame(window)
        self.cf.pack(side='top', expand=1, fill='both')

        buttons = Tkinter.Frame(window)
        buttons.pack(side='bottom', fill='x')

        self.prev = Tkinter.Button(buttons, text="Prev", command=self.prev)
        self.prev.pack(side='left')
        self.next = Tkinter.Button(buttons, text="Next", command=self.next)
        self.next.pack(side='left')
        self.printps = Tkinter.Button(buttons,
                                      text="Print to Postscript",
                                      command=self.cf.print_to_file)
        self.printps.pack(side='left')
        self.done = Tkinter.Button(buttons,
                                   text="Done",
                                   command=window.destroy)
        self.done.pack(side='left')
        self.label = Tkinter.Label(buttons, text="Step 1 of %d" % len(trees))
        self.label.pack(side='right')
        self.prev.configure(state='disabled')

        if make_node:
            self.make_node = make_node

        if not len(trees):
            raise "No trees available to draw"

        self.trees = trees
        self.tree = self.trees[0]
        self.pos = 0
        self.showTree()

        if root is None:
            Tkinter.mainloop()