aghilTQ commited on
Commit
bbae9c6
·
verified ·
1 Parent(s): 31b4775

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +34 -39
src/streamlit_app.py CHANGED
@@ -137,43 +137,37 @@ def adjust_syllables(word: str, target_count: int) -> List[str]:
137
 
138
  def process_text(text: str) -> Tuple[List[Tuple[str, str]], List[Tuple[str, List[Tuple[str, str]]]]]:
139
  """Process text and return colored words and syllable breakdown"""
140
- words = re.findall(r'\S+|\s+', text) # Keep spaces
 
141
 
142
  colored_words = []
143
  syllable_breakdown = []
144
 
145
- for word in words:
146
- if word.strip(): # If it's a word (not just whitespace)
147
- syllables_list = syllabify_word(word.strip())
148
-
149
- # Generate colors for this word's syllables
150
- colors = generate_color_palette(len(syllables_list))
151
-
152
- # For original text coloring, we need to map syllables back to original word
153
- word_colored = []
154
- syllable_colored = []
155
-
156
- # Handle punctuation
157
- clean_word = re.sub(r'[^\w]', '', word.strip())
158
- prefix_punct = re.match(r'^[^\w]*', word.strip()).group()
159
- suffix_punct = re.search(r'[^\w]*$', word.strip()).group()
160
-
161
- if clean_word:
162
- # Color each syllable
163
- for i, syl in enumerate(syllables_list):
164
- color = colors[i % len(colors)]
165
- syllable_colored.append((syl, color))
166
 
167
- # For the original word, we'll color it uniformly with the first syllable color
168
- # or create a gradient effect
169
- word_with_punct = prefix_punct + clean_word + suffix_punct
170
- colored_words.append((word_with_punct, colors[0] if colors else "#000000"))
171
 
172
- syllable_breakdown.append((word.strip(), syllable_colored))
 
 
 
 
173
  else:
174
- colored_words.append((word, "#000000"))
175
  else:
176
- colored_words.append((word, "#000000")) # Spaces remain uncolored
177
 
178
  return colored_words, syllable_breakdown
179
 
@@ -194,22 +188,23 @@ def display_syllable_breakdown(syllable_breakdown: List[Tuple[str, List[Tuple[st
194
  html_parts = []
195
 
196
  for word, syllables in syllable_breakdown:
197
- if syllables:
198
- word_html = []
 
199
  for syl, color in syllables:
200
- word_html.append(f'<span style="color: {color}; font-weight: bold; margin-right: 2px;">{syl}</span>')
201
 
202
- # Add separator between syllables
203
- syllable_display = '<span style="color: #666;">|</span>'.join([
204
- f'<span style="color: {color}; font-weight: bold;">{syl}</span>'
205
- for syl, color in syllables
206
- ])
207
 
208
- html_parts.append(f'<div style="margin-bottom: 10px;"><strong>{word}:</strong> {syllable_display}</div>')
209
 
210
  if html_parts:
211
  html_content = ''.join(html_parts)
212
- st.markdown(f'<div style="font-size: 16px; line-height: 1.8; padding: 15px; border: 2px solid #ddd; border-radius: 10px; background-color: #f0f8ff;">{html_content}</div>', unsafe_allow_html=True)
213
 
214
  # Main app
215
  def main():
 
137
 
138
  def process_text(text: str) -> Tuple[List[Tuple[str, str]], List[Tuple[str, List[Tuple[str, str]]]]]:
139
  """Process text and return colored words and syllable breakdown"""
140
+ # Split into words while preserving spaces
141
+ words = re.findall(r'\b\w+\b', text) # Only actual words, no spaces
142
 
143
  colored_words = []
144
  syllable_breakdown = []
145
 
146
+ # Process original text with spaces preserved
147
+ text_parts = re.findall(r'\S+|\s+', text)
148
+
149
+ word_index = 0
150
+ for part in text_parts:
151
+ if part.strip() and re.match(r'\w', part): # If it's a word
152
+ if word_index < len(words):
153
+ word = words[word_index]
154
+ syllables_list = syllabify_word(word)
155
+
156
+ # Generate colors for this word's syllables
157
+ colors = generate_color_palette(len(syllables_list))
 
 
 
 
 
 
 
 
 
158
 
159
+ # For original text, color with first syllable color
160
+ colored_words.append((part, colors[0] if colors else "#000000"))
 
 
161
 
162
+ # Add to syllable breakdown (no duplicates)
163
+ syllable_colored = [(syl, colors[i % len(colors)]) for i, syl in enumerate(syllables_list)]
164
+ syllable_breakdown.append((word, syllable_colored))
165
+
166
+ word_index += 1
167
  else:
168
+ colored_words.append((part, "#000000"))
169
  else:
170
+ colored_words.append((part, "#000000")) # Spaces and punctuation
171
 
172
  return colored_words, syllable_breakdown
173
 
 
188
  html_parts = []
189
 
190
  for word, syllables in syllable_breakdown:
191
+ if syllables and len(syllables) > 0:
192
+ # Create hyphenated syllable display
193
+ syllable_spans = []
194
  for syl, color in syllables:
195
+ syllable_spans.append(f'<span style="color: {color}; font-weight: bold;">{syl}</span>')
196
 
197
+ # Join with hyphens for multi-syllable words
198
+ if len(syllables) > 1:
199
+ syllable_display = '<span style="color: #666;">-</span>'.join(syllable_spans)
200
+ else:
201
+ syllable_display = syllable_spans[0]
202
 
203
+ html_parts.append(f'<span style="margin-right: 20px; display: inline-block; margin-bottom: 8px;">{syllable_display}</span>')
204
 
205
  if html_parts:
206
  html_content = ''.join(html_parts)
207
+ st.markdown(f'<div style="font-size: 18px; line-height: 2.0; padding: 15px; border: 2px solid #ddd; border-radius: 10px; background-color: #f0f8ff;">{html_content}</div>', unsafe_allow_html=True)
208
 
209
  # Main app
210
  def main():