Spaces:
Runtime error
Runtime error
update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,11 @@
|
|
|
|
1 |
import nltk
|
2 |
-
nltk.download('punkt')
|
3 |
from nltk.corpus import treebank
|
4 |
from nltk import PCFG, ViterbiParser
|
|
|
5 |
nltk.download('all')
|
6 |
# Load the treebank dataset
|
7 |
-
nltk.download('treebank')
|
8 |
corpus = treebank.parsed_sents()
|
9 |
|
10 |
# Train a PCFG parser
|
@@ -17,43 +18,24 @@ grammar = nltk.induce_pcfg(S, productions)
|
|
17 |
# Initialize the parser with the trained grammar
|
18 |
parser = ViterbiParser(grammar)
|
19 |
|
20 |
-
|
21 |
-
sentences = ["this is a beautiful"]
|
22 |
-
|
23 |
-
# Prepare gold standard parse trees for the sentences
|
24 |
-
gold_standard_trees = list(treebank.parsed_sents()[:len(sentences)])
|
25 |
-
|
26 |
-
# Initialize counters
|
27 |
-
true_positives = 0
|
28 |
-
false_positives = 0
|
29 |
-
false_negatives = 0
|
30 |
-
|
31 |
-
# Evaluate each sentence
|
32 |
-
for sentence, gold_tree in zip(sentences, gold_standard_trees):
|
33 |
tokens = nltk.word_tokenize(sentence)
|
34 |
parsed_trees = list(parser.parse(tokens))
|
|
|
35 |
if parsed_trees:
|
36 |
-
# If the parser produced a parse tree, consider the first one
|
37 |
parsed_tree = parsed_trees[0]
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
true_positives += 1
|
42 |
-
else:
|
43 |
-
false_negatives += 1
|
44 |
-
for production in parsed_tree.productions():
|
45 |
-
if production not in gold_tree.productions():
|
46 |
-
false_positives += 1
|
47 |
|
48 |
-
#
|
49 |
-
|
50 |
-
recall = true_positives / (true_positives + false_negatives)
|
51 |
-
f1_score = 2 * (precision * recall) / (precision + recall)
|
52 |
|
53 |
-
#
|
54 |
-
|
55 |
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
|
|
|
1 |
+
import streamlit as st
|
2 |
import nltk
|
|
|
3 |
from nltk.corpus import treebank
|
4 |
from nltk import PCFG, ViterbiParser
|
5 |
+
nltk.download('punkt')
|
6 |
nltk.download('all')
|
7 |
# Load the treebank dataset
|
8 |
+
# nltk.download('treebank')
|
9 |
corpus = treebank.parsed_sents()
|
10 |
|
11 |
# Train a PCFG parser
|
|
|
18 |
# Initialize the parser with the trained grammar
|
19 |
parser = ViterbiParser(grammar)
|
20 |
|
21 |
+
def evaluate_parser(sentence):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
tokens = nltk.word_tokenize(sentence)
|
23 |
parsed_trees = list(parser.parse(tokens))
|
24 |
+
|
25 |
if parsed_trees:
|
|
|
26 |
parsed_tree = parsed_trees[0]
|
27 |
+
return parsed_tree
|
28 |
+
else:
|
29 |
+
return "Failed to parse the sentence."
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
+
# Streamlit UI
|
32 |
+
st.title("PCFG Parser Evaluation")
|
|
|
|
|
33 |
|
34 |
+
# Input text box for entering sentences
|
35 |
+
sentence = st.text_input("Enter a sentence:", "this is a beautiful")
|
36 |
|
37 |
+
# Button to trigger parsing
|
38 |
+
if st.button("Parse"):
|
39 |
+
parsed_tree = evaluate_parser(sentence)
|
40 |
+
st.write("Parsed Tree:")
|
41 |
+
st.write(parsed_tree)
|