nanom commited on
Commit
4abd544
·
1 Parent(s): bb18842
Files changed (1) hide show
  1. app.py +21 -20
app.py CHANGED
@@ -7,19 +7,20 @@ nlp = spacy.load("en_core_web_md")
7
 
8
  # -- Main functions --
9
 
10
- def markTk(tk):
11
- noun_tags = ['NN', 'NNS', 'WP']
12
- if tk.tag_ in noun_tags:
13
- return " (Plural)"
14
- return ""
15
-
16
  def postag(tk):
17
- return f" ({tk.tag_})"
 
 
 
 
 
 
18
 
19
  def genFlatDepthTree(expr):
20
  doc = nlp(expr)
21
  root = next(doc.sents).root
22
- node = Node(root.text+": 0 (Root)"+markTk(root), parent=None)
23
 
24
  def tree(tk, last_node, depth):
25
  if tk.n_lefts + tk.n_rights > 0:
@@ -36,22 +37,22 @@ def genFlatDepthTree(expr):
36
 
37
  return "<center><div style='max-width: 800px; overflow-x:auto;'>"+img_tree+"</div></center>", flat_tree
38
 
39
- def syntacticTree(expr):
40
- doc = nlp(expr)
41
- depths = {}
42
 
43
- def walk_tree(tk, depth):
44
- depths[tk.text] = depth
45
- if tk.n_lefts + tk.n_rights > 0: # if Doesn't root
46
- [walk_tree(child, depth + 1) for child in tk.children]
47
 
48
- [walk_tree(sent.root, 0) for sent in doc.sents]
49
 
50
- flat_tree = [tk.text+" - "+str(depths[tk.text]) for tk in doc]
51
- image_tree = displacy.render(doc, style='dep', options={'distance': 100})
52
- image_tree = "<center><div style='max-width: 800px; overflow-x:auto;'>"+image_tree+"</div></center>"
53
 
54
- return image_tree,flat_tree
55
 
56
  # -- Interface --
57
  demo = gr.Blocks(css=".container { max-width: 900px; margin: auto;}", title="Syntactic tree generator")
 
7
 
8
  # -- Main functions --
9
 
10
+
 
 
 
 
 
11
  def postag(tk):
12
+ tag = ""
13
+ plural_tags = ["NNS", "NNPS"]
14
+ if tk.tag_ in plural_tags:
15
+ tag = " (Plural)"
16
+ else:
17
+ tag = " ({})".format(tk.tag_)
18
+ return tag
19
 
20
  def genFlatDepthTree(expr):
21
  doc = nlp(expr)
22
  root = next(doc.sents).root
23
+ node = Node(root.text+": 0 (Root)"+postag(root), parent=None)
24
 
25
  def tree(tk, last_node, depth):
26
  if tk.n_lefts + tk.n_rights > 0:
 
37
 
38
  return "<center><div style='max-width: 800px; overflow-x:auto;'>"+img_tree+"</div></center>", flat_tree
39
 
40
+ # def syntacticTree(expr):
41
+ # doc = nlp(expr)
42
+ # depths = {}
43
 
44
+ # def walk_tree(tk, depth):
45
+ # depths[tk.text] = depth
46
+ # if tk.n_lefts + tk.n_rights > 0: # if Doesn't root
47
+ # [walk_tree(child, depth + 1) for child in tk.children]
48
 
49
+ # [walk_tree(sent.root, 0) for sent in doc.sents]
50
 
51
+ # flat_tree = [tk.text+" - "+str(depths[tk.text]) for tk in doc]
52
+ # image_tree = displacy.render(doc, style='dep', options={'distance': 100})
53
+ # image_tree = "<center><div style='max-width: 800px; overflow-x:auto;'>"+image_tree+"</div></center>"
54
 
55
+ # return image_tree,flat_tree
56
 
57
  # -- Interface --
58
  demo = gr.Blocks(css=".container { max-width: 900px; margin: auto;}", title="Syntactic tree generator")