AIdeaText commited on
Commit
7ef8110
1 Parent(s): 77a25bb

Update modules/database/semantic_mongo_db.py

Browse files
Files changed (1) hide show
  1. modules/database/semantic_mongo_db.py +38 -15
modules/database/semantic_mongo_db.py CHANGED
@@ -1,12 +1,24 @@
1
  #/modules/database/semantic_mongo_db.py
2
- import matplotlib.pyplot as plt # Añadir esta importación al inicio
 
3
  import io
4
  import base64
5
- from .mongo_db import insert_document, find_documents, update_document, delete_document
6
  from datetime import datetime, timezone
7
  import logging
8
 
9
- logger = logging.getLogger(__name__)
 
 
 
 
 
 
 
 
 
 
 
 
10
  COLLECTION_NAME = 'student_semantic_analysis'
11
 
12
  def store_student_semantic_result(username, text, analysis_result):
@@ -16,22 +28,33 @@ def store_student_semantic_result(username, text, analysis_result):
16
  username: Nombre del usuario
17
  text: Texto analizado
18
  analysis_result: Resultado del análisis
 
 
19
  """
20
  try:
21
- # Convertir gráfico a formato base64
22
  concept_graph_data = None
23
- if 'concept_graph' in analysis_result:
24
  buf = io.BytesIO()
25
- analysis_result['concept_graph'].savefig(buf, format='png')
26
- buf.seek(0)
27
- concept_graph_data = base64.b64encode(buf.getvalue()).decode('utf-8')
 
 
 
 
28
 
 
29
  entity_graph_data = None
30
- if 'entity_graph' in analysis_result:
31
  buf = io.BytesIO()
32
- analysis_result['entity_graph'].savefig(buf, format='png')
33
- buf.seek(0)
34
- entity_graph_data = base64.b64encode(buf.getvalue()).decode('utf-8')
 
 
 
 
35
 
36
  # Crear documento para MongoDB
37
  analysis_document = {
@@ -57,9 +80,9 @@ def store_student_semantic_result(username, text, analysis_result):
57
  except Exception as e:
58
  logger.error(f"Error al guardar el análisis semántico: {str(e)}")
59
  return False
60
-
61
-
62
-
63
 
64
  def get_student_semantic_analysis(username, limit=10):
65
  """
 
1
  #/modules/database/semantic_mongo_db.py
2
+
3
+ # Importaciones estándar
4
  import io
5
  import base64
 
6
  from datetime import datetime, timezone
7
  import logging
8
 
9
+ # Importaciones de terceros
10
+ import matplotlib.pyplot as plt
11
+
12
+ # Importaciones locales
13
+ from .mongo_db import (
14
+ insert_document,
15
+ find_documents,
16
+ update_document,
17
+ delete_document
18
+ )
19
+
20
+ # Configuración del logger
21
+ logger = logging.getLogger(__name__) # Cambiado de name a __name__
22
  COLLECTION_NAME = 'student_semantic_analysis'
23
 
24
  def store_student_semantic_result(username, text, analysis_result):
 
28
  username: Nombre del usuario
29
  text: Texto analizado
30
  analysis_result: Resultado del análisis
31
+ Returns:
32
+ bool: True si se guardó correctamente, False en caso contrario
33
  """
34
  try:
35
+ # Convertir gráfico conceptual a formato base64
36
  concept_graph_data = None
37
+ if 'concept_graph' in analysis_result and analysis_result['concept_graph'] is not None:
38
  buf = io.BytesIO()
39
+ try:
40
+ analysis_result['concept_graph'].savefig(buf, format='png', dpi=300, bbox_inches='tight')
41
+ buf.seek(0)
42
+ concept_graph_data = base64.b64encode(buf.getvalue()).decode('utf-8')
43
+ plt.close(analysis_result['concept_graph']) # Cerrar la figura
44
+ except Exception as e:
45
+ logger.error(f"Error al convertir gráfico conceptual: {str(e)}")
46
 
47
+ # Convertir gráfico de entidades a formato base64
48
  entity_graph_data = None
49
+ if 'entity_graph' in analysis_result and analysis_result['entity_graph'] is not None:
50
  buf = io.BytesIO()
51
+ try:
52
+ analysis_result['entity_graph'].savefig(buf, format='png', dpi=300, bbox_inches='tight')
53
+ buf.seek(0)
54
+ entity_graph_data = base64.b64encode(buf.getvalue()).decode('utf-8')
55
+ plt.close(analysis_result['entity_graph']) # Cerrar la figura
56
+ except Exception as e:
57
+ logger.error(f"Error al convertir gráfico de entidades: {str(e)}")
58
 
59
  # Crear documento para MongoDB
60
  analysis_document = {
 
80
  except Exception as e:
81
  logger.error(f"Error al guardar el análisis semántico: {str(e)}")
82
  return False
83
+ finally:
84
+ # Asegurarnos de cerrar cualquier figura pendiente
85
+ plt.close('all')
86
 
87
  def get_student_semantic_analysis(username, limit=10):
88
  """