-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataCleaner.py
176 lines (139 loc) · 3.6 KB
/
DataCleaner.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import numpy as np
import nltk
import re
from nltk.corpus import stopwords
from nltk.stem import SnowballStemmer
import string
#Note: Uncomment to first time usage
#nltk.download('punkt')
#nltk.download('wordnet')
#nltk.download('stopwords')
np.random.seed(42)
def pack(data):
"""
Perform data preprocessing and add some features.
Parameters
----------
data : DataFrame with Messages.
"""
data['length'] = [len(text) for text in data['text']]
data['longest_word_len'] = [longest_word_len(text) for text in data['text']]
data['mean_word_len'] = [mean_word_len(text) for text in data['text']]
data['subject_len'] = [len(subject) for subject in data['subject']]
data['stop_words_num'] = [stop_words_count(text) for text in data['text']]
data['pounctuation_num'] = [punctuation_count(text) for text in data['text']]
data['text'] = [clean_text(text) for text in data['text']]
def clean_text(text):
"""
Perform text normalisation.
Parameters
----------
text : Text.
Returns
-------
Normalized Text.
"""
words = tokenization(text)
words = lowercasing(words)
words = clean(words)
words = stemming(words)
return ' '.join(words)
def longest_word_len(text):
"""
Find length of the longest word in text.
Parameters
----------
text : Text.
Returns
-------
Length of the longest word.
"""
return np.max(np.array([len(word) for word in tokenization(text)]))
def mean_word_len(text):
"""
Find mean length of word in text.
Parameters
----------
text : Text.
Returns
-------
Mean length of word.
"""
return np.mean(np.array([len(word) for word in tokenization(text)]))
def punctuation_count(text):
"""
Count the number of punctuations.
Parameters
----------
text : Text.
Returns
-------
Number of punctuations.
"""
return sum([1 if text[i] in string.punctuation else 0 for i in range(len(text))])
def stop_words_count(text):
"""
Count the number of stop-words in text.
Parameters
----------
text : Text.
Returns
-------
Number of stop-words.
"""
words = tokenization(text)
stop_words = stopwords.words('english')
return len([word for word in words if word not in stop_words])
def clean(words):
"""
Remove non-literal symbols from tokenized text.
Parameters
----------
words : List of words.
Returns
-------
tockens: List of processed words.
"""
tokens = []
try:
for token in words:
token = re.sub(r'[\W\d_]', " ", token)
tokens.append(token)
except:
token = ""
tokens.append(token)
return tokens
def lowercasing(words):
"""
Lowercase words.
Parameters
----------
words : List of words.
Returns
-------
List of lowercased words.
"""
return [word.lower() for word in words]
def tokenization(text):
"""
Tokenize Text.
Parameters
----------
text : Text.
Returns
-------
Tokenized Text.
"""
return nltk.word_tokenize(text)
def stemming(words):
"""
Perform stemming.
Parameters
----------
words : List of words.
Returns
-------
List of stemmed words.
"""
stemmer = SnowballStemmer('english')
return [stemmer.stem(word) for word in words]