Update modules/semantic/semantic_process.py
Browse files
modules/semantic/semantic_process.py
CHANGED
@@ -1,27 +1,108 @@
|
|
1 |
-
|
2 |
import streamlit as st
|
3 |
-
|
4 |
-
|
5 |
from ..text_analysis.semantic_analysis import (
|
6 |
perform_semantic_analysis,
|
7 |
fig_to_bytes,
|
8 |
-
fig_to_html
|
9 |
-
identify_key_concepts,
|
10 |
-
create_concept_graph,
|
11 |
-
visualize_concept_graph,
|
12 |
-
create_entity_graph,
|
13 |
-
visualize_entity_graph,
|
14 |
-
|
15 |
visualize_topic_graph,
|
16 |
-
generate_summary,
|
17 |
-
extract_entities,
|
18 |
analyze_sentiment,
|
19 |
-
extract_topics
|
20 |
)
|
21 |
|
22 |
-
from ..database.
|
23 |
|
24 |
import logging
|
25 |
logger = logging.getLogger(__name__)
|
26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
|
|
|
1 |
+
#modules/semantic/semantic_process.py
|
2 |
import streamlit as st
|
|
|
|
|
3 |
from ..text_analysis.semantic_analysis import (
|
4 |
perform_semantic_analysis,
|
5 |
fig_to_bytes,
|
6 |
+
fig_to_html,
|
7 |
+
identify_key_concepts,
|
8 |
+
create_concept_graph,
|
9 |
+
visualize_concept_graph,
|
10 |
+
create_entity_graph,
|
11 |
+
visualize_entity_graph,
|
12 |
+
create_topic_graph,
|
13 |
visualize_topic_graph,
|
14 |
+
generate_summary,
|
15 |
+
extract_entities,
|
16 |
analyze_sentiment,
|
17 |
+
extract_topics
|
18 |
)
|
19 |
|
20 |
+
from ..database.semantic_mongo_db import store_student_semantic_result
|
21 |
|
22 |
import logging
|
23 |
logger = logging.getLogger(__name__)
|
24 |
|
25 |
+
def process_semantic_input(text, lang_code, nlp_models, t):
|
26 |
+
"""
|
27 |
+
Procesa el texto ingresado para realizar el análisis semántico.
|
28 |
+
|
29 |
+
Args:
|
30 |
+
text: Texto a analizar
|
31 |
+
lang_code: Código del idioma
|
32 |
+
nlp_models: Diccionario de modelos spaCy
|
33 |
+
t: Diccionario de traducciones
|
34 |
+
|
35 |
+
Returns:
|
36 |
+
dict: Resultados del análisis
|
37 |
+
"""
|
38 |
+
try:
|
39 |
+
# Realizar el análisis semántico
|
40 |
+
doc = nlp_models[lang_code](text)
|
41 |
+
|
42 |
+
# Obtener el análisis completo
|
43 |
+
analysis = perform_semantic_analysis(text, nlp_models[lang_code], lang_code)
|
44 |
+
|
45 |
+
# Guardar el análisis en la base de datos
|
46 |
+
store_student_semantic_result(
|
47 |
+
st.session_state.username,
|
48 |
+
text,
|
49 |
+
analysis
|
50 |
+
)
|
51 |
+
|
52 |
+
return {
|
53 |
+
'analysis': analysis,
|
54 |
+
'success': True,
|
55 |
+
'message': t.get('success_message', 'Analysis completed successfully')
|
56 |
+
}
|
57 |
+
|
58 |
+
except Exception as e:
|
59 |
+
logger.error(f"Error en el análisis semántico: {str(e)}")
|
60 |
+
return {
|
61 |
+
'analysis': None,
|
62 |
+
'success': False,
|
63 |
+
'message': t.get('error_message', f'Error in analysis: {str(e)}')
|
64 |
+
}
|
65 |
+
|
66 |
+
def format_semantic_results(analysis_result, t):
|
67 |
+
"""
|
68 |
+
Formatea los resultados del análisis para su visualización.
|
69 |
+
|
70 |
+
Args:
|
71 |
+
analysis_result: Resultado del análisis semántico
|
72 |
+
t: Diccionario de traducciones
|
73 |
+
|
74 |
+
Returns:
|
75 |
+
dict: Resultados formateados para visualización
|
76 |
+
"""
|
77 |
+
if not analysis_result['success']:
|
78 |
+
return {
|
79 |
+
'formatted_text': analysis_result['message'],
|
80 |
+
'visualizations': None
|
81 |
+
}
|
82 |
+
|
83 |
+
# Formatear los resultados
|
84 |
+
formatted_sections = []
|
85 |
+
|
86 |
+
# Formatear conceptos clave
|
87 |
+
if 'key_concepts' in analysis_result['analysis']:
|
88 |
+
concepts_section = [f"### {t.get('key_concepts', 'Key Concepts')}"]
|
89 |
+
concepts_section.extend([
|
90 |
+
f"- {concept}: {frequency:.2f}"
|
91 |
+
for concept, frequency in analysis_result['analysis']['key_concepts']
|
92 |
+
])
|
93 |
+
formatted_sections.append('\n'.join(concepts_section))
|
94 |
+
|
95 |
+
return {
|
96 |
+
'formatted_text': '\n\n'.join(formatted_sections),
|
97 |
+
'visualizations': {
|
98 |
+
'concept_graph': analysis_result['analysis'].get('concept_graph'),
|
99 |
+
'entity_graph': analysis_result['analysis'].get('entity_graph')
|
100 |
+
}
|
101 |
+
}
|
102 |
+
|
103 |
+
# Re-exportar funciones necesarias
|
104 |
+
__all__ = [
|
105 |
+
'process_semantic_input',
|
106 |
+
'format_semantic_results'
|
107 |
+
]
|
108 |
|