Improve correction UI: inline word display with dynamic popover layout
Browse files- Show all words inline as natural text flow (no column grid)
- Blue dashed underline for swappable words, green tick for changed
- Popover buttons rendered only for swappable words with proportional widths
- Update caption to accurately describe the interaction
app.py
CHANGED
|
@@ -170,45 +170,75 @@ if "output_words" in st.session_state and st.session_state["output_words"]:
|
|
| 170 |
)
|
| 171 |
|
| 172 |
if correction_mode:
|
| 173 |
-
st.caption("
|
| 174 |
-
|
| 175 |
-
#
|
| 176 |
-
|
| 177 |
-
for
|
| 178 |
-
|
| 179 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 180 |
|
| 181 |
-
|
| 182 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 183 |
was_changed = output_words[i] != original_words[i]
|
| 184 |
with col:
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
"Not listed? Type correct word:",
|
| 204 |
-
key=f"custom_{i}",
|
| 205 |
-
placeholder="Type Sinhala word",
|
| 206 |
-
)
|
| 207 |
-
if custom and st.button("Use this", key=f"custom_apply_{i}", use_container_width=True):
|
| 208 |
-
st.session_state["output_words"][i] = custom
|
| 209 |
st.rerun()
|
| 210 |
-
|
| 211 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
|
| 213 |
# ββ Submit correction button (only when changes exist, once per result) ββ
|
| 214 |
# Guard key: (original sentence, original output) β stable regardless of swaps
|
|
|
|
| 170 |
)
|
| 171 |
|
| 172 |
if correction_mode:
|
| 173 |
+
st.caption("Use the buttons below to swap alternative transliterations.")
|
| 174 |
+
|
| 175 |
+
# ββ Inline sentence display (natural text flow, no grid) βββββ
|
| 176 |
+
word_spans = []
|
| 177 |
+
for i, diag in enumerate(diagnostics):
|
| 178 |
+
has_alts = len(diag.candidate_breakdown) > 1
|
| 179 |
+
was_changed = output_words[i] != original_words[i]
|
| 180 |
+
w = html_lib.escape(output_words[i])
|
| 181 |
+
if was_changed:
|
| 182 |
+
word_spans.append(
|
| 183 |
+
f'<span style="color:#68d391;font-weight:700;">{w} β</span>'
|
| 184 |
+
)
|
| 185 |
+
elif has_alts:
|
| 186 |
+
word_spans.append(
|
| 187 |
+
f'<span style="color:#63b3ed;font-weight:700;'
|
| 188 |
+
f'border-bottom:2px dashed #63b3ed;cursor:default;">{w}</span>'
|
| 189 |
+
)
|
| 190 |
+
else:
|
| 191 |
+
word_spans.append(f'<span style="font-weight:600;">{w}</span>')
|
| 192 |
|
| 193 |
+
st.markdown(
|
| 194 |
+
'<div style="font-size:1.15em;line-height:2.4;">'
|
| 195 |
+
+ "   ".join(word_spans)
|
| 196 |
+
+ "</div>",
|
| 197 |
+
unsafe_allow_html=True,
|
| 198 |
+
)
|
| 199 |
+
# ββ Popover buttons only for swappable words βββββββββββββββββ
|
| 200 |
+
swappable = [
|
| 201 |
+
(i, diag)
|
| 202 |
+
for i, diag in enumerate(diagnostics)
|
| 203 |
+
if len(diag.candidate_breakdown) > 1
|
| 204 |
+
]
|
| 205 |
+
if swappable:
|
| 206 |
+
widths = [max(len(output_words[i]), 3) for i, _ in swappable]
|
| 207 |
+
cols = st.columns(widths, gap="small")
|
| 208 |
+
|
| 209 |
+
for col, (i, diag) in zip(cols, swappable):
|
| 210 |
was_changed = output_words[i] != original_words[i]
|
| 211 |
with col:
|
| 212 |
+
chip = (
|
| 213 |
+
f":green[**{output_words[i]}**] β"
|
| 214 |
+
if was_changed
|
| 215 |
+
else f":blue[**{output_words[i]}**]"
|
| 216 |
+
)
|
| 217 |
+
with st.popover(chip, use_container_width=True):
|
| 218 |
+
st.markdown(f"**`{diag.input_word}`** β pick alternative:")
|
| 219 |
+
for scored in diag.candidate_breakdown[:5]:
|
| 220 |
+
eng_tag = " π€" if scored.is_english else ""
|
| 221 |
+
is_sel = scored.text == output_words[i]
|
| 222 |
+
if st.button(
|
| 223 |
+
f"{'β
' if is_sel else ''}{scored.text}{eng_tag}",
|
| 224 |
+
key=f"alt_{i}_{scored.text}",
|
| 225 |
+
help=f"Score: {scored.combined_score:.2f}",
|
| 226 |
+
use_container_width=True,
|
| 227 |
+
type="primary" if is_sel else "secondary",
|
| 228 |
+
):
|
| 229 |
+
st.session_state["output_words"][i] = scored.text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 230 |
st.rerun()
|
| 231 |
+
st.markdown("---")
|
| 232 |
+
custom = st.text_input(
|
| 233 |
+
"Not listed? Type correct word:",
|
| 234 |
+
key=f"custom_{i}",
|
| 235 |
+
placeholder="Type Sinhala word",
|
| 236 |
+
)
|
| 237 |
+
if custom and st.button(
|
| 238 |
+
"Use this", key=f"custom_apply_{i}", use_container_width=True
|
| 239 |
+
):
|
| 240 |
+
st.session_state["output_words"][i] = custom
|
| 241 |
+
st.rerun()
|
| 242 |
|
| 243 |
# ββ Submit correction button (only when changes exist, once per result) ββ
|
| 244 |
# Guard key: (original sentence, original output) β stable regardless of swaps
|