File size: 13,528 Bytes
55c034b
 
831e193
 
 
 
 
 
 
 
 
 
 
 
 
 
b6ee9f7
 
831e193
 
 
 
 
 
 
 
 
 
 
 
72b2a4f
 
 
 
 
 
 
 
 
 
 
 
1ee3f25
1966cb7
 
831e193
 
010fa35
72b2a4f
 
 
 
 
 
 
 
 
010fa35
 
72b2a4f
010fa35
 
 
 
72b2a4f
010fa35
 
 
 
 
72b2a4f
 
010fa35
 
 
 
 
 
 
 
 
 
 
72b2a4f
010fa35
 
72b2a4f
 
 
 
010fa35
831e193
1966cb7
 
 
 
 
 
 
 
010fa35
 
1966cb7
 
 
 
 
831e193
010fa35
 
 
 
 
 
 
 
 
1966cb7
010fa35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72b2a4f
010fa35
 
 
 
 
 
 
 
 
 
 
 
 
72b2a4f
010fa35
 
 
 
 
 
 
 
1966cb7
010fa35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72b2a4f
010fa35
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b6ee9f7
831e193
 
 
 
72b2a4f
831e193
404e1f8
 
 
831e193
 
 
 
 
 
b6ee9f7
404e1f8
 
 
831e193
 
404e1f8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
#modules/morphosyntax/morphosyntax_interface.py

import streamlit as st
from streamlit_float import *
from streamlit_antd_components import *
from streamlit.components.v1 import html
import spacy
from spacy import displacy
import spacy_streamlit
import pandas as pd
import base64
import re

from .morphosyntax_process import (
    process_morphosyntactic_input,
    format_analysis_results,
    perform_advanced_morphosyntactic_analysis,
    get_repeated_words_colors,
    highlight_repeated_words,
    POS_COLORS,
    POS_TRANSLATIONS
)

from ..utils.widget_utils import generate_unique_key
from ..database.morphosintax_mongo_db import store_student_morphosyntax_result
from ..database.chat_mongo_db import store_chat_history, get_chat_history

import logging
logger = logging.getLogger(__name__)

###########################################################################

import streamlit as st
from streamlit_float import *
from streamlit_antd_components import *
from streamlit.components.v1 import html
import spacy
from spacy import displacy
import spacy_streamlit
import pandas as pd
import base64
import re

############################################################################

def display_morphosyntax_interface(lang_code, nlp_models, morpho_t):
    try:
        # CSS mejorado para estabilidad y layout horizontal
        st.markdown("""
            <style>
            .stTextArea textarea {
                font-size: 1rem;
                line-height: 1.5;
                padding: 0.5rem;
                border-radius: 0.375rem;
                border: 1px solid #e2e8f0;
                background-color: white;
                min-height: 100px !important;
                height: 100px !important;
            }
            .block-container {
                padding-top: 0.5rem !important;
                padding-bottom: 0.5rem !important;
                margin: 0 !important;
            }
            .main-content {
                display: flex;
                flex-direction: column;
                gap: 1rem;
                padding: 0.5rem;
            }
            .arc-diagram-container {
                width: 100%;
                overflow-x: auto;
                background-color: white;
                padding: 0.5rem;
                border-radius: 0.375rem;
                box-shadow: 0 1px 2px rgba(0,0,0,0.1);
                margin-top: 0.5rem;
            }
            .edit-container {
                position: sticky;
                top: 0;
                background-color: white;
                z-index: 100;
                padding: 0.5rem 0;
            }
            </style>
        """, unsafe_allow_html=True)

        # Inicializaci贸n del estado con valores por defecto
        if 'morphosyntax_state' not in st.session_state:
            st.session_state['morphosyntax_state'] = {}
        
        default_state = {
            'has_original': False,
            'original_text': '',
            'original_analysis': None,
            'current_text': '',
            'iterations': [],
            'analysis_count': 0,
            'current_tab': 0  # Para mantener el tab activo
        }
        
        for key, value in default_state.items():
            if key not in st.session_state.morphosyntax_state:
                st.session_state.morphosyntax_state[key] = value

        with st.container():
            # 脕rea de edici贸n del texto original
            if not st.session_state.morphosyntax_state['has_original']:
                st.markdown("### Texto Original")
                original_text = st.text_area(
                    "Ingrese una oraci贸n",
                    key="original_text_input",
                    placeholder="Ingresar solo una oraci贸n hasta el punto y aparte. Si es punto seguido, dejar as铆.",
                    height=100
                )
                
                col1, col2, col3 = st.columns([2,1,2])
                with col1:
                    if st.button(
                        "Analizar Texto Original", 
                        type="primary",
                        use_container_width=True,
                        disabled=not bool(original_text.strip())
                    ):
                        try:
                            # Procesar texto original
                            doc = nlp_models[lang_code](original_text)
                            analysis = perform_advanced_morphosyntactic_analysis(
                                original_text,
                                nlp_models[lang_code]
                            )
                            
                            # Actualizar estado
                            st.session_state.morphosyntax_state.update({
                                'has_original': True,
                                'original_text': original_text,
                                'original_analysis': {
                                    'doc': doc,
                                    'advanced_analysis': analysis
                                },
                                'current_text': original_text
                            })
                            
                            if store_student_morphosyntax_result(
                                username=st.session_state.username,
                                text=original_text,
                                arc_diagrams=analysis['arc_diagrams']
                            ):
                                st.success("Texto analizado exitosamente")
                                st.rerun()
                            else:
                                st.error("Error al guardar el an谩lisis")
                        except Exception as e:
                            logger.error(f"Error procesando texto original: {str(e)}")
                            st.error("Error al procesar el texto")
            else:
                # Layout horizontal para edici贸n y visualizaci贸n
                st.markdown("### Versi贸n Original")
                
                # Permitir editar texto original
                edit_original = st.button("Editar texto original")
                if edit_original:
                    st.session_state.morphosyntax_state['has_original'] = False
                    st.rerun()
                
                col1, col2 = st.columns([1, 1])
                with col1:
                    st.text_area(
                        "Texto Original",
                        value=st.session_state.morphosyntax_state['original_text'],
                        disabled=True,
                        height=100
                    )
                
                with col2:
                    display_morphosyntax_results(
                        st.session_state.morphosyntax_state['original_analysis'],
                        lang_code,
                        morpho_t
                    )

                st.markdown("---")
                st.markdown("### Iteraci贸n Actual")
                
                # Campo para nueva versi贸n con layout horizontal
                col1, col2 = st.columns([1, 1])
                with col1:
                    iteration_text = st.text_area(
                        "Modifique la oraci贸n",
                        value=st.session_state.morphosyntax_state['current_text'],
                        key=f"iteration_input_{st.session_state.morphosyntax_state['analysis_count']}",
                        placeholder="Ingresar solo una oraci贸n hasta el punto y aparte. Si es punto seguido, dejar as铆.",
                        height=100
                    )

                    analyze_button = st.button(
                        "Analizar Cambios",
                        type="primary",
                        icon="馃攳",
                        key=f"analyze_{st.session_state.morphosyntax_state['analysis_count']}",
                        disabled=not bool(iteration_text.strip()),
                        use_container_width=True
                    )
                
                with col2:
                    if analyze_button and iteration_text.strip():
                        try:
                            with st.spinner("Procesando..."):
                                doc = nlp_models[lang_code](iteration_text)
                                analysis = perform_advanced_morphosyntactic_analysis(
                                    iteration_text,
                                    nlp_models[lang_code]
                                )
                                
                                current_analysis = {
                                    'doc': doc,
                                    'advanced_analysis': analysis
                                }
                                
                                # Guardar iteraci贸n
                                st.session_state.morphosyntax_state['iterations'].append({
                                    'text': iteration_text,
                                    'analysis': current_analysis,
                                    'timestamp': pd.Timestamp.now()
                                })
                                
                                st.session_state.morphosyntax_state['current_text'] = iteration_text
                                st.session_state.morphosyntax_state['analysis_count'] += 1
                                
                                if store_student_morphosyntax_result(
                                    username=st.session_state.username,
                                    text=iteration_text,
                                    arc_diagrams=analysis['arc_diagrams']
                                ):
                                    display_morphosyntax_results(
                                        current_analysis,
                                        lang_code,
                                        morpho_t
                                    )
                                else:
                                    st.error("Error al guardar el an谩lisis")
                        except Exception as e:
                            logger.error(f"Error procesando iteraci贸n: {str(e)}")
                            st.error("Error al procesar los cambios")

                # Historial de iteraciones
                if st.session_state.morphosyntax_state['iterations']:
                    with st.expander("Historial de Iteraciones", expanded=False):
                        for idx, iteration in enumerate(reversed(st.session_state.morphosyntax_state['iterations'])):
                            col1, col2 = st.columns([1, 1])
                            with col1:
                                st.markdown(f"**Iteraci贸n {idx + 1} ({iteration['timestamp'].strftime('%H:%M:%S')})**")
                                st.text_area(
                                    f"Texto {idx + 1}",
                                    value=iteration['text'],
                                    disabled=True,
                                    height=100,
                                    key=f"hist_text_{idx}"
                                )
                            with col2:
                                display_morphosyntax_results(
                                    iteration['analysis'],
                                    lang_code,
                                    morpho_t
                                )
                            st.markdown("---")

    except Exception as e:
        logger.error(f"Error general en display_morphosyntax_interface: {str(e)}")
        st.error("Se produjo un error. Por favor, intente de nuevo.")

#########################################################################3
def display_morphosyntax_results(result, lang_code, morpho_t):
    """
    Muestra solo el an谩lisis sint谩ctico con diagramas de arco.
    """
    if result is None:
        st.warning(morpho_t.get('no_results', 'No results available'))
        return

    doc = result['doc']

    # An谩lisis sint谩ctico (diagramas de arco)
    st.markdown(f"### {morpho_t.get('arc_diagram', 'Syntactic analysis: Arc diagram')}")
    
    with st.container():
        sentences = list(doc.sents)
        for i, sent in enumerate(sentences):
            with st.container():
                st.subheader(f"{morpho_t.get('sentence', 'Sentence')} {i+1}")
                try:
                    html = displacy.render(sent, style="dep", options={
                        "distance": 100,
                        "arrow_spacing": 20,
                        "word_spacing": 30
                    })
                    # Ajustar dimensiones del SVG
                    html = html.replace('height="375"', 'height="200"')
                    html = re.sub(r'<svg[^>]*>', lambda m: m.group(0).replace('height="450"', 'height="300"'), html)
                    html = re.sub(r'<g [^>]*transform="translate\((\d+),(\d+)\)"', 
                                lambda m: f'<g transform="translate({m.group(1)},50)"', html)
                    
                    # Envolver en un div con clase para estilos
                    html = f'<div class="arc-diagram-container">{html}</div>'
                    st.write(html, unsafe_allow_html=True)
                except Exception as e:
                    logger.error(f"Error rendering sentence {i}: {str(e)}")
                    st.error(f"Error displaying diagram for sentence {i+1}")