# Fundamentos de NLP com NLTK
import nltk
from nltk.tokenize import word_tokenize, sent_tokenize
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer, WordNetLemmatizer
import string
import re
# Download dos recursos necessários
nltk.download('punkt')
nltk.download('stopwords')
nltk.download('wordnet')
class
ProcessadorTexto
:
def
__init__
(self, idioma='english'):
self.stop_words = set(stopwords.words(idioma))
self.stemmer = PorterStemmer()
self.lemmatizer = WordNetLemmatizer()
def
limpar_texto
(self, texto):
"""Limpeza básica do texto"""
# Converter para minúsculas
texto = texto.lower()
# Remover URLs
texto = re.sub(r'http\S+', '', texto)
# Remover emails
texto = re.sub(r'\S+@\S+', '', texto)
# Remover números
texto = re.sub(r'\d+', '', texto)
# Remover pontuação
texto = texto.translate(str.maketrans('', '', string.punctuation))
return
texto.strip()
def
tokenizar
(self, texto):
"""Divide texto em tokens (palavras)"""
texto_limpo = self.limpar_texto(texto)
tokens = word_tokenize(texto_limpo)
# Remover stopwords
tokens = [token for token in tokens if token not in self.stop_words]
return
tokens
def
processar_completo
(self, texto, usar_stemming=True):
"""Processamento completo: tokenização + stemming/lemmatização"""
tokens = self.tokenizar(texto)
if
usar_stemming:
# Stemming: mais rápido, menos preciso
tokens = [self.stemmer.stem(token) for token in tokens]
else
:
# Lemmatização: mais lento, mais preciso
tokens = [self.lemmatizer.lemmatize(token) for token in tokens]
return
tokens