nanom commited on
Commit
7cabef0
1 Parent(s): 8fad694

New flat depth tree

Browse files
Files changed (1) hide show
  1. app.py +26 -4
app.py CHANGED
@@ -1,10 +1,32 @@
1
  import gradio as gr
 
2
  import spacy
3
  from spacy import displacy
4
 
5
  nlp = spacy.load("en_core_web_md")
6
 
7
  # -- Main functions --
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
  def syntacticTree(expr):
9
  doc = nlp(expr)
10
  depths = {}
@@ -32,19 +54,19 @@ with demo:
32
  gr.Image(value="https://img.unocero.com/2019/11/facebook-app-para-hacer-memes-1-1024x576.jpg",label="", type="URL")
33
  with gr.Column():
34
  input_expr = gr.Textbox(label="Input", placeholder="Enter an expression here")
35
- out_flat_tree = gr.Text(label="Flat tree", value="")
36
 
37
  out_image_tree = gr.HTML(label="")
38
 
39
  gr.Examples(
40
  examples=[
41
- "glass in guys hand on right first guy",
42
- "i have been happy",
43
  "the happy spiders",
44
  "gradio is a package that allows users to create simple web apps with just a few lines of code",
45
  "the best dog out there"],
46
  inputs=input_expr,
47
- examples_per_page=20,
48
  )
49
 
50
  input_expr.change(
 
1
  import gradio as gr
2
+ from anytree import Node, RenderTree
3
  import spacy
4
  from spacy import displacy
5
 
6
  nlp = spacy.load("en_core_web_md")
7
 
8
  # -- Main functions --
9
+
10
+ def genFlatDepthTree(expr):
11
+ doc = nlp(expr)
12
+ root = next(doc.sents).root
13
+ node = Node(root.text+" : depth="+str(0), parent=None)
14
+
15
+ def tree(tk, last_node, depth):
16
+ if tk.n_lefts + tk.n_rights > 0:
17
+ for ch in tk.children:
18
+ tree(ch, Node(ch.text+" : depth="+str(depth+1), parent=last_node), depth+1)
19
+
20
+ tree(root, node, 0)
21
+ flat_tree = ""
22
+
23
+ for pre, fill, node in RenderTree(node):
24
+ flat_tree += "{}{}\n".format(pre, node.name)
25
+
26
+ img_tree = displacy.render(doc, style='dep', options={'distance': 100})
27
+
28
+ return "<center><div style='max-width: 800px; overflow-x:auto;'>"+img_tree+"</div></center>", flat_tree
29
+
30
  def syntacticTree(expr):
31
  doc = nlp(expr)
32
  depths = {}
 
54
  gr.Image(value="https://img.unocero.com/2019/11/facebook-app-para-hacer-memes-1-1024x576.jpg",label="", type="URL")
55
  with gr.Column():
56
  input_expr = gr.Textbox(label="Input", placeholder="Enter an expression here")
57
+ out_flat_tree = gr.Text(label="Flat depth tree", value="")
58
 
59
  out_image_tree = gr.HTML(label="")
60
 
61
  gr.Examples(
62
  examples=[
63
+ "glass in guys hand on right first guy",
64
+ [ "i have been happy",
65
  "the happy spiders",
66
  "gradio is a package that allows users to create simple web apps with just a few lines of code",
67
  "the best dog out there"],
68
  inputs=input_expr,
69
+ examples_per_page=4,
70
  )
71
 
72
  input_expr.change(