-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEmail_Project.py
188 lines (89 loc) · 3.24 KB
/
Email_Project.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
177
178
179
180
181
182
183
184
#!/usr/bin/env python
# coding: utf-8
# # To classify emails as spam or not-spam using NLP
# In[3]:
from nltk.tokenize import RegexpTokenizer , word_tokenize
from nltk.stem import PorterStemmer
from nltk.corpus import stopwords
import pandas as pd
import numpy as np
from sklearn.preprocessing import LabelEncoder
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.model_selection import train_test_split
# In[5]:
df = pd.read_csv('./spam.csv' , encoding = 'ISO-8859-1')
le = LabelEncoder()
# In[6]:
data = df.to_numpy()
# In[9]:
y = data[:,0]
X = data[:,1]
# In[10]:
X.shape , y.shape
# In[11]:
tokenizer = RegexpTokenizer('\w+')
sw = set(stopwords.words('english'))
ps = PorterStemmer()
# In[12]:
def getStemmedReview(review):
review = review.lower()
# tokenize
tokens = tokenizer.tokenize(review)
# removing the stopwords
new_tokens = [token for token in tokens if token not in sw]
# stemming
stemmed_tokens = [ps.stem(token) for token in new_tokens]
cleaned_review = ' '.join(stemmed_tokens)
return cleaned_review
# In[15]:
def getStemmedDocument(document):
d = []
for doc in document:
d.append(getStemmedReview(doc))
return d
# In[18]:
stemmed_document = getStemmedDocument(X)
# In[20]:
stemmed_document[:10]
# In[21]:
cv = CountVectorizer()
# In[22]:
vectorized_corpus = cv.fit_transform(stemmed_document)
# In[24]:
X = vectorized_corpus.todense()
# In[25]:
X_train , X_test , y_train, y_test = train_test_split(X ,y ,test_size = 0.33 , random_state = 42)
# In[26]:
from sklearn.naive_bayes import MultinomialNB
# In[27]:
model = MultinomialNB()
# In[28]:
model.fit(X_train , y_train)
# In[29]:
model.score(X_test , y_test)
# In[34]:
messages = [
"""
Hi Oshi,
We invite you to participate in MishMash - India’s largest online diversity hackathon.
The hackathon is a Skillenza initiative and sponsored by Microsoft, Unity, Unilever, Gojek, Rocketium and Jharkhand Government.
We have a special theme for you - Deep Tech/Machine Learning - sponsored by Unilever, which will be perfect for you.""",
"""Join us today at 12:00 PM ET / 16:00 UTC for a Red Hat DevNation tech talk on AWS Lambda and serverless Java with Bill Burke.
Have you ever tried Java on AWS Lambda but found that the cold-start latency and memory usage were far too high?
In this session, we will show how we optimized Java for serverless applications by leveraging GraalVM with Quarkus to
provide both supersonic startup speed and a subatomic memory footprint.""",
"""We really appreciate your interest and wanted to let you know that we have received your application.
There is strong competition for jobs at Intel, and we receive many applications. As a result, it may take some time to get back to you.
Whether or not this position ends up being a fit, we will keep your information per data retention policies,
so we can contact you for other positions that align to your experience and skill set.
"""
]
# In[35]:
def prepare_message(messages):
d = getStemmedDocument(messages)
return cv.transform(d)
messages = prepare_message(messages)
# In[36]:
y_pred = model.predict(messages)
print(y_pred)
# In[ ]: