Spaces:
Sleeping
Sleeping
Download textcleaning.py from PyarasaniSrinivas/CyberShield: direct link, hf CLI and curl.
- Browser
- Download file 1.14 kB
-
https://huggingface.co/spaces/PyarasaniSrinivas/CyberShield/resolve/main/textcleaning.py
- Command line
-
hf download hf://spaces/PyarasaniSrinivas/CyberShield/textcleaning.py
-
curl -L -o textcleaning.py https://huggingface.co/spaces/PyarasaniSrinivas/CyberShield/resolve/main/textcleaning.py
1.14 kB
| import nltk | |
| # For word tokenization | |
| from nltk.tokenize import word_tokenize | |
| nltk.download("punkt") | |
| nltk.download("punkt_tab") | |
| # For Removing the stop words | |
| from nltk.corpus import stopwords | |
| nltk.download("stopwords") | |
| # For Stemming or Lemmitization | |
| from nltk.stem import WordNetLemmatizer,PorterStemmer | |
| nltk.download('wordnet') | |
| import re | |
| # tfidf vectorizer | |
| from sklearn.feature_extraction.text import TfidfVectorizer | |
| def cleaning(document): | |
| # Lower casing | |
| document=document.lower() | |
| # Removing the Numbers and Special Symbols and puntuation | |
| regex=r"[^a-zA-Z]+" | |
| document=re.sub(regex," ",document) | |
| # word Tokenization | |
| document=word_tokenize(document) | |
| # Removing the stopwords | |
| stop_words=set(stopwords.words('english')) | |
| document=[ word for word in document if word not in stop_words ] | |
| # Stemming or Lemmitization | |
| lemmitizer=WordNetLemmatizer() | |
| document=[lemmitizer.lemmatize(word) for word in document] | |
| # Join and return | |
| return " ".join(document) | |
| def clean_documents(corpus): | |
| return corpus.apply(cleaning) |