Update modules/database/semantic_mongo_db.py
Browse files
modules/database/semantic_mongo_db.py
CHANGED
@@ -25,37 +25,16 @@ COLLECTION_NAME = 'student_semantic_analysis'
|
|
25 |
def store_student_semantic_result(username, text, analysis_result):
|
26 |
"""
|
27 |
Guarda el resultado del análisis semántico en MongoDB.
|
28 |
-
Args:
|
29 |
-
username: Nombre del usuario
|
30 |
-
text: Texto analizado
|
31 |
-
analysis_result: Resultado del análisis
|
32 |
-
Returns:
|
33 |
-
bool: True si se guardó correctamente, False en caso contrario
|
34 |
"""
|
35 |
try:
|
36 |
-
#
|
37 |
concept_graph_data = None
|
38 |
if 'concept_graph' in analysis_result and analysis_result['concept_graph'] is not None:
|
39 |
-
buf = io.BytesIO()
|
40 |
-
try:
|
41 |
-
analysis_result['concept_graph'].savefig(buf, format='png', dpi=300, bbox_inches='tight')
|
42 |
-
buf.seek(0)
|
43 |
-
concept_graph_data = base64.b64encode(buf.getvalue()).decode('utf-8')
|
44 |
-
plt.close(analysis_result['concept_graph']) # Cerrar la figura
|
45 |
-
except Exception as e:
|
46 |
-
logger.error(f"Error al convertir gráfico conceptual: {str(e)}")
|
47 |
-
|
48 |
-
# Convertir gráfico de entidades a formato base64
|
49 |
-
entity_graph_data = None
|
50 |
-
if 'entity_graph' in analysis_result and analysis_result['entity_graph'] is not None:
|
51 |
-
buf = io.BytesIO()
|
52 |
try:
|
53 |
-
|
54 |
-
|
55 |
-
entity_graph_data = base64.b64encode(buf.getvalue()).decode('utf-8')
|
56 |
-
plt.close(analysis_result['entity_graph']) # Cerrar la figura
|
57 |
except Exception as e:
|
58 |
-
logger.error(f"Error al
|
59 |
|
60 |
# Crear documento para MongoDB
|
61 |
analysis_document = {
|
@@ -64,9 +43,7 @@ def store_student_semantic_result(username, text, analysis_result):
|
|
64 |
'text': text,
|
65 |
'analysis_type': 'semantic',
|
66 |
'key_concepts': analysis_result.get('key_concepts', []),
|
67 |
-
'concept_graph': concept_graph_data
|
68 |
-
'entities': analysis_result.get('entities', {}),
|
69 |
-
'entity_graph': entity_graph_data
|
70 |
}
|
71 |
|
72 |
# Insertar en MongoDB
|
@@ -81,9 +58,6 @@ def store_student_semantic_result(username, text, analysis_result):
|
|
81 |
except Exception as e:
|
82 |
logger.error(f"Error al guardar el análisis semántico: {str(e)}")
|
83 |
return False
|
84 |
-
finally:
|
85 |
-
# Asegurarnos de cerrar cualquier figura pendiente
|
86 |
-
plt.close('all')
|
87 |
|
88 |
####################################################################################
|
89 |
def get_student_semantic_analysis(username, limit=10):
|
|
|
25 |
def store_student_semantic_result(username, text, analysis_result):
|
26 |
"""
|
27 |
Guarda el resultado del análisis semántico en MongoDB.
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
"""
|
29 |
try:
|
30 |
+
# El gráfico ya viene en bytes, solo necesitamos codificarlo a base64
|
31 |
concept_graph_data = None
|
32 |
if 'concept_graph' in analysis_result and analysis_result['concept_graph'] is not None:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
try:
|
34 |
+
# Ya está en bytes, solo codificar a base64
|
35 |
+
concept_graph_data = base64.b64encode(analysis_result['concept_graph']).decode('utf-8')
|
|
|
|
|
36 |
except Exception as e:
|
37 |
+
logger.error(f"Error al codificar gráfico conceptual: {str(e)}")
|
38 |
|
39 |
# Crear documento para MongoDB
|
40 |
analysis_document = {
|
|
|
43 |
'text': text,
|
44 |
'analysis_type': 'semantic',
|
45 |
'key_concepts': analysis_result.get('key_concepts', []),
|
46 |
+
'concept_graph': concept_graph_data
|
|
|
|
|
47 |
}
|
48 |
|
49 |
# Insertar en MongoDB
|
|
|
58 |
except Exception as e:
|
59 |
logger.error(f"Error al guardar el análisis semántico: {str(e)}")
|
60 |
return False
|
|
|
|
|
|
|
61 |
|
62 |
####################################################################################
|
63 |
def get_student_semantic_analysis(username, limit=10):
|