AIdeaText commited on
Commit
ea9f452
·
verified ·
1 Parent(s): 4272d5a

Update modules/studentact/current_situation_analysis.py

Browse files
modules/studentact/current_situation_analysis.py CHANGED
@@ -88,29 +88,51 @@ def analyze_vocabulary_diversity(doc):
88
 
89
  def analyze_cohesion(doc):
90
  """Analiza la cohesión textual"""
91
- sentences = list(doc.sents)
92
- connections = 0
93
- for i in range(len(sentences)-1):
94
- sent1_words = {token.lemma_ for token in sentences[i]}
95
- sent2_words = {token.lemma_ for token in sentences[i+1]}
96
- connections += len(sent1_words.intersection(sent2_words))
97
- return normalize_score(connections, optimal_connections=5)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
98
 
99
  def analyze_structure(doc):
100
  """Analiza la complejidad estructural"""
101
  try:
 
 
 
 
102
  root_distances = []
103
  for token in doc:
104
  if token.dep_ == 'ROOT':
105
  depths = get_dependency_depths(token)
106
  root_distances.extend(depths)
107
- avg_depth = sum(root_distances) / len(root_distances) if root_distances else 0
108
- return normalize_score(avg_depth, optimal_depth=3) # Usando optimal_depth en lugar de optimal_value
 
 
 
 
 
109
  except Exception as e:
110
  logger.error(f"Error en analyze_structure: {str(e)}")
111
  return 0.0
112
 
113
-
114
  # Funciones auxiliares de análisis
115
  def get_dependency_depths(token, depth=0):
116
  """Obtiene las profundidades de dependencia"""
@@ -122,7 +144,7 @@ def get_dependency_depths(token, depth=0):
122
  def normalize_score(value, optimal_value=1.0, range_factor=2.0, optimal_length=None,
123
  optimal_connections=None, optimal_depth=None):
124
  """
125
- Normaliza un valor a una escala de 0-1.
126
 
127
  Args:
128
  value: Valor a normalizar
@@ -136,23 +158,46 @@ def normalize_score(value, optimal_value=1.0, range_factor=2.0, optimal_length=N
136
  float: Valor normalizado entre 0 y 1
137
  """
138
  try:
 
 
 
 
 
 
 
 
 
 
 
139
  if optimal_depth is not None:
140
- diff = abs(value - optimal_depth)
141
- max_diff = optimal_depth * range_factor
142
- return 1.0 - min(diff / max_diff, 1.0)
143
  elif optimal_connections is not None:
144
- diff = abs(value - optimal_connections)
145
- max_diff = optimal_connections * range_factor
146
- return 1.0 - min(diff / max_diff, 1.0)
147
  elif optimal_length is not None:
148
- diff = abs(value - optimal_length)
149
- max_diff = optimal_length * range_factor
150
- return 1.0 - min(diff / max_diff, 1.0)
151
  else:
152
- diff = abs(value - optimal_value)
153
- max_diff = optimal_value * range_factor
154
- return 1.0 - min(diff / max_diff, 1.0)
155
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
156
  except Exception as e:
157
  logger.error(f"Error en normalize_score: {str(e)}")
158
  return 0.0
 
88
 
89
  def analyze_cohesion(doc):
90
  """Analiza la cohesión textual"""
91
+ try:
92
+ sentences = list(doc.sents)
93
+ if len(sentences) < 2:
94
+ logger.warning("Texto demasiado corto para análisis de cohesión")
95
+ return 0.0
96
+
97
+ connections = 0
98
+ for i in range(len(sentences)-1):
99
+ sent1_words = {token.lemma_ for token in sentences[i]}
100
+ sent2_words = {token.lemma_ for token in sentences[i+1]}
101
+ connections += len(sent1_words.intersection(sent2_words))
102
+
103
+ # Validar que haya conexiones antes de normalizar
104
+ if connections == 0:
105
+ logger.warning("No se encontraron conexiones entre oraciones")
106
+ return 0.0
107
+
108
+ return normalize_score(connections, optimal_connections=max(5, len(sentences) * 0.2))
109
+ except Exception as e:
110
+ logger.error(f"Error en analyze_cohesion: {str(e)}")
111
+ return 0.0
112
 
113
  def analyze_structure(doc):
114
  """Analiza la complejidad estructural"""
115
  try:
116
+ if len(doc) == 0:
117
+ logger.warning("Documento vacío")
118
+ return 0.0
119
+
120
  root_distances = []
121
  for token in doc:
122
  if token.dep_ == 'ROOT':
123
  depths = get_dependency_depths(token)
124
  root_distances.extend(depths)
125
+
126
+ if not root_distances:
127
+ logger.warning("No se encontraron estructuras de dependencia")
128
+ return 0.0
129
+
130
+ avg_depth = sum(root_distances) / len(root_distances)
131
+ return normalize_score(avg_depth, optimal_depth=max(3, len(doc) * 0.1))
132
  except Exception as e:
133
  logger.error(f"Error en analyze_structure: {str(e)}")
134
  return 0.0
135
 
 
136
  # Funciones auxiliares de análisis
137
  def get_dependency_depths(token, depth=0):
138
  """Obtiene las profundidades de dependencia"""
 
144
  def normalize_score(value, optimal_value=1.0, range_factor=2.0, optimal_length=None,
145
  optimal_connections=None, optimal_depth=None):
146
  """
147
+ Normaliza un valor a una escala de 0-1 con manejo de casos extremos.
148
 
149
  Args:
150
  value: Valor a normalizar
 
158
  float: Valor normalizado entre 0 y 1
159
  """
160
  try:
161
+ # Validar valores negativos o cero
162
+ if value < 0:
163
+ logger.warning(f"Valor negativo recibido: {value}")
164
+ return 0.0
165
+
166
+ # Manejar caso donde el valor es cero
167
+ if value == 0:
168
+ logger.warning("Valor cero recibido")
169
+ return 0.0
170
+
171
+ # Identificar el valor de referencia a usar
172
  if optimal_depth is not None:
173
+ reference = optimal_depth
 
 
174
  elif optimal_connections is not None:
175
+ reference = optimal_connections
 
 
176
  elif optimal_length is not None:
177
+ reference = optimal_length
 
 
178
  else:
179
+ reference = optimal_value
180
+
181
+ # Validar valor de referencia
182
+ if reference <= 0:
183
+ logger.warning(f"Valor de referencia inválido: {reference}")
184
+ return 0.0
185
+
186
+ # Calcular diferencia y máxima diferencia permitida
187
+ diff = abs(value - reference)
188
+ max_diff = reference * range_factor
189
+
190
+ # Validar max_diff
191
+ if max_diff <= 0:
192
+ logger.warning(f"Máxima diferencia inválida: {max_diff}")
193
+ return 0.0
194
+
195
+ # Calcular score normalizado
196
+ score = 1.0 - min(diff / max_diff, 1.0)
197
+
198
+ # Asegurar que el resultado esté entre 0 y 1
199
+ return max(0.0, min(1.0, score))
200
+
201
  except Exception as e:
202
  logger.error(f"Error en normalize_score: {str(e)}")
203
  return 0.0