Update modules/morphosyntax/morphosyntax_interface.py
Browse files
modules/morphosyntax/morphosyntax_interface.py
CHANGED
@@ -42,216 +42,184 @@ import base64
|
|
42 |
import re
|
43 |
|
44 |
############################################################################
|
|
|
45 |
def display_morphosyntax_interface(lang_code, nlp_models, morpho_t):
|
46 |
try:
|
47 |
-
# CSS
|
48 |
st.markdown("""
|
49 |
<style>
|
50 |
.stTextArea textarea {
|
51 |
font-size: 1rem;
|
52 |
line-height: 1.5;
|
53 |
-
padding: 0.5rem;
|
54 |
-
border-radius: 0.375rem;
|
55 |
-
border: 1px solid #e2e8f0;
|
56 |
-
background-color: white;
|
57 |
min-height: 100px !important;
|
58 |
height: 100px !important;
|
59 |
}
|
60 |
.arc-diagram-container {
|
61 |
width: 100%;
|
62 |
-
overflow-x: auto;
|
63 |
-
background-color: white;
|
64 |
padding: 0.5rem;
|
65 |
-
border-radius: 0.375rem;
|
66 |
-
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
67 |
margin: 0.5rem 0;
|
68 |
}
|
69 |
-
.stTab {
|
70 |
-
position: sticky;
|
71 |
-
top: 0;
|
72 |
-
z-index: 999;
|
73 |
-
}
|
74 |
-
[data-testid="stVerticalBlock"] {
|
75 |
-
gap: 0 !important;
|
76 |
-
}
|
77 |
</style>
|
78 |
""", unsafe_allow_html=True)
|
79 |
|
80 |
-
#
|
81 |
-
if '
|
82 |
-
st.session_state.
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
'original_analysis': None,
|
90 |
-
'current_text': '',
|
91 |
-
'current_analysis': None,
|
92 |
-
'analysis_count': 0,
|
93 |
-
'show_original': False,
|
94 |
-
'show_iteration': False
|
95 |
-
}
|
96 |
-
|
97 |
-
for key, default_value in default_state.items():
|
98 |
-
if key not in st.session_state.morphosyntax_state:
|
99 |
-
st.session_state.morphosyntax_state[key] = default_value
|
100 |
-
|
101 |
-
def update_analysis():
|
102 |
-
st.session_state.current_tab = "morphosyntax" # Mantener tab activo
|
103 |
|
104 |
-
#
|
105 |
-
st.
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
height=100
|
111 |
-
on_change=update_analysis
|
112 |
)
|
113 |
|
|
|
114 |
col1, col2, col3 = st.columns([2,1,2])
|
115 |
with col1:
|
116 |
-
|
117 |
"Analizar Texto Original",
|
118 |
type="primary",
|
119 |
-
use_container_width=True
|
120 |
-
disabled=not bool(st.session_state.get("original_text_input", "").strip())
|
121 |
)
|
122 |
|
123 |
-
# Procesar
|
124 |
-
if
|
125 |
try:
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
)
|
132 |
-
|
133 |
-
st.session_state.morphosyntax_state.update({
|
134 |
-
'original_text': st.session_state.original_text_input,
|
135 |
-
'original_analysis': {'doc': doc, 'analysis': analysis},
|
136 |
-
'current_text': st.session_state.original_text_input,
|
137 |
-
'show_original': True,
|
138 |
-
'analysis_count': st.session_state.morphosyntax_state['analysis_count'] + 1
|
139 |
-
})
|
140 |
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
|
|
|
|
147 |
|
148 |
-
|
149 |
-
|
150 |
-
|
|
|
|
|
|
|
|
|
|
|
151 |
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
morpho_t
|
158 |
-
)
|
159 |
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
placeholder="Modifique el texto para mejorar la estructura",
|
168 |
-
height=100,
|
169 |
-
on_change=update_analysis
|
170 |
-
)
|
171 |
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
"Analizar Cambios",
|
176 |
-
type="primary",
|
177 |
-
use_container_width=True,
|
178 |
-
disabled=not bool(iteration_text.strip())
|
179 |
-
)
|
180 |
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
with st.spinner(""): # Spinner sin texto
|
185 |
-
doc = nlp_models[lang_code](iteration_text)
|
186 |
-
analysis = perform_advanced_morphosyntactic_analysis(
|
187 |
-
iteration_text,
|
188 |
-
nlp_models[lang_code]
|
189 |
-
)
|
190 |
-
|
191 |
-
st.session_state.morphosyntax_state.update({
|
192 |
-
'current_text': iteration_text,
|
193 |
-
'current_analysis': {'doc': doc, 'analysis': analysis},
|
194 |
-
'show_iteration': True,
|
195 |
-
'analysis_count': st.session_state.morphosyntax_state['analysis_count'] + 1
|
196 |
-
})
|
197 |
-
|
198 |
-
if store_student_morphosyntax_result(
|
199 |
-
username=st.session_state.username,
|
200 |
-
text=iteration_text,
|
201 |
-
arc_diagrams=analysis['arc_diagrams']
|
202 |
-
):
|
203 |
-
# Actualizar solo el diagrama de iteraci贸n
|
204 |
-
display_morphosyntax_results(
|
205 |
-
st.session_state.morphosyntax_state['current_analysis'],
|
206 |
-
lang_code,
|
207 |
-
morpho_t
|
208 |
-
)
|
209 |
-
|
210 |
-
except Exception as e:
|
211 |
-
logger.error(f"Error en iteraci贸n: {str(e)}")
|
212 |
-
st.error("Error al procesar los cambios")
|
213 |
|
214 |
except Exception as e:
|
215 |
-
|
216 |
-
|
217 |
-
|
218 |
-
|
219 |
-
|
220 |
|
221 |
#########################################################################3
|
|
|
222 |
def display_morphosyntax_results(result, lang_code, morpho_t):
|
223 |
"""
|
224 |
Muestra solo el an谩lisis sint谩ctico con diagramas de arco.
|
|
|
|
|
|
|
|
|
225 |
"""
|
226 |
if result is None:
|
227 |
-
|
228 |
return
|
229 |
|
230 |
-
|
231 |
-
|
232 |
-
|
233 |
-
|
234 |
-
|
235 |
-
|
236 |
-
sentences = list(doc.sents)
|
237 |
-
for i, sent in enumerate(sentences):
|
238 |
-
with st.container():
|
239 |
-
st.subheader(f"{morpho_t.get('sentence', 'Sentence')} {i+1}")
|
240 |
try:
|
|
|
|
|
|
|
241 |
html = displacy.render(sent, style="dep", options={
|
242 |
"distance": 100,
|
243 |
"arrow_spacing": 20,
|
244 |
"word_spacing": 30
|
245 |
})
|
|
|
246 |
# Ajustar dimensiones del SVG
|
247 |
html = html.replace('height="375"', 'height="200"')
|
248 |
-
html = re.sub(r'<svg[^>]*>',
|
|
|
|
|
249 |
html = re.sub(r'<g [^>]*transform="translate\((\d+),(\d+)\)"',
|
250 |
-
lambda m: f'<g transform="translate({m.group(1)},50)"',
|
|
|
251 |
|
252 |
-
# Envolver en
|
253 |
html = f'<div class="arc-diagram-container">{html}</div>'
|
|
|
|
|
254 |
st.write(html, unsafe_allow_html=True)
|
|
|
255 |
except Exception as e:
|
256 |
-
logger.error(f"Error
|
257 |
-
|
|
|
|
|
|
|
|
|
|
42 |
import re
|
43 |
|
44 |
############################################################################
|
45 |
+
|
46 |
def display_morphosyntax_interface(lang_code, nlp_models, morpho_t):
|
47 |
try:
|
48 |
+
# CSS para layout estable
|
49 |
st.markdown("""
|
50 |
<style>
|
51 |
.stTextArea textarea {
|
52 |
font-size: 1rem;
|
53 |
line-height: 1.5;
|
|
|
|
|
|
|
|
|
54 |
min-height: 100px !important;
|
55 |
height: 100px !important;
|
56 |
}
|
57 |
.arc-diagram-container {
|
58 |
width: 100%;
|
|
|
|
|
59 |
padding: 0.5rem;
|
|
|
|
|
60 |
margin: 0.5rem 0;
|
61 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
</style>
|
63 |
""", unsafe_allow_html=True)
|
64 |
|
65 |
+
# Estado consistente con keys 煤nicas
|
66 |
+
if 'morpho_state' not in st.session_state:
|
67 |
+
st.session_state.morpho_state = {
|
68 |
+
'original_text': '',
|
69 |
+
'original_analysis': None,
|
70 |
+
'iteration_text': '',
|
71 |
+
'iteration_analysis': None,
|
72 |
+
'analysis_count': 0
|
73 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
+
# Texto original - Key 煤nica basada en timestamp
|
76 |
+
text_key = f"original_text_{st.session_state.morpho_state['analysis_count']}"
|
77 |
+
text_input = st.text_area(
|
78 |
+
"",
|
79 |
+
value=st.session_state.morpho_state['original_text'],
|
80 |
+
key=text_key,
|
81 |
+
height=100
|
|
|
82 |
)
|
83 |
|
84 |
+
# Bot贸n analizar original
|
85 |
col1, col2, col3 = st.columns([2,1,2])
|
86 |
with col1:
|
87 |
+
analyze_button = st.button(
|
88 |
"Analizar Texto Original",
|
89 |
type="primary",
|
90 |
+
use_container_width=True
|
|
|
91 |
)
|
92 |
|
93 |
+
# Procesar texto original
|
94 |
+
if analyze_button and text_input.strip():
|
95 |
try:
|
96 |
+
doc = nlp_models[lang_code](text_input)
|
97 |
+
analysis = perform_advanced_morphosyntactic_analysis(
|
98 |
+
text_input,
|
99 |
+
nlp_models[lang_code]
|
100 |
+
)
|
101 |
+
|
102 |
+
# Actualizar estado
|
103 |
+
st.session_state.morpho_state.update({
|
104 |
+
'original_text': text_input,
|
105 |
+
'original_analysis': {'doc': doc, 'analysis': analysis},
|
106 |
+
'iteration_text': text_input,
|
107 |
+
'analysis_count': st.session_state.morpho_state['analysis_count'] + 1
|
108 |
+
})
|
109 |
+
|
110 |
+
# Guardar en base de datos
|
111 |
+
if store_student_morphosyntax_result(
|
112 |
+
username=st.session_state.username,
|
113 |
+
text=text_input,
|
114 |
+
arc_diagrams=analysis['arc_diagrams']
|
115 |
+
):
|
116 |
+
# Mostrar diagrama original
|
117 |
+
display_arc_diagram(doc, morpho_t)
|
118 |
+
|
119 |
+
st.markdown("---")
|
120 |
+
|
121 |
+
# Campo para iteraci贸n
|
122 |
+
iteration_key = f"iteration_{st.session_state.morpho_state['analysis_count']}"
|
123 |
+
iteration_text = st.text_area(
|
124 |
+
"",
|
125 |
+
value=st.session_state.morpho_state['iteration_text'],
|
126 |
+
key=iteration_key,
|
127 |
+
height=100
|
128 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
|
130 |
+
# Bot贸n analizar iteraci贸n
|
131 |
+
col1, col2, col3 = st.columns([2,1,2])
|
132 |
+
with col1:
|
133 |
+
iterate_button = st.button(
|
134 |
+
"Analizar Cambios",
|
135 |
+
type="primary",
|
136 |
+
use_container_width=True
|
137 |
+
)
|
138 |
|
139 |
+
# Procesar iteraci贸n
|
140 |
+
if iterate_button and iteration_text.strip():
|
141 |
+
try:
|
142 |
+
doc = nlp_models[lang_code](iteration_text)
|
143 |
+
analysis = perform_advanced_morphosyntactic_analysis(
|
144 |
+
iteration_text,
|
145 |
+
nlp_models[lang_code]
|
146 |
+
)
|
147 |
|
148 |
+
# Actualizar estado
|
149 |
+
st.session_state.morpho_state.update({
|
150 |
+
'iteration_text': iteration_text,
|
151 |
+
'iteration_analysis': {'doc': doc, 'analysis': analysis}
|
152 |
+
})
|
|
|
|
|
153 |
|
154 |
+
# Guardar y mostrar
|
155 |
+
if store_student_morphosyntax_result(
|
156 |
+
username=st.session_state.username,
|
157 |
+
text=iteration_text,
|
158 |
+
arc_diagrams=analysis['arc_diagrams']
|
159 |
+
):
|
160 |
+
display_arc_diagram(doc, morpho_t)
|
|
|
|
|
|
|
|
|
161 |
|
162 |
+
except Exception as e:
|
163 |
+
st.error("Error al procesar iteraci贸n")
|
164 |
+
logger.error(f"Error: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
165 |
|
166 |
+
except Exception as e:
|
167 |
+
st.error("Error al procesar texto original")
|
168 |
+
logger.error(f"Error: {str(e)}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
169 |
|
170 |
except Exception as e:
|
171 |
+
st.error("Error general en la interfaz")
|
172 |
+
logger.error(f"Error: {str(e)}")
|
|
|
|
|
|
|
173 |
|
174 |
#########################################################################3
|
175 |
+
|
176 |
def display_morphosyntax_results(result, lang_code, morpho_t):
|
177 |
"""
|
178 |
Muestra solo el an谩lisis sint谩ctico con diagramas de arco.
|
179 |
+
Args:
|
180 |
+
result: Resultado del an谩lisis que contiene el documento procesado
|
181 |
+
lang_code: C贸digo del idioma actual
|
182 |
+
morpho_t: Diccionario de traducciones
|
183 |
"""
|
184 |
if result is None:
|
185 |
+
logger.warning("No hay resultados para mostrar")
|
186 |
return
|
187 |
|
188 |
+
try:
|
189 |
+
doc = result['doc']
|
190 |
+
|
191 |
+
with st.container():
|
192 |
+
sentences = list(doc.sents)
|
193 |
+
for i, sent in enumerate(sentences):
|
|
|
|
|
|
|
|
|
194 |
try:
|
195 |
+
st.subheader(f"{morpho_t.get('sentence', 'Sentence')} {i+1}")
|
196 |
+
|
197 |
+
# Renderizar con opciones optimizadas
|
198 |
html = displacy.render(sent, style="dep", options={
|
199 |
"distance": 100,
|
200 |
"arrow_spacing": 20,
|
201 |
"word_spacing": 30
|
202 |
})
|
203 |
+
|
204 |
# Ajustar dimensiones del SVG
|
205 |
html = html.replace('height="375"', 'height="200"')
|
206 |
+
html = re.sub(r'<svg[^>]*>',
|
207 |
+
lambda m: m.group(0).replace('height="450"', 'height="300"'),
|
208 |
+
html)
|
209 |
html = re.sub(r'<g [^>]*transform="translate\((\d+),(\d+)\)"',
|
210 |
+
lambda m: f'<g transform="translate({m.group(1)},50)"',
|
211 |
+
html)
|
212 |
|
213 |
+
# Envolver en contenedor con clase CSS
|
214 |
html = f'<div class="arc-diagram-container">{html}</div>'
|
215 |
+
|
216 |
+
# Mostrar diagrama
|
217 |
st.write(html, unsafe_allow_html=True)
|
218 |
+
|
219 |
except Exception as e:
|
220 |
+
logger.error(f"Error al renderizar oraci贸n {i}: {str(e)}")
|
221 |
+
continue # Continuar con la siguiente oraci贸n si hay error
|
222 |
+
|
223 |
+
except Exception as e:
|
224 |
+
logger.error(f"Error general en display_morphosyntax_results: {str(e)}")
|
225 |
+
|