File size: 2,830 Bytes
a52faf6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import language_tool_python
import streamlit as st
from pathlib import Path
import html


@st.cache_resource
def load_assets():
    tool = language_tool_python.LanguageTool('ru-RU',
                                         language_tool_download_version="6.1")
    return tool


def check_text(text, tool):
    matches = tool.check(text)
    errors = []
    for match in matches:
        error_info = {
            'start': match.offset,
            'end': match.offset + match.errorLength,
            'message': match.message,
            'context': match.context
        }
        errors.append(error_info)
    return errors


def main():
    st.title('Проверка орфографии')
    text = st.text_area("Введите текст для проверки:", height=200)


    if st.button('Проверить текст'):
        if not text.strip():
            st.warning("Введите текст для проверки")
        else:
            errors = check_text(text)
            
            if not errors:
                st.success("Ошибок не найдено! 👍")
            else:
                sorted_errors = sorted(errors, key=lambda x: x['start'])
                
                highlighted = []
                last_pos = 0
                
                for error in sorted_errors:
                    highlighted.append(html.escape(text[last_pos:error['start']]))
                    
                    highlighted.append(
                        f'<span style="background-color: #ffe066; border-bottom: 2px dotted #ff9900; '
                        f'position: relative;" title="{html.escape(error["message"])}">'
                        f'{html.escape(text[error["start"]:error["end"]])}'
                        f'</span>'
                    )
                    
                    last_pos = error['end']
                
                highlighted.append(html.escape(text[last_pos:]))
                
                html_content = f"""
                <div style="
                    background: white;
                    padding: 20px;
                    border-radius: 8px;
                    border: 1px solid #ddd;
                    white-space: pre-wrap;
                    font-family: monospace;
                ">
                    {''.join(highlighted)}
                </div>
                """
                
                st.markdown("### Результат проверки:")
                st.markdown(html_content, unsafe_allow_html=True)
                
                st.markdown("### Найденные ошибки:")
                for i, error in enumerate(errors, 1):
                    st.markdown(f"{i}. **Позиция {error['start']}-{error['end']}**: {error['message']}")


if __name__ == "__main__":
    main()