Spaces:
Sleeping
Sleeping
Download preprocessing.py from Rindhuja/intelligent-doc-api: direct link, hf CLI and curl.
- Browser
- Download file 767 Bytes
-
https://huggingface.co/spaces/Rindhuja/intelligent-doc-api/resolve/main/preprocessing.py
- Command line
-
hf download hf://spaces/Rindhuja/intelligent-doc-api/preprocessing.py
-
curl -L -o preprocessing.py https://huggingface.co/spaces/Rindhuja/intelligent-doc-api/resolve/main/preprocessing.py
767 Bytes
| import re | |
| import nltk | |
| from nltk.corpus import stopwords | |
| from nltk.stem import WordNetLemmatizer | |
| def _ensure_nltk_data(): | |
| try: | |
| stopwords.words("english") | |
| except LookupError: | |
| nltk.download("stopwords", quiet=True) | |
| try: | |
| nltk.data.find("corpora/wordnet") | |
| except LookupError: | |
| nltk.download("wordnet", quiet=True) | |
| _ensure_nltk_data() | |
| stop_words = set(stopwords.words("english")) | |
| lemmatizer = WordNetLemmatizer() | |
| def preprocess_text(text): | |
| text = text.lower() | |
| text = re.sub(r"[^a-z\s]", " ", text) | |
| tokens = text.split() | |
| cleaned_tokens = [ | |
| lemmatizer.lemmatize(token) | |
| for token in tokens | |
| if token not in stop_words and len(token) > 2 | |
| ] | |
| return " ".join(cleaned_tokens) | |