-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
193 lines (175 loc) · 6.56 KB
/
app.py
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import streamlit as st
from tokenization import perform_tokenization
from pos_tagging import perform_pos_tagging
from stemming import perform_stemming
from lemmatization import perform_lemmatization
from ner import perform_ner
import os
# Ensure the NLTK resources are downloaded
if not os.path.exists("nltk_data"):
os.makedirs("nltk_data")
# Set the NLTK data path to the correct location
import nltk
nltk.data.path.append(os.path.join(os.getcwd(), "nltk_data"))
# Import and execute the resource download function
import download_nltk_resources
download_nltk_resources.download_nltk_resources()
# Streamlit App Configuration
st.set_page_config(page_title="NLP Explorer", layout="wide")
# Custom CSS to enhance UI and layout
st.markdown(
"""
<style>
body {
background-color: #f4f7fc;
font-family: 'Roboto', sans-serif;
}
.title {
font-weight: 700;
font-size: 36px;
color: #4A6FA2;
text-align: center;
margin-bottom: 30px;
}
.card {
background-color: #ffffff;
padding: 20px;
border-radius: 12px;
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
transition: transform 0.3s ease, box-shadow 0.3s ease;
}
.card:hover {
transform: translateY(-8px);
box-shadow: 0 6px 20px rgba(0, 0, 0, 0.2);
}
.card h3 {
color: #5e6e79;
font-weight: 600;
}
.button {
background-color: #4A90E2;
color: white;
padding: 12px 25px;
border-radius: 8px;
border: none;
cursor: pointer;
transition: all 0.3s ease;
}
.button:hover {
background-color: #357ABD;
}
.feature-checkbox {
font-size: 16px;
}
.footer {
text-align: center;
font-size: 14px;
color: #7a8d9c;
margin-top: 40px;
}
.container {
display: flex;
justify-content: space-between;
margin-top: 30px;
}
.feature-container {
flex: 1;
margin-right: 20px;
}
.footer-link {
color: #4A90E2;
text-decoration: none;
}
</style>
""", unsafe_allow_html=True
)
# App title and description
st.markdown("<h1 class='title'>💬 Natural Language Processing (NLP) Explorer</h1>", unsafe_allow_html=True)
st.markdown("""
Welcome to **NLP Explorer**! Use this app to analyze and process text with the following NLP techniques:
- Tokenization
- Part-of-Speech (POS) Tagging
- Stemming
- Lemmatization
- Named Entity Recognition (NER)
Simply choose the feature you want to apply, input your text, and see the results!
""")
# Text input area with more space
text_input = st.text_area("📝 Enter text for analysis", height=100, placeholder="Type or paste your text here...", max_chars=1000)
# Card layout for NLP features
st.write("### 🛠️ Choose NLP features to apply:")
selected_features = {
'Tokenization': False,
'POS Tagging': False,
'Stemming': False,
'Lemmatization': False,
'NER': False
}
# Display features as interactive cards in a 2-column layout
col1, col2 = st.columns(2)
with col1:
with st.container():
if st.checkbox("🔍 Tokenization", key="tokenization"):
selected_features['Tokenization'] = True
st.markdown("**Tokenization**: Splits text into words or sentences.")
if st.checkbox("🏷️ POS Tagging", key="pos_tagging"):
selected_features['POS Tagging'] = True
st.markdown("**POS Tagging**: Assigns part of speech to each word.")
with col2:
with st.container():
if st.checkbox("🌿 Stemming", key="stemming"):
selected_features['Stemming'] = True
st.markdown("**Stemming**: Reduces words to their root form.")
if st.checkbox("📝 Lemmatization", key="lemmatization"):
selected_features['Lemmatization'] = True
st.markdown("**Lemmatization**: Converts words to their base form based on context.")
if st.checkbox("🔍 NER", key="ner"):
selected_features['NER'] = True
st.markdown("**NER**: Recognizes named entities in the text.")
# Analyze Text Button with loading and feedback
st.write("---")
if st.button("Analyze Text", key="analyze_button", use_container_width=True):
if not text_input:
st.error("⚠️ Please enter some text to analyze.")
else:
st.write("### 📊 NLP Analysis Results")
# Display loading indicator during analysis
with st.spinner('Analyzing your text...'):
# Perform Tokenization
if selected_features['Tokenization']:
st.markdown("#### 🧩 Tokenization")
tokenization_result = perform_tokenization(text_input)
st.write("**Word Tokens:**", tokenization_result['word_tokens'])
st.write("**Sentence Tokens:**", tokenization_result['sentence_tokens'])
# Perform POS Tagging
if selected_features['POS Tagging']:
st.markdown("#### 🏷️ POS Tagging")
pos_tagging_result = perform_pos_tagging(text_input)
st.write("**POS Tags:**", pos_tagging_result)
# Perform Stemming
if selected_features['Stemming']:
st.markdown("#### 🌿 Stemming")
stemming_result = perform_stemming(text_input)
st.write("**Stemmed Words:**", stemming_result)
# Perform Lemmatization
if selected_features['Lemmatization']:
st.markdown("#### 📝 Lemmatization")
lemmatization_result = perform_lemmatization(text_input)
st.write("**Lemmatized Words:**", lemmatization_result)
# Perform NER
if selected_features['NER']:
st.markdown("#### 🔍 Named Entity Recognition (NER)")
ner_result = perform_ner(text_input)
if ner_result:
for entity, entity_type in ner_result:
st.write(f"**{entity}**: {entity_type}")
else:
st.write("No named entities found.")
# Footer with additional info and social link
st.markdown("---")
st.markdown("""
<div class='footer'>
💡 NLP Explorer | Built with <a href='https://streamlit.io/' class='footer-link'>Streamlit</a> |
Visit the <a href='https://github.com/arya-io/nlp-explorer' class='footer-link'>GitHub Repo</a>
</div>
""", unsafe_allow_html=True)