File size: 1,156 Bytes
63aa5b2 |
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 |
import re
def clean_text(text):
"""Pembersihan teks yang lebih robust"""
if not isinstance(text, str):
return ""
# Remove extra whitespaces
text = re.sub(r'\s+', ' ', text)
# Remove special characters but keep punctuation
text = re.sub(r'[^\w\s.,!?;:\-()]', '', text)
# Remove multiple punctuation
text = re.sub(r'[.,!?;:]{2,}', '.', text)
return text.strip()
def simple_sentence_tokenize(text):
"""Tokenisasi kalimat sederhana tanpa NLTK"""
# Bersihkan teks terlebih dahulu
text = text.replace('\n', ' ').strip()
# Pisahkan berdasarkan tanda baca umum
sentences = []
for part in re.split(r'(?<=[.!?])\s+', text):
if part.strip():
sentences.append(part.strip())
# Jika tidak ada kalimat yang ditemukan, kembalikan seluruh teks sebagai satu kalimat
if not sentences:
return [text]
return sentences
def tokenize_words(text):
"""Tokenisasi kata sederhana tanpa NLTK"""
text = text.lower()
# Bersihkan teks
text = re.sub(r'[^\w\s]', ' ', text)
# Split kata-kata
return [word for word in text.split() if word.strip()]
|