Update src/streamlit_app.py
Browse files- src/streamlit_app.py +174 -212
src/streamlit_app.py
CHANGED
|
@@ -4,7 +4,6 @@ import re
|
|
| 4 |
import random
|
| 5 |
from typing import List, Tuple, Dict
|
| 6 |
import requests
|
| 7 |
-
import json
|
| 8 |
|
| 9 |
# Configure page
|
| 10 |
st.set_page_config(
|
|
@@ -24,12 +23,10 @@ def get_pyphen_dict():
|
|
| 24 |
def get_ipa_pronunciation(word: str) -> str:
|
| 25 |
"""Get IPA pronunciation for a word using a free dictionary API"""
|
| 26 |
try:
|
| 27 |
-
# Clean the word
|
| 28 |
clean_word = re.sub(r'[^\w]', '', word.lower())
|
| 29 |
if not clean_word:
|
| 30 |
return ""
|
| 31 |
|
| 32 |
-
# Try Free Dictionary API
|
| 33 |
url = f"https://api.dictionaryapi.dev/api/v2/entries/en/{clean_word}"
|
| 34 |
response = requests.get(url, timeout=2)
|
| 35 |
|
|
@@ -40,27 +37,26 @@ def get_ipa_pronunciation(word: str) -> str:
|
|
| 40 |
for phonetic in phonetics:
|
| 41 |
if 'text' in phonetic and phonetic['text']:
|
| 42 |
return phonetic['text']
|
| 43 |
-
|
| 44 |
-
# Fallback - return empty if no pronunciation found
|
| 45 |
return ""
|
| 46 |
except:
|
| 47 |
return ""
|
| 48 |
|
| 49 |
def generate_color_palette(num_colors: int) -> List[str]:
|
| 50 |
-
"""Generate
|
| 51 |
-
|
| 52 |
-
"#2C3E50", "#
|
| 53 |
-
"#
|
| 54 |
-
"#
|
| 55 |
-
"#
|
| 56 |
]
|
| 57 |
|
| 58 |
-
while len(
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
| 62 |
|
| 63 |
-
return
|
| 64 |
|
| 65 |
def syllabify_word(word: str, pyphen_dict) -> List[str]:
|
| 66 |
"""Syllabify a single word using pyphen"""
|
|
@@ -93,198 +89,119 @@ def process_text(text: str) -> Tuple[List[Tuple[str, str, str]], List[Tuple[str,
|
|
| 93 |
"""Process text and return original words, syllable breakdown, and IPA dictionary"""
|
| 94 |
pyphen_dict = get_pyphen_dict()
|
| 95 |
|
| 96 |
-
|
| 97 |
-
word_pattern = r'\b\w+\b'
|
| 98 |
-
words_with_positions = []
|
| 99 |
-
for match in re.finditer(word_pattern, text):
|
| 100 |
-
words_with_positions.append((match.group(), match.start(), match.end()))
|
| 101 |
-
|
| 102 |
original_words = []
|
| 103 |
syllable_breakdown = []
|
| 104 |
ipa_dict = {}
|
| 105 |
|
| 106 |
-
# Generate
|
| 107 |
-
unique_words = list(set([word.lower() for word
|
| 108 |
word_colors = {}
|
| 109 |
colors = generate_color_palette(len(unique_words))
|
| 110 |
|
| 111 |
for i, unique_word in enumerate(unique_words):
|
| 112 |
word_colors[unique_word] = colors[i % len(colors)]
|
| 113 |
|
| 114 |
-
for i,
|
| 115 |
-
# Get IPA pronunciation
|
| 116 |
ipa = get_ipa_pronunciation(word)
|
| 117 |
ipa_dict[word.lower()] = ipa
|
| 118 |
|
| 119 |
-
# Get color for this word
|
| 120 |
color = word_colors[word.lower()]
|
| 121 |
-
|
| 122 |
-
# Add to original words with unique identifier
|
| 123 |
word_id = f"word_{i}"
|
|
|
|
|
|
|
| 124 |
original_words.append((word, color, word_id))
|
| 125 |
|
| 126 |
# Process syllables
|
| 127 |
syllables_list = syllabify_word(word, pyphen_dict)
|
| 128 |
syllable_colored = [(syl, color) for syl in syllables_list]
|
| 129 |
-
syllable_breakdown.append((word, syllable_colored
|
| 130 |
|
| 131 |
return original_words, syllable_breakdown, ipa_dict
|
| 132 |
|
| 133 |
-
def
|
| 134 |
-
"""
|
| 135 |
-
|
| 136 |
-
<style>
|
| 137 |
-
.word-hover {
|
| 138 |
-
cursor: pointer;
|
| 139 |
-
padding: 2px 4px;
|
| 140 |
-
border-radius: 3px;
|
| 141 |
-
transition: all 0.2s ease;
|
| 142 |
-
position: relative;
|
| 143 |
-
display: inline-block;
|
| 144 |
-
}
|
| 145 |
-
|
| 146 |
-
.word-hover:hover {
|
| 147 |
-
background-color: rgba(255, 255, 0, 0.3) !important;
|
| 148 |
-
transform: scale(1.05);
|
| 149 |
-
}
|
| 150 |
-
|
| 151 |
-
.tooltip {
|
| 152 |
-
visibility: hidden;
|
| 153 |
-
background-color: #333;
|
| 154 |
-
color: white;
|
| 155 |
-
text-align: center;
|
| 156 |
-
border-radius: 6px;
|
| 157 |
-
padding: 8px 12px;
|
| 158 |
-
position: absolute;
|
| 159 |
-
z-index: 1000;
|
| 160 |
-
bottom: 125%;
|
| 161 |
-
left: 50%;
|
| 162 |
-
margin-left: -60px;
|
| 163 |
-
opacity: 0;
|
| 164 |
-
transition: opacity 0.3s;
|
| 165 |
-
font-family: 'Courier New', monospace;
|
| 166 |
-
font-size: 14px;
|
| 167 |
-
white-space: nowrap;
|
| 168 |
-
box-shadow: 0 4px 8px rgba(0,0,0,0.3);
|
| 169 |
-
}
|
| 170 |
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
top: 100%;
|
| 175 |
-
left: 50%;
|
| 176 |
-
margin-left: -5px;
|
| 177 |
-
border-width: 5px;
|
| 178 |
-
border-style: solid;
|
| 179 |
-
border-color: #333 transparent transparent transparent;
|
| 180 |
-
}
|
| 181 |
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
document.querySelectorAll('[data-word-id="' + wordId + '"]').forEach(el => {
|
| 202 |
-
el.classList.add('highlighted');
|
| 203 |
-
});
|
| 204 |
-
}
|
| 205 |
-
|
| 206 |
-
function removeHighlight() {
|
| 207 |
-
document.querySelectorAll('.word-hover').forEach(el => {
|
| 208 |
-
el.classList.remove('highlighted');
|
| 209 |
-
});
|
| 210 |
-
}
|
| 211 |
-
</script>
|
| 212 |
-
"""
|
| 213 |
|
| 214 |
-
def
|
| 215 |
-
"""Display
|
| 216 |
-
html_parts = []
|
| 217 |
-
|
| 218 |
-
for word, color, word_id in original_words:
|
| 219 |
-
ipa = ipa_dict.get(word.lower(), "")
|
| 220 |
-
tooltip_text = f"IPA: {ipa}" if ipa else "IPA: Not available"
|
| 221 |
-
|
| 222 |
-
html_parts.append(f'''
|
| 223 |
-
<span class="word-hover"
|
| 224 |
-
style="color: {color}; font-weight: bold; font-size: 16px;"
|
| 225 |
-
data-word-id="{word_id}"
|
| 226 |
-
onmouseenter="highlightWord('{word_id}')"
|
| 227 |
-
onmouseleave="removeHighlight()">
|
| 228 |
-
{word}
|
| 229 |
-
<span class="tooltip">{tooltip_text}</span>
|
| 230 |
-
</span> ''')
|
| 231 |
|
| 232 |
-
|
| 233 |
-
st.
|
| 234 |
-
<div style="font-size: 16px; line-height: 1.8; padding: 15px; border: 2px solid #ddd; border-radius: 8px; background-color: #f9f9f9;">
|
| 235 |
-
{html_content}
|
| 236 |
-
</div>
|
| 237 |
-
''', unsafe_allow_html=True)
|
| 238 |
-
|
| 239 |
-
def display_syllable_breakdown(syllable_breakdown: List[Tuple[str, List[Tuple[str, str]], str]], ipa_dict: Dict[str, str]):
|
| 240 |
-
"""Display syllable breakdown with hyphens, hover effects, and IPA tooltips"""
|
| 241 |
-
html_parts = []
|
| 242 |
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
if len(syllables) > 1:
|
| 253 |
-
syllable_display = '<span style="color: #666; font-weight: bold;">-</span>'.join(syllable_spans)
|
| 254 |
else:
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
style="margin-right: 12px; display: inline-block; margin-bottom: 6px;"
|
| 260 |
-
data-word-id="{word_id}"
|
| 261 |
-
onmouseenter="highlightWord('{word_id}')"
|
| 262 |
-
onmouseleave="removeHighlight()">
|
| 263 |
-
{syllable_display}
|
| 264 |
-
<span class="tooltip">{tooltip_text}</span>
|
| 265 |
-
</span>''')
|
| 266 |
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
|
| 275 |
def main():
|
| 276 |
st.title("π€ Enhanced Text Syllabification Tool")
|
| 277 |
-
st.markdown("*Enter text to see syllable breakdown with IPA pronunciation and interactive
|
| 278 |
-
|
| 279 |
-
# Add CSS and JavaScript
|
| 280 |
-
st.markdown(create_hover_css_and_js(), unsafe_allow_html=True)
|
| 281 |
|
| 282 |
# Input text area
|
| 283 |
input_text = st.text_area(
|
| 284 |
"Enter your text:",
|
| 285 |
placeholder="Type or paste your text here...",
|
| 286 |
height=100,
|
| 287 |
-
help="Enter any text to see syllables
|
| 288 |
)
|
| 289 |
|
| 290 |
# Submit button
|
|
@@ -292,45 +209,93 @@ def main():
|
|
| 292 |
|
| 293 |
if submit_clicked and input_text.strip():
|
| 294 |
with st.spinner("Processing text and fetching pronunciations..."):
|
| 295 |
-
# Process the text
|
| 296 |
original_words, syllable_breakdown, ipa_dict = process_text(input_text)
|
| 297 |
|
| 298 |
-
#
|
| 299 |
-
st.subheader("
|
| 300 |
-
|
| 301 |
-
|
|
|
|
| 302 |
|
| 303 |
-
# Display
|
| 304 |
-
st.
|
| 305 |
-
|
| 306 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 307 |
|
| 308 |
# Statistics
|
| 309 |
total_words = len(syllable_breakdown)
|
| 310 |
-
total_syllables = sum(len(syls) for _, syls
|
| 311 |
|
| 312 |
-
st.markdown(f"**π {total_words} words β’ {total_syllables} syllables**")
|
| 313 |
|
| 314 |
-
#
|
| 315 |
-
|
| 316 |
-
|
|
|
|
|
|
|
| 317 |
st.markdown("**Syllable Counts:**")
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
|
|
|
| 321 |
st.markdown("**IPA Pronunciations:**")
|
| 322 |
ipa_available = {word: ipa for word, ipa in ipa_dict.items() if ipa}
|
| 323 |
if ipa_available:
|
| 324 |
for word, ipa in sorted(ipa_available.items()):
|
| 325 |
st.text(f"{word}: {ipa}")
|
| 326 |
else:
|
| 327 |
-
st.text("No IPA pronunciations available
|
| 328 |
|
| 329 |
elif submit_clicked and not input_text.strip():
|
| 330 |
st.warning("β οΈ Please enter some text first!")
|
| 331 |
|
| 332 |
-
|
| 333 |
-
st.info("π Enter some text above and click Submit to
|
| 334 |
|
| 335 |
# Example
|
| 336 |
st.subheader("Example")
|
|
@@ -341,42 +306,39 @@ def main():
|
|
| 341 |
with st.spinner("Processing example..."):
|
| 342 |
original_words, syllable_breakdown, ipa_dict = process_text(example_text)
|
| 343 |
|
| 344 |
-
|
| 345 |
-
display_original_text(original_words, ipa_dict)
|
| 346 |
-
|
| 347 |
-
st.subheader("π€ Syllable Breakdown")
|
| 348 |
-
display_syllable_breakdown(syllable_breakdown, ipa_dict)
|
| 349 |
|
| 350 |
total_words = len(syllable_breakdown)
|
| 351 |
-
total_syllables = sum(len(syls) for _, syls
|
| 352 |
st.markdown(f"**π {total_words} words β’ {total_syllables} syllables**")
|
| 353 |
|
| 354 |
# About section
|
| 355 |
with st.expander("About this Enhanced Tool"):
|
| 356 |
st.markdown("""
|
| 357 |
-
This enhanced tool provides:
|
| 358 |
|
| 359 |
-
**π€
|
| 360 |
- Accurate English syllabification using pyphen (Hunspell algorithm)
|
| 361 |
-
- Color-coded syllables for easy
|
| 362 |
-
-
|
| 363 |
-
- Proper capitalization handling
|
| 364 |
|
| 365 |
**π― Interactive Features:**
|
| 366 |
-
- **
|
| 367 |
-
- **
|
| 368 |
-
- **IPA pronunciations**:
|
| 369 |
-
- **
|
| 370 |
|
| 371 |
-
**
|
| 372 |
-
-
|
| 373 |
-
-
|
| 374 |
-
-
|
| 375 |
|
| 376 |
-
**π‘
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
|
|
|
|
|
|
| 380 |
""")
|
| 381 |
|
| 382 |
if __name__ == "__main__":
|
|
|
|
| 4 |
import random
|
| 5 |
from typing import List, Tuple, Dict
|
| 6 |
import requests
|
|
|
|
| 7 |
|
| 8 |
# Configure page
|
| 9 |
st.set_page_config(
|
|
|
|
| 23 |
def get_ipa_pronunciation(word: str) -> str:
|
| 24 |
"""Get IPA pronunciation for a word using a free dictionary API"""
|
| 25 |
try:
|
|
|
|
| 26 |
clean_word = re.sub(r'[^\w]', '', word.lower())
|
| 27 |
if not clean_word:
|
| 28 |
return ""
|
| 29 |
|
|
|
|
| 30 |
url = f"https://api.dictionaryapi.dev/api/v2/entries/en/{clean_word}"
|
| 31 |
response = requests.get(url, timeout=2)
|
| 32 |
|
|
|
|
| 37 |
for phonetic in phonetics:
|
| 38 |
if 'text' in phonetic and phonetic['text']:
|
| 39 |
return phonetic['text']
|
|
|
|
|
|
|
| 40 |
return ""
|
| 41 |
except:
|
| 42 |
return ""
|
| 43 |
|
| 44 |
def generate_color_palette(num_colors: int) -> List[str]:
|
| 45 |
+
"""Generate colors for syllables"""
|
| 46 |
+
colors = [
|
| 47 |
+
"#2C3E50", "#E74C3C", "#3498DB", "#2ECC71", "#F39C12",
|
| 48 |
+
"#9B59B6", "#1ABC9C", "#E67E22", "#34495E", "#F1C40F",
|
| 49 |
+
"#8E44AD", "#16A085", "#27AE60", "#E8753F", "#BDC3C7",
|
| 50 |
+
"#95A5A6", "#D35400", "#C0392B", "#8B4513", "#FF6347"
|
| 51 |
]
|
| 52 |
|
| 53 |
+
while len(colors) < num_colors:
|
| 54 |
+
r = random.randint(50, 200)
|
| 55 |
+
g = random.randint(50, 200)
|
| 56 |
+
b = random.randint(50, 200)
|
| 57 |
+
colors.append(f"#{r:02x}{g:02x}{b:02x}")
|
| 58 |
|
| 59 |
+
return colors[:num_colors]
|
| 60 |
|
| 61 |
def syllabify_word(word: str, pyphen_dict) -> List[str]:
|
| 62 |
"""Syllabify a single word using pyphen"""
|
|
|
|
| 89 |
"""Process text and return original words, syllable breakdown, and IPA dictionary"""
|
| 90 |
pyphen_dict = get_pyphen_dict()
|
| 91 |
|
| 92 |
+
words = re.findall(r'\b\w+\b', text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
original_words = []
|
| 94 |
syllable_breakdown = []
|
| 95 |
ipa_dict = {}
|
| 96 |
|
| 97 |
+
# Generate colors for unique words
|
| 98 |
+
unique_words = list(set([word.lower() for word in words]))
|
| 99 |
word_colors = {}
|
| 100 |
colors = generate_color_palette(len(unique_words))
|
| 101 |
|
| 102 |
for i, unique_word in enumerate(unique_words):
|
| 103 |
word_colors[unique_word] = colors[i % len(colors)]
|
| 104 |
|
| 105 |
+
for i, word in enumerate(words):
|
| 106 |
+
# Get IPA pronunciation
|
| 107 |
ipa = get_ipa_pronunciation(word)
|
| 108 |
ipa_dict[word.lower()] = ipa
|
| 109 |
|
|
|
|
| 110 |
color = word_colors[word.lower()]
|
|
|
|
|
|
|
| 111 |
word_id = f"word_{i}"
|
| 112 |
+
|
| 113 |
+
# Add to original words
|
| 114 |
original_words.append((word, color, word_id))
|
| 115 |
|
| 116 |
# Process syllables
|
| 117 |
syllables_list = syllabify_word(word, pyphen_dict)
|
| 118 |
syllable_colored = [(syl, color) for syl in syllables_list]
|
| 119 |
+
syllable_breakdown.append((word, syllable_colored))
|
| 120 |
|
| 121 |
return original_words, syllable_breakdown, ipa_dict
|
| 122 |
|
| 123 |
+
def display_text_with_colors(words_data: List[Tuple[str, str, str]], title: str, is_syllables: bool = False):
|
| 124 |
+
"""Display text with colors using Streamlit columns for hover effect simulation"""
|
| 125 |
+
st.markdown(f"### {title}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
|
| 127 |
+
# Create a grid layout for words
|
| 128 |
+
cols_per_row = 8
|
| 129 |
+
word_chunks = [words_data[i:i + cols_per_row] for i in range(0, len(words_data), cols_per_row)]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
+
for chunk in word_chunks:
|
| 132 |
+
cols = st.columns(len(chunk))
|
| 133 |
+
for i, (word_info, col) in enumerate(zip(chunk, cols)):
|
| 134 |
+
if is_syllables:
|
| 135 |
+
word, syllables = word_info
|
| 136 |
+
if len(syllables) > 1:
|
| 137 |
+
display_text = '-'.join([syl for syl, _ in syllables])
|
| 138 |
+
else:
|
| 139 |
+
display_text = syllables[0][0] if syllables else word
|
| 140 |
+
color = syllables[0][1] if syllables else "#333333"
|
| 141 |
+
else:
|
| 142 |
+
word, color, word_id = word_info
|
| 143 |
+
display_text = word
|
| 144 |
+
|
| 145 |
+
with col:
|
| 146 |
+
st.markdown(
|
| 147 |
+
f'<span style="color: {color}; font-weight: bold; font-size: 16px; padding: 4px; display: inline-block;">{display_text}</span>',
|
| 148 |
+
unsafe_allow_html=True
|
| 149 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
+
def display_word_grid_with_ipa(original_words, syllable_breakdown, ipa_dict, selected_word_idx=None):
|
| 152 |
+
"""Display words in a grid format with IPA information"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 153 |
|
| 154 |
+
# Create tabs for different views
|
| 155 |
+
tab1, tab2 = st.tabs(["π Original Text", "π€ Syllabified Text"])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
+
with tab1:
|
| 158 |
+
st.markdown("*Click on a word to see its IPA pronunciation*")
|
| 159 |
+
|
| 160 |
+
# Display original words in a flowing text format
|
| 161 |
+
html_words = []
|
| 162 |
+
for i, (word, color, word_id) in enumerate(original_words):
|
| 163 |
+
if i == selected_word_idx:
|
| 164 |
+
# Highlight selected word
|
| 165 |
+
html_words.append(f'<strong style="color: {color}; background-color: yellow; padding: 2px 4px; border-radius: 3px; font-size: 18px;">{word}</strong>')
|
|
|
|
|
|
|
| 166 |
else:
|
| 167 |
+
html_words.append(f'<span style="color: {color}; font-weight: bold; font-size: 16px; margin: 0 2px;">{word}</span>')
|
| 168 |
+
|
| 169 |
+
text_html = ' '.join(html_words)
|
| 170 |
+
st.markdown(f'<div style="line-height: 1.8; padding: 15px; border: 2px solid #ddd; border-radius: 8px; background-color: #f9f9f9;">{text_html}</div>', unsafe_allow_html=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
|
| 172 |
+
with tab2:
|
| 173 |
+
st.markdown("*Click on a word to see its IPA pronunciation*")
|
| 174 |
+
|
| 175 |
+
# Display syllabified words
|
| 176 |
+
html_words = []
|
| 177 |
+
for i, (word, syllables) in enumerate(syllable_breakdown):
|
| 178 |
+
if syllables:
|
| 179 |
+
if len(syllables) > 1:
|
| 180 |
+
syllable_parts = []
|
| 181 |
+
for syl, color in syllables:
|
| 182 |
+
syllable_parts.append(f'<span style="color: {color}; font-weight: bold;">{syl}</span>')
|
| 183 |
+
syllable_text = '<span style="color: #666;">-</span>'.join(syllable_parts)
|
| 184 |
+
else:
|
| 185 |
+
syllable_text = f'<span style="color: {syllables[0][1]}; font-weight: bold;">{syllables[0][0]}</span>'
|
| 186 |
+
|
| 187 |
+
if i == selected_word_idx:
|
| 188 |
+
html_words.append(f'<span style="background-color: yellow; padding: 2px 4px; border-radius: 3px; font-size: 18px;">{syllable_text}</span>')
|
| 189 |
+
else:
|
| 190 |
+
html_words.append(f'<span style="font-size: 16px; margin: 0 4px;">{syllable_text}</span>')
|
| 191 |
+
|
| 192 |
+
syllable_html = ' '.join(html_words)
|
| 193 |
+
st.markdown(f'<div style="line-height: 1.8; padding: 15px; border: 2px solid #ddd; border-radius: 8px; background-color: #f0f8ff;">{syllable_html}</div>', unsafe_allow_html=True)
|
| 194 |
|
| 195 |
def main():
|
| 196 |
st.title("π€ Enhanced Text Syllabification Tool")
|
| 197 |
+
st.markdown("*Enter text to see syllable breakdown with IPA pronunciation and interactive word selection*")
|
|
|
|
|
|
|
|
|
|
| 198 |
|
| 199 |
# Input text area
|
| 200 |
input_text = st.text_area(
|
| 201 |
"Enter your text:",
|
| 202 |
placeholder="Type or paste your text here...",
|
| 203 |
height=100,
|
| 204 |
+
help="Enter any text to see syllables and IPA pronunciations"
|
| 205 |
)
|
| 206 |
|
| 207 |
# Submit button
|
|
|
|
| 209 |
|
| 210 |
if submit_clicked and input_text.strip():
|
| 211 |
with st.spinner("Processing text and fetching pronunciations..."):
|
|
|
|
| 212 |
original_words, syllable_breakdown, ipa_dict = process_text(input_text)
|
| 213 |
|
| 214 |
+
# Word selection interface
|
| 215 |
+
st.subheader("π Interactive Word Explorer")
|
| 216 |
+
|
| 217 |
+
# Create word selection buttons
|
| 218 |
+
word_list = [word for word, _, _ in original_words]
|
| 219 |
|
| 220 |
+
# Display words as clickable buttons in columns
|
| 221 |
+
st.markdown("**Click on any word to see detailed information:**")
|
| 222 |
+
|
| 223 |
+
cols_per_row = 6
|
| 224 |
+
word_chunks = [word_list[i:i + cols_per_row] for i in range(0, len(word_list), cols_per_row)]
|
| 225 |
+
|
| 226 |
+
selected_word_idx = None
|
| 227 |
+
selected_word = None
|
| 228 |
+
|
| 229 |
+
for chunk_idx, chunk in enumerate(word_chunks):
|
| 230 |
+
cols = st.columns(len(chunk))
|
| 231 |
+
for word_idx_in_chunk, (word, col) in enumerate(zip(chunk, cols)):
|
| 232 |
+
actual_word_idx = chunk_idx * cols_per_row + word_idx_in_chunk
|
| 233 |
+
with col:
|
| 234 |
+
if st.button(word, key=f"word_btn_{actual_word_idx}", use_container_width=True):
|
| 235 |
+
selected_word_idx = actual_word_idx
|
| 236 |
+
selected_word = word
|
| 237 |
+
|
| 238 |
+
# Display text with highlighting
|
| 239 |
+
display_word_grid_with_ipa(original_words, syllable_breakdown, ipa_dict, selected_word_idx)
|
| 240 |
+
|
| 241 |
+
# Show detailed information for selected word
|
| 242 |
+
if selected_word_idx is not None and selected_word:
|
| 243 |
+
st.subheader(f"π Details for: **{selected_word}**")
|
| 244 |
+
|
| 245 |
+
col1, col2, col3 = st.columns(3)
|
| 246 |
+
|
| 247 |
+
with col1:
|
| 248 |
+
st.markdown("**Original Word:**")
|
| 249 |
+
word_info = original_words[selected_word_idx]
|
| 250 |
+
st.markdown(f'<span style="color: {word_info[1]}; font-weight: bold; font-size: 20px;">{word_info[0]}</span>', unsafe_allow_html=True)
|
| 251 |
+
|
| 252 |
+
with col2:
|
| 253 |
+
st.markdown("**Syllables:**")
|
| 254 |
+
syllables = syllable_breakdown[selected_word_idx][1]
|
| 255 |
+
if len(syllables) > 1:
|
| 256 |
+
syllable_display = " - ".join([syl for syl, _ in syllables])
|
| 257 |
+
st.markdown(f"**{syllable_display}** ({len(syllables)} syllables)")
|
| 258 |
+
else:
|
| 259 |
+
st.markdown(f"**{syllables[0][0]}** (1 syllable)")
|
| 260 |
+
|
| 261 |
+
with col3:
|
| 262 |
+
st.markdown("**IPA Pronunciation:**")
|
| 263 |
+
ipa = ipa_dict.get(selected_word.lower(), "")
|
| 264 |
+
if ipa:
|
| 265 |
+
st.markdown(f"**{ipa}**")
|
| 266 |
+
st.markdown("π *Hover to hear pronunciation*")
|
| 267 |
+
else:
|
| 268 |
+
st.markdown("*Not available*")
|
| 269 |
|
| 270 |
# Statistics
|
| 271 |
total_words = len(syllable_breakdown)
|
| 272 |
+
total_syllables = sum(len(syls) for _, syls in syllable_breakdown)
|
| 273 |
|
| 274 |
+
st.markdown(f"**π Statistics: {total_words} words β’ {total_syllables} syllables**")
|
| 275 |
|
| 276 |
+
# Detailed information in expandable section
|
| 277 |
+
with st.expander("π Complete Analysis", expanded=False):
|
| 278 |
+
col1, col2 = st.columns(2)
|
| 279 |
+
|
| 280 |
+
with col1:
|
| 281 |
st.markdown("**Syllable Counts:**")
|
| 282 |
+
for word, syllables in syllable_breakdown:
|
| 283 |
+
st.text(f"{word}: {len(syllables)} syllable{'s' if len(syllables) > 1 else ''}")
|
| 284 |
+
|
| 285 |
+
with col2:
|
| 286 |
st.markdown("**IPA Pronunciations:**")
|
| 287 |
ipa_available = {word: ipa for word, ipa in ipa_dict.items() if ipa}
|
| 288 |
if ipa_available:
|
| 289 |
for word, ipa in sorted(ipa_available.items()):
|
| 290 |
st.text(f"{word}: {ipa}")
|
| 291 |
else:
|
| 292 |
+
st.text("No IPA pronunciations available.")
|
| 293 |
|
| 294 |
elif submit_clicked and not input_text.strip():
|
| 295 |
st.warning("β οΈ Please enter some text first!")
|
| 296 |
|
| 297 |
+
else:
|
| 298 |
+
st.info("π Enter some text above and click Submit to start exploring!")
|
| 299 |
|
| 300 |
# Example
|
| 301 |
st.subheader("Example")
|
|
|
|
| 306 |
with st.spinner("Processing example..."):
|
| 307 |
original_words, syllable_breakdown, ipa_dict = process_text(example_text)
|
| 308 |
|
| 309 |
+
display_word_grid_with_ipa(original_words, syllable_breakdown, ipa_dict)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 310 |
|
| 311 |
total_words = len(syllable_breakdown)
|
| 312 |
+
total_syllables = sum(len(syls) for _, syls in syllable_breakdown)
|
| 313 |
st.markdown(f"**π {total_words} words β’ {total_syllables} syllables**")
|
| 314 |
|
| 315 |
# About section
|
| 316 |
with st.expander("About this Enhanced Tool"):
|
| 317 |
st.markdown("""
|
| 318 |
+
This enhanced syllabification tool provides:
|
| 319 |
|
| 320 |
+
**π€ Core Features:**
|
| 321 |
- Accurate English syllabification using pyphen (Hunspell algorithm)
|
| 322 |
+
- Color-coded words and syllables for easy identification
|
| 323 |
+
- Two view modes: Original text and Syllabified text
|
|
|
|
| 324 |
|
| 325 |
**π― Interactive Features:**
|
| 326 |
+
- **Click-to-explore**: Click any word button to see detailed analysis
|
| 327 |
+
- **Word highlighting**: Selected words are highlighted in both views
|
| 328 |
+
- **IPA pronunciations**: See phonetic transcriptions for each word
|
| 329 |
+
- **Syllable counting**: Visual breakdown of syllable structure
|
| 330 |
|
| 331 |
+
**π Analysis Tools:**
|
| 332 |
+
- Complete syllable counts for all words
|
| 333 |
+
- Statistical overview (total words and syllables)
|
| 334 |
+
- Pronunciation data from Free Dictionary API
|
| 335 |
|
| 336 |
+
**π‘ How to Use:**
|
| 337 |
+
1. Enter your text in the input area
|
| 338 |
+
2. Click Submit to process the text
|
| 339 |
+
3. Click on any word button to see detailed information
|
| 340 |
+
4. Switch between Original and Syllabified views using tabs
|
| 341 |
+
5. Check the Complete Analysis section for full details
|
| 342 |
""")
|
| 343 |
|
| 344 |
if __name__ == "__main__":
|