Spaces:
Sleeping
Sleeping
Update modules/text_analysis/semantic_analysis.py
Browse files
modules/text_analysis/semantic_analysis.py
CHANGED
|
@@ -256,21 +256,25 @@ def create_concept_graph(doc, key_concepts):
|
|
| 256 |
###############################################################################
|
| 257 |
def visualize_concept_graph(G, lang_code):
|
| 258 |
"""
|
| 259 |
-
Visualiza el grafo de conceptos.
|
| 260 |
"""
|
| 261 |
try:
|
| 262 |
-
# Crear nueva figura
|
| 263 |
-
fig = plt.figure(figsize=(12, 8))
|
| 264 |
|
| 265 |
if not G.nodes():
|
| 266 |
logger.warning("Grafo vacío, retornando figura vacía")
|
| 267 |
return fig
|
| 268 |
|
| 269 |
-
# Calcular layout
|
| 270 |
-
pos = nx.spring_layout(G, k=
|
| 271 |
|
| 272 |
-
#
|
| 273 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 274 |
edge_weights = [G[u][v].get('weight', 1) for u, v in G.edges()]
|
| 275 |
|
| 276 |
# Dibujar grafo
|
|
@@ -284,11 +288,14 @@ def visualize_concept_graph(G, lang_code):
|
|
| 284 |
alpha=0.5,
|
| 285 |
edge_color='gray')
|
| 286 |
|
|
|
|
|
|
|
|
|
|
| 287 |
nx.draw_networkx_labels(G, pos,
|
| 288 |
-
font_size=
|
| 289 |
font_weight='bold')
|
| 290 |
|
| 291 |
-
plt.title("Red de conceptos relacionados")
|
| 292 |
plt.axis('off')
|
| 293 |
|
| 294 |
return fig
|
|
|
|
| 256 |
###############################################################################
|
| 257 |
def visualize_concept_graph(G, lang_code):
|
| 258 |
"""
|
| 259 |
+
Visualiza el grafo de conceptos con nodos ajustados según la longitud del texto.
|
| 260 |
"""
|
| 261 |
try:
|
| 262 |
+
# Crear nueva figura con mayor tamaño
|
| 263 |
+
fig = plt.figure(figsize=(15, 10)) # Aumentado de (12, 8) a (15, 10)
|
| 264 |
|
| 265 |
if not G.nodes():
|
| 266 |
logger.warning("Grafo vacío, retornando figura vacía")
|
| 267 |
return fig
|
| 268 |
|
| 269 |
+
# Calcular layout con más espacio
|
| 270 |
+
pos = nx.spring_layout(G, k=2, iterations=50) # Aumentado k de 1 a 2
|
| 271 |
|
| 272 |
+
# Calcular factor de escala basado en número de nodos
|
| 273 |
+
num_nodes = len(G.nodes())
|
| 274 |
+
scale_factor = 1000 if num_nodes < 10 else 500 if num_nodes < 20 else 200
|
| 275 |
+
|
| 276 |
+
# Obtener pesos ajustados
|
| 277 |
+
node_weights = [G.nodes[node].get('weight', 1) * scale_factor for node in G.nodes()]
|
| 278 |
edge_weights = [G[u][v].get('weight', 1) for u, v in G.edges()]
|
| 279 |
|
| 280 |
# Dibujar grafo
|
|
|
|
| 288 |
alpha=0.5,
|
| 289 |
edge_color='gray')
|
| 290 |
|
| 291 |
+
# Ajustar tamaño de fuente según número de nodos
|
| 292 |
+
font_size = 12 if num_nodes < 10 else 10 if num_nodes < 20 else 8
|
| 293 |
+
|
| 294 |
nx.draw_networkx_labels(G, pos,
|
| 295 |
+
font_size=font_size,
|
| 296 |
font_weight='bold')
|
| 297 |
|
| 298 |
+
plt.title("Red de conceptos relacionados", pad=20)
|
| 299 |
plt.axis('off')
|
| 300 |
|
| 301 |
return fig
|