Update app.py
Browse files
app.py
CHANGED
|
@@ -1,59 +1,59 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
|
| 3 |
-
from
|
| 4 |
-
from
|
| 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 |
-
st.
|
| 51 |
-
sentence = st.text_input('Enter a sentence:', '')
|
| 52 |
|
| 53 |
if sentence:
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
st.
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import spacy
|
| 3 |
+
from spacy import displacy
|
| 4 |
+
from anytree import Node, RenderTree
|
| 5 |
+
from anytree.exporter import DotExporter
|
| 6 |
+
import subprocess
|
| 7 |
+
from typing import Tuple
|
| 8 |
+
import os
|
| 9 |
+
import base64
|
| 10 |
+
|
| 11 |
+
# Initialize spaCy
|
| 12 |
+
nlp = spacy.load("en_core_web_md")
|
| 13 |
+
|
| 14 |
+
def postag(tk) -> str:
|
| 15 |
+
tag = ""
|
| 16 |
+
plural_tags = ["NNS", "NNPS"]
|
| 17 |
+
if tk.tag_ in plural_tags:
|
| 18 |
+
tag = f" ({tk.tag_}) (Plural)"
|
| 19 |
+
else:
|
| 20 |
+
tag = f" ({tk.tag_})"
|
| 21 |
+
return tag
|
| 22 |
+
|
| 23 |
+
def gen_syntactic_tree(expr: str) -> Tuple[str, str]:
|
| 24 |
+
doc = nlp(expr)
|
| 25 |
+
root = next(doc.sents).root
|
| 26 |
+
node = Node("" + root.text + ": (Root)" + postag(root), parent=None)
|
| 27 |
+
|
| 28 |
+
def tree(tk, last_node: Node, depth: int) -> None:
|
| 29 |
+
if tk.n_lefts + tk.n_rights > 0:
|
| 30 |
+
for child in tk.children:
|
| 31 |
+
tree(child, Node("" + child.text + ": " + str(depth + 1) + postag(child), parent=last_node), depth + 1)
|
| 32 |
+
|
| 33 |
+
tree(root, node, 0)
|
| 34 |
+
syntactic_str_tree = ""
|
| 35 |
+
|
| 36 |
+
for pre, fill, node in RenderTree(node):
|
| 37 |
+
syntactic_str_tree += f"{pre}{node.name}\n"
|
| 38 |
+
|
| 39 |
+
syntactic_tree = displacy.render(doc, style='dep', options={'distance': 100})
|
| 40 |
+
syntactic_html_tree = f"""
|
| 41 |
+
<center>
|
| 42 |
+
<div style='max-width: 800px; overflow-x:auto;'>
|
| 43 |
+
{syntactic_tree}
|
| 44 |
+
</div>
|
| 45 |
+
</center>
|
| 46 |
+
"""
|
| 47 |
+
return syntactic_html_tree, syntactic_str_tree
|
| 48 |
+
|
| 49 |
+
st.title('Syntactic Tree Generator')
|
| 50 |
+
sentence = st.text_area("Enter a sentence:")
|
|
|
|
| 51 |
|
| 52 |
if sentence:
|
| 53 |
+
error, syntactic_html_tree, syntactic_str_tree = "", "", ""
|
| 54 |
+
if sentence.strip() == "":
|
| 55 |
+
st.warning("The sentence cannot be empty!")
|
| 56 |
+
else:
|
| 57 |
+
syntactic_html_tree, syntactic_str_tree = gen_syntactic_tree(sentence)
|
| 58 |
+
st.markdown(syntactic_html_tree, unsafe_allow_html=True)
|
| 59 |
+
st.text(syntactic_str_tree)
|