AIdeaText commited on
Commit
69ed6a9
verified
1 Parent(s): 96c0e0b

Update modules/morphosyntax/morphosyntax_interface.py

Browse files
modules/morphosyntax/morphosyntax_interface.py CHANGED
@@ -5,12 +5,9 @@ import re
5
  import logging
6
  from spacy import displacy
7
 
8
- # Importa tu pipeline de spacy, por ejemplo:
9
- # nlp_models = {"es": spacy.load("es_core_news_sm")}
10
-
11
- # Supongamos que estas funciones existen en tus otros m贸dulos:
12
  from ..morphosyntax.morphosyntax_process import perform_advanced_morphosyntactic_analysis
13
-
14
  from ..database.morphosyntax_iterative_mongo_db import (
15
  store_student_morphosyntax_base,
16
  store_student_morphosyntax_iteration,
@@ -25,11 +22,11 @@ def initialize_arc_analysis_state():
25
  """
26
  if "arc_analysis_state" not in st.session_state:
27
  st.session_state.arc_analysis_state = {
28
- "base_id": None, # ObjectId del documento base
29
- "base_text": "", # Texto inicial
30
- "base_diagram": None, # HTML del diagrama base
31
- "iteration_text": "", # Texto de iteraci贸n
32
- "iteration_diagram": None, # HTML del diagrama de iteraci贸n
33
  }
34
  logger.info("Estado de an谩lisis de arcos inicializado.")
35
 
@@ -45,13 +42,14 @@ def reset_arc_analysis_state():
45
  "iteration_text": "",
46
  "iteration_diagram": None,
47
  }
 
48
 
49
  ###########################################################################
50
  def display_arc_diagram(doc):
51
  """
52
  Genera y retorna el HTML del diagrama de arco para un `Doc` de spaCy.
53
- No imprime directamente en pantalla; regresa el HTML para que se
54
- renderice con `st.write(..., unsafe_allow_html=True)`.
55
  """
56
  try:
57
  diagram_html = ""
@@ -62,10 +60,10 @@ def display_arc_diagram(doc):
62
  options={
63
  "distance": 100,
64
  "arrow_spacing": 20,
65
- "word_spacing": 30,
66
  }
67
  )
68
- # Ajustar tama帽o y posici贸n
69
  svg_html = svg_html.replace('height="375"', 'height="200"')
70
  svg_html = re.sub(
71
  r'<svg[^>]*>',
@@ -77,9 +75,10 @@ def display_arc_diagram(doc):
77
  lambda m: f'<g transform="translate({m.group(1)},50)"',
78
  svg_html
79
  )
80
- # Envolver en contenedor con estilo
81
  diagram_html += f'<div class="arc-diagram-container">{svg_html}</div>'
82
  return diagram_html
 
83
  except Exception as e:
84
  logger.error(f"Error en display_arc_diagram: {str(e)}")
85
  return "<p style='color:red;'>Error generando diagrama</p>"
@@ -90,7 +89,7 @@ def display_morphosyntax_interface(lang_code, nlp_models, morpho_t):
90
  Interfaz principal para la visualizaci贸n de diagramas de arco
91
  (Texto Base vs Iteraciones).
92
  """
93
- # CSS para layout estable
94
  st.markdown("""
95
  <style>
96
  .stTextArea textarea {
@@ -113,31 +112,33 @@ def display_morphosyntax_interface(lang_code, nlp_models, morpho_t):
113
  </style>
114
  """, unsafe_allow_html=True)
115
 
116
- # Inicializar el estado si no existe
117
  initialize_arc_analysis_state()
118
- arc_state = st.session_state.arc_analysis_state # Para abreviar
119
 
120
- # Creamos pesta帽as: "Texto Base" y "Iteraciones"
121
  tabs = st.tabs(["Texto Base", "Iteraciones"])
122
 
123
- # ------------------- PESTA脩A 1: Texto Base --------------------------
124
  with tabs[0]:
125
  st.subheader("An谩lisis de Texto Base")
126
 
127
- # Bot贸n para resetear todo
128
  if st.button("Nuevo An谩lisis", key="btn_reset_base"):
 
 
 
129
  reset_arc_analysis_state()
130
- st.experimental_rerun()
131
 
132
- # Input para texto base
133
  arc_state["base_text"] = st.text_area(
134
  "Ingrese su texto inicial",
135
  value=arc_state["base_text"],
136
  key="base_text_input",
137
- height=120
138
  )
139
 
140
- # Bot贸n de an谩lisis base
141
  if st.button("Analizar Texto Base", key="btn_analyze_base"):
142
  if not arc_state["base_text"].strip():
143
  st.warning("Ingrese un texto para analizar.")
@@ -146,10 +147,10 @@ def display_morphosyntax_interface(lang_code, nlp_models, morpho_t):
146
  # Procesar con spaCy
147
  doc = nlp_models[lang_code](arc_state["base_text"])
148
  # Generar HTML del arco
149
- base_arc_html = display_arc_diagram(doc)
150
- arc_state["base_diagram"] = base_arc_html
151
 
152
- # Guardar en BD usando tu an谩lisis avanzado
153
  analysis = perform_advanced_morphosyntactic_analysis(
154
  arc_state["base_text"],
155
  nlp_models[lang_code]
@@ -161,45 +162,43 @@ def display_morphosyntax_interface(lang_code, nlp_models, morpho_t):
161
  )
162
  if base_id:
163
  arc_state["base_id"] = base_id
164
- st.success(f"An谩lisis base guardado con ID: {base_id}")
165
 
166
- except Exception as e:
167
  st.error("Error procesando texto base")
168
- logger.error(f"Error en an谩lisis base: {str(e)}")
169
 
170
- # Mostrar el arco base, si existe
171
  if arc_state["base_diagram"]:
172
  st.markdown("<hr class='divider'>", unsafe_allow_html=True)
173
  st.markdown("#### Diagrama de Arco (Texto Base)")
174
  st.write(arc_state["base_diagram"], unsafe_allow_html=True)
175
 
176
- # ------------------- PESTA脩A 2: Iteraciones -------------------------
177
  with tabs[1]:
178
  st.subheader("An谩lisis de Cambios / Iteraciones")
179
 
180
- # Si no se ha analizado nada en la primera pesta帽a
181
  if not arc_state["base_id"]:
182
  st.info("Primero analiza un texto base en la pesta帽a anterior.")
183
  return
184
 
185
- # Mostrar el texto base como referencia, en modo solo lectura
186
  st.text_area(
187
  "Texto Base (solo lectura)",
188
  value=arc_state["base_text"],
189
- key="base_text_ref",
190
  height=80,
191
  disabled=True
192
  )
193
 
194
- # Texto de iteraci贸n
195
  arc_state["iteration_text"] = st.text_area(
196
  "Texto de Iteraci贸n",
197
  value=arc_state["iteration_text"],
198
- key="iteration_text_input",
199
- height=120
200
  )
201
 
202
- # Bot贸n para analizar iteraci贸n
203
  if st.button("Analizar Cambios", key="btn_analyze_iteration"):
204
  if not arc_state["iteration_text"].strip():
205
  st.warning("Ingrese texto de iteraci贸n.")
@@ -207,11 +206,11 @@ def display_morphosyntax_interface(lang_code, nlp_models, morpho_t):
207
  try:
208
  # Procesar con spaCy
209
  doc_iter = nlp_models[lang_code](arc_state["iteration_text"])
210
- iteration_arc_html = display_arc_diagram(doc_iter)
211
- arc_state["iteration_diagram"] = iteration_arc_html
212
 
213
- # Guardar en BD
214
- iteration_analysis = perform_advanced_morphosyntactic_analysis(
215
  arc_state["iteration_text"],
216
  nlp_models[lang_code]
217
  )
@@ -220,29 +219,29 @@ def display_morphosyntax_interface(lang_code, nlp_models, morpho_t):
220
  base_id=arc_state["base_id"],
221
  original_text=arc_state["base_text"],
222
  iteration_text=arc_state["iteration_text"],
223
- arc_diagrams=iteration_analysis["arc_diagrams"]
224
  )
225
  if iteration_id:
226
- st.success(f"Iteraci贸n guardada con ID: {iteration_id}")
227
 
228
- except Exception as e:
229
  st.error("Error procesando iteraci贸n")
230
- logger.error(f"Error en iteraci贸n: {str(e)}")
231
 
232
- # Mostrar el arco de iteraci贸n, si existe
233
  if arc_state["iteration_diagram"]:
234
  st.markdown("<hr class='divider'>", unsafe_allow_html=True)
235
  st.markdown("#### Diagrama de Arco (Iteraci贸n)")
236
  st.write(arc_state["iteration_diagram"], unsafe_allow_html=True)
237
 
238
- # Mostrar comparaci贸n (opcional)
239
  if arc_state["base_diagram"] and arc_state["iteration_diagram"]:
240
  st.markdown("<hr class='divider'>", unsafe_allow_html=True)
241
- st.markdown("### Comparaci贸n de Diagrama Base vs. Iteraci贸n")
242
- col_base, col_iter = st.columns(2)
243
- with col_base:
244
- st.markdown("**Diagrama Base**")
245
- st.write(arc_state["base_diagram"], unsafe_allow_html=True)
246
- with col_iter:
247
- st.markdown("**Diagrama Iterado**")
248
- st.write(arc_state["iteration_diagram"], unsafe_allow_html=True)
 
5
  import logging
6
  from spacy import displacy
7
 
8
+ # Se asume que la funci贸n perform_advanced_morphosyntactic_analysis
9
+ # y los m茅todos store_student_morphosyntax_base/iteration existen.
 
 
10
  from ..morphosyntax.morphosyntax_process import perform_advanced_morphosyntactic_analysis
 
11
  from ..database.morphosyntax_iterative_mongo_db import (
12
  store_student_morphosyntax_base,
13
  store_student_morphosyntax_iteration,
 
22
  """
23
  if "arc_analysis_state" not in st.session_state:
24
  st.session_state.arc_analysis_state = {
25
+ "base_id": None,
26
+ "base_text": "",
27
+ "base_diagram": None,
28
+ "iteration_text": "",
29
+ "iteration_diagram": None,
30
  }
31
  logger.info("Estado de an谩lisis de arcos inicializado.")
32
 
 
42
  "iteration_text": "",
43
  "iteration_diagram": None,
44
  }
45
+ logger.info("Estado de arcos reseteado.")
46
 
47
  ###########################################################################
48
  def display_arc_diagram(doc):
49
  """
50
  Genera y retorna el HTML del diagrama de arco para un `Doc` de spaCy.
51
+ No imprime directamente en pantalla; regresa el HTML para
52
+ usar con `st.write(..., unsafe_allow_html=True)`.
53
  """
54
  try:
55
  diagram_html = ""
 
60
  options={
61
  "distance": 100,
62
  "arrow_spacing": 20,
63
+ "word_spacing": 30
64
  }
65
  )
66
+ # Ajustar tama帽os
67
  svg_html = svg_html.replace('height="375"', 'height="200"')
68
  svg_html = re.sub(
69
  r'<svg[^>]*>',
 
75
  lambda m: f'<g transform="translate({m.group(1)},50)"',
76
  svg_html
77
  )
78
+ # Envolver en contenedor
79
  diagram_html += f'<div class="arc-diagram-container">{svg_html}</div>'
80
  return diagram_html
81
+
82
  except Exception as e:
83
  logger.error(f"Error en display_arc_diagram: {str(e)}")
84
  return "<p style='color:red;'>Error generando diagrama</p>"
 
89
  Interfaz principal para la visualizaci贸n de diagramas de arco
90
  (Texto Base vs Iteraciones).
91
  """
92
+ # CSS para layout vertical y estable
93
  st.markdown("""
94
  <style>
95
  .stTextArea textarea {
 
112
  </style>
113
  """, unsafe_allow_html=True)
114
 
115
+ # 1) Inicializar estados
116
  initialize_arc_analysis_state()
117
+ arc_state = st.session_state.arc_analysis_state
118
 
119
+ # 2) Creamos pesta帽as: "Texto Base" y "Iteraciones"
120
  tabs = st.tabs(["Texto Base", "Iteraciones"])
121
 
122
+ # =================== PESTA脩A 1: Texto Base ==========================
123
  with tabs[0]:
124
  st.subheader("An谩lisis de Texto Base")
125
 
126
+ # Bot贸n para iniciar nuevo an谩lisis
127
  if st.button("Nuevo An谩lisis", key="btn_reset_base"):
128
+ # Solo limpiamos el estado; si requieres forzar reload,
129
+ # descomenta la siguiente l铆nea:
130
+ # st.experimental_rerun()
131
  reset_arc_analysis_state()
 
132
 
133
+ # Textarea de texto base
134
  arc_state["base_text"] = st.text_area(
135
  "Ingrese su texto inicial",
136
  value=arc_state["base_text"],
137
  key="base_text_input",
138
+ height=150
139
  )
140
 
141
+ # Bot贸n para analizar texto base
142
  if st.button("Analizar Texto Base", key="btn_analyze_base"):
143
  if not arc_state["base_text"].strip():
144
  st.warning("Ingrese un texto para analizar.")
 
147
  # Procesar con spaCy
148
  doc = nlp_models[lang_code](arc_state["base_text"])
149
  # Generar HTML del arco
150
+ arc_html = display_arc_diagram(doc)
151
+ arc_state["base_diagram"] = arc_html
152
 
153
+ # Guardar en Mongo
154
  analysis = perform_advanced_morphosyntactic_analysis(
155
  arc_state["base_text"],
156
  nlp_models[lang_code]
 
162
  )
163
  if base_id:
164
  arc_state["base_id"] = base_id
165
+ st.success(f"An谩lisis base guardado. ID: {base_id}")
166
 
167
+ except Exception as exc:
168
  st.error("Error procesando texto base")
169
+ logger.error(f"Error en an谩lisis base: {str(exc)}")
170
 
171
+ # Mostrar diagrama base
172
  if arc_state["base_diagram"]:
173
  st.markdown("<hr class='divider'>", unsafe_allow_html=True)
174
  st.markdown("#### Diagrama de Arco (Texto Base)")
175
  st.write(arc_state["base_diagram"], unsafe_allow_html=True)
176
 
177
+ # ================== PESTA脩A 2: Iteraciones ==========================
178
  with tabs[1]:
179
  st.subheader("An谩lisis de Cambios / Iteraciones")
180
 
181
+ # Verificar que exista texto base analizado
182
  if not arc_state["base_id"]:
183
  st.info("Primero analiza un texto base en la pesta帽a anterior.")
184
  return
185
 
186
+ # Mostrar texto base como referencia (solo lectura)
187
  st.text_area(
188
  "Texto Base (solo lectura)",
189
  value=arc_state["base_text"],
 
190
  height=80,
191
  disabled=True
192
  )
193
 
194
+ # Caja de texto para la iteraci贸n
195
  arc_state["iteration_text"] = st.text_area(
196
  "Texto de Iteraci贸n",
197
  value=arc_state["iteration_text"],
198
+ height=150
 
199
  )
200
 
201
+ # Bot贸n analizar iteraci贸n
202
  if st.button("Analizar Cambios", key="btn_analyze_iteration"):
203
  if not arc_state["iteration_text"].strip():
204
  st.warning("Ingrese texto de iteraci贸n.")
 
206
  try:
207
  # Procesar con spaCy
208
  doc_iter = nlp_models[lang_code](arc_state["iteration_text"])
209
+ arc_html_iter = display_arc_diagram(doc_iter)
210
+ arc_state["iteration_diagram"] = arc_html_iter
211
 
212
+ # Guardar en Mongo
213
+ analysis_iter = perform_advanced_morphosyntactic_analysis(
214
  arc_state["iteration_text"],
215
  nlp_models[lang_code]
216
  )
 
219
  base_id=arc_state["base_id"],
220
  original_text=arc_state["base_text"],
221
  iteration_text=arc_state["iteration_text"],
222
+ arc_diagrams=analysis_iter["arc_diagrams"]
223
  )
224
  if iteration_id:
225
+ st.success(f"Iteraci贸n guardada. ID: {iteration_id}")
226
 
227
+ except Exception as exc:
228
  st.error("Error procesando iteraci贸n")
229
+ logger.error(f"Error en iteraci贸n: {str(exc)}")
230
 
231
+ # Mostrar diagrama de iteraci贸n
232
  if arc_state["iteration_diagram"]:
233
  st.markdown("<hr class='divider'>", unsafe_allow_html=True)
234
  st.markdown("#### Diagrama de Arco (Iteraci贸n)")
235
  st.write(arc_state["iteration_diagram"], unsafe_allow_html=True)
236
 
237
+ # Comparaci贸n vertical (uno abajo del otro)
238
  if arc_state["base_diagram"] and arc_state["iteration_diagram"]:
239
  st.markdown("<hr class='divider'>", unsafe_allow_html=True)
240
+ st.markdown("### Comparaci贸n Vertical: Base vs. Iteraci贸n")
241
+
242
+ st.markdown("**Diagrama Base**")
243
+ st.write(arc_state["base_diagram"], unsafe_allow_html=True)
244
+
245
+ st.markdown("---")
246
+ st.markdown("**Diagrama Iterado**")
247
+ st.write(arc_state["iteration_diagram"], unsafe_allow_html=True)