-
Notifications
You must be signed in to change notification settings - Fork 3
/
classifier2.m
executable file
·204 lines (186 loc) · 6.57 KB
/
classifier2.m
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
classdef classifier2 < handle % subclass of 'handle' superclass
% CLASSIFIER defines a class of objects that classifies documents based on
% their features. Constructor method takes an optional feature extraction
% function reference.
% Created to experiment with MATLAB OOP features.
% Modified to store data in SQLite database using mksqlite
properties
% Function to extract features
getfeatures
% probability thresholds for classifying into given categories
thresholds;
% Database Connection ID
con;
end
% Public Methods
methods
% Constructor Method
function self=classifier(getfeatures)
if nargin==0
self.getfeatures=@getwords;
else
self.getfeatures=getfeatures;
end
self.thresholds={};
end
% Open or create a database
function setdb(self,dbfile)
self.con= mksqlite(0, 'open', dbfile);
mksqlite(self.con,'create table if not exists fc(feature,category,count)');
mksqlite(self.con,'create table if not exists cc(category,count)');
end
% Increase the count of a feature/category pair
function incf(self,f,cat)
count=self.fcount(f,cat);
if count==0
sq='insert into fc values (';
sq=[sq sprintf('''%s'',''%s'',1',f,cat) ')'];
mksqlite(self.con, sq);
else
sq='update fc set count=';
sq=[sq sprintf('%d where feature=''%s'' and category=''%s''',count+1,f,cat)];
mksqlite(self.con, sq);
end
end
% Increase the count of a category
function incc(self,cat)
count=self.catcount(cat);
if count==0
sq='insert into cc values (';
sq=[sq sprintf('''%s'',1',cat) ')'];
mksqlite(self.con, sq);
else
sq='update cc set count=';
sq=[sq sprintf('%d where category=''%s''',count+1,cat)];
mksqlite(self.con,sq);
end
end
% The number of times a feature has appeared in a category
function c=fcount(self,f,cat)
sq='select count from fc where feature=';
sq=[sq sprintf('''%s'' and category=''%s''',f,cat)];
sq = [sq ' limit 1'];
res=mksqlite(self.con, sq);
if isempty(res)
c=0;
else
c=res.count;
end
end
% The number of item in a category
function c=catcount(self,cat)
sq=sprintf('select count from cc where category=''%s''',cat);
sq=[sq ' limit 1'];
res=mksqlite(self.con, sq);
if isempty(res)
c=0;
else
c=res.count;
end
end
% The total number of items
function c=totalcount(self)
res=mksqlite(self.con, 'select sum(count) from cc');
if isempty(res)
c=0;
else
c=res.('sum(count)');
end
end
% The list of all categories
function list=categories(self)
res=mksqlite(self.con, 'select category from cc');
list={res.category}';
end
% 'train' method takes an item (a document) and a category and
% extract features using getfeature function, then increment the
% feature/category counts.
function train(self,item,cat)
features=self.getfeatures(item);
for f=1:size(features,1)
self.incf(features{f},cat);
end
self.incc(cat);
end
function p=fprob(self,f,cat)
if self.catcount(cat)==0
p=0;
else
% The total number of times this feature appeared in this
% category divided by the total number of items in this
% category... Pr(feature|category)
p=self.fcount(f,cat)/self.catcount(cat);
end
end
%
function bp=weightedprob(self,f,cat,prf,weight,ap)
if nargin <3
error('Not enough input arguments.')
elseif nargin <4
prf=@fprob; % function reference for basic probability
weight=1.0; % weight of assumed probability
ap=0.5; % assumed probability, initially 0.5 = neutral
elseif nargin <5
weight=1.0;
ap=0.5;
elseif nargin <6
ap=0.5;
end
% Calculate current probability
basicprob=prf(f,cat);
% Count the number of times this feature has appeared in all
% categories
cat=self.categories();
totals=0;
for i=1:size(cat,1)
totals=totals+self.fcount(f,cat{i});
end
% Calculate the weighted average
bp=((weight*ap)+(totals*basicprob))/(weight+totals);
end
function setthreshold(self,cat,t)
if isempty(strmatch(cat,char(self.thresholds{:,1}), 'exact'))
self.thresholds{end+1,1}=cat;
self.thresholds{end,2}=t;
else
idx=strmatch(cat,char(self.thresholds{:,1}), 'exact');
self.thresholds{idx,2}=t;
end
end
function t=getthreshold(self,cat)
if isempty(strmatch(cat,char(self.thresholds{:,1}), 'exact'))
t=1;
else
idx=strmatch(cat,char(self.thresholds{:,1}), 'exact');
t=self.thresholds{idx,2};
end
end
function category=classify(self,item,default)
if nargin <3
default='';
end
% Find the category with the highest probability
max=0.0;
cat=self.categories();
probs=zeros(size(cat,1),1);
for i=1:size(cat,1)
probs(i,1)=self.prob(item,cat{i});
if probs(i,1)>max
max=probs(i,1);
best=cat{i};
end
end
% Make sure the probability exceeds threshold*next best
for i=1:size(cat,1)
if strcmp(cat{i},best)
continue;
end
if probs(i,1)*self.getthreshold(best)>probs(strmatch(best,char(cat),'exact'))
category=default;
else
category=best;
end
end
end
end
end