Update modules/morphosyntax/morphosyntax_interface.py
Browse files
modules/morphosyntax/morphosyntax_interface.py
CHANGED
@@ -14,87 +14,174 @@ from ..database.morphosintaxis_export import export_user_interactions
|
|
14 |
import logging
|
15 |
logger = logging.getLogger(__name__)
|
16 |
|
17 |
-
def
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
-
|
21 |
-
chat_container = st.container()
|
22 |
|
23 |
-
|
24 |
-
user_input = st.chat_input(t['morpho_input_label'])
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
st.session_state.morphosyntax_chat_history = []
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
35 |
|
36 |
-
|
|
|
|
|
|
|
37 |
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
|
|
|
|
|
|
46 |
|
47 |
-
|
48 |
-
|
49 |
-
store_student_morphosyntax_result(
|
50 |
st.session_state.username,
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
</div>
|
70 |
-
""",
|
71 |
-
height=370,
|
72 |
-
scrolling=True
|
73 |
-
)
|
74 |
-
if i < len(message["visualizations"]) - 1:
|
75 |
-
st.markdown("---") # Separador entre diagramas
|
76 |
|
77 |
-
# Botón para limpiar el historial del chat
|
78 |
-
if st.button(t['clear_chat'], key=generate_unique_key('morphosyntax', 'clear_chat')):
|
79 |
-
st.session_state.morphosyntax_chat_history = []
|
80 |
-
st.rerun()
|
81 |
|
82 |
-
# Botón de exportación
|
83 |
-
if st.button("Exportar Interacciones"):
|
84 |
-
pdf_buffer = export_user_interactions(st.session_state.username, 'morphosyntax')
|
85 |
-
st.download_button(
|
86 |
-
label="Descargar PDF",
|
87 |
-
data=pdf_buffer,
|
88 |
-
file_name="interacciones_morfosintaxis.pdf",
|
89 |
-
mime="application/pdf"
|
90 |
-
)
|
91 |
-
|
92 |
-
def display_visualizations(visualizations, t):
|
93 |
-
for i, viz in enumerate(visualizations):
|
94 |
-
st.markdown(f"**{t['sentence']} {i+1}**")
|
95 |
-
st.components.v1.html(viz, height=370, scrolling=True)
|
96 |
-
if i < len(visualizations) - 1:
|
97 |
-
st.markdown("---")
|
98 |
|
99 |
'''
|
100 |
if user_input:
|
|
|
14 |
import logging
|
15 |
logger = logging.getLogger(__name__)
|
16 |
|
17 |
+
def display_morphosyntax_analysis_interface(nlp_models, lang_code):
|
18 |
+
translations = {
|
19 |
+
'es': {
|
20 |
+
'title': "AIdeaText - Análisis morfológico y sintáctico",
|
21 |
+
'input_label': "Ingrese un texto para analizar (máximo 5,000 palabras",
|
22 |
+
'input_placeholder': "Esta funcionalidad le ayudará con dos competencias:\n"
|
23 |
+
"[1] \"Escribe diversos tipos de textos en su lengua materna\"\n"
|
24 |
+
"[2] \"Lee diversos tipos de textos escritos en su lengua materna\"\n\n"
|
25 |
+
"Ingrese su texto aquí para analizar...",
|
26 |
+
'analyze_button': "Analizar texto",
|
27 |
+
'repeated_words': "Palabras repetidas",
|
28 |
+
'legend': "Leyenda: Categorías gramaticales",
|
29 |
+
'arc_diagram': "Análisis sintáctico: Diagrama de arco",
|
30 |
+
'sentence': "Oración",
|
31 |
+
'success_message': "Análisis guardado correctamente.",
|
32 |
+
'error_message': "Hubo un problema al guardar el análisis. Por favor, inténtelo de nuevo.",
|
33 |
+
'warning_message': "Por favor, ingrese un texto para analizar.",
|
34 |
+
'initial_message': "Ingrese un texto y presione 'Analizar texto' para comenzar.",
|
35 |
+
'no_results': "No hay resultados disponibles. Por favor, realice un análisis primero.",
|
36 |
+
'pos_analysis': "Análisis de categorías gramaticales",
|
37 |
+
'morphological_analysis': "Análisis morfológico",
|
38 |
+
'sentence_structure': "Estructura de oraciones",
|
39 |
+
'word': "Palabra",
|
40 |
+
'count': "Cantidad",
|
41 |
+
'percentage': "Porcentaje",
|
42 |
+
'examples': "Ejemplos",
|
43 |
+
'lemma': "Lema",
|
44 |
+
'tag': "Etiqueta",
|
45 |
+
'dep': "Dependencia",
|
46 |
+
'morph': "Morfología",
|
47 |
+
'root': "Raíz",
|
48 |
+
'subjects': "Sujetos",
|
49 |
+
'objects': "Objetos",
|
50 |
+
'verbs': "Verbos",
|
51 |
+
'grammatical_category': "Categoría gramatical",
|
52 |
+
'dependency': "Dependencia",
|
53 |
+
'morphology': "Morfología"
|
54 |
+
},
|
55 |
+
'en': {
|
56 |
+
'title': "AIdeaText - Morphological and Syntactic Analysis",
|
57 |
+
'input_label': "Enter a text to analyze (max 5,000 words):",
|
58 |
+
'input_placeholder': "This functionality will help you with two competencies:\n"
|
59 |
+
"[1] \"Write various types of texts in your native language\"\n"
|
60 |
+
"[2] \"Read various types of written texts in your native language\"\n\n"
|
61 |
+
"Enter your text here to analyze...",
|
62 |
+
'analyze_button': "Analyze text",
|
63 |
+
'repeated_words': "Repeated words",
|
64 |
+
'legend': "Legend: Grammatical categories",
|
65 |
+
'arc_diagram': "Syntactic analysis: Arc diagram",
|
66 |
+
'sentence': "Sentence",
|
67 |
+
'success_message': "Analysis saved successfully.",
|
68 |
+
'error_message': "There was a problem saving the analysis. Please try again.",
|
69 |
+
'warning_message': "Please enter a text to analyze.",
|
70 |
+
'initial_message': "Enter a text and press 'Analyze text' to start.",
|
71 |
+
'no_results': "No results available. Please perform an analysis first.",
|
72 |
+
'pos_analysis': "Part of Speech Analysis",
|
73 |
+
'morphological_analysis': "Morphological Analysis",
|
74 |
+
'sentence_structure': "Sentence Structure",
|
75 |
+
'word': "Word",
|
76 |
+
'count': "Count",
|
77 |
+
'percentage': "Percentage",
|
78 |
+
'examples': "Examples",
|
79 |
+
'lemma': "Lemma",
|
80 |
+
'tag': "Tag",
|
81 |
+
'dep': "Dependency",
|
82 |
+
'morph': "Morphology",
|
83 |
+
'root': "Root",
|
84 |
+
'subjects': "Subjects",
|
85 |
+
'objects': "Objects",
|
86 |
+
'verbs': "Verbs",
|
87 |
+
'grammatical_category': "Grammatical category",
|
88 |
+
'dependency': "Dependency",
|
89 |
+
'morphology': "Morphology"
|
90 |
+
},
|
91 |
+
'fr': {
|
92 |
+
'title': "AIdeaText - Analyse morphologique et syntaxique",
|
93 |
+
'input_label': "Entrez un texte à analyser (max 5 000 mots) :",
|
94 |
+
'input_placeholder': "Cette fonctionnalité vous aidera avec deux compétences :\n"
|
95 |
+
"[1] \"Écrire divers types de textes dans votre langue maternelle\"\n"
|
96 |
+
"[2] \"Lire divers types de textes écrits dans votre langue maternelle\"\n\n"
|
97 |
+
"Entrez votre texte ici pour l'analyser...",
|
98 |
+
'analyze_button': "Analyser le texte",
|
99 |
+
'repeated_words': "Mots répétés",
|
100 |
+
'legend': "Légende : Catégories grammaticales",
|
101 |
+
'arc_diagram': "Analyse syntaxique : Diagramme en arc",
|
102 |
+
'sentence': "Phrase",
|
103 |
+
'success_message': "Analyse enregistrée avec succès.",
|
104 |
+
'error_message': "Un problème est survenu lors de l'enregistrement de l'analyse. Veuillez réessayer.",
|
105 |
+
'warning_message': "Veuillez entrer un texte à analyser.",
|
106 |
+
'initial_message': "Entrez un texte et appuyez sur 'Analyser le texte' pour commencer.",
|
107 |
+
'no_results': "Aucun résultat disponible. Veuillez d'abord effectuer une analyse.",
|
108 |
+
'pos_analysis': "Analyse des parties du discours",
|
109 |
+
'morphological_analysis': "Analyse morphologique",
|
110 |
+
'sentence_structure': "Structure des phrases",
|
111 |
+
'word': "Mot",
|
112 |
+
'count': "Nombre",
|
113 |
+
'percentage': "Pourcentage",
|
114 |
+
'examples': "Exemples",
|
115 |
+
'lemma': "Lemme",
|
116 |
+
'tag': "Étiquette",
|
117 |
+
'dep': "Dépendance",
|
118 |
+
'morph': "Morphologie",
|
119 |
+
'root': "Racine",
|
120 |
+
'subjects': "Sujets",
|
121 |
+
'objects': "Objets",
|
122 |
+
'verbs': "Verbes",
|
123 |
+
'grammatical_category': "Catégorie grammaticale",
|
124 |
+
'dependency': "Dépendance",
|
125 |
+
'morphology': "Morphologie"
|
126 |
+
}
|
127 |
+
}
|
128 |
|
129 |
+
t = translations[lang_code]
|
|
|
130 |
|
131 |
+
input_key = f"morphosyntax_input_{lang_code}"
|
|
|
132 |
|
133 |
+
if input_key not in st.session_state:
|
134 |
+
st.session_state[input_key] = ""
|
|
|
135 |
|
136 |
+
sentence_input = st.text_area(
|
137 |
+
t['input_label'],
|
138 |
+
height=150,
|
139 |
+
placeholder=t['input_placeholder'],
|
140 |
+
value=st.session_state[input_key],
|
141 |
+
key=f"text_area_{lang_code}",
|
142 |
+
on_change=lambda: setattr(st.session_state, input_key, st.session_state[f"text_area_{lang_code}"])
|
143 |
+
)
|
144 |
|
145 |
+
if st.button(t['analyze_button'], key=f"analyze_button_{lang_code}"):
|
146 |
+
current_input = st.session_state[input_key]
|
147 |
+
if current_input:
|
148 |
+
doc = nlp_models[lang_code](current_input)
|
149 |
|
150 |
+
# Análisis morfosintáctico avanzado
|
151 |
+
advanced_analysis = perform_advanced_morphosyntactic_analysis(current_input, nlp_models[lang_code])
|
152 |
+
|
153 |
+
# Guardar el resultado en el estado de la sesión
|
154 |
+
st.session_state.morphosyntax_result = {
|
155 |
+
'doc': doc,
|
156 |
+
'advanced_analysis': advanced_analysis
|
157 |
+
}
|
158 |
+
|
159 |
+
# Mostrar resultados
|
160 |
+
display_morphosyntax_results(st.session_state.morphosyntax_result, lang_code, t)
|
161 |
|
162 |
+
# Guardar resultados
|
163 |
+
if store_morphosyntax_result(
|
|
|
164 |
st.session_state.username,
|
165 |
+
current_input,
|
166 |
+
get_repeated_words_colors(doc),
|
167 |
+
advanced_analysis['arc_diagram'],
|
168 |
+
advanced_analysis['pos_analysis'],
|
169 |
+
advanced_analysis['morphological_analysis'],
|
170 |
+
advanced_analysis['sentence_structure']
|
171 |
+
):
|
172 |
+
st.success(t['success_message'])
|
173 |
+
else:
|
174 |
+
st.error(t['error_message'])
|
175 |
+
else:
|
176 |
+
st.warning(t['warning_message'])
|
177 |
+
elif 'morphosyntax_result' in st.session_state and st.session_state.morphosyntax_result is not None:
|
178 |
+
|
179 |
+
# Si hay un resultado guardado, mostrarlo
|
180 |
+
display_morphosyntax_results(st.session_state.morphosyntax_result, lang_code, t)
|
181 |
+
else:
|
182 |
+
st.info(t['initial_message']) # Añade esta traducción a tu diccionario
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
183 |
|
|
|
|
|
|
|
|
|
184 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
185 |
|
186 |
'''
|
187 |
if user_input:
|