Update modules/database/database.py
Browse files- modules/database/database.py +48 -21
modules/database/database.py
CHANGED
|
@@ -289,45 +289,72 @@ def store_semantic_result(username, text, analysis_result):
|
|
| 289 |
|
| 290 |
def store_discourse_analysis_result(username, text1, text2, analysis_result):
|
| 291 |
try:
|
| 292 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 293 |
buf1 = io.BytesIO()
|
| 294 |
analysis_result['graph1'].savefig(buf1, format='png')
|
| 295 |
buf1.seek(0)
|
| 296 |
img_str1 = base64.b64encode(buf1.getvalue()).decode('utf-8')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 297 |
|
|
|
|
| 298 |
buf2 = io.BytesIO()
|
| 299 |
analysis_result['graph2'].savefig(buf2, format='png')
|
| 300 |
buf2.seek(0)
|
| 301 |
img_str2 = base64.b64encode(buf2.getvalue()).decode('utf-8')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 302 |
|
| 303 |
-
#
|
| 304 |
buf_sankey = io.BytesIO()
|
| 305 |
analysis_result['sankey_diagram'].write_image(buf_sankey, format='png')
|
| 306 |
buf_sankey.seek(0)
|
| 307 |
img_str_sankey = base64.b64encode(buf_sankey.getvalue()).decode('utf-8')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 308 |
|
| 309 |
-
|
| 310 |
-
key_concepts1 = [(concept, float(frequency)) for concept, frequency in analysis_result['key_concepts1']]
|
| 311 |
-
key_concepts2 = [(concept, float(frequency)) for concept, frequency in analysis_result['key_concepts2']]
|
| 312 |
-
|
| 313 |
-
analysis_document = {
|
| 314 |
-
'username': username,
|
| 315 |
-
'timestamp': datetime.utcnow(),
|
| 316 |
-
'text1': text1,
|
| 317 |
-
'text2': text2,
|
| 318 |
-
'graph1': img_str1,
|
| 319 |
-
'graph2': img_str2,
|
| 320 |
-
'sankey_diagram': img_str_sankey,
|
| 321 |
-
'key_concepts1': key_concepts1,
|
| 322 |
-
'key_concepts2': key_concepts2,
|
| 323 |
-
'analysis_type': 'discourse'
|
| 324 |
-
}
|
| 325 |
-
|
| 326 |
-
result = analysis_collection.insert_one(analysis_document)
|
| 327 |
-
logger.info(f"An谩lisis discursivo guardado con ID: {result.inserted_id} para el usuario: {username}")
|
| 328 |
return True
|
|
|
|
| 329 |
except Exception as e:
|
| 330 |
logger.error(f"Error al guardar el an谩lisis discursivo para el usuario {username}: {str(e)}")
|
|
|
|
|
|
|
| 331 |
return False
|
| 332 |
|
| 333 |
###############################################################################################################
|
|
|
|
| 289 |
|
| 290 |
def store_discourse_analysis_result(username, text1, text2, analysis_result):
|
| 291 |
try:
|
| 292 |
+
# Primero, guardamos la informaci贸n b谩sica y los conceptos clave
|
| 293 |
+
base_document = {
|
| 294 |
+
'username': username,
|
| 295 |
+
'timestamp': datetime.utcnow(),
|
| 296 |
+
'text1': text1,
|
| 297 |
+
'text2': text2,
|
| 298 |
+
'analysis_type': 'discourse'
|
| 299 |
+
}
|
| 300 |
+
|
| 301 |
+
result = analysis_collection.insert_one(base_document)
|
| 302 |
+
document_id = result.inserted_id
|
| 303 |
+
logger.info(f"Documento base guardado con ID: {document_id}")
|
| 304 |
+
|
| 305 |
+
# Guardar los conceptos clave
|
| 306 |
+
key_concepts1 = [(concept, float(frequency)) for concept, frequency in analysis_result['key_concepts1']]
|
| 307 |
+
key_concepts2 = [(concept, float(frequency)) for concept, frequency in analysis_result['key_concepts2']]
|
| 308 |
+
|
| 309 |
+
analysis_collection.update_one(
|
| 310 |
+
{'_id': document_id},
|
| 311 |
+
{'$set': {'key_concepts1': key_concepts1, 'key_concepts2': key_concepts2}}
|
| 312 |
+
)
|
| 313 |
+
logger.info("Conceptos clave guardados exitosamente")
|
| 314 |
+
|
| 315 |
+
# Guardar el primer grafo
|
| 316 |
buf1 = io.BytesIO()
|
| 317 |
analysis_result['graph1'].savefig(buf1, format='png')
|
| 318 |
buf1.seek(0)
|
| 319 |
img_str1 = base64.b64encode(buf1.getvalue()).decode('utf-8')
|
| 320 |
+
|
| 321 |
+
analysis_collection.update_one(
|
| 322 |
+
{'_id': document_id},
|
| 323 |
+
{'$set': {'graph1': img_str1}}
|
| 324 |
+
)
|
| 325 |
+
logger.info("Grafo 1 guardado exitosamente")
|
| 326 |
|
| 327 |
+
# Guardar el segundo grafo
|
| 328 |
buf2 = io.BytesIO()
|
| 329 |
analysis_result['graph2'].savefig(buf2, format='png')
|
| 330 |
buf2.seek(0)
|
| 331 |
img_str2 = base64.b64encode(buf2.getvalue()).decode('utf-8')
|
| 332 |
+
|
| 333 |
+
analysis_collection.update_one(
|
| 334 |
+
{'_id': document_id},
|
| 335 |
+
{'$set': {'graph2': img_str2}}
|
| 336 |
+
)
|
| 337 |
+
logger.info("Grafo 2 guardado exitosamente")
|
| 338 |
|
| 339 |
+
# Guardar el diagrama de Sankey
|
| 340 |
buf_sankey = io.BytesIO()
|
| 341 |
analysis_result['sankey_diagram'].write_image(buf_sankey, format='png')
|
| 342 |
buf_sankey.seek(0)
|
| 343 |
img_str_sankey = base64.b64encode(buf_sankey.getvalue()).decode('utf-8')
|
| 344 |
+
|
| 345 |
+
analysis_collection.update_one(
|
| 346 |
+
{'_id': document_id},
|
| 347 |
+
{'$set': {'sankey_diagram': img_str_sankey}}
|
| 348 |
+
)
|
| 349 |
+
logger.info("Diagrama Sankey guardado exitosamente")
|
| 350 |
|
| 351 |
+
logger.info(f"An谩lisis discursivo completo guardado con ID: {document_id} para el usuario: {username}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 352 |
return True
|
| 353 |
+
|
| 354 |
except Exception as e:
|
| 355 |
logger.error(f"Error al guardar el an谩lisis discursivo para el usuario {username}: {str(e)}")
|
| 356 |
+
logger.error(f"Tipo de excepci贸n: {type(e).__name__}")
|
| 357 |
+
logger.error(f"Detalles de la excepci贸n: {e.args}")
|
| 358 |
return False
|
| 359 |
|
| 360 |
###############################################################################################################
|