File size: 3,430 Bytes
e512f7f
7cabef0
e512f7f
 
 
 
 
 
7cabef0
4abd544
5f007b2
4abd544
 
 
1a1f823
4abd544
3a6ed89
4abd544
5f007b2
7cabef0
 
 
403e543
7cabef0
 
 
 
403e543
7cabef0
 
 
 
 
403e543
7cabef0
 
 
403e543
7cabef0
4abd544
 
 
e512f7f
4abd544
 
 
 
e512f7f
4abd544
e512f7f
4abd544
 
 
e512f7f
4abd544
e512f7f
 
 
 
 
2310bfe
e512f7f
 
 
 
 
403e543
e512f7f
 
 
 
18e49ea
7cabef0
702f8f4
18e49ea
ed177cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ddd94be
e512f7f
 
026f6fc
f29f0a2
e512f7f
 
 
4ae50e3
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import gradio as gr
from anytree import Node, RenderTree
import spacy
from spacy import displacy

nlp = spacy.load("en_core_web_md")

# -- Main functions --


def postag(tk):
    tag = ""
    plural_tags = ["NNS", "NNPS"]
    if tk.tag_ in plural_tags:
        tag = " ({}) (Plural)".format(tk.tag_)
    else:
        tag = " ({})".format(tk.tag_)
    return tag
    
def genFlatDepthTree(expr):
    doc = nlp(expr)
    root = next(doc.sents).root
    node = Node(""+root.text+": (Root)"+postag(root), parent=None)

    def tree(tk, last_node, depth):
        if tk.n_lefts + tk.n_rights > 0:
            for ch in tk.children:
                tree(ch, Node(""+ch.text+": "+str(depth+1)+postag(ch), parent=last_node), depth+1)
    
    tree(root, node, 0)
    flat_tree = ""

    for pre, fill, node in RenderTree(node):
        flat_tree += """{}{}\n""".format(pre, node.name)

    img_tree = displacy.render(doc, style='dep', options={'distance': 100})
    
    return "<center><div style='max-width: 800px; overflow-x:auto;'>"+img_tree+"</div></center>", flat_tree
    
# def syntacticTree(expr):
#     doc = nlp(expr)
#     depths = {}

#     def walk_tree(tk, depth):
#         depths[tk.text] = depth
#         if tk.n_lefts + tk.n_rights > 0: # if Doesn't root
#             [walk_tree(child, depth + 1) for child in tk.children]

#     [walk_tree(sent.root, 0) for sent in doc.sents]

#     flat_tree = [tk.text+" - "+str(depths[tk.text]) for tk in doc]
#     image_tree = displacy.render(doc, style='dep', options={'distance': 100})
#     image_tree = "<center><div style='max-width: 800px; overflow-x:auto;'>"+image_tree+"</div></center>"
    
#     return image_tree,flat_tree

# -- Interface --
demo = gr.Blocks(css=".container { max-width: 900px; margin: auto;}", title="Syntactic tree generator")

with demo:
    gr.Markdown("""<center> <h2>🌳 Syntactic Tree Generator 🌳</h2> </center>""")
    with gr.Row():
        with gr.Column():
            gr.Image(value="https://img.unocero.com/2019/11/facebook-app-para-hacer-memes-1-1024x576.jpg",label="", type="URL")
        with gr.Column():
            input_expr = gr.Textbox(label="Input", placeholder="Enter an expression here")
            out_flat_tree = gr.Textbox(label="Flat tree", value="")
    
    out_image_tree = gr.HTML(label="")

    gr.Examples(
        examples=[ 
            "glass in guys hand on right first guy",
            "i have been happy",
            "the happy spiders",
            "the best dog out there",
             "girl 's face",
             'bottom grass',
             'rock people are sitting on',
             'blue sky center below clouds',
             'group of people on left',
             'tree middle',
             'the lump of grass above the bright rock in the bottom left',
             'berries in middle',
             'red shirt kid',
             'middle rock',
             'grass below the person in the straw hat',
             'grass on the left',
             'wall between stairs 2nd lv on right',
             'the large group of clouds',
             'sky top left',
             'rock structure on far right',
             'left donkey'],
    inputs=input_expr,
        examples_per_page=25,
    )
    
    input_expr.change(     
        fn=genFlatDepthTree, 
        inputs=input_expr, 
        outputs=[out_image_tree, out_flat_tree])

demo.queue(concurrency_count=10)
demo.launch(debug=True)