-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnaivebayes.py
178 lines (140 loc) · 5.38 KB
/
naivebayes.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
# -*- coding: utf-8 -*-
from __future__ import division
import re
import string
import io
import random
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
from nltk.stem import WordNetLemmatizer
from nltk.stem import PorterStemmer
from nltk.stem import WordNetLemmatizer
from collections import *
import json
import random
import math
class1file = "raw_data_sport.txt"
class2file = "raw_data_politics.txt"
impWOrd_politics = {}
impWOrd_sport = {}
def randonPartitioner(text, percent):
firstPartition = text.split(" ")
partitionedLength = len(firstPartition)
secondPartiotionLength = int(partitionedLength * percent)
secondPartiotion = []
for i in range(secondPartiotionLength):
r = random.randint(0, partitionedLength -i-1)
secondPartiotion.append(firstPartition[r])
del firstPartition[r]
firstPartitionStringified = ' '.join(firstPartition)
secondPartiotionStringified = ' '.join(secondPartiotion)
return firstPartitionStringified, secondPartiotionStringified
def cleanText(text):
text = text.lower() # Convert text to lowercase
text = re.sub(r'\d+', '', text) # Remove numbers
text = text.replace("\n", " ")
text = text.replace("-", "")
stop_words = set(stopwords.words('english'))
for word in stop_words:
text = text.replace(" " + word + " ", " ")
return text
def pClass1(word, countallword1, countDistWords1, class1words):
if word in class1words.keys():
tmp = (class1words[word])/(countDistWords1+countallword1)
return math.log10(tmp )
else:
tmp = 1/(countDistWords1+countallword1) # unknown words
return math.log10(tmp )
def pClass2(word, countallword2, countDistWords2, class2words):
if word in class2words.keys():
tmp = (class2words[word])/(countDistWords2+countallword2)
return math.log10(tmp )
else:
tmp = 1/(countDistWords2+countallword2)
return math.log10(tmp)
def calculateP(text, pclass1, pclass2, countallword1, countallword2, countDistWords1,countDistWords2,class1words,class2words):
pWordInClass1 = pclass1
pWordInClass2 = pclass2
pmax1 = 0
pmax2 = 0
impword2 = 0
impword1 = 0
for word in text:
p = pClass1(word, countallword1, countDistWords1, class1words)
pmax1 = p
impword1 = word
p = pClass2(word, countallword2, countDistWords2, class2words)
pmax2 = p
impword2 = word
pWordInClass1 += pClass1(word, countallword1, countDistWords1, class1words)
pWordInClass2 += pClass2(word, countallword2, countDistWords2, class2words)
if pWordInClass1 > pWordInClass2:
impWOrd_sport[impword1] = pmax1
return 1
else:
impWOrd_politics[impword2] = pmax2
return 2
def countWords(train1, train2):
train1 = train1.split("," and "\n" and "." and ":" and "?" and "\"" and "" and " ")
counter1 = OrderedDict(Counter(train1))
train2 = train2.split("," and "\n" and "." and ":" and "?" and "\"" and "" and " ")
counter2 = OrderedDict(Counter(train2))
return counter1, counter2
def stringifyEvery5Words(arr):
LEN = int(len(arr) / 50)
result = []
for i in range(LEN):
result.append(" ".join(arr[: 50]))
del arr[: 50]
if len(" ".join(arr)):
result.append(" ".join(arr))
return result
def classifier():
class1 = open(class1file).read()
class2 = open(class2file).read()
class1 = class1.decode("utf8", 'ignore')
class2 = class2.decode("utf8", 'ignore')
class1 = cleanText(class1)
class2 = cleanText(class2)
train1, test1 = randonPartitioner(class1, 0.10)
train2, test2 = randonPartitioner(class2, 0.10)
countSentence1 = sum(Counter(train1.split("." and "\n" and "\r" and "?" and "!" and ":")).values())
countSentence2 = sum(Counter(train2.split("." and "\n" and "\r" and "?" and "!" and ":")).values())
class1words, class2words = countWords(train1, train2) # a dictionary with count of words
countallword1 = sum(class1words.values())
countallword2 = sum(class2words.values())
print (countallword1, "count words 1")
print (countallword2, "count words 2")
countDistWords1 = len(class1words.keys())
countDistWords2 = len(class2words.keys())
# test :
pclass1 = (countSentence1/(countSentence1+countSentence2))
pclass2 = (countSentence2/(countSentence1+countSentence2))
print(countSentence1,"Count sentence 1")
sentence1 = test1.split("." and "\n" and "\r" and "?" and "!" and ":" and " ")
sentence2 = test2.split("." and "\n" and "\r" and "?" and "!" and ":" and " ")
sentence1 = stringifyEvery5Words(sentence1)
sentence2 = stringifyEvery5Words(sentence2)
# compute precision and recall:
tp = 0
fn = 0
fp = 0
tn = 0
for sentence in sentence1:
c = calculateP(sentence, pclass1, pclass2, countallword1, countallword2, countDistWords1, countDistWords2, class1words, class2words)
if c == 1:
tp += 1
else: # if it says its in class 2
fn += 1
for sentence in sentence2:
c = calculateP(sentence, pclass1, pclass2, countallword1, countallword2, countDistWords1, countDistWords2,
class1words, class2words)
if c == 2:
tn += 1
else:
fp += 1
print(fp, tp, fn, tn)
precision = tp/(tp+fp)
recall = tp/(tp+fn)
print(precision, recall)
classifier()