-
Notifications
You must be signed in to change notification settings - Fork 0
/
DocClass.py
214 lines (163 loc) · 5.3 KB
/
DocClass.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import re
import math
import sqlite3 as sqlite
def getWords(doc):
splitter = re.compile('\\W*')
words = [s.lower() for s in splitter.split(doc) if len(s) > 2 and len(s) < 20]
return dict([(w, 1) for w in words])
def sampleTrain(cl):
cl.train('Nobody owns the water.', 'good')
cl.train('the quick rabbit jumps fences', 'good')
cl.train('the quick brown fox jumps', 'good')
cl.train('buy pharmaceuticals now', 'bad')
cl.train('make quick money at the online casino', 'bad')
class classifier:
def __init__(self, getFeatures, filename = None):
self.featureCatCount = {}
self.catCount = {}
self.getFeatures = getFeatures
self.thresholdes = {}
def setDB(self, dbFile):
self.con = sqlite.connect(dbFile)
self.con.execute('create table if not exists fc(feature, category, count)')
self.con.execute('create table if not exists cc(category, count)')
def setThreshold(self, cat, t):
self.thresholdes[cat] = totalItemCount
def getThreshold(self, cat):
if cat not in self.thresholdes:
return 1.0
return self.thresholdes[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
def incFeatureCatCount(self, f, cat):
# self.featureCatCount.setdefault(f, {})
# self.featureCatCount[f].setdefault(cat, 0)
# self.featureCatCount[f][cat] += 1
count = self.featureCount(f, cat)
if count == 0:
self.con.execute("insert into fc values ('%s', '%s', 1)" % (f, cat))
else:
self.con.execute("update fc set count = %d where feature = '%s' and category = '%s'" % (count + 1, f, cat))
def incCatCount(self, cat):
# self.catCount.setdefault(cat, 0)
# self.catCount[cat] += 1
count = self.itemCountInCat(cat)
if count == 0:
self.con.execute("insert into cc values ('%s', 1)" % (cat))
else:
self.con.execute("update cc set count = %d where category = '%s'" % (count + 1, cat))
def featureCount(self, f, cat):
# if f in self.featureCatCount and cat in self.featureCatCount[f]:
# return float(self.featureCatCount[f][cat])
# return 0.0
res = self.con.execute("select count from fc where feature = '%s' and category = '%s'" % (f, cat)).fetchone()
if res == None:
return 0
else:
return float(res[0])
def itemCountInCat(self, cat):
# if cat in self.catCount:
# return float(self.catCount[cat])
# return 0
res = self.con.execute("select count from cc where category = '%s'" % (cat)).fetchone()
if res == None:
return 0
else:
return float(res[0])
def totalItemCount(self):
# return sum(self.catCount.values())
res = self.con.execute("select sum(count) from cc").fetchone()
if res == None:
return 0
else:
return res[0]
def categories(self):
# return self.catCount.keys()
cur = self.con.execute('select category from cc')
return [d[0] for d in cur]
def train(self, item, cat):
features = self.getFeatures(item)
for f in features:
self.incFeatureCatCount(f, cat)
self.incCatCount(cat)
self.con.commit()
def featureProb(self, f, cat):
if self.itemCountInCat(cat) == 0:
return 0
return self.featureCount(f, cat) / self.itemCountInCat(cat)
def weightedProb(self, f, cat, prf, weight = 1.0, assumedProb = 0.5):
basicProb = prf(f, cat)
total = sum([self.featureCount(f, c) for c in self.categories()])
# a weight of 1 means the assumed probability is weighted the same as one word
# (weight * assumedProb) => the assumed appear time in this cat
# (total * basicProb) => the average appear time in this cat
# (weight + total) => total appear time
bp = ((weight * assumedProb) + (total * basicProb)) / (weight + total)
return bp
class naiveBayes(classifier):
def docProb(self, item, cat):
features = self.getFeatures(item)
p = 1
for f in features:
p *= self.weightedProb(f, cat, self.featureProb)
return p
def prob(self, item, cat):
catProb = self.itemCountInCat(cat) / self.totalItemCount()
docProb = self.docProb(item, cat)
return docProb * catProb
class fisherClassifier(classifier):
def __init__(self, getFeatures):
classifier.__init__(self, getFeatures)
self.minimums = {}
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 catProb(self, f, cat):
freInThisCat = self.featureProb(f, cat)
if freInThisCat == 0:
return 0
freSum = sum([self.featureProb(f, c) for c in self.categories()])
p = freInThisCat / freSum
return p
def fisherProb(self, item, cat):
p = 1
features = self.getFeatures(item)
for f in features:
p *= (self.weightedProb(f, cat, self.catProb))
fScore = -2 * math.log(p)
return self.invChi2(fScore, len(features) * 2)
def invChi2(self, chi, df):
m = chi / 2.0
print 'm: %f' % m
sum = term = math.exp(-m)
print 'sum&term: %f' % sum
for i in range(1, df // 2):
print 'i: %d' % i
term *= m / i
print 'term: %f' % term
sum += term
return min(sum, 1.0)
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