Create morphosyntax_iterative_mongo_db.py
Browse files
modules/database/morphosyntax_iterative_mongo_db.py
ADDED
@@ -0,0 +1,154 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# modules/database/morphosyntax_iterative_mongo_db.py
|
2 |
+
|
3 |
+
from datetime import datetime, timezone
|
4 |
+
import logging
|
5 |
+
from .mongo_db import get_collection, insert_document, find_documents, update_document, delete_document
|
6 |
+
|
7 |
+
logger = logging.getLogger(__name__)
|
8 |
+
|
9 |
+
# Constantes para las colecciones
|
10 |
+
BASE_COLLECTION = 'student_morphosyntax_base'
|
11 |
+
ITERATION_COLLECTION = 'student_morphosyntax_iterations'
|
12 |
+
|
13 |
+
def store_student_morphosyntax_base(username, text, arc_diagrams):
|
14 |
+
"""
|
15 |
+
Almacena el análisis morfosintáctico base
|
16 |
+
Returns: ID del documento base o None si hay error
|
17 |
+
"""
|
18 |
+
try:
|
19 |
+
base_document = {
|
20 |
+
'username': username,
|
21 |
+
'timestamp': datetime.now(timezone.utc).isoformat(),
|
22 |
+
'text': text,
|
23 |
+
'arc_diagrams': arc_diagrams,
|
24 |
+
'analysis_type': 'morphosyntax_base',
|
25 |
+
'has_iterations': False
|
26 |
+
}
|
27 |
+
|
28 |
+
collection = get_collection(BASE_COLLECTION)
|
29 |
+
result = collection.insert_one(base_document)
|
30 |
+
logger.info(f"Análisis base guardado para {username}")
|
31 |
+
return str(result.inserted_id)
|
32 |
+
|
33 |
+
except Exception as e:
|
34 |
+
logger.error(f"Error almacenando análisis base: {str(e)}")
|
35 |
+
return None
|
36 |
+
|
37 |
+
def store_student_morphosyntax_iteration(username, base_id, original_text, iteration_text, arc_diagrams):
|
38 |
+
"""
|
39 |
+
Almacena una iteración de análisis morfosintáctico
|
40 |
+
Returns: ID de la iteración o None si hay error
|
41 |
+
"""
|
42 |
+
try:
|
43 |
+
iteration_document = {
|
44 |
+
'username': username,
|
45 |
+
'base_id': base_id,
|
46 |
+
'timestamp': datetime.now(timezone.utc).isoformat(),
|
47 |
+
'original_text': original_text,
|
48 |
+
'iteration_text': iteration_text,
|
49 |
+
'arc_diagrams': arc_diagrams,
|
50 |
+
'analysis_type': 'morphosyntax_iteration'
|
51 |
+
}
|
52 |
+
|
53 |
+
collection = get_collection(ITERATION_COLLECTION)
|
54 |
+
result = collection.insert_one(iteration_document)
|
55 |
+
|
56 |
+
# Actualizar documento base para indicar que tiene iteraciones
|
57 |
+
base_collection = get_collection(BASE_COLLECTION)
|
58 |
+
base_collection.update_one(
|
59 |
+
{'_id': base_id},
|
60 |
+
{'$set': {'has_iterations': True}}
|
61 |
+
)
|
62 |
+
|
63 |
+
logger.info(f"Iteración guardada para {username}, base_id: {base_id}")
|
64 |
+
return str(result.inserted_id)
|
65 |
+
|
66 |
+
except Exception as e:
|
67 |
+
logger.error(f"Error almacenando iteración: {str(e)}")
|
68 |
+
return None
|
69 |
+
|
70 |
+
def get_student_morphosyntax_analysis(username, limit=10):
|
71 |
+
"""
|
72 |
+
Obtiene los análisis base y sus iteraciones
|
73 |
+
Returns: Lista de análisis con sus iteraciones
|
74 |
+
"""
|
75 |
+
try:
|
76 |
+
# Obtener análisis base
|
77 |
+
base_collection = get_collection(BASE_COLLECTION)
|
78 |
+
base_query = {
|
79 |
+
"username": username,
|
80 |
+
"analysis_type": "morphosyntax_base"
|
81 |
+
}
|
82 |
+
base_analyses = list(base_collection.find(base_query).sort("timestamp", -1).limit(limit))
|
83 |
+
|
84 |
+
# Para cada análisis base, obtener sus iteraciones
|
85 |
+
iteration_collection = get_collection(ITERATION_COLLECTION)
|
86 |
+
for analysis in base_analyses:
|
87 |
+
base_id = analysis['_id']
|
88 |
+
iterations = list(iteration_collection.find({"base_id": base_id}).sort("timestamp", -1))
|
89 |
+
analysis['iterations'] = iterations
|
90 |
+
|
91 |
+
return base_analyses
|
92 |
+
|
93 |
+
except Exception as e:
|
94 |
+
logger.error(f"Error obteniendo análisis: {str(e)}")
|
95 |
+
return []
|
96 |
+
|
97 |
+
def update_student_morphosyntax_analysis(analysis_id, is_base, update_data):
|
98 |
+
"""
|
99 |
+
Actualiza un análisis base o iteración
|
100 |
+
"""
|
101 |
+
try:
|
102 |
+
collection_name = BASE_COLLECTION if is_base else ITERATION_COLLECTION
|
103 |
+
collection = get_collection(collection_name)
|
104 |
+
|
105 |
+
query = {"_id": analysis_id}
|
106 |
+
update = {"$set": update_data}
|
107 |
+
|
108 |
+
result = update_document(collection_name, query, update)
|
109 |
+
return result
|
110 |
+
|
111 |
+
except Exception as e:
|
112 |
+
logger.error(f"Error actualizando análisis: {str(e)}")
|
113 |
+
return False
|
114 |
+
|
115 |
+
def delete_student_morphosyntax_analysis(analysis_id, is_base):
|
116 |
+
"""
|
117 |
+
Elimina un análisis base o iteración
|
118 |
+
Si es base, también elimina todas sus iteraciones
|
119 |
+
"""
|
120 |
+
try:
|
121 |
+
if is_base:
|
122 |
+
# Primero eliminar todas las iteraciones
|
123 |
+
iteration_collection = get_collection(ITERATION_COLLECTION)
|
124 |
+
iteration_collection.delete_many({"base_id": analysis_id})
|
125 |
+
|
126 |
+
# Luego eliminar el análisis base
|
127 |
+
collection = get_collection(BASE_COLLECTION)
|
128 |
+
else:
|
129 |
+
collection = get_collection(ITERATION_COLLECTION)
|
130 |
+
|
131 |
+
query = {"_id": analysis_id}
|
132 |
+
result = delete_document(collection.name, query)
|
133 |
+
return result
|
134 |
+
|
135 |
+
except Exception as e:
|
136 |
+
logger.error(f"Error eliminando análisis: {str(e)}")
|
137 |
+
return False
|
138 |
+
|
139 |
+
def get_student_morphosyntax_data(username):
|
140 |
+
"""
|
141 |
+
Obtiene todos los datos de análisis morfosintáctico de un estudiante
|
142 |
+
Returns: Diccionario con todos los análisis y sus iteraciones
|
143 |
+
"""
|
144 |
+
try:
|
145 |
+
analyses = get_student_morphosyntax_analysis(username, limit=None)
|
146 |
+
return {
|
147 |
+
'entries': analyses,
|
148 |
+
'total_analyses': len(analyses),
|
149 |
+
'has_iterations': any(a.get('has_iterations', False) for a in analyses)
|
150 |
+
}
|
151 |
+
|
152 |
+
except Exception as e:
|
153 |
+
logger.error(f"Error obteniendo datos del estudiante: {str(e)}")
|
154 |
+
return {'entries': [], 'total_analyses': 0, 'has_iterations': False}
|