-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocclasscsveval.py
148 lines (110 loc) · 3 KB
/
docclasscsveval.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
import re
import math
import pickle
def getwordsnew(doc):
splitter = re.compile("\W*")
f={}
if re.findall(r'\?',doc):
f['?'] = 1
words = [s.lower() for s in splitter.split(doc) if len(s) > 2 and len(s) < 20]
for i in range(len(words)):
oneword = words[i]
f[oneword] = 1
if i<len(words)-1:
twowords=' '.join(words[i:i+2])
f[twowords] = 1
return f
class classifier:
def __init__(self, getfeatures, filename = None):
self.fc = {}
self.cc = {}
self.getfeatures = getfeatures
self.thresholds={}
def readfc(self,outfile):
with open(outfile, 'rb') as handle:
self.fc = pickle.loads(handle.read())
def readcc(self,outfile):
with open(outfile,'rb') as handle:
self.cc = pickle.loads(handle.read())
def fcount(self, f, cat):
if f in self.fc and cat in self.fc[f]:
return float(self.fc[f][cat])
return 0.0
def catcount(self, cat):
if cat in self.cc:
return float(self.cc[cat])
return 0.0
def totalcount(self):
return sum(self.cc.values())
def categories(self):
return self.cc.keys()
def fprob(self, f, cat):
if self.catcount(cat)==0: return 0
return self.fcount(f,cat)/self.catcount(cat)
def weightedprob(self, f, cat, prf, weight=1.0, ap=0.5):
basicprob = prf(f,cat)
totals = sum([self.fcount(f,c) for c in self.categories()])
bp = ((weight*ap)+(totals*basicprob))/(weight+totals)
return bp
def setthreshold(self, cat, t):
self.thresholds[cat] = t
def getthreshold(self, cat):
if cat not in self.thresholds: return 1.0
return self.thresholds[cat]
def classify(self, item, default=None):
probs={}
max = 0.0
for cat in self.categories():
probs[cat] = self.prob(item,cat)
if probs[cat]>max:
max=probs[cat]
best=cat
for cat in probs:
if cat==best: continue
if probs[cat]*self.getthreshold(best)>probs[best]: return default
return best
class naivebayes(classifier):
def prob(self,item,cat):
features = self.getfeatures(item)
p = 1
for f in features:
p *= self.weightedprob(f,cat,self.fprob)
return p
class fisherclassifier(classifier):
def __init__(self,getfeatures):
classifier.__init__(self,getfeatures)
self.minimums = {}
def cprob(self,f,cat):
clf = self.fprob(f,cat)
if clf == 0: return 0
freqsum = sum([self.fprob(f,c) for c in self.categories()])
p = clf/freqsum
return p
def fisherprob(self,item,cat):
p = 1
features = self.getfeatures(item)
for f in features:
p *= (self.weightedprob(f,cat,self.cprob))
fscore = -2*math.log(p)
return self.invchi2(fscore,len(features)*2)
def invchi2(self,chi,df):
m = chi/2.0
sum = term = math.exp(-m)
for i in range(1,df//2):
term *= m/i
sum += term
return min(sum, 1.0)
def setminimum(self,cat, min):
self.minimums[cat]=min
def getminimum(self,cat):
if cat not in self.minimums: return 0
return self.minimums[cat]
def classify(self,item,default=None):
best = default
max = 0.0
for c in self.categories():
p = self.fisherprob(item,c)
if p > self.getminimum(c) and p > max:
best = c
max = p
return best