File size: 1,052 Bytes
9685bab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import re

# очистка
def clean_text(text):
    text = re.sub(r'\[[^\]]*\]', '', text)  # []
    text = re.sub(r'<[^>]*>', '', text)     # <>
    text = re.sub(r'\d+', '', text)         # цифры
    text = re.sub(r'\s+', ' ', text)        # пробелы
    return text.strip().lower()

# токенизирует на предложения
def split_sentences(text):
    text = clean_text(text)
    sentences = re.split(r'[.!?;:]+', text)
    return [s.strip() for s in sentences if s.strip()]

# токенизирует на слова в предложениях
def split_words(sentence):
    words = sentence.split()
    cleaned = []
    for w in words:
        w = w.strip('.,!?;:()"\'—')
        if w:
            cleaned.append(w)
    return cleaned

# токенизирует на слова сразу
def split_words_from_text(text):
    text = clean_text(text)
    words = text.split()
    cleaned = []
    for w in words:
        w = w.strip('.,!?;:()"\'—')
        if w:
            cleaned.append(w)
    return cleaned