AIdeaText commited on
Commit
dd8c05b
·
verified ·
1 Parent(s): 330acad

Update modules/semantic/semantic_process.py

Browse files
Files changed (1) hide show
  1. modules/semantic/semantic_process.py +108 -107
modules/semantic/semantic_process.py CHANGED
@@ -1,108 +1,109 @@
1
- # modules/semantic/semantic_process.py
2
- import streamlit as st
3
- import matplotlib.pyplot as plt
4
- import io
5
- import base64
6
- import logging
7
-
8
- from ..text_analysis.semantic_analysis import (
9
- perform_semantic_analysis,
10
- identify_key_concepts,
11
- create_concept_graph,
12
- visualize_concept_graph
13
- )
14
- from ..database.semantic_mongo_db import store_student_semantic_result
15
-
16
- logger = logging.getLogger(__name__)
17
-
18
- def process_semantic_input(text, lang_code, nlp_models, t):
19
- """
20
- Procesa el texto ingresado para realizar el análisis semántico.
21
- """
22
- try:
23
- logger.info(f"Iniciando análisis semántico para texto de {len(text)} caracteres")
24
-
25
- # Realizar el análisis semántico
26
- nlp = nlp_models[lang_code]
27
- analysis_result = perform_semantic_analysis(text, nlp, lang_code)
28
-
29
- if not analysis_result['success']:
30
- return {
31
- 'success': False,
32
- 'message': analysis_result['error'],
33
- 'analysis': None
34
- }
35
-
36
- logger.info("Análisis semántico completado. Guardando resultados...")
37
-
38
- # Intentar guardar en la base de datos
39
- try:
40
- store_result = store_student_semantic_result(
41
- st.session_state.username,
42
- text,
43
- analysis_result
44
- )
45
- if not store_result:
46
- logger.warning("No se pudo guardar el análisis en la base de datos")
47
- except Exception as db_error:
48
- logger.error(f"Error al guardar en base de datos: {str(db_error)}")
49
-
50
- # Devolver el resultado incluso si falla el guardado
51
- return {
52
- 'success': True,
53
- 'message': t.get('success_message', 'Analysis completed successfully'),
54
- 'analysis': {
55
- 'key_concepts': analysis_result['key_concepts'],
56
- 'concept_graph': analysis_result['concept_graph']
57
- }
58
- }
59
-
60
- except Exception as e:
61
- logger.error(f"Error en process_semantic_input: {str(e)}")
62
- return {
63
- 'success': False,
64
- 'message': str(e),
65
- 'analysis': None
66
- }
67
-
68
- def format_semantic_results(analysis_result, t):
69
- """
70
- Formatea los resultados del análisis para su visualización.
71
- """
72
- try:
73
- if not analysis_result['success']:
74
- return {
75
- 'formatted_text': analysis_result['message'],
76
- 'visualizations': None
77
- }
78
-
79
- formatted_sections = []
80
- analysis = analysis_result['analysis']
81
-
82
- # Formatear conceptos clave
83
- if 'key_concepts' in analysis:
84
- concepts_section = [f"### {t.get('key_concepts', 'Key Concepts')}"]
85
- concepts_section.extend([
86
- f"- {concept}: {frequency:.2f}"
87
- for concept, frequency in analysis['key_concepts']
88
- ])
89
- formatted_sections.append('\n'.join(concepts_section))
90
-
91
- return {
92
- 'formatted_text': '\n\n'.join(formatted_sections),
93
- 'visualizations': {
94
- 'concept_graph': analysis.get('concept_graph')
95
- }
96
- }
97
-
98
- except Exception as e:
99
- logger.error(f"Error en format_semantic_results: {str(e)}")
100
- return {
101
- 'formatted_text': str(e),
102
- 'visualizations': None
103
- }
104
-
105
- __all__ = [
106
- 'process_semantic_input',
107
- 'format_semantic_results'
 
108
  ]
 
1
+ # modules/semantic/semantic_process.py
2
+ import streamlit as st
3
+ import matplotlib.pyplot as plt
4
+ import io
5
+ import base64
6
+ import logging
7
+
8
+ from ..text_analysis.semantic_analysis import (
9
+ perform_semantic_analysis,
10
+ identify_key_concepts,
11
+ create_concept_graph,
12
+ visualize_concept_graph
13
+ )
14
+ from ..database.semantic_mongo_db import store_student_semantic_result
15
+
16
+ logger = logging.getLogger(__name__)
17
+
18
+ def process_semantic_input(text_content, lang_code, nlp_models, semantic_t):
19
+ """
20
+ Procesa el texto ingresado para realizar el análisis semántico.
21
+ """
22
+ try:
23
+ logger.info(f"Iniciando análisis semántico para texto de {len(text)} caracteres")
24
+
25
+ # Realizar el análisis semántico
26
+ nlp = nlp_models[lang_code]
27
+ analysis_result = (text_content, nlp_models[lang_code], lang_code, semantic_t)
28
+ return format_semantic_results(result, lang_code, semantic_t)
29
+
30
+ if not analysis_result['success']:
31
+ return {
32
+ 'success': False,
33
+ 'message': analysis_result['error'],
34
+ 'analysis': None
35
+ }
36
+
37
+ logger.info("Análisis semántico completado. Guardando resultados...")
38
+
39
+ # Intentar guardar en la base de datos
40
+ try:
41
+ store_result = store_student_semantic_result(
42
+ st.session_state.username,
43
+ text,
44
+ analysis_result
45
+ )
46
+ if not store_result:
47
+ logger.warning("No se pudo guardar el análisis en la base de datos")
48
+ except Exception as db_error:
49
+ logger.error(f"Error al guardar en base de datos: {str(db_error)}")
50
+
51
+ # Devolver el resultado incluso si falla el guardado
52
+ return {
53
+ 'success': True,
54
+ 'message': t.get('success_message', 'Analysis completed successfully'),
55
+ 'analysis': {
56
+ 'key_concepts': analysis_result['key_concepts'],
57
+ 'concept_graph': analysis_result['concept_graph']
58
+ }
59
+ }
60
+
61
+ except Exception as e:
62
+ logger.error(f"Error en process_semantic_input: {str(e)}")
63
+ return {
64
+ 'success': False,
65
+ 'message': str(e),
66
+ 'analysis': None
67
+ }
68
+
69
+ def format_semantic_results(analysis_result, t):
70
+ """
71
+ Formatea los resultados del análisis para su visualización.
72
+ """
73
+ try:
74
+ if not analysis_result['success']:
75
+ return {
76
+ 'formatted_text': analysis_result['message'],
77
+ 'visualizations': None
78
+ }
79
+
80
+ formatted_sections = []
81
+ analysis = analysis_result['analysis']
82
+
83
+ # Formatear conceptos clave
84
+ if 'key_concepts' in analysis:
85
+ concepts_section = [f"### {t.get('key_concepts', 'Key Concepts')}"]
86
+ concepts_section.extend([
87
+ f"- {concept}: {frequency:.2f}"
88
+ for concept, frequency in analysis['key_concepts']
89
+ ])
90
+ formatted_sections.append('\n'.join(concepts_section))
91
+
92
+ return {
93
+ 'formatted_text': '\n\n'.join(formatted_sections),
94
+ 'visualizations': {
95
+ 'concept_graph': analysis.get('concept_graph')
96
+ }
97
+ }
98
+
99
+ except Exception as e:
100
+ logger.error(f"Error en format_semantic_results: {str(e)}")
101
+ return {
102
+ 'formatted_text': str(e),
103
+ 'visualizations': None
104
+ }
105
+
106
+ __all__ = [
107
+ 'process_semantic_input',
108
+ 'format_semantic_results'
109
  ]