File size: 1,781 Bytes
2a16478
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import re
from Sastrawi.Stemmer.StemmerFactory import StemmerFactory
from Sastrawi.StopWordRemover.StopWordRemoverFactory import StopWordRemoverFactory

class TextPreprocessor:
    def __init__(self):
        # Initialize Sastrawi components
        self.stemmer = StemmerFactory().create_stemmer()
        self.stopword_remover = StopWordRemoverFactory().create_stop_word_remover()

    def clean_text(self, text: str) -> str:
        """
        Membersihkan teks dari karakter yang tidak diinginkan (HTML tags, URL, simbol).
        """
        # Remove HTML tags
        text = re.sub(r'<[^>]+>', ' ', text)
        # Remove URLs
        text = re.sub(r'http\S+|www\S+|https\S+', '', text, flags=re.MULTILINE)
        # Remove multiple spaces
        text = re.sub(r'\s+', ' ', text)
        # Remove special characters (keep only alphanumeric and spaces)
        text = re.sub(r'[^a-zA-Z0-9\s.,!?]', '', text)
        return text.strip()

    def preprocess_for_nlp(self, text: str, do_stemming: bool = False, remove_stopwords: bool = True) -> str:
        """
        Preprocessing teks penuh untuk NLP task seperti klasifikasi atau semantic search.
        """
        text = self.clean_text(text)
        
        # Lowercasing
        text = text.lower()
        
        # Stopword removal (opsional tapi dianjurkan untuk task tertentu)
        if remove_stopwords:
            text = self.stopword_remover.remove(text)
            
        # Stemming (opsional, terkadang menurunkan performa semantic search/summarization)
        if do_stemming:
            text = self.stemmer.stem(text)
            
        return text

# Example usage:
# preprocessor = TextPreprocessor()
# clean_txt = preprocessor.clean_text("Teks <b>kotor</b> dengan URL http://example.com !!!")