File size: 705 Bytes
dc7ab9d | 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 |
import re
def preprocess_text(text):
"""
Preprocess text by removing URLs, mentions, hashtags, and special characters.
Convert to lowercase and remove extra whitespace.
"""
# Convert to lowercase
text = text.lower()
# Remove URLs
text = re.sub(r'http\S+|www\S+|https\S+', '', text, flags=re.MULTILINE)
# Remove mentions and hashtags
text = re.sub(r'@\w+|#\w+', '', text)
# Remove RT (retweet) indicators
text = re.sub(r'\brt\b', '', text)
# Remove punctuation except spaces
text = re.sub(r'[^\w\s]', '', text)
# Remove extra whitespace
text = ' '.join(text.split())
return text
|