Spaces:
Running
Running
Update modules/database/semantic_mongo_db.py
Browse files
modules/database/semantic_mongo_db.py
CHANGED
|
@@ -25,21 +25,24 @@ COLLECTION_NAME = 'student_semantic_analysis'
|
|
| 25 |
####################################################################
|
| 26 |
# modules/database/semantic_mongo_db.py
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
| 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
|
| 38 |
-
if not
|
| 39 |
-
logger.error("
|
| 40 |
return False
|
| 41 |
|
| 42 |
-
# Preparar el gráfico conceptual
|
| 43 |
concept_graph_data = None
|
| 44 |
if 'concept_graph' in analysis_result and analysis_result['concept_graph'] is not None:
|
| 45 |
try:
|
|
@@ -50,31 +53,24 @@ def store_student_semantic_result(username, text, analysis_result):
|
|
| 50 |
except Exception as e:
|
| 51 |
logger.error(f"Error al codificar gráfico conceptual: {str(e)}")
|
| 52 |
|
| 53 |
-
# Crear documento
|
| 54 |
analysis_document = {
|
| 55 |
'username': username,
|
| 56 |
'timestamp': datetime.now(timezone.utc),
|
| 57 |
'text': text,
|
| 58 |
'analysis_type': 'semantic',
|
| 59 |
-
'
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
'graph_data': concept_graph_data
|
| 64 |
-
#'language': st.session_state.get('lang_code', 'en')
|
| 65 |
}
|
| 66 |
|
| 67 |
# Insertar en MongoDB
|
| 68 |
-
|
| 69 |
-
if
|
| 70 |
-
logger.
|
| 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 |
|
|
|
|
| 25 |
####################################################################
|
| 26 |
# modules/database/semantic_mongo_db.py
|
| 27 |
|
| 28 |
+
# modules/database/semantic_mongo_db.py
|
| 29 |
+
|
| 30 |
+
def store_student_semantic_result(username, text, analysis_result, lang_code='en'):
|
| 31 |
"""
|
| 32 |
Guarda el resultado del análisis semántico en MongoDB.
|
| 33 |
Args:
|
| 34 |
username: Nombre del usuario
|
| 35 |
text: Texto completo analizado
|
| 36 |
analysis_result: Diccionario con los resultados del análisis
|
| 37 |
+
lang_code: Código de idioma (opcional, default 'en')
|
| 38 |
"""
|
| 39 |
try:
|
| 40 |
+
# Verificar datos mínimos requeridos
|
| 41 |
+
if not username or not text or not analysis_result:
|
| 42 |
+
logger.error("Datos insuficientes para guardar el análisis")
|
| 43 |
return False
|
| 44 |
|
| 45 |
+
# Preparar el gráfico conceptual
|
| 46 |
concept_graph_data = None
|
| 47 |
if 'concept_graph' in analysis_result and analysis_result['concept_graph'] is not None:
|
| 48 |
try:
|
|
|
|
| 53 |
except Exception as e:
|
| 54 |
logger.error(f"Error al codificar gráfico conceptual: {str(e)}")
|
| 55 |
|
| 56 |
+
# Crear documento para MongoDB
|
| 57 |
analysis_document = {
|
| 58 |
'username': username,
|
| 59 |
'timestamp': datetime.now(timezone.utc),
|
| 60 |
'text': text,
|
| 61 |
'analysis_type': 'semantic',
|
| 62 |
+
'key_concepts': analysis_result.get('key_concepts', []),
|
| 63 |
+
'concept_centrality': analysis_result.get('concept_centrality', {}),
|
| 64 |
+
'concept_graph': concept_graph_data,
|
| 65 |
+
'language': lang_code # Usamos el parámetro directamente
|
|
|
|
|
|
|
| 66 |
}
|
| 67 |
|
| 68 |
# Insertar en MongoDB
|
| 69 |
+
result = insert_document(COLLECTION_NAME, analysis_document)
|
| 70 |
+
if result:
|
| 71 |
+
logger.info(f"Análisis semántico guardado para {username}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
return True
|
| 73 |
+
|
| 74 |
logger.error("No se pudo insertar el documento en MongoDB")
|
| 75 |
return False
|
| 76 |
|