Spaces:
Running
Running
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:
|
|
@@ -284,65 +285,63 @@ def create_concept_graph(doc, key_concepts):
|
|
| 284 |
###############################################################################
|
| 285 |
|
| 286 |
def visualize_concept_graph(G, lang_code):
|
| 287 |
-
"""
|
| 288 |
-
Visualiza el grafo de conceptos con layout consistente.
|
| 289 |
-
Args:
|
| 290 |
-
G: networkx.Graph - Grafo de conceptos
|
| 291 |
-
lang_code: str - Código del idioma
|
| 292 |
-
Returns:
|
| 293 |
-
matplotlib.figure.Figure - Figura del grafo
|
| 294 |
-
"""
|
| 295 |
try:
|
| 296 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
fig, ax = plt.subplots(figsize=(15, 10))
|
| 298 |
|
| 299 |
if not G.nodes():
|
| 300 |
logger.warning("Grafo vacío, retornando figura vacía")
|
| 301 |
return fig
|
| 302 |
|
| 303 |
-
# Convertir grafo
|
| 304 |
DG = nx.DiGraph(G)
|
| 305 |
-
|
| 306 |
-
# Calcular centralidad de los nodos para el color
|
| 307 |
centrality = nx.degree_centrality(G)
|
| 308 |
|
| 309 |
-
#
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
# Calcular layout con parámetros fijos
|
| 313 |
-
pos = nx.spring_layout(
|
| 314 |
-
DG,
|
| 315 |
-
k=2, # Distancia ideal entre nodos
|
| 316 |
-
iterations=50, # Número de iteraciones
|
| 317 |
-
seed=seed # Semilla fija para reproducibilidad
|
| 318 |
-
)
|
| 319 |
|
| 320 |
-
#
|
| 321 |
num_nodes = len(DG.nodes())
|
| 322 |
scale_factor = 1000 if num_nodes < 10 else 500 if num_nodes < 20 else 200
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
node_weights = [DG.nodes[node].get('weight', 1) * scale_factor for node in DG.nodes()]
|
| 326 |
-
edge_weights = [DG[u][v].get('weight', 1) for u, v in DG.edges()]
|
| 327 |
-
|
| 328 |
-
# Crear mapa de colores basado en centralidad
|
| 329 |
node_colors = [plt.cm.viridis(centrality[node]) for node in DG.nodes()]
|
| 330 |
|
| 331 |
-
# Dibujar
|
| 332 |
-
|
| 333 |
-
DG,
|
| 334 |
-
|
| 335 |
-
node_size=node_weights,
|
| 336 |
node_color=node_colors,
|
| 337 |
alpha=0.7,
|
| 338 |
ax=ax
|
| 339 |
)
|
| 340 |
|
| 341 |
-
|
| 342 |
-
|
| 343 |
-
|
| 344 |
-
pos,
|
| 345 |
-
width=edge_weights,
|
| 346 |
alpha=0.6,
|
| 347 |
edge_color='gray',
|
| 348 |
arrows=True,
|
|
@@ -352,42 +351,34 @@ def visualize_concept_graph(G, lang_code):
|
|
| 352 |
ax=ax
|
| 353 |
)
|
| 354 |
|
| 355 |
-
#
|
| 356 |
font_size = 12 if num_nodes < 10 else 10 if num_nodes < 20 else 8
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
labels = nx.draw_networkx_labels(
|
| 360 |
-
DG,
|
| 361 |
-
pos,
|
| 362 |
font_size=font_size,
|
| 363 |
font_weight='bold',
|
| 364 |
-
bbox=dict(
|
| 365 |
-
facecolor='white',
|
| 366 |
-
edgecolor='none',
|
| 367 |
-
alpha=0.7
|
| 368 |
-
),
|
| 369 |
ax=ax
|
| 370 |
)
|
| 371 |
|
| 372 |
-
#
|
| 373 |
sm = plt.cm.ScalarMappable(
|
| 374 |
cmap=plt.cm.viridis,
|
| 375 |
norm=plt.Normalize(vmin=0, vmax=1)
|
| 376 |
)
|
| 377 |
sm.set_array([])
|
| 378 |
-
plt.colorbar(sm, ax=ax, label='
|
| 379 |
|
| 380 |
-
|
|
|
|
| 381 |
ax.set_axis_off()
|
| 382 |
-
|
| 383 |
-
# Ajustar el layout para que la barra de color no se superponga
|
| 384 |
plt.tight_layout()
|
| 385 |
|
| 386 |
return fig
|
| 387 |
|
| 388 |
except Exception as e:
|
| 389 |
logger.error(f"Error en visualize_concept_graph: {str(e)}")
|
| 390 |
-
return plt.figure()
|
| 391 |
|
| 392 |
########################################################################
|
| 393 |
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:
|
|
|
|
| 285 |
###############################################################################
|
| 286 |
|
| 287 |
def visualize_concept_graph(G, lang_code):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 288 |
try:
|
| 289 |
+
# 1. Diccionario de traducciones
|
| 290 |
+
GRAPH_LABELS = {
|
| 291 |
+
'es': {
|
| 292 |
+
'concept_network': 'Relaciones entre conceptos clave',
|
| 293 |
+
'concept_centrality': 'Centralidad de conceptos clave'
|
| 294 |
+
},
|
| 295 |
+
'en': {
|
| 296 |
+
'concept_network': 'Relationships between key concepts',
|
| 297 |
+
'concept_centrality': 'Concept centrality'
|
| 298 |
+
},
|
| 299 |
+
'fr': {
|
| 300 |
+
'concept_network': 'Relations entre concepts clés',
|
| 301 |
+
'concept_centrality': 'Centralité des concepts'
|
| 302 |
+
},
|
| 303 |
+
'pt': {
|
| 304 |
+
'concept_network': 'Relações entre conceitos-chave',
|
| 305 |
+
'concept_centrality': 'Centralidade dos conceitos'
|
| 306 |
+
}
|
| 307 |
+
}
|
| 308 |
+
|
| 309 |
+
# 2. Obtener traducciones (inglés por defecto)
|
| 310 |
+
translations = GRAPH_LABELS.get(lang_code, GRAPH_LABELS['en'])
|
| 311 |
+
|
| 312 |
+
# Configuración de la figura
|
| 313 |
fig, ax = plt.subplots(figsize=(15, 10))
|
| 314 |
|
| 315 |
if not G.nodes():
|
| 316 |
logger.warning("Grafo vacío, retornando figura vacía")
|
| 317 |
return fig
|
| 318 |
|
| 319 |
+
# Convertir a grafo dirigido para flechas
|
| 320 |
DG = nx.DiGraph(G)
|
|
|
|
|
|
|
| 321 |
centrality = nx.degree_centrality(G)
|
| 322 |
|
| 323 |
+
# Layout consistente
|
| 324 |
+
pos = nx.spring_layout(DG, k=2, iterations=50, seed=42)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 325 |
|
| 326 |
+
# Escalado de elementos visuales
|
| 327 |
num_nodes = len(DG.nodes())
|
| 328 |
scale_factor = 1000 if num_nodes < 10 else 500 if num_nodes < 20 else 200
|
| 329 |
+
node_sizes = [DG.nodes[node].get('weight', 1) * scale_factor for node in DG.nodes()]
|
| 330 |
+
edge_widths = [DG[u][v].get('weight', 1) for u, v in DG.edges()]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 331 |
node_colors = [plt.cm.viridis(centrality[node]) for node in DG.nodes()]
|
| 332 |
|
| 333 |
+
# Dibujar elementos del grafo
|
| 334 |
+
nx.draw_networkx_nodes(
|
| 335 |
+
DG, pos,
|
| 336 |
+
node_size=node_sizes,
|
|
|
|
| 337 |
node_color=node_colors,
|
| 338 |
alpha=0.7,
|
| 339 |
ax=ax
|
| 340 |
)
|
| 341 |
|
| 342 |
+
nx.draw_networkx_edges(
|
| 343 |
+
DG, pos,
|
| 344 |
+
width=edge_widths,
|
|
|
|
|
|
|
| 345 |
alpha=0.6,
|
| 346 |
edge_color='gray',
|
| 347 |
arrows=True,
|
|
|
|
| 351 |
ax=ax
|
| 352 |
)
|
| 353 |
|
| 354 |
+
# Etiquetas de nodos
|
| 355 |
font_size = 12 if num_nodes < 10 else 10 if num_nodes < 20 else 8
|
| 356 |
+
nx.draw_networkx_labels(
|
| 357 |
+
DG, pos,
|
|
|
|
|
|
|
|
|
|
| 358 |
font_size=font_size,
|
| 359 |
font_weight='bold',
|
| 360 |
+
bbox=dict(facecolor='white', edgecolor='none', alpha=0.7),
|
|
|
|
|
|
|
|
|
|
|
|
|
| 361 |
ax=ax
|
| 362 |
)
|
| 363 |
|
| 364 |
+
# Barra de color (centralidad)
|
| 365 |
sm = plt.cm.ScalarMappable(
|
| 366 |
cmap=plt.cm.viridis,
|
| 367 |
norm=plt.Normalize(vmin=0, vmax=1)
|
| 368 |
)
|
| 369 |
sm.set_array([])
|
| 370 |
+
plt.colorbar(sm, ax=ax, label=translations['concept_centrality'])
|
| 371 |
|
| 372 |
+
# Título del gráfico
|
| 373 |
+
plt.title(translations['concept_network'], pad=20, fontsize=14)
|
| 374 |
ax.set_axis_off()
|
|
|
|
|
|
|
| 375 |
plt.tight_layout()
|
| 376 |
|
| 377 |
return fig
|
| 378 |
|
| 379 |
except Exception as e:
|
| 380 |
logger.error(f"Error en visualize_concept_graph: {str(e)}")
|
| 381 |
+
return plt.figure()
|
| 382 |
|
| 383 |
########################################################################
|
| 384 |
def create_entity_graph(entities):
|