forked from crux82/ganbert
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPreprocess.py
118 lines (102 loc) · 3.73 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# Description: Cleaning textual dataset
# Dependencies: nltk, autocorrect
# Source: https://github.com/pemagrg1/Text-Pre-Processing-in-Python/blob/master/Preprocess.py
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
nltk.download('punkt')
nltk.download('wordnet')
nltk.download('stopwords')
snowball_stemmer = SnowballStemmer('english')
wordnet_lemmatizer = WordNetLemmatizer()
class Preprocess:
def __init__(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:
clean_text = each_sent
clean_text = self.lemmatize(clean_text)
clean_text = self.remove_numbers(clean_text)
clean_text = self.remove_punct(clean_text)
clean_text = self.remove_Tags(clean_text)
#clean_text = self.autospell(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
if __name__ == '__main__':
# Demo
text = "pema is coding this part. who are you? Pema 123 <with>"
print(Preprocess().preprocess(text))