forked from toshiakit/classification
-
Notifications
You must be signed in to change notification settings - Fork 0
/
classifier.m
executable file
·199 lines (182 loc) · 6.37 KB
/
classifier.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
classdef classifier < 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.
properties
% Counts of feature/category combinations
fc;
% Counts of documents in each category
cc;
% Function to extract features
getfeatures
% probability thresholds for classifying into given categories
thresholds;
end
% Public Methods
methods
% Constructor Method
function self=classifier(getfeatures)
if nargin==0
self.getfeatures=@getwords;
else
self.getfeatures=getfeatures;
end
self.fc={};
self.cc={};
self.thresholds={};
end
% Increase the count of a feature/category pair
function incf(self,f,cat)
if isempty(strmatch(f,char(self.fc{:,1}), 'exact'))
self.fc{end+1,1}=f;
self.fc{end,2}=cat;
self.fc{end,3}=1;
else
idx=strmatch(f,char(self.fc{:,1}), 'exact');
if isempty(strmatch(cat,char(self.fc{idx,2}), 'exact'))
self.fc{end+1,1}=f;
self.fc{end,2}=cat;
self.fc{end,3}=1;
else
idx=idx(strmatch(cat,char(self.fc{idx,2})));
self.fc{idx,3}=self.fc{idx,3}+1;
end
end
end
% Increase the count of a category
function incc(self,cat)
if isempty(strmatch(cat,char(self.cc{:,1}), 'exact'))
self.cc{end+1,1}=cat;
self.cc{end,2}=1;
else
idx=strmatch(cat,char(self.cc{:,1}), 'exact');
self.cc{idx,2}=self.cc{idx,2}+1;
end
end
% The number of times a feature has appeared in a category
function c=fcount(self,f,cat)
idx=strmatch(f,char(self.fc{:,1}), 'exact');
if ~isempty(idx) && ~isempty(strmatch(cat,char(self.fc{idx,2}), 'exact'))
idx=idx(strmatch(cat,char(self.fc{idx,2})));
c=self.fc{idx,3};
else
c=0;
end
end
% The number of item in a category
function c=catcount(self,cat)
idx=strmatch(cat,char(self.cc{:,1}), 'exact');
if ~isempty(idx)
c=self.cc{idx,2};
else
c=0;
end
end
% The total number of items
function c=totalcount(self)
c=sum(cell2mat(self.cc(:,2)));
end
% The list of all categories
function list=categories(self)
if ~isempty(self.cc)
list=self.cc(:,1);
else
list=cell(0,1);
end
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)
% Extract features
features=self.getfeatures(item);
% Increment the count for every feature with this category
for i=1:size(features,1)
self.incf(features{i},cat);
end
% Increment the count for this category
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