Spaces:
Sleeping
Sleeping
Download text_utils.py from changweikoy/backendAPI: direct link, hf CLI and curl.
- Browser
- Download file 592 Bytes
-
https://huggingface.co/spaces/changweikoy/backendAPI/resolve/main/text_utils.py
- Command line
-
hf download hf://spaces/changweikoy/backendAPI/text_utils.py
-
curl -L -o text_utils.py https://huggingface.co/spaces/changweikoy/backendAPI/resolve/main/text_utils.py
592 Bytes
| 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 | |