Update modules/text_analysis/discourse_analysis.py
Browse files
modules/text_analysis/discourse_analysis.py
CHANGED
@@ -23,26 +23,6 @@ from .stopwords import (
|
|
23 |
get_stopwords_for_spacy
|
24 |
)
|
25 |
|
26 |
-
def fig_to_bytes(fig):
|
27 |
-
"""
|
28 |
-
Convierte una figura de matplotlib a bytes en formato PNG.
|
29 |
-
|
30 |
-
Args:
|
31 |
-
fig: Figura de matplotlib
|
32 |
-
|
33 |
-
Returns:
|
34 |
-
bytes: Representación en bytes de la figura en formato PNG
|
35 |
-
"""
|
36 |
-
try:
|
37 |
-
import io
|
38 |
-
buf = io.BytesIO()
|
39 |
-
fig.savefig(buf, format='png', dpi=100, bbox_inches='tight')
|
40 |
-
buf.seek(0)
|
41 |
-
return buf.getvalue()
|
42 |
-
except Exception as e:
|
43 |
-
logger.error(f"Error al convertir figura a bytes: {str(e)}")
|
44 |
-
return None
|
45 |
-
|
46 |
#####################
|
47 |
# Define colors for grammatical categories
|
48 |
POS_COLORS = {
|
@@ -184,13 +164,6 @@ def create_concept_table(key_concepts):
|
|
184 |
def perform_discourse_analysis(text1, text2, nlp, lang):
|
185 |
"""
|
186 |
Realiza el análisis completo del discurso
|
187 |
-
Args:
|
188 |
-
text1: Primer texto a analizar
|
189 |
-
text2: Segundo texto a analizar
|
190 |
-
nlp: Modelo de spaCy cargado
|
191 |
-
lang: Código de idioma
|
192 |
-
Returns:
|
193 |
-
dict: Resultados del análisis con gráficos convertidos a bytes
|
194 |
"""
|
195 |
try:
|
196 |
logger.info("Iniciando análisis del discurso...")
|
@@ -201,33 +174,27 @@ def perform_discourse_analysis(text1, text2, nlp, lang):
|
|
201 |
|
202 |
if not nlp:
|
203 |
raise ValueError("Modelo de lenguaje no inicializado")
|
204 |
-
|
205 |
# Realizar análisis comparativo
|
206 |
-
|
207 |
-
|
208 |
-
|
209 |
-
|
210 |
-
|
211 |
-
|
212 |
-
|
213 |
-
graph1_bytes = fig_to_bytes(fig1)
|
214 |
-
graph2_bytes = fig_to_bytes(fig2)
|
215 |
-
|
216 |
-
logger.info(f"Figura 1 convertida a {len(graph1_bytes) if graph1_bytes else 0} bytes")
|
217 |
-
logger.info(f"Figura 2 convertida a {len(graph2_bytes) if graph2_bytes else 0} bytes")
|
218 |
|
219 |
# Crear tablas de resultados
|
220 |
-
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
|
227 |
result = {
|
228 |
-
'graph1':
|
229 |
-
'graph2':
|
230 |
-
'combined_graph': None, # No hay gráfico combinado por ahora
|
231 |
'key_concepts1': key_concepts1,
|
232 |
'key_concepts2': key_concepts2,
|
233 |
'table1': table1,
|
@@ -235,20 +202,17 @@ def perform_discourse_analysis(text1, text2, nlp, lang):
|
|
235 |
'success': True
|
236 |
}
|
237 |
|
238 |
-
logger.info("Análisis del discurso completado
|
239 |
return result
|
240 |
|
241 |
except Exception as e:
|
242 |
logger.error(f"Error en perform_discourse_analysis: {str(e)}")
|
243 |
-
# Asegurar limpieza de recursos
|
244 |
-
plt.close('all')
|
245 |
return {
|
246 |
'success': False,
|
247 |
'error': str(e)
|
248 |
}
|
249 |
finally:
|
250 |
-
# Asegurar limpieza en todos los casos
|
251 |
-
plt.close('all')
|
252 |
|
253 |
#################################################################
|
254 |
def create_concept_table(key_concepts):
|
|
|
23 |
get_stopwords_for_spacy
|
24 |
)
|
25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
#####################
|
27 |
# Define colors for grammatical categories
|
28 |
POS_COLORS = {
|
|
|
164 |
def perform_discourse_analysis(text1, text2, nlp, lang):
|
165 |
"""
|
166 |
Realiza el análisis completo del discurso
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
167 |
"""
|
168 |
try:
|
169 |
logger.info("Iniciando análisis del discurso...")
|
|
|
174 |
|
175 |
if not nlp:
|
176 |
raise ValueError("Modelo de lenguaje no inicializado")
|
177 |
+
|
178 |
# Realizar análisis comparativo
|
179 |
+
try:
|
180 |
+
fig1, fig2, key_concepts1, key_concepts2 = compare_semantic_analysis(
|
181 |
+
text1, text2, nlp, lang
|
182 |
+
)
|
183 |
+
except Exception as e:
|
184 |
+
logger.error(f"Error en el análisis comparativo: {str(e)}")
|
185 |
+
raise
|
|
|
|
|
|
|
|
|
|
|
186 |
|
187 |
# Crear tablas de resultados
|
188 |
+
try:
|
189 |
+
table1 = create_concept_table(key_concepts1)
|
190 |
+
table2 = create_concept_table(key_concepts2)
|
191 |
+
except Exception as e:
|
192 |
+
logger.error(f"Error creando tablas de conceptos: {str(e)}")
|
193 |
+
raise
|
194 |
|
195 |
result = {
|
196 |
+
'graph1': fig1,
|
197 |
+
'graph2': fig2,
|
|
|
198 |
'key_concepts1': key_concepts1,
|
199 |
'key_concepts2': key_concepts2,
|
200 |
'table1': table1,
|
|
|
202 |
'success': True
|
203 |
}
|
204 |
|
205 |
+
logger.info("Análisis del discurso completado exitosamente")
|
206 |
return result
|
207 |
|
208 |
except Exception as e:
|
209 |
logger.error(f"Error en perform_discourse_analysis: {str(e)}")
|
|
|
|
|
210 |
return {
|
211 |
'success': False,
|
212 |
'error': str(e)
|
213 |
}
|
214 |
finally:
|
215 |
+
plt.close('all') # Asegurar limpieza en todos los casos
|
|
|
216 |
|
217 |
#################################################################
|
218 |
def create_concept_table(key_concepts):
|