File size: 1,139 Bytes
9b19228
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

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)