示例#1
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)
示例#2
0
    def __visualize(self, gold_tree: str, parsed_tree: str):
        if self.GOLD_tc != None:
            self.CANVAS.destroy_widget(self.GOLD_tc)
            self.CANVAS.destroy_widget(self.PARSED_tc)

        GOLD = Tree.fromstring('(' + gold_tree + ')')
        PARSED = Tree.fromstring('(' + parsed_tree + ')')

        self.GOLD_tc = TreeWidget(self.CANVAS.canvas(), GOLD)
        self.PARSED_tc = TreeWidget(self.CANVAS.canvas(), PARSED)

        self.CANVAS.add_widget(self.GOLD_tc, 0, 0)
        self.CANVAS.add_widget(self.PARSED_tc, 0, self.GOLD_tc.height() + 10)
        self.CANVAS.pack(expand=True)
示例#3
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)
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
示例#5
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")
示例#6
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')
示例#7
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()
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()
示例#9
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()
示例#10
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()
示例#11
0
 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)
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()
示例#13
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()
示例#14
0
def draw_tree(tree, cf, text_box, d, prob=None):
    cf.canvas().delete('all')
    copy = get_drawable_tree(tree, d)
    tc = TreeWidget(cf.canvas(),
                    copy,
                    xspace=50,
                    yspace=50,
                    line_width=2,
                    node_font=('helvetica', -28))

    def show_description(sth):
        t = sth.__repr__()
        t = t.replace('\'', '')
        t = int(t[t.find('Text:') + len('Text: '):t.find(']')])
        text_box['text'] = d[t]

    tc.bind_click_nodes(show_description, button=1)
    cf.add_widget(tc, 400, 0)
    if prob is not None:
        tp = TreeWidget(cf.canvas(), prob)
        cf.add_widget(tp, 0, 400)
示例#15
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()
 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'))
示例#17
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()
示例#19
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')
示例#20
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')
示例#22
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)
示例#23
0
def generar_arbol(gramatica, cadena_input, funcion_split, graficar=False):

    cadena = funcion_split(cadena_input)

    arboles = gramatica_to_arbol(gramatica, cadena)

    if graficar:

        cf = CanvasFrame()

        for arbol in arboles:

            file_name = str(uuid.uuid4())

            tc = TreeWidget(cf.canvas(), arbol)

            tc['node_font'] = 'arial 10 bold'
            tc['leaf_font'] = 'arial 10'
            tc['node_color'] = '#005990'
            tc['leaf_color'] = '#3F8F57'
            tc['line_color'] = '#175252'

            cf.add_widget(tc)

            cf.canvas().update()

            # Bug si hay parentesis en los terminales de la gramatica
            t = Tree.fromstring(str(arbol))

            TreeView(t)._cframe.print_to_file('./res/{}.ps'.format(file_name))

            # No funciona en windows
            try:
                generar_png(file_name)
            except Exception as ex:
                print(ex)

        cf.mainloop()

    if arboles:
        return True
    else:
        return False
示例#24
0
def save_image(parsed, index: str) -> Binary:
    cf = CanvasFrame()
    tc = TreeWidget(cf.canvas(), parsed, xspace=40, yspace=40)
    tc['node_font'] = 'arial 20 bold'
    tc['leaf_font'] = 'arial 20 bold'
    tc['node_color'] = '#005990'
    tc['leaf_color'] = '#3F8F57'
    tc['line_color'] = '#175252'
    cf.add_widget(tc, 50, 50)

    cf.print_to_file(os.path.join(trees_path, f'tree_{index}.ps'))
    cf.destroy()

    os.system(f'convert {trees_path}/tree_{index}.ps '
              f'{trees_path}/tree_{index}.png')
    os.remove(f'{trees_path}/tree_{index}.ps')

    with open(f'{trees_path}/tree_{index}.png', 'rb') as tree_img:
        tree_bin = Binary(tree_img.read())

    return tree_bin
示例#25
0
def draw_one_tree(strtree, draw_file):
    cf = CanvasFrame()
    t = Tree.fromstring(strtree)
    tc = TreeWidget(cf.canvas(), t, draggable=1)
    cf.add_widget(tc, 1200, 0)  # (10,10) offsets
    # edus 文本
    edus_txt = ""
    c = cf.canvas()
    edu_path = RAW_TXT + "/" + draw_file.split("/")[2].split(
        ".")[0] + ".out.edu"
    with open(edu_path, "r") as f:
        for line in f:
            edus_txt += line
    edus_txt = TextWidget(c, edus_txt, draggable=1)
    cf.add_widget(edus_txt, 1400, 0)
    user_choice = input("直接打印(a) or 存到文件(b): ")
    if user_choice == "a":
        cf.mainloop()
    else:
        cf.print_to_file(draw_file)
        cf.destroy()
示例#26
0
def build_syntax_tree(txt):
    sentences = nltk.sent_tokenize(txt)
    for sent in sentences:
        tsent = pos_tag_sentence(sent, 'tree')
        ch = nltk.RegexpParser(grammar)
        tree = ch.parse(tsent)

        cf = CanvasFrame()
        tc = TreeWidget(cf.canvas(), 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)
        cf.print_to_file(path + 'tree.ps')
        cf.destroy()

        os.system('convert {0}tree.ps {0}tree.png'.format(path))

        return open(path + 'tree.png', 'rb')
示例#27
0
    def convertPsToPng(self, treeVal):
        cf = CanvasFrame()

        tr = Tree.fromstring(treeVal)
        tc = TreeWidget(cf.canvas(), tr)

        tc['node_font'] = 'arial 13 bold'
        tc['leaf_font'] = 'arial 11'
        tc['node_color'] = '#005990'
        tc['leaf_color'] = '#3F8F57'
        tc['line_color'] = '#175252'
        tc['xspace'] = 25
        tc['yspace'] = 25

        cf.add_widget(tc, 10, 10)  # (10,10) offsets

        cf.print_to_file(self.filenamePS)
        cf.destroy()

        #MagickImage doit etre installée ainsi que convert
        os.system("convert %s %s" % (self.filenamePS, self.filenamePNG))
def make_tree_png(sentence, file_number, sentence_number, outputDirectory):
    '''
    sentence: bracket notation of the sentence
    file_number: file the sentence originates from
    sentence_number: number of sentences
    outputDirectory: directory for the files (has to exist)
    Creates a png image with a syntax tree from the bracket notation of the given sentence
    '''
    filename = 'parse{}_sentence{}'.format(file_number, sentence_number)
    # make syntax tree using nltk
    cf = CanvasFrame()
    t = Tree.fromstring(sentence) # make tree from sentence
    tc = TreeWidget(cf.canvas(),t)
    cf.add_widget(tc,10,10) # (10,10) offsets
    cf.print_to_file('./{}/{}.ps'.format(outputDirectory, filename)) # print tree in a .ps file
    cf.destroy()

    # convert the ps files to usable (non-transparent) png images
    filepath = "./{}/{}".format(outputDirectory, filename)

    os.system('convert {}.ps {}.png'.format(filepath, filepath)) #convert to png
    os.system('convert -flatten {}.png {}.png'.format(filepath, filepath)) #make bg white
    os.system('del ".\{}\{}.ps"'.format(outputDirectory, filename)) #delete old .ps files
示例#29
0
def save_tree(sentence):

    name = hashlib.md5(sentence).hexdigest()

    result = loads(server.parse(sentence))

    if os.path.exists(os.path.join(path, "{0}.jpg".format(name))):
        print "File exists"

    else:
        with cd(path):
            try:

                tree = result["sentences"][0]["parsetree"]
            except Exception as e:
                print "Error %s occurred while processing the sentence %s" % (
                    e, sentence)
                return [None, None, None]

            cf = CanvasFrame()
            t = nltk.tree.Tree.fromstring(tree)
            tc = TreeWidget(cf.canvas(), t)
            cf.add_widget(tc, 10, 10)  # (10,10) offsets
            cf.print_to_file('{0}.ps'.format(name))
            subprocess.call([
                "convert", "-density", "100", "{0}.ps".format(name), "-resize",
                "100%", "-gamma", "2.2", "-quality", "92",
                "{0}.jpg".format(name)
            ])
            #subprocess.call(["convert",  "{0}.ps".format(name), "{0}.jpg".format(name)])
            cf.destroy()

    return [
        "{0}/{1}.jpg".format(path,
                             name), result["sentences"][0]["dependencies"],
        result["sentences"][0]["indexeddependencies"]
    ]
示例#30
0
    for subtree in tree:
        if type(subtree) == Tree:
            levels.extend(traverse_tree(subtree, level / 1.5))
        else:
            levels.extend([level])

    return levels


# print(len("( event : ( action : move ( token : 1 ) ) ( entity : ( color : blue ( token : 3 ) ) ( type : cube ( token : 4 ) ) ) ( destination : ( spatial-relation : ( relation : above ( token : 5 7 ) ) ( entity : ( color : blue ( token : 9 ) ) ( type : cube ( token : 10 ) ) ) ) ) )".split(' ')))
cf = CanvasFrame()
t = Tree.fromstring(
    "(sequence: (event: (action: take (token: 1)) (entity: (id: 1) (color: blue (token: 3)) (type: cube (token: 4)) (spatial-relation: (relation: above (token: 7 9)) (entity: (color: yellow (token: 10)) (type: cube (token: 11)))))) (event: (action: drop (token: 13)) (entity: (type: reference (token: 14)) (reference-id: 1)) (destination: (spatial-relation: (relation: above (token: 15 17)) (entity: (color: red (token: 18)) (type: cube (token: 19)))))))"
    .replace(':', ''))
t.pretty_print()
tc = TreeWidget(cf.canvas(), t)
cf.add_widget(tc, 10, 10)  # (10,10) offsets
cf.print_to_file('tree.ps')

# print(repr(t))
# print(recursive_len(t))
levels = traverse_tree(t, 15)

import plotly.offline as py
import plotly.figure_factory as ff

import numpy as np

dend_matrix = None
for level in levels:
    dend = np.ones([1, 47]) * level