fixes_v4
Browse files
app.py
CHANGED
@@ -51,48 +51,36 @@ def main():
|
|
51 |
.error-highlight {
|
52 |
background-color: #ffe066;
|
53 |
border-bottom: 2px dotted #ff9900;
|
54 |
-
cursor: pointer;
|
55 |
color: black !important;
|
56 |
}
|
57 |
-
.error-highlight:hover {
|
58 |
-
background-color: #ffd700;
|
59 |
-
}
|
60 |
</style>
|
61 |
""", unsafe_allow_html=True)
|
62 |
|
63 |
st.title('Проверка орфографии')
|
64 |
|
65 |
-
|
66 |
-
text_input = st.text_area("Введите текст для проверки:",
|
67 |
-
value=st.session_state.get('text_to_check', ''),
|
68 |
-
height=200)
|
69 |
text = text_input.replace("\n", " ")
|
70 |
tool = load_assets()
|
71 |
|
72 |
if st.button('Проверить текст'):
|
73 |
-
st.session_state.text_to_check = text_input
|
74 |
if not text.strip():
|
75 |
st.warning("Введите текст для проверки")
|
76 |
else:
|
77 |
errors = check_text(text, tool)
|
78 |
-
st.session_state.errors = errors
|
79 |
-
st.session_state.
|
80 |
-
st.session_state.selected_error = None # Сброс выбранной ошибки
|
81 |
|
82 |
-
# Используем сохраненные ошибки при наличии
|
83 |
errors = st.session_state.get('errors', [])
|
84 |
-
|
85 |
-
|
86 |
if errors:
|
87 |
-
#
|
88 |
highlighted = []
|
89 |
last_pos = 0
|
90 |
|
91 |
-
for
|
92 |
highlighted.append(html.escape(text[last_pos:error['start']]))
|
93 |
highlighted.append(
|
94 |
-
f'<span class="error-highlight"
|
95 |
-
f'title="{html.escape(error["message"])}">'
|
96 |
f'{html.escape(text[error["start"]:error["end"]])}'
|
97 |
f'</span>'
|
98 |
)
|
@@ -100,55 +88,30 @@ def main():
|
|
100 |
|
101 |
highlighted.append(html.escape(text[last_pos:]))
|
102 |
|
103 |
-
html_content = f"""
|
104 |
-
<div style="
|
105 |
-
background: white;
|
106 |
-
padding: 20px;
|
107 |
-
border-radius: 8px;
|
108 |
-
border: 1px solid #ddd;
|
109 |
-
white-space: pre-wrap;
|
110 |
-
font-family: monospace;
|
111 |
-
color: #000000;
|
112 |
-
">
|
113 |
-
{''.join(highlighted)}
|
114 |
-
</div>
|
115 |
-
"""
|
116 |
-
|
117 |
st.markdown("### Результат проверки:")
|
118 |
-
st.markdown(
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
if (event.data.index !== undefined) {
|
125 |
-
const index = event.data.index;
|
126 |
-
// Отправляем индекс в Streamlit
|
127 |
-
const streamlitDoc = window.parent.document;
|
128 |
-
const iframe = streamlitDoc.querySelector('iframe[data-testid="stIFrame"]');
|
129 |
-
if (iframe) {
|
130 |
-
iframe.contentWindow.postMessage({type: 'streamlit:setComponentValue', value: index}, '*');
|
131 |
-
}
|
132 |
-
}
|
133 |
-
});
|
134 |
-
</script>
|
135 |
-
""", unsafe_allow_html=True)
|
136 |
-
|
137 |
-
# Обработка выбранной ошибки
|
138 |
-
if 'selected_error' not in st.session_state:
|
139 |
-
st.session_state.selected_error = None
|
140 |
|
141 |
-
#
|
142 |
-
|
143 |
-
selected_error = st.
|
|
|
|
|
|
|
|
|
|
|
144 |
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
|
|
149 |
else:
|
150 |
-
st.
|
151 |
-
st.markdown("🔍 **Кликните на выделенный текст, чтобы увидеть описание ошибки**")
|
152 |
|
153 |
if __name__ == "__main__":
|
154 |
main()
|
|
|
51 |
.error-highlight {
|
52 |
background-color: #ffe066;
|
53 |
border-bottom: 2px dotted #ff9900;
|
|
|
54 |
color: black !important;
|
55 |
}
|
|
|
|
|
|
|
56 |
</style>
|
57 |
""", unsafe_allow_html=True)
|
58 |
|
59 |
st.title('Проверка орфографии')
|
60 |
|
61 |
+
text_input = st.text_area("Введите текст для проверки:", height=200)
|
|
|
|
|
|
|
62 |
text = text_input.replace("\n", " ")
|
63 |
tool = load_assets()
|
64 |
|
65 |
if st.button('Проверить текст'):
|
|
|
66 |
if not text.strip():
|
67 |
st.warning("Введите текст для проверки")
|
68 |
else:
|
69 |
errors = check_text(text, tool)
|
70 |
+
st.session_state.errors = sorted(errors, key=lambda x: x['start'])
|
71 |
+
st.session_state.show_all_errors = True # Показывать все ошибки по умолчанию
|
|
|
72 |
|
|
|
73 |
errors = st.session_state.get('errors', [])
|
74 |
+
|
|
|
75 |
if errors:
|
76 |
+
# Выводим текст с подсветкой ошибок
|
77 |
highlighted = []
|
78 |
last_pos = 0
|
79 |
|
80 |
+
for error in errors:
|
81 |
highlighted.append(html.escape(text[last_pos:error['start']]))
|
82 |
highlighted.append(
|
83 |
+
f'<span class="error-highlight" title="{html.escape(error["message"])}">'
|
|
|
84 |
f'{html.escape(text[error["start"]:error["end"]])}'
|
85 |
f'</span>'
|
86 |
)
|
|
|
88 |
|
89 |
highlighted.append(html.escape(text[last_pos:]))
|
90 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
91 |
st.markdown("### Результат проверки:")
|
92 |
+
st.markdown(
|
93 |
+
f'<div style="background: white; padding: 20px; border-radius: 8px; color: black;">'
|
94 |
+
f'{"".join(highlighted)}'
|
95 |
+
f'</div>',
|
96 |
+
unsafe_allow_html=True
|
97 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
|
99 |
+
# Выбор ошибки через радио-кнопки
|
100 |
+
st.markdown("### Выберите ошибку для подробного описания:")
|
101 |
+
selected_error = st.radio(
|
102 |
+
"Список ошибок:",
|
103 |
+
options=[f"Ошибка {i+1} (позиция {e['start']}-{e['end']})" for i, e in enumerate(errors)],
|
104 |
+
index=None,
|
105 |
+
key="error_selector"
|
106 |
+
)
|
107 |
|
108 |
+
# Показываем описание выбранной ошибки
|
109 |
+
if selected_error:
|
110 |
+
error_index = int(selected_error.split()[1]) - 1 # "Ошибка 1" → индекс 0
|
111 |
+
error = errors[error_index]
|
112 |
+
st.markdown(f"**Описание:** {error['message']}")
|
113 |
else:
|
114 |
+
st.info("Кликните на ошибку в списке выше, чтобы увидеть её описание.")
|
|
|
115 |
|
116 |
if __name__ == "__main__":
|
117 |
main()
|