CyberShield / textcleaning.py
PyarasaniSrinivas's picture
Upload 5 files
9b19228 verified
Raw History Blame Contribute Delete
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)