Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import streamlit as st
|
| 3 |
+
from nltk import pos_tag, word_tokenize
|
| 4 |
+
from nltk.tree import Tree
|
| 5 |
+
from nltk.chunk import conlltags2tree, tree2conlltags
|
| 6 |
+
from nltk.chunk.regexp import RegexpParser
|
| 7 |
+
import nltk
|
| 8 |
+
nltk.download('punkt')
|
| 9 |
+
|
| 10 |
+
# A simple function to create a chunked tree.
|
| 11 |
+
def create_syntax_tree(sentence):
|
| 12 |
+
tokens = word_tokenize(sentence)
|
| 13 |
+
tagged = pos_tag(tokens)
|
| 14 |
+
# Define your grammar or use a parser here. This is a simple grammar as an example.
|
| 15 |
+
grammar = "NP: {<DT>?<JJ>*<NN>}"
|
| 16 |
+
cp = RegexpParser(grammar)
|
| 17 |
+
chunked = cp.parse(tagged)
|
| 18 |
+
|
| 19 |
+
# Convert the chunked structure to a tree
|
| 20 |
+
iob_tagged = tree2conlltags(chunked)
|
| 21 |
+
tree = conlltags2tree(iob_tagged)
|
| 22 |
+
return tree
|
| 23 |
+
|
| 24 |
+
# Streamlit app
|
| 25 |
+
st.title('Syntax Tree Generator')
|
| 26 |
+
|
| 27 |
+
# User input
|
| 28 |
+
sentence = st.text_input('Enter a sentence:', '')
|
| 29 |
+
|
| 30 |
+
if sentence:
|
| 31 |
+
tree = create_syntax_tree(sentence)
|
| 32 |
+
tree.pretty_print() # This prints the tree to the console. See note below for displaying within Streamlit.
|