Spaces:
Running
Running
Update modules/database/semantic_mongo_db.py
Browse files
modules/database/semantic_mongo_db.py
CHANGED
@@ -22,45 +22,64 @@ from .mongo_db import (
|
|
22 |
logger = logging.getLogger(__name__) # Cambiado de name a __name__
|
23 |
COLLECTION_NAME = 'student_semantic_analysis'
|
24 |
|
|
|
|
|
|
|
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 |
-
#
|
|
|
|
|
|
|
|
|
|
|
31 |
concept_graph_data = None
|
32 |
if 'concept_graph' in analysis_result and analysis_result['concept_graph'] is not None:
|
33 |
try:
|
34 |
-
|
35 |
-
|
|
|
|
|
36 |
except Exception as e:
|
37 |
logger.error(f"Error al codificar gráfico conceptual: {str(e)}")
|
38 |
|
39 |
-
# Crear documento
|
40 |
analysis_document = {
|
41 |
'username': username,
|
42 |
-
'timestamp': datetime.now(timezone.utc)
|
43 |
'text': text,
|
44 |
'analysis_type': 'semantic',
|
45 |
'metrics': {
|
46 |
'key_concepts': analysis_result.get('key_concepts', []),
|
47 |
'concept_centrality': analysis_result.get('concept_centrality', {})
|
48 |
},
|
49 |
-
'graph_data':
|
50 |
'language': st.session_state.get('lang_code', 'en')
|
51 |
}
|
52 |
|
53 |
# Insertar en MongoDB
|
54 |
-
|
55 |
-
if
|
56 |
-
logger.
|
|
|
|
|
|
|
|
|
|
|
57 |
return True
|
58 |
-
|
59 |
logger.error("No se pudo insertar el documento en MongoDB")
|
60 |
return False
|
61 |
|
62 |
except Exception as e:
|
63 |
-
logger.error(f"Error al guardar el análisis semántico: {str(e)}")
|
64 |
return False
|
65 |
|
66 |
####################################################################################
|
|
|
22 |
logger = logging.getLogger(__name__) # Cambiado de name a __name__
|
23 |
COLLECTION_NAME = 'student_semantic_analysis'
|
24 |
|
25 |
+
####################################################################
|
26 |
+
# modules/database/semantic_mongo_db.py
|
27 |
+
|
28 |
def store_student_semantic_result(username, text, analysis_result):
|
29 |
"""
|
30 |
Guarda el resultado del análisis semántico en MongoDB.
|
31 |
+
Args:
|
32 |
+
username: Nombre del usuario
|
33 |
+
text: Texto completo analizado
|
34 |
+
analysis_result: Diccionario con los resultados del análisis
|
35 |
"""
|
36 |
try:
|
37 |
+
# Verificar que los datos requeridos estén presentes
|
38 |
+
if not isinstance(analysis_result, dict):
|
39 |
+
logger.error("El resultado del análisis no es un diccionario")
|
40 |
+
return False
|
41 |
+
|
42 |
+
# Preparar el gráfico conceptual (si existe)
|
43 |
concept_graph_data = None
|
44 |
if 'concept_graph' in analysis_result and analysis_result['concept_graph'] is not None:
|
45 |
try:
|
46 |
+
if isinstance(analysis_result['concept_graph'], bytes):
|
47 |
+
concept_graph_data = base64.b64encode(analysis_result['concept_graph']).decode('utf-8')
|
48 |
+
else:
|
49 |
+
logger.warning("El gráfico conceptual no está en formato bytes")
|
50 |
except Exception as e:
|
51 |
logger.error(f"Error al codificar gráfico conceptual: {str(e)}")
|
52 |
|
53 |
+
# Crear documento con estructura consistente
|
54 |
analysis_document = {
|
55 |
'username': username,
|
56 |
+
'timestamp': datetime.now(timezone.utc),
|
57 |
'text': text,
|
58 |
'analysis_type': 'semantic',
|
59 |
'metrics': {
|
60 |
'key_concepts': analysis_result.get('key_concepts', []),
|
61 |
'concept_centrality': analysis_result.get('concept_centrality', {})
|
62 |
},
|
63 |
+
'graph_data': concept_graph_data,
|
64 |
'language': st.session_state.get('lang_code', 'en')
|
65 |
}
|
66 |
|
67 |
# Insertar en MongoDB
|
68 |
+
collection = get_collection(COLLECTION_NAME)
|
69 |
+
if not collection:
|
70 |
+
logger.error("No se pudo obtener la colección de MongoDB")
|
71 |
+
return False
|
72 |
+
|
73 |
+
result = collection.insert_one(analysis_document)
|
74 |
+
if result.inserted_id:
|
75 |
+
logger.info(f"Análisis semántico guardado para {username} con ID: {result.inserted_id}")
|
76 |
return True
|
77 |
+
|
78 |
logger.error("No se pudo insertar el documento en MongoDB")
|
79 |
return False
|
80 |
|
81 |
except Exception as e:
|
82 |
+
logger.error(f"Error al guardar el análisis semántico: {str(e)}", exc_info=True)
|
83 |
return False
|
84 |
|
85 |
####################################################################################
|