-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathspamdetector.py
53 lines (36 loc) · 1.42 KB
/
spamdetector.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
from nltk import word_tokenize,WordNetLemmatizer,NaiveBayesClassifier,classify,MaxentClassifier
from nltk.corpus import stopwords
import random
import os, glob,re
commonwords = stopwords.words('english')
wordlemmatizer = WordNetLemmatizer()
def email_features(sent):
features = {}
wordtokens = [wordlemmatizer.lemmatize(word.lower()) for word in word_tokenize(sent)]
for word in wordtokens:
if word not in commonwords:
features[word] = True
return features
hamtexts = []
spamtexts = []
for infile in glob.glob( os.path.join('ham/', '*.txt')):
text_file = open(infile, "r")
hamtexts.append(text_file.read())
text_file.close()
for infile in glob.glob( os.path.join('spam/', '*.txt') ):
text_file = open(infile, "r")
spamtexts.append(text_file.read())
text_file.close()
mixedemails = ([(email,'spam') for email in spamtexts] + [(email,'ham') for email in hamtexts])
random.shuffle(mixedemails)
featuresets = [(email_features(n), g) for (n,g) in mixedemails]
size = int(len(featuresets) * 0.35)
train_set, test_set = featuresets[size:], featuresets[:size]
classifier = NaiveBayesClassifier.train(train_set)
#classifier = MaxentClassifier.train(train_set,'Powell',3)
print 'accuracy: ', classify.accuracy(classifier,test_set)
classifier.show_most_informative_features(30)
print 'labels:',classifier.labels()
while(True):
featset = email_features(raw_input("Enter text to classify: "))
print classifier.classify(featset)