import re def clean_text(text): """Clean and preprocess text for sentiment analysis""" if not isinstance(text, str): return "" # Convert to lowercase text = text.lower() # Remove URLs text = re.sub(r'http\S+|www\S+|https\S+', '', text, flags=re.MULTILINE) # Remove email addresses text = re.sub(r'\S+@\S+', '', text) # Remove special characters and digits, keep only letters and basic punctuation text = re.sub(r'[^a-zA-Z\s\.\,\!\?]', '', text) # Remove extra whitespaces text = re.sub(r'\s+', ' ', text).strip() return text