-
Notifications
You must be signed in to change notification settings - Fork 15
/
Preprocess.py
104 lines (90 loc) · 3.28 KB
/
Preprocess.py
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import nltk
import re
from nltk.corpus import stopwords
from nltk.stem import SnowballStemmer
from nltk.stem import WordNetLemmatizer
from string import punctuation
from autocorrect import spell
snowball_stemmer = SnowballStemmer('english')
wordnet_lemmatizer = WordNetLemmatizer()
class Preprocess:
def __int__(self):
pass
def autospell(self,text):
"""
correct the spelling of the word.
"""
spells = [spell(w) for w in (nltk.word_tokenize(text))]
return " ".join(spells)
def to_lower(self,text):
"""
:param text:
:return:
Converted text to lower case as in, converting "Hello" to "hello" or "HELLO" to "hello".
"""
return text.lower()
def remove_numbers(self,text):
"""
take string input and return a clean text without numbers.
Use regex to discard the numbers.
"""
output = ''.join(c for c in text if not c.isdigit())
return output
def remove_punct(self,text):
"""
take string input and clean string without punctuations.
use regex to remove the punctuations.
"""
return ''.join(c for c in text if c not in punctuation)
def remove_Tags(self,text):
"""
take string input and clean string without tags.
use regex to remove the html tags.
"""
cleaned_text = re.sub('<[^<]+?>', '', text)
return cleaned_text
def sentence_tokenize(self,text):
"""
take string input and return list of sentences.
use nltk.sent_tokenize() to split the sentences.
"""
sent_list = []
for w in nltk.sent_tokenize(text):
sent_list.append(w)
return sent_list
def word_tokenize(self,text):
"""
:param text:
:return: list of words
"""
return [w for sent in nltk.sent_tokenize(text) for w in nltk.word_tokenize(sent)]
def remove_stopwords(self,sentence):
"""
removes all the stop words like "is,the,a, etc."
"""
stop_words = stopwords.words('english')
return ' '.join([w for w in nltk.word_tokenize(sentence) if not w in stop_words])
def stem(self,text):
"""
:param word_tokens:
:return: list of words
"""
stemmed_word = [snowball_stemmer.stem(word) for sent in nltk.sent_tokenize(text)for word in nltk.word_tokenize(sent)]
return " ".join(stemmed_word)
def lemmatize(self,text):
lemmatized_word = [wordnet_lemmatizer.lemmatize(word)for sent in nltk.sent_tokenize(text)for word in nltk.word_tokenize(sent)]
return " ".join(lemmatized_word)
def preprocess(self,text):
lower_text = self.to_lower(text)
sentence_tokens = self.sentence_tokenize(lower_text)
word_list = []
for each_sent in sentence_tokens:
lemmatizzed_sent = self.lemmatize(each_sent)
clean_text = self.remove_numbers(lemmatizzed_sent)
clean_text = self.remove_punct(clean_text)
clean_text = self.remove_Tags(clean_text)
clean_text = self.remove_stopwords(clean_text)
word_tokens = self.word_tokenize(clean_text)
for i in word_tokens:
word_list.append(i)
return word_list