Update modules/text_analysis/semantic_analysis.py
Browse files
modules/text_analysis/semantic_analysis.py
CHANGED
@@ -264,8 +264,8 @@ def visualize_concept_graph(G, lang_code):
|
|
264 |
matplotlib.figure.Figure - Figura del grafo
|
265 |
"""
|
266 |
try:
|
267 |
-
# Crear nueva figura con mayor tamaño
|
268 |
-
fig = plt.
|
269 |
|
270 |
if not G.nodes():
|
271 |
logger.warning("Grafo vacío, retornando figura vacía")
|
@@ -292,20 +292,22 @@ def visualize_concept_graph(G, lang_code):
|
|
292 |
node_colors = [plt.cm.viridis(centrality[node]) for node in DG.nodes()]
|
293 |
|
294 |
# Dibujar nodos
|
295 |
-
nx.draw_networkx_nodes(DG, pos,
|
296 |
-
|
297 |
-
|
298 |
-
|
|
|
299 |
|
300 |
# Dibujar aristas con flechas
|
301 |
-
nx.draw_networkx_edges(DG, pos,
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
|
|
309 |
|
310 |
# Ajustar tamaño de fuente según número de nodos
|
311 |
font_size = 12 if num_nodes < 10 else 10 if num_nodes < 20 else 8
|
@@ -316,15 +318,20 @@ def visualize_concept_graph(G, lang_code):
|
|
316 |
font_weight='bold',
|
317 |
bbox=dict(facecolor='white',
|
318 |
edgecolor='none',
|
319 |
-
alpha=0.7)
|
|
|
320 |
|
321 |
-
# Añadir leyenda de centralidad
|
322 |
-
sm = plt.cm.ScalarMappable(cmap=plt.cm.viridis
|
|
|
323 |
sm.set_array([])
|
324 |
-
plt.colorbar(sm, label='Centralidad del concepto')
|
325 |
|
326 |
plt.title("Red de conceptos relacionados", pad=20, fontsize=14)
|
327 |
-
|
|
|
|
|
|
|
328 |
|
329 |
return fig
|
330 |
|
@@ -333,6 +340,9 @@ def visualize_concept_graph(G, lang_code):
|
|
333 |
return plt.figure() # Retornar figura vacía en caso de error
|
334 |
|
335 |
|
|
|
|
|
|
|
336 |
########################################################################
|
337 |
def create_entity_graph(entities):
|
338 |
G = nx.Graph()
|
|
|
264 |
matplotlib.figure.Figure - Figura del grafo
|
265 |
"""
|
266 |
try:
|
267 |
+
# Crear nueva figura con mayor tamaño y definir los ejes explícitamente
|
268 |
+
fig, ax = plt.subplots(figsize=(15, 10))
|
269 |
|
270 |
if not G.nodes():
|
271 |
logger.warning("Grafo vacío, retornando figura vacía")
|
|
|
292 |
node_colors = [plt.cm.viridis(centrality[node]) for node in DG.nodes()]
|
293 |
|
294 |
# Dibujar nodos
|
295 |
+
nodes = nx.draw_networkx_nodes(DG, pos,
|
296 |
+
node_size=node_weights,
|
297 |
+
node_color=node_colors,
|
298 |
+
alpha=0.7,
|
299 |
+
ax=ax)
|
300 |
|
301 |
# Dibujar aristas con flechas
|
302 |
+
edges = nx.draw_networkx_edges(DG, pos,
|
303 |
+
width=edge_weights,
|
304 |
+
alpha=0.6,
|
305 |
+
edge_color='gray',
|
306 |
+
arrows=True,
|
307 |
+
arrowsize=20,
|
308 |
+
arrowstyle='->',
|
309 |
+
connectionstyle='arc3,rad=0.2',
|
310 |
+
ax=ax)
|
311 |
|
312 |
# Ajustar tamaño de fuente según número de nodos
|
313 |
font_size = 12 if num_nodes < 10 else 10 if num_nodes < 20 else 8
|
|
|
318 |
font_weight='bold',
|
319 |
bbox=dict(facecolor='white',
|
320 |
edgecolor='none',
|
321 |
+
alpha=0.7),
|
322 |
+
ax=ax)
|
323 |
|
324 |
+
# Añadir leyenda de centralidad correctamente
|
325 |
+
sm = plt.cm.ScalarMappable(cmap=plt.cm.viridis,
|
326 |
+
norm=plt.Normalize(vmin=0, vmax=1))
|
327 |
sm.set_array([])
|
328 |
+
plt.colorbar(sm, ax=ax, label='Centralidad del concepto')
|
329 |
|
330 |
plt.title("Red de conceptos relacionados", pad=20, fontsize=14)
|
331 |
+
ax.set_axis_off()
|
332 |
+
|
333 |
+
# Ajustar el layout para que la barra de color no se superponga
|
334 |
+
plt.tight_layout()
|
335 |
|
336 |
return fig
|
337 |
|
|
|
340 |
return plt.figure() # Retornar figura vacía en caso de error
|
341 |
|
342 |
|
343 |
+
|
344 |
+
|
345 |
+
|
346 |
########################################################################
|
347 |
def create_entity_graph(entities):
|
348 |
G = nx.Graph()
|