Update modules/text_analysis/semantic_analysis.py
Browse files
modules/text_analysis/semantic_analysis.py
CHANGED
@@ -282,9 +282,10 @@ def create_concept_graph(doc, key_concepts):
|
|
282 |
return nx.Graph()
|
283 |
|
284 |
###############################################################################
|
|
|
285 |
def visualize_concept_graph(G, lang_code):
|
286 |
"""
|
287 |
-
Visualiza el grafo de conceptos con
|
288 |
Args:
|
289 |
G: networkx.Graph - Grafo de conceptos
|
290 |
lang_code: str - Código del idioma
|
@@ -305,8 +306,16 @@ def visualize_concept_graph(G, lang_code):
|
|
305 |
# Calcular centralidad de los nodos para el color
|
306 |
centrality = nx.degree_centrality(G)
|
307 |
|
308 |
-
#
|
309 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
310 |
|
311 |
# Calcular factor de escala basado en número de nodos
|
312 |
num_nodes = len(DG.nodes())
|
@@ -320,38 +329,51 @@ def visualize_concept_graph(G, lang_code):
|
|
320 |
node_colors = [plt.cm.viridis(centrality[node]) for node in DG.nodes()]
|
321 |
|
322 |
# Dibujar nodos
|
323 |
-
nodes = nx.draw_networkx_nodes(
|
324 |
-
|
325 |
-
|
326 |
-
|
327 |
-
|
|
|
|
|
|
|
328 |
|
329 |
# Dibujar aristas con flechas
|
330 |
-
edges = nx.draw_networkx_edges(
|
331 |
-
|
332 |
-
|
333 |
-
|
334 |
-
|
335 |
-
|
336 |
-
|
337 |
-
|
338 |
-
|
|
|
|
|
|
|
339 |
|
340 |
# Ajustar tamaño de fuente según número de nodos
|
341 |
font_size = 12 if num_nodes < 10 else 10 if num_nodes < 20 else 8
|
342 |
|
343 |
# Dibujar etiquetas con fondo blanco para mejor legibilidad
|
344 |
-
labels = nx.draw_networkx_labels(
|
345 |
-
|
346 |
-
|
347 |
-
|
348 |
-
|
349 |
-
|
350 |
-
|
351 |
-
|
352 |
-
|
353 |
-
|
354 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
355 |
sm.set_array([])
|
356 |
plt.colorbar(sm, ax=ax, label='Centralidad del concepto')
|
357 |
|
@@ -367,10 +389,6 @@ def visualize_concept_graph(G, lang_code):
|
|
367 |
logger.error(f"Error en visualize_concept_graph: {str(e)}")
|
368 |
return plt.figure() # Retornar figura vacía en caso de error
|
369 |
|
370 |
-
|
371 |
-
|
372 |
-
|
373 |
-
|
374 |
########################################################################
|
375 |
def create_entity_graph(entities):
|
376 |
G = nx.Graph()
|
|
|
282 |
return nx.Graph()
|
283 |
|
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
|
|
|
306 |
# Calcular centralidad de los nodos para el color
|
307 |
centrality = nx.degree_centrality(G)
|
308 |
|
309 |
+
# Establecer semilla para reproducibilidad
|
310 |
+
seed = 42
|
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 |
# Calcular factor de escala basado en número de nodos
|
321 |
num_nodes = len(DG.nodes())
|
|
|
329 |
node_colors = [plt.cm.viridis(centrality[node]) for node in DG.nodes()]
|
330 |
|
331 |
# Dibujar nodos
|
332 |
+
nodes = nx.draw_networkx_nodes(
|
333 |
+
DG,
|
334 |
+
pos,
|
335 |
+
node_size=node_weights,
|
336 |
+
node_color=node_colors,
|
337 |
+
alpha=0.7,
|
338 |
+
ax=ax
|
339 |
+
)
|
340 |
|
341 |
# Dibujar aristas con flechas
|
342 |
+
edges = nx.draw_networkx_edges(
|
343 |
+
DG,
|
344 |
+
pos,
|
345 |
+
width=edge_weights,
|
346 |
+
alpha=0.6,
|
347 |
+
edge_color='gray',
|
348 |
+
arrows=True,
|
349 |
+
arrowsize=20,
|
350 |
+
arrowstyle='->',
|
351 |
+
connectionstyle='arc3,rad=0.2',
|
352 |
+
ax=ax
|
353 |
+
)
|
354 |
|
355 |
# Ajustar tamaño de fuente según número de nodos
|
356 |
font_size = 12 if num_nodes < 10 else 10 if num_nodes < 20 else 8
|
357 |
|
358 |
# Dibujar etiquetas con fondo blanco para mejor legibilidad
|
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 |
+
# Añadir leyenda de centralidad
|
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='Centralidad del concepto')
|
379 |
|
|
|
389 |
logger.error(f"Error en visualize_concept_graph: {str(e)}")
|
390 |
return plt.figure() # Retornar figura vacía en caso de error
|
391 |
|
|
|
|
|
|
|
|
|
392 |
########################################################################
|
393 |
def create_entity_graph(entities):
|
394 |
G = nx.Graph()
|