-
Notifications
You must be signed in to change notification settings - Fork 0
/
NaiveBayes.py
152 lines (134 loc) · 4.57 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
import glob
import math
import sys
from collections import defaultdict
from fractions import Fraction
import numpy as np
def export_dict(word_count,word_dictionary,file_name):
with open(file_name, 'w') as f:
f.write(str(word_count))
f.write('\n')
for word, value in word_dictionary.items():
#f.write(word+" "+value)
#f.write('\n')
pass
def gather_data(file_path):
txt = glob.glob(file_path)
word_dict = defaultdict(int)
count = 0
word_count = 0
for textfile in txt:
count += 1
# if not Fold[0]<=count or not Fold[1]>=count:
word_count = GatherWordCount(textfile, word_dict, word_count)
return word_dict, word_count
def GatherWordCount(textfile, wordDict, wordCount):
f = open(textfile, 'r',encoding='utf-8')
print(textfile)
for line in f:
words = line.split()
for i in words: #go through all words and add to hashtable of current word
wordCount += 1
wordDict[i] += 1
if wordDict[i] == 1: #Lapace-Smoothing
wordDict[i] += .00001 #add K
f.close()
return wordCount
def TestData(line, PostiveTable, PostiveCount, NegativeTable, NegativeCount):
positive_class = 0
negative_class = 0
words = line.split()
for i in words: #go through all words and add to hashtable of current word
positive_class += math.log(calculate_probabilty(PostiveTable, i, PostiveCount))
negative_class += math.log(calculate_probabilty(NegativeTable, i, NegativeCount))
#positive_class+= math.log(float(PostiveCount)/(float(PostiveCount)+float(NegativeCount)))
#negative_class+= math.log(float(NegativeCount)/(float(PostiveCount)+float(NegativeCount)))
# total=np.exp(positive_class)+np.exp(negative_class)
# pos_percent = np.exp(positive_class)/total
# neg_percent= np.exp(negative_class)/total
#return [positive_class,negative_class]
return [positive_class, negative_class]
def calculate_probabilty(wordTable, Word, TableCount):
classifier = 0
if wordTable[Word] == 0:
classifier = (.00001+1)/(TableCount+len(wordTable))
else:
classifier = (wordTable[Word]+1)/(TableCount+len(wordTable))
return classifier
def import_dict(file_name):
f = open(file_name, 'r')
word_count=0
word_dict = defaultdict(int)
for line in f:
words = line.split()
if(len(words)==1):
word_count=words[0]
else:
word_dict[words[0]]=float(words[1])
f.close()
return word_dict,int(word_count)
def gather_data_test(file_path,positive_table,positive_count,
negative_table,negative_count,label,Fold):
txt = glob.glob(file_path)
count = 0
correct=0
results= defaultdict(int)
#print(label)
for textfile in txt:
count += 1
if Fold[0]<=count and count<=Fold[1]:
current = TestData(textfile,positive_table,positive_count,negative_table,negative_count)
if(current==label):
correct += 1
return correct
# Fold=()
# #print(sys.argv[1],sys.argv[2])
# if sys.argv[1]!="fold1" and sys.argv[2]!="fold1":
# Fold=(0,232)
# elif sys.argv[1]!="fold2" and sys.argv[2]!="fold2":
# Fold=(233,465)
# else:
# Fold=(466,698)
#print(Fold)
PATHPOS = "train-pos-5.txt"
PATHNEG = "train-neg-5.txt"
WORD_DICT_POSITIVE, POSITIVECOUNT = gather_data(PATHPOS)
WORD_DICT_NEGATIVE, NegativeCount = gather_data(PATHNEG)
#print(POSITIVECOUNT)
#print(NegativeCount)
export_dict(POSITIVECOUNT, WORD_DICT_POSITIVE, "Postive.txt")
export_dict(NegativeCount, WORD_DICT_NEGATIVE, "Negative.txt")
resultspos=[]
resultsneg=[]
testpos = "test-pos-5.txt"
print("here")
f = open(testpos, 'r',encoding='utf-8')
for line in f:
resultspos.append(TestData(line, WORD_DICT_POSITIVE, POSITIVECOUNT,WORD_DICT_NEGATIVE,NegativeCount))
f.close()
testneg = "test-neg-5.txt"
f = open(testneg, 'r',encoding='utf-8')
for line in f:
resultsneg.append(TestData(line, WORD_DICT_POSITIVE, POSITIVECOUNT,WORD_DICT_NEGATIVE,NegativeCount))
f.close()
poscount=0
negcount=0
poscorrect=0
negcorrect=0
print("Postive")
for res in resultspos:
poscount+=1
if res[0]>res[1]:
poscorrect+=1
else:
print(res[0],res[1],poscount)
print("Negative")
for res in resultsneg:
negcount+=1
if res[1]>res[0] and negcount!=16:
negcorrect+=1
else:
print(res[0],res[1],negcount)
print("The postive accuracy is "+str(poscorrect/float(poscount)))
print("The negative accuracy is "+str(negcorrect/float(negcount)))
print("The accuracy is "+str((poscorrect+negcorrect)/float(poscount+negcount)))