Spaces:
Sleeping
Sleeping
Update modules/text_analysis/semantic_analysis.py
Browse files
modules/text_analysis/semantic_analysis.py
CHANGED
@@ -82,6 +82,7 @@ ENTITY_LABELS = {
|
|
82 |
}
|
83 |
}
|
84 |
|
|
|
85 |
def fig_to_bytes(fig):
|
86 |
"""Convierte una figura de matplotlib a bytes."""
|
87 |
try:
|
@@ -282,10 +283,9 @@ def create_concept_graph(doc, key_concepts):
|
|
282 |
return nx.Graph()
|
283 |
|
284 |
###############################################################################
|
285 |
-
|
286 |
def visualize_concept_graph(G, lang_code):
|
287 |
try:
|
288 |
-
# 1. Diccionario de traducciones
|
289 |
GRAPH_LABELS = {
|
290 |
'es': {
|
291 |
'concept_network': 'Relaciones entre conceptos clave',
|
@@ -305,59 +305,42 @@ def visualize_concept_graph(G, lang_code):
|
|
305 |
}
|
306 |
}
|
307 |
|
308 |
-
# 2. Obtener
|
309 |
-
|
310 |
|
311 |
-
#
|
312 |
fig, ax = plt.subplots(figsize=(15, 10))
|
313 |
|
314 |
if not G.nodes():
|
315 |
logger.warning("Grafo vacío, retornando figura vacía")
|
316 |
return fig
|
317 |
|
318 |
-
# Convertir grafo
|
319 |
DG = nx.DiGraph(G)
|
320 |
-
|
321 |
-
# Calcular centralidad de los nodos para el color
|
322 |
centrality = nx.degree_centrality(G)
|
323 |
|
324 |
-
#
|
325 |
-
|
326 |
|
327 |
-
#
|
328 |
-
pos = nx.spring_layout(
|
329 |
-
DG,
|
330 |
-
k=2, # Distancia ideal entre nodos
|
331 |
-
iterations=50, # Número de iteraciones
|
332 |
-
seed=seed # Semilla fija para reproducibilidad
|
333 |
-
)
|
334 |
-
|
335 |
-
# Calcular factor de escala basado en número de nodos
|
336 |
num_nodes = len(DG.nodes())
|
337 |
scale_factor = 1000 if num_nodes < 10 else 500 if num_nodes < 20 else 200
|
338 |
-
|
339 |
-
|
340 |
-
node_weights = [DG.nodes[node].get('weight', 1) * scale_factor for node in DG.nodes()]
|
341 |
-
edge_weights = [DG[u][v].get('weight', 1) for u, v in DG.edges()]
|
342 |
-
|
343 |
-
# Crear mapa de colores basado en centralidad
|
344 |
node_colors = [plt.cm.viridis(centrality[node]) for node in DG.nodes()]
|
345 |
|
346 |
-
# Dibujar
|
347 |
-
|
348 |
-
DG,
|
349 |
-
|
350 |
-
node_size=node_weights,
|
351 |
node_color=node_colors,
|
352 |
alpha=0.7,
|
353 |
ax=ax
|
354 |
)
|
355 |
|
356 |
-
|
357 |
-
|
358 |
-
|
359 |
-
pos,
|
360 |
-
width=edge_weights,
|
361 |
alpha=0.6,
|
362 |
edge_color='gray',
|
363 |
arrows=True,
|
@@ -367,42 +350,34 @@ def visualize_concept_graph(G, lang_code):
|
|
367 |
ax=ax
|
368 |
)
|
369 |
|
370 |
-
#
|
371 |
font_size = 12 if num_nodes < 10 else 10 if num_nodes < 20 else 8
|
372 |
-
|
373 |
-
|
374 |
-
labels = nx.draw_networkx_labels(
|
375 |
-
DG,
|
376 |
-
pos,
|
377 |
font_size=font_size,
|
378 |
font_weight='bold',
|
379 |
-
bbox=dict(
|
380 |
-
facecolor='white',
|
381 |
-
edgecolor='none',
|
382 |
-
alpha=0.7
|
383 |
-
),
|
384 |
ax=ax
|
385 |
)
|
386 |
|
387 |
-
#
|
388 |
sm = plt.cm.ScalarMappable(
|
389 |
cmap=plt.cm.viridis,
|
390 |
norm=plt.Normalize(vmin=0, vmax=1)
|
391 |
-
)
|
392 |
sm.set_array([])
|
393 |
-
plt.colorbar(sm, ax=ax, label=
|
394 |
|
395 |
-
|
|
|
396 |
ax.set_axis_off()
|
397 |
-
|
398 |
-
# Ajustar el layout para que la barra de color no se superponga
|
399 |
plt.tight_layout()
|
400 |
|
401 |
return fig
|
402 |
|
403 |
except Exception as e:
|
404 |
logger.error(f"Error en visualize_concept_graph: {str(e)}")
|
405 |
-
return plt.figure()
|
|
|
406 |
|
407 |
########################################################################
|
408 |
def create_entity_graph(entities):
|
|
|
82 |
}
|
83 |
}
|
84 |
|
85 |
+
###########################################################
|
86 |
def fig_to_bytes(fig):
|
87 |
"""Convierte una figura de matplotlib a bytes."""
|
88 |
try:
|
|
|
283 |
return nx.Graph()
|
284 |
|
285 |
###############################################################################
|
|
|
286 |
def visualize_concept_graph(G, lang_code):
|
287 |
try:
|
288 |
+
# 1. Diccionario de traducciones
|
289 |
GRAPH_LABELS = {
|
290 |
'es': {
|
291 |
'concept_network': 'Relaciones entre conceptos clave',
|
|
|
305 |
}
|
306 |
}
|
307 |
|
308 |
+
# 2. Obtener traducciones (inglés por defecto)
|
309 |
+
translations = GRAPH_LABELS.get(lang_code, GRAPH_LABELS['en'])
|
310 |
|
311 |
+
# Configuración de la figura
|
312 |
fig, ax = plt.subplots(figsize=(15, 10))
|
313 |
|
314 |
if not G.nodes():
|
315 |
logger.warning("Grafo vacío, retornando figura vacía")
|
316 |
return fig
|
317 |
|
318 |
+
# Convertir a grafo dirigido para flechas
|
319 |
DG = nx.DiGraph(G)
|
|
|
|
|
320 |
centrality = nx.degree_centrality(G)
|
321 |
|
322 |
+
# Layout consistente
|
323 |
+
pos = nx.spring_layout(DG, k=2, iterations=50, seed=42)
|
324 |
|
325 |
+
# Escalado de elementos visuales
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
326 |
num_nodes = len(DG.nodes())
|
327 |
scale_factor = 1000 if num_nodes < 10 else 500 if num_nodes < 20 else 200
|
328 |
+
node_sizes = [DG.nodes[node].get('weight', 1) * scale_factor for node in DG.nodes()]
|
329 |
+
edge_widths = [DG[u][v].get('weight', 1) for u, v in DG.edges()]
|
|
|
|
|
|
|
|
|
330 |
node_colors = [plt.cm.viridis(centrality[node]) for node in DG.nodes()]
|
331 |
|
332 |
+
# Dibujar elementos del grafo
|
333 |
+
nx.draw_networkx_nodes(
|
334 |
+
DG, pos,
|
335 |
+
node_size=node_sizes,
|
|
|
336 |
node_color=node_colors,
|
337 |
alpha=0.7,
|
338 |
ax=ax
|
339 |
)
|
340 |
|
341 |
+
nx.draw_networkx_edges(
|
342 |
+
DG, pos,
|
343 |
+
width=edge_widths,
|
|
|
|
|
344 |
alpha=0.6,
|
345 |
edge_color='gray',
|
346 |
arrows=True,
|
|
|
350 |
ax=ax
|
351 |
)
|
352 |
|
353 |
+
# Etiquetas de nodos
|
354 |
font_size = 12 if num_nodes < 10 else 10 if num_nodes < 20 else 8
|
355 |
+
nx.draw_networkx_labels(
|
356 |
+
DG, pos,
|
|
|
|
|
|
|
357 |
font_size=font_size,
|
358 |
font_weight='bold',
|
359 |
+
bbox=dict(facecolor='white', edgecolor='none', alpha=0.7),
|
|
|
|
|
|
|
|
|
360 |
ax=ax
|
361 |
)
|
362 |
|
363 |
+
# Barra de color (centralidad)
|
364 |
sm = plt.cm.ScalarMappable(
|
365 |
cmap=plt.cm.viridis,
|
366 |
norm=plt.Normalize(vmin=0, vmax=1)
|
|
|
367 |
sm.set_array([])
|
368 |
+
plt.colorbar(sm, ax=ax, label=translations['concept_centrality'])
|
369 |
|
370 |
+
# Título del gráfico
|
371 |
+
plt.title(translations['concept_network'], pad=20, fontsize=14)
|
372 |
ax.set_axis_off()
|
|
|
|
|
373 |
plt.tight_layout()
|
374 |
|
375 |
return fig
|
376 |
|
377 |
except Exception as e:
|
378 |
logger.error(f"Error en visualize_concept_graph: {str(e)}")
|
379 |
+
return plt.figure()
|
380 |
+
|
381 |
|
382 |
########################################################################
|
383 |
def create_entity_graph(entities):
|