File size: 1,947 Bytes
98ffeff
b16059e
98ffeff
b16059e
98ffeff
b16059e
5687e79
b6bbf8d
b16059e
b6bbf8d
 
b16059e
56359e1
 
 
 
 
b16059e
98ffeff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
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
51
52
53
54
import os
import subprocess
import nltk
import spacy

# Setup NLTK
nltk_data_path = "/tmp/nltk_data"
nltk.download('punkt', download_dir=nltk_data_path)
nltk.download('benepar_en3', download_dir=nltk_data_path)
nltk.data.path.append(nltk_data_path)

# Load spaCy model with fallback
try:
    nlp = spacy.load("en_core_web_sm")
except:
    subprocess.run(["python", "-m", "spacy", "download", "en_core_web_sm"])
    nlp = spacy.load("en_core_web_sm")
    
if "benepar" not in nlp.pipe_names:
    nlp.add_pipe("benepar", config={"model": "benepar_en3"})

st.set_page_config(page_title="Syntax Parser Comparison Tool", layout="wide")
st.title("🌐 Syntax Parser Comparison Tool")
st.write("This tool compares Dependency Parsing, Constituency Parsing, and a simulated Abstract Syntax Representation (ASR).")

sentence = st.text_input("Enter a sentence:", "John eats an apple.")

if sentence:
    doc = nlp(sentence)
    sent = list(doc.sents)[0]

    col1, col2, col3 = st.columns(3)

    with col1:
        st.header("Dependency Parsing")
        for token in sent:
            st.write(f"{token.text} --> {token.dep_} --> {token.head.text}")
        st.code(" ".join(f"({token.text}, {token.dep_}, {token.head.text})" for token in sent))

    with col2:
        st.header("Constituency Parsing")
        tree = sent._.parse_string
        st.text(tree)
        st.code(Tree.fromstring(tree).pformat())

    with col3:
        st.header("Simulated ASR Output")
        st.write("Combining phrase structure with dependency head annotations:")
        for token in sent:
            if token.dep_ in ("nsubj", "obj", "det", "ROOT"):
                st.write(f"[{token.text}] - {token.dep_} --> {token.head.text} ({token.pos_})")
        st.markdown("_(ASR is simulated by combining POS tags, dependency heads, and phrase information.)_")
        st.code(" ".join(f"[{token.text}: {token.dep_} β†’ {token.head.text}]({token.pos_})" for token in sent))