Update modules/morphosyntax/morphosyntax_interface.py
Browse files
modules/morphosyntax/morphosyntax_interface.py
CHANGED
@@ -31,82 +31,91 @@ import logging
|
|
31 |
logger = logging.getLogger(__name__)
|
32 |
|
33 |
def display_morphosyntax_interface(lang_code, nlp_models, morpho_t):
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
# Inicializar el estado de la entrada
|
45 |
-
input_key = f"morphosyntax_input_{lang_code}"
|
46 |
-
if input_key not in st.session_state:
|
47 |
-
st.session_state[input_key] = ""
|
48 |
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
|
|
58 |
|
59 |
-
|
60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
85 |
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
else:
|
94 |
-
st.error(morpho_t.get('error_message', 'Error saving analysis'))
|
95 |
-
|
96 |
-
except Exception as e:
|
97 |
-
st.error(morpho_t.get('error_processing', f'Error processing text: {str(e)}'))
|
98 |
else:
|
99 |
-
st.
|
100 |
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
st.session_state.morphosyntax_result,
|
105 |
-
lang_code,
|
106 |
-
morpho_t
|
107 |
-
)
|
108 |
-
else:
|
109 |
-
st.info(morpho_t.get('morpho_initial_message', 'Enter text to begin analysis'))
|
110 |
|
111 |
############################################################################################################
|
112 |
def display_morphosyntax_results(result, lang_code, morpho_t):
|
|
|
31 |
logger = logging.getLogger(__name__)
|
32 |
|
33 |
def display_morphosyntax_interface(lang_code, nlp_models, morpho_t):
|
34 |
+
try:
|
35 |
+
# Inicializar el estado de la entrada
|
36 |
+
input_key = f"morphosyntax_input_{lang_code}"
|
37 |
+
if input_key not in st.session_state:
|
38 |
+
st.session_state[input_key] = ""
|
39 |
+
|
40 |
+
# Inicializar contador de análisis si no existe
|
41 |
+
if 'analysis_counter' not in st.session_state:
|
42 |
+
st.session_state.analysis_counter = 0
|
|
|
|
|
|
|
|
|
|
|
43 |
|
44 |
+
# Campo de entrada de texto
|
45 |
+
sentence_input = st.text_area(
|
46 |
+
morpho_t.get('morpho_input_label', 'Enter text to analyze'),
|
47 |
+
height=150,
|
48 |
+
placeholder=morpho_t.get('morpho_input_placeholder', 'Enter your text here...'),
|
49 |
+
value=st.session_state[input_key],
|
50 |
+
key=f"text_area_{lang_code}_{st.session_state.analysis_counter}", # Clave única
|
51 |
+
on_change=lambda: setattr(st.session_state, input_key,
|
52 |
+
st.session_state[f"text_area_{lang_code}_{st.session_state.analysis_counter}"])
|
53 |
+
)
|
54 |
|
55 |
+
# Botón de análisis con clave única
|
56 |
+
if st.button(
|
57 |
+
morpho_t.get('analyze_button', 'Analyze text'),
|
58 |
+
key=f"analyze_button_{lang_code}_{st.session_state.analysis_counter}"
|
59 |
+
):
|
60 |
+
current_input = st.session_state[input_key]
|
61 |
+
|
62 |
+
if current_input:
|
63 |
+
try:
|
64 |
+
with st.spinner(morpho_t.get('processing', 'Processing...')):
|
65 |
+
# Procesar el texto
|
66 |
+
doc = nlp_models[lang_code](current_input)
|
67 |
+
|
68 |
+
# Realizar análisis morfosintáctico
|
69 |
+
advanced_analysis = perform_advanced_morphosyntactic_analysis(
|
70 |
+
current_input,
|
71 |
+
nlp_models[lang_code]
|
72 |
+
)
|
73 |
|
74 |
+
# Guardar resultado en el estado de la sesión
|
75 |
+
st.session_state.morphosyntax_result = {
|
76 |
+
'doc': doc,
|
77 |
+
'advanced_analysis': advanced_analysis
|
78 |
+
}
|
79 |
+
|
80 |
+
# Incrementar el contador de análisis
|
81 |
+
st.session_state.analysis_counter += 1
|
82 |
+
|
83 |
+
# Mostrar resultados
|
84 |
+
display_morphosyntax_results(
|
85 |
+
st.session_state.morphosyntax_result,
|
86 |
+
lang_code,
|
87 |
+
morpho_t
|
88 |
+
)
|
89 |
+
|
90 |
+
# Guardar el análisis en la base de datos
|
91 |
+
if store_student_morphosyntax_result(
|
92 |
+
username=st.session_state.username,
|
93 |
+
text=current_input,
|
94 |
+
arc_diagrams=advanced_analysis['arc_diagrams']
|
95 |
+
):
|
96 |
+
st.success(morpho_t.get('success_message', 'Analysis saved successfully'))
|
97 |
+
else:
|
98 |
+
st.error(morpho_t.get('error_message', 'Error saving analysis'))
|
99 |
+
|
100 |
+
except Exception as e:
|
101 |
+
logger.error(f"Error en análisis morfosintáctico: {str(e)}")
|
102 |
+
st.error(morpho_t.get('error_processing', f'Error processing text: {str(e)}'))
|
103 |
+
else:
|
104 |
+
st.warning(morpho_t.get('warning_message', 'Please enter a text to analyze'))
|
105 |
|
106 |
+
# Si no se presionó el botón, verificar si hay resultados previos
|
107 |
+
elif 'morphosyntax_result' in st.session_state and st.session_state.morphosyntax_result is not None:
|
108 |
+
display_morphosyntax_results(
|
109 |
+
st.session_state.morphosyntax_result,
|
110 |
+
lang_code,
|
111 |
+
morpho_t
|
112 |
+
)
|
|
|
|
|
|
|
|
|
|
|
113 |
else:
|
114 |
+
st.info(morpho_t.get('morpho_initial_message', 'Enter text to begin analysis'))
|
115 |
|
116 |
+
except Exception as e:
|
117 |
+
logger.error(f"Error general en display_morphosyntax_interface: {str(e)}")
|
118 |
+
st.error("Se produjo un error. Por favor, intente de nuevo.")
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
|
120 |
############################################################################################################
|
121 |
def display_morphosyntax_results(result, lang_code, morpho_t):
|