Spaces:
Running
Running
Update modules/studentact/current_situation_analysis.py
Browse files
modules/studentact/current_situation_analysis.py
CHANGED
|
@@ -12,32 +12,6 @@ import logging
|
|
| 12 |
|
| 13 |
logger = logging.getLogger(__name__)
|
| 14 |
|
| 15 |
-
def display_current_situation_visual(doc, metrics):
|
| 16 |
-
try:
|
| 17 |
-
with st.container():
|
| 18 |
-
st.subheader("Riqueza de Vocabulario")
|
| 19 |
-
vocabulary_graph = create_vocabulary_network(doc)
|
| 20 |
-
if vocabulary_graph:
|
| 21 |
-
st.pyplot(vocabulary_graph)
|
| 22 |
-
plt.close(vocabulary_graph)
|
| 23 |
-
|
| 24 |
-
st.subheader("Estructura de Oraciones")
|
| 25 |
-
syntax_graph = create_syntax_complexity_graph(doc)
|
| 26 |
-
if syntax_graph:
|
| 27 |
-
st.pyplot(syntax_graph)
|
| 28 |
-
plt.close(syntax_graph)
|
| 29 |
-
|
| 30 |
-
st.subheader("Cohesi贸n del Texto")
|
| 31 |
-
cohesion_map = create_cohesion_heatmap(doc)
|
| 32 |
-
if cohesion_map:
|
| 33 |
-
st.pyplot(cohesion_map)
|
| 34 |
-
plt.close(cohesion_map)
|
| 35 |
-
|
| 36 |
-
except Exception as e:
|
| 37 |
-
logger.error(f"Error mostrando visualizaciones: {str(e)}")
|
| 38 |
-
st.error("Error al generar visualizaciones")
|
| 39 |
-
|
| 40 |
-
|
| 41 |
def analyze_text_dimensions(doc):
|
| 42 |
"""
|
| 43 |
Analiza las diferentes dimensiones del texto.
|
|
@@ -112,6 +86,8 @@ def analyze_structure(doc):
|
|
| 112 |
avg_depth = sum(root_distances) / len(root_distances) if root_distances else 0
|
| 113 |
return normalize_score(avg_depth, optimal_depth=3)
|
| 114 |
|
|
|
|
|
|
|
| 115 |
def get_dependency_depths(token, depth=0):
|
| 116 |
"""Obtiene las profundidades de dependencia"""
|
| 117 |
depths = [depth]
|
|
@@ -123,7 +99,8 @@ def normalize_score(value, optimal_value=1.0, range_factor=2.0):
|
|
| 123 |
"""Normaliza un valor a un score entre 0 y 1"""
|
| 124 |
return 1 / (1 + abs(value - optimal_value) / range_factor)
|
| 125 |
|
| 126 |
-
|
|
|
|
| 127 |
def generate_sentence_graphs(doc):
|
| 128 |
"""Genera visualizaciones de estructura de oraciones"""
|
| 129 |
fig, ax = plt.subplots(figsize=(10, 6))
|
|
@@ -191,56 +168,6 @@ def create_vocabulary_network(doc):
|
|
| 191 |
plt.axis('off')
|
| 192 |
return fig
|
| 193 |
|
| 194 |
-
def create_cohesion_heatmap(doc):
|
| 195 |
-
"""
|
| 196 |
-
Genera un mapa de calor que muestra la cohesi贸n entre p谩rrafos/oraciones.
|
| 197 |
-
"""
|
| 198 |
-
try:
|
| 199 |
-
# Dividir en oraciones
|
| 200 |
-
sentences = list(doc.sents)
|
| 201 |
-
n_sentences = len(sentences)
|
| 202 |
-
|
| 203 |
-
if n_sentences < 2:
|
| 204 |
-
return None
|
| 205 |
-
|
| 206 |
-
# Crear matriz de similitud
|
| 207 |
-
similarity_matrix = np.zeros((n_sentences, n_sentences))
|
| 208 |
-
|
| 209 |
-
# Calcular similitud entre pares de oraciones
|
| 210 |
-
for i in range(n_sentences):
|
| 211 |
-
for j in range(n_sentences):
|
| 212 |
-
sent1_lemmas = {token.lemma_ for token in sentences[i]
|
| 213 |
-
if token.is_alpha and not token.is_stop}
|
| 214 |
-
sent2_lemmas = {token.lemma_ for token in sentences[j]
|
| 215 |
-
if token.is_alpha and not token.is_stop}
|
| 216 |
-
|
| 217 |
-
if sent1_lemmas and sent2_lemmas:
|
| 218 |
-
intersection = len(sent1_lemmas & sent2_words)
|
| 219 |
-
union = len(sent1_lemmas | sent2_words)
|
| 220 |
-
similarity_matrix[i, j] = intersection / union if union > 0 else 0
|
| 221 |
-
|
| 222 |
-
# Crear visualizaci贸n
|
| 223 |
-
fig, ax = plt.subplots(figsize=(10, 8))
|
| 224 |
-
|
| 225 |
-
sns.heatmap(similarity_matrix,
|
| 226 |
-
cmap='YlOrRd',
|
| 227 |
-
square=True,
|
| 228 |
-
xticklabels=False,
|
| 229 |
-
yticklabels=False,
|
| 230 |
-
cbar_kws={'label': 'Cohesi贸n'},
|
| 231 |
-
ax=ax)
|
| 232 |
-
|
| 233 |
-
plt.title("Mapa de Cohesi贸n Textual")
|
| 234 |
-
plt.xlabel("Oraciones")
|
| 235 |
-
plt.ylabel("Oraciones")
|
| 236 |
-
|
| 237 |
-
plt.tight_layout()
|
| 238 |
-
return fig
|
| 239 |
-
|
| 240 |
-
except Exception as e:
|
| 241 |
-
logger.error(f"Error en create_cohesion_heatmap: {str(e)}")
|
| 242 |
-
return None
|
| 243 |
-
|
| 244 |
def create_syntax_complexity_graph(doc):
|
| 245 |
"""
|
| 246 |
Genera el diagrama de arco de complejidad sint谩ctica.
|
|
@@ -319,3 +246,54 @@ def create_syntax_complexity_graph(doc):
|
|
| 319 |
except Exception as e:
|
| 320 |
logger.error(f"Error en create_syntax_complexity_graph: {str(e)}")
|
| 321 |
return None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
logger = logging.getLogger(__name__)
|
| 14 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
def analyze_text_dimensions(doc):
|
| 16 |
"""
|
| 17 |
Analiza las diferentes dimensiones del texto.
|
|
|
|
| 86 |
avg_depth = sum(root_distances) / len(root_distances) if root_distances else 0
|
| 87 |
return normalize_score(avg_depth, optimal_depth=3)
|
| 88 |
|
| 89 |
+
|
| 90 |
+
# Funciones auxiliares de an谩lisis
|
| 91 |
def get_dependency_depths(token, depth=0):
|
| 92 |
"""Obtiene las profundidades de dependencia"""
|
| 93 |
depths = [depth]
|
|
|
|
| 99 |
"""Normaliza un valor a un score entre 0 y 1"""
|
| 100 |
return 1 / (1 + abs(value - optimal_value) / range_factor)
|
| 101 |
|
| 102 |
+
|
| 103 |
+
# Funciones de generaci贸n de gr谩ficos
|
| 104 |
def generate_sentence_graphs(doc):
|
| 105 |
"""Genera visualizaciones de estructura de oraciones"""
|
| 106 |
fig, ax = plt.subplots(figsize=(10, 6))
|
|
|
|
| 168 |
plt.axis('off')
|
| 169 |
return fig
|
| 170 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
def create_syntax_complexity_graph(doc):
|
| 172 |
"""
|
| 173 |
Genera el diagrama de arco de complejidad sint谩ctica.
|
|
|
|
| 246 |
except Exception as e:
|
| 247 |
logger.error(f"Error en create_syntax_complexity_graph: {str(e)}")
|
| 248 |
return None
|
| 249 |
+
|
| 250 |
+
|
| 251 |
+
def create_cohesion_heatmap(doc):
|
| 252 |
+
"""
|
| 253 |
+
Genera un mapa de calor que muestra la cohesi贸n entre p谩rrafos/oraciones.
|
| 254 |
+
"""
|
| 255 |
+
try:
|
| 256 |
+
# Dividir en oraciones
|
| 257 |
+
sentences = list(doc.sents)
|
| 258 |
+
n_sentences = len(sentences)
|
| 259 |
+
|
| 260 |
+
if n_sentences < 2:
|
| 261 |
+
return None
|
| 262 |
+
|
| 263 |
+
# Crear matriz de similitud
|
| 264 |
+
similarity_matrix = np.zeros((n_sentences, n_sentences))
|
| 265 |
+
|
| 266 |
+
# Calcular similitud entre pares de oraciones
|
| 267 |
+
for i in range(n_sentences):
|
| 268 |
+
for j in range(n_sentences):
|
| 269 |
+
sent1_lemmas = {token.lemma_ for token in sentences[i]
|
| 270 |
+
if token.is_alpha and not token.is_stop}
|
| 271 |
+
sent2_lemmas = {token.lemma_ for token in sentences[j]
|
| 272 |
+
if token.is_alpha and not token.is_stop}
|
| 273 |
+
|
| 274 |
+
if sent1_lemmas and sent2_lemmas:
|
| 275 |
+
intersection = len(sent1_lemmas & sent2_words)
|
| 276 |
+
union = len(sent1_lemmas | sent2_words)
|
| 277 |
+
similarity_matrix[i, j] = intersection / union if union > 0 else 0
|
| 278 |
+
|
| 279 |
+
# Crear visualizaci贸n
|
| 280 |
+
fig, ax = plt.subplots(figsize=(10, 8))
|
| 281 |
+
|
| 282 |
+
sns.heatmap(similarity_matrix,
|
| 283 |
+
cmap='YlOrRd',
|
| 284 |
+
square=True,
|
| 285 |
+
xticklabels=False,
|
| 286 |
+
yticklabels=False,
|
| 287 |
+
cbar_kws={'label': 'Cohesi贸n'},
|
| 288 |
+
ax=ax)
|
| 289 |
+
|
| 290 |
+
plt.title("Mapa de Cohesi贸n Textual")
|
| 291 |
+
plt.xlabel("Oraciones")
|
| 292 |
+
plt.ylabel("Oraciones")
|
| 293 |
+
|
| 294 |
+
plt.tight_layout()
|
| 295 |
+
return fig
|
| 296 |
+
|
| 297 |
+
except Exception as e:
|
| 298 |
+
logger.error(f"Error en create_cohesion_heatmap: {str(e)}")
|
| 299 |
+
return None
|