Create semantic_mongo_live_db.py
Browse files
modules/database/semantic_mongo_live_db.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# modules/database/semantic_mongo_live_db.py
|
| 2 |
+
import logging
|
| 3 |
+
from datetime import datetime, timezone
|
| 4 |
+
import base64
|
| 5 |
+
|
| 6 |
+
# Importaciones locales
|
| 7 |
+
from .mongo_db import get_collection, insert_document, find_documents
|
| 8 |
+
|
| 9 |
+
logger = logging.getLogger(__name__)
|
| 10 |
+
COLLECTION_NAME = 'student_semantic_live_analysis'
|
| 11 |
+
|
| 12 |
+
def store_student_semantic_live_result(username, text, analysis_result, lang_code='en'):
|
| 13 |
+
"""
|
| 14 |
+
Guarda el resultado del análisis semántico en vivo en MongoDB.
|
| 15 |
+
"""
|
| 16 |
+
try:
|
| 17 |
+
if not username or not text or not analysis_result:
|
| 18 |
+
logger.error("Datos insuficientes para guardar el análisis")
|
| 19 |
+
return False
|
| 20 |
+
|
| 21 |
+
# Preparar el gráfico conceptual
|
| 22 |
+
concept_graph_data = None
|
| 23 |
+
if 'concept_graph' in analysis_result and analysis_result['concept_graph'] is not None:
|
| 24 |
+
try:
|
| 25 |
+
if isinstance(analysis_result['concept_graph'], bytes):
|
| 26 |
+
concept_graph_data = base64.b64encode(analysis_result['concept_graph']).decode('utf-8')
|
| 27 |
+
else:
|
| 28 |
+
logger.warning("El gráfico conceptual no está en formato bytes")
|
| 29 |
+
except Exception as e:
|
| 30 |
+
logger.error(f"Error al codificar gráfico conceptual: {str(e)}")
|
| 31 |
+
|
| 32 |
+
# Crear documento para MongoDB
|
| 33 |
+
analysis_document = {
|
| 34 |
+
'username': username,
|
| 35 |
+
'timestamp': datetime.now(timezone.utc),
|
| 36 |
+
'text': text,
|
| 37 |
+
'analysis_type': 'semantic_live',
|
| 38 |
+
'key_concepts': analysis_result.get('key_concepts', []),
|
| 39 |
+
'concept_centrality': analysis_result.get('concept_centrality', {}),
|
| 40 |
+
'concept_graph': concept_graph_data,
|
| 41 |
+
'language': lang_code
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
# Insertar en MongoDB
|
| 45 |
+
result = insert_document(COLLECTION_NAME, analysis_document)
|
| 46 |
+
if result:
|
| 47 |
+
logger.info(f"Análisis semántico en vivo guardado para {username}")
|
| 48 |
+
return True
|
| 49 |
+
|
| 50 |
+
logger.error("No se pudo insertar el documento en MongoDB")
|
| 51 |
+
return False
|
| 52 |
+
|
| 53 |
+
except Exception as e:
|
| 54 |
+
logger.error(f"Error al guardar el análisis semántico en vivo: {str(e)}")
|
| 55 |
+
return False
|
| 56 |
+
|
| 57 |
+
def get_student_semantic_live_analysis(username, limit=10):
|
| 58 |
+
"""
|
| 59 |
+
Recupera los análisis semánticos en vivo de un estudiante.
|
| 60 |
+
"""
|
| 61 |
+
try:
|
| 62 |
+
query = {
|
| 63 |
+
"username": username,
|
| 64 |
+
"analysis_type": "semantic_live"
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
projection = {
|
| 68 |
+
"timestamp": 1,
|
| 69 |
+
"text": 1,
|
| 70 |
+
"key_concepts": 1,
|
| 71 |
+
"concept_graph": 1,
|
| 72 |
+
"_id": 1
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
results = find_documents(
|
| 76 |
+
COLLECTION_NAME,
|
| 77 |
+
query,
|
| 78 |
+
projection=projection,
|
| 79 |
+
sort=[("timestamp", -1)],
|
| 80 |
+
limit=limit
|
| 81 |
+
)
|
| 82 |
+
|
| 83 |
+
logger.info(f"Recuperados {len(results)} análisis semánticos en vivo para {username}")
|
| 84 |
+
return results
|
| 85 |
+
|
| 86 |
+
except Exception as e:
|
| 87 |
+
logger.error(f"Error recuperando análisis semántico en vivo: {str(e)}")
|
| 88 |
+
return []
|
| 89 |
+
|
| 90 |
+
__all__ = [
|
| 91 |
+
'store_student_semantic_live_result',
|
| 92 |
+
'get_student_semantic_live_analysis'
|
| 93 |
+
]
|