Update modules/database/database.py
Browse files- modules/database/database.py +24 -13
modules/database/database.py
CHANGED
|
@@ -256,20 +256,26 @@ def store_semantic_result(username, text, analysis_result):
|
|
| 256 |
if analysis_collection is None:
|
| 257 |
logger.error("La conexión a MongoDB no está inicializada")
|
| 258 |
return False
|
|
|
|
| 259 |
try:
|
|
|
|
| 260 |
buf = io.BytesIO()
|
| 261 |
analysis_result['relations_graph'].savefig(buf, format='png')
|
| 262 |
buf.seek(0)
|
| 263 |
img_str = base64.b64encode(buf.getvalue()).decode('utf-8')
|
|
|
|
|
|
|
|
|
|
|
|
|
| 264 |
analysis_document = {
|
| 265 |
'username': username,
|
| 266 |
'timestamp': datetime.utcnow(),
|
| 267 |
'text': text,
|
| 268 |
-
'
|
| 269 |
-
'
|
| 270 |
-
'network_diagram': img_str, # Cambiado de 'relations_graph' a 'network_diagram'
|
| 271 |
'analysis_type': 'semantic'
|
| 272 |
}
|
|
|
|
| 273 |
result = analysis_collection.insert_one(analysis_document)
|
| 274 |
logger.info(f"Análisis semántico guardado con ID: {result.inserted_id} para el usuario: {username}")
|
| 275 |
logger.info(f"Longitud de la imagen guardada: {len(img_str)}")
|
|
@@ -280,19 +286,19 @@ def store_semantic_result(username, text, analysis_result):
|
|
| 280 |
|
| 281 |
###############################################################################################################
|
| 282 |
|
| 283 |
-
def store_discourse_analysis_result(username, text1, text2,
|
| 284 |
try:
|
| 285 |
# Crear una nueva figura combinada
|
| 286 |
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(20, 10))
|
| 287 |
|
| 288 |
-
# Añadir la primera imagen
|
| 289 |
-
ax1.imshow(graph1.
|
| 290 |
-
ax1.set_title("Documento
|
| 291 |
ax1.axis('off')
|
| 292 |
|
| 293 |
-
# Añadir la segunda imagen
|
| 294 |
-
ax2.imshow(graph2.
|
| 295 |
-
ax2.set_title("Documento
|
| 296 |
ax2.axis('off')
|
| 297 |
|
| 298 |
# Ajustar el diseño
|
|
@@ -306,8 +312,12 @@ def store_discourse_analysis_result(username, text1, text2, graph1, graph2):
|
|
| 306 |
|
| 307 |
# Cerrar las figuras para liberar memoria
|
| 308 |
plt.close(fig)
|
| 309 |
-
plt.close(graph1
|
| 310 |
-
plt.close(graph2
|
|
|
|
|
|
|
|
|
|
|
|
|
| 311 |
|
| 312 |
analysis_document = {
|
| 313 |
'username': username,
|
|
@@ -315,11 +325,12 @@ def store_discourse_analysis_result(username, text1, text2, graph1, graph2):
|
|
| 315 |
'text1': text1,
|
| 316 |
'text2': text2,
|
| 317 |
'combined_graph': img_str,
|
|
|
|
|
|
|
| 318 |
'analysis_type': 'discourse'
|
| 319 |
}
|
| 320 |
|
| 321 |
result = analysis_collection.insert_one(analysis_document)
|
| 322 |
-
|
| 323 |
logger.info(f"Análisis discursivo guardado con ID: {result.inserted_id} para el usuario: {username}")
|
| 324 |
return True
|
| 325 |
except Exception as e:
|
|
|
|
| 256 |
if analysis_collection is None:
|
| 257 |
logger.error("La conexión a MongoDB no está inicializada")
|
| 258 |
return False
|
| 259 |
+
|
| 260 |
try:
|
| 261 |
+
# Convertir el gráfico a imagen base64
|
| 262 |
buf = io.BytesIO()
|
| 263 |
analysis_result['relations_graph'].savefig(buf, format='png')
|
| 264 |
buf.seek(0)
|
| 265 |
img_str = base64.b64encode(buf.getvalue()).decode('utf-8')
|
| 266 |
+
|
| 267 |
+
# Convertir los conceptos clave a una lista de tuplas
|
| 268 |
+
key_concepts = [(concept, float(frequency)) for concept, frequency in analysis_result['key_concepts']]
|
| 269 |
+
|
| 270 |
analysis_document = {
|
| 271 |
'username': username,
|
| 272 |
'timestamp': datetime.utcnow(),
|
| 273 |
'text': text,
|
| 274 |
+
'key_concepts': key_concepts,
|
| 275 |
+
'network_diagram': img_str,
|
|
|
|
| 276 |
'analysis_type': 'semantic'
|
| 277 |
}
|
| 278 |
+
|
| 279 |
result = analysis_collection.insert_one(analysis_document)
|
| 280 |
logger.info(f"Análisis semántico guardado con ID: {result.inserted_id} para el usuario: {username}")
|
| 281 |
logger.info(f"Longitud de la imagen guardada: {len(img_str)}")
|
|
|
|
| 286 |
|
| 287 |
###############################################################################################################
|
| 288 |
|
| 289 |
+
def store_discourse_analysis_result(username, text1, text2, analysis_result):
|
| 290 |
try:
|
| 291 |
# Crear una nueva figura combinada
|
| 292 |
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(20, 10))
|
| 293 |
|
| 294 |
+
# Añadir la primera imagen
|
| 295 |
+
ax1.imshow(analysis_result['graph1'].canvas.renderer.buffer_rgba())
|
| 296 |
+
ax1.set_title("Documento 1: Relaciones Conceptuales")
|
| 297 |
ax1.axis('off')
|
| 298 |
|
| 299 |
+
# Añadir la segunda imagen
|
| 300 |
+
ax2.imshow(analysis_result['graph2'].canvas.renderer.buffer_rgba())
|
| 301 |
+
ax2.set_title("Documento 2: Relaciones Conceptuales")
|
| 302 |
ax2.axis('off')
|
| 303 |
|
| 304 |
# Ajustar el diseño
|
|
|
|
| 312 |
|
| 313 |
# Cerrar las figuras para liberar memoria
|
| 314 |
plt.close(fig)
|
| 315 |
+
plt.close(analysis_result['graph1'])
|
| 316 |
+
plt.close(analysis_result['graph2'])
|
| 317 |
+
|
| 318 |
+
# Convertir los conceptos clave a listas de tuplas
|
| 319 |
+
key_concepts1 = [(concept, float(frequency)) for concept, frequency in analysis_result['table1'].values.tolist()]
|
| 320 |
+
key_concepts2 = [(concept, float(frequency)) for concept, frequency in analysis_result['table2'].values.tolist()]
|
| 321 |
|
| 322 |
analysis_document = {
|
| 323 |
'username': username,
|
|
|
|
| 325 |
'text1': text1,
|
| 326 |
'text2': text2,
|
| 327 |
'combined_graph': img_str,
|
| 328 |
+
'key_concepts1': key_concepts1,
|
| 329 |
+
'key_concepts2': key_concepts2,
|
| 330 |
'analysis_type': 'discourse'
|
| 331 |
}
|
| 332 |
|
| 333 |
result = analysis_collection.insert_one(analysis_document)
|
|
|
|
| 334 |
logger.info(f"Análisis discursivo guardado con ID: {result.inserted_id} para el usuario: {username}")
|
| 335 |
return True
|
| 336 |
except Exception as e:
|