-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbayes.php
More file actions
211 lines (189 loc) · 3.74 KB
/
bayes.php
File metadata and controls
211 lines (189 loc) · 3.74 KB
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
<?php
require_once 'stemmer.php';
/**
* PHP version of Ruby Bayes classifier library
* @see http://github.com/xaviershay/classifier
*/
class Bayes
{
private $_categories;
private $_totalWords;
/**
* The class can be created with one or more categories, each of which will be
* initialized and given a training method. E.g.,
* $b = new Bayes('Interesting', 'Uninteresting', 'Spam')
*/
public function __construct()
{
$categories = func_get_args();
$this->_totalWords = 0;
$this->_categories = array();
foreach ($categories as $category)
{
$this->_categories[$category] = array();
}
}
/**
* Provides a general training method for all categories specified in Bayes#new
* For example:
* $b = new Bayes('this', 'that', 'the_other')
* $b->train('this', 'This text')
* $b->train('that', 'That text')
* $b->train('the_other', 'The other text')
*/
public function train($category, $text)
{
foreach ($this->_wordArray($text) as $word => $count)
{
if (!isset($this->_categories[$category][$word]))
$this->_categories[$category][$word] = 0;
$this->_categories[$category][$word] += $count;
$this->_totalWords += $count;
}
}
/**
* Returns the scores in each category the provided +text+. E.g.,
* $b->classifications("I hate bad words and you")
* => {"Uninteresting" => -12.6997928013932, "Interesting" => -18.4206807439524}
* The largest of these scores (the one closest to 0) is the one picked out by classify()
*/
public function classifications($text)
{
$score = array();
foreach ($this->_categories as $category => $categoryWords)
{
$score[$category] = 0.0;
$total = array_sum(array_values($categoryWords));
foreach ($this->_wordArray($text) as $word => $count)
{
$s = isset($categoryWords[$word]) ? $categoryWords[$word] : 0.1;
$score[$category] += log($s / $total);
}
}
return $score;
}
/**
* Returns the classification of the provided +text+, which is one of the
* categories given in the initializer. E.g.,
* $b->classify("I hate bad words and you")
* => 'Uninteresting'
*/
public function classify($text)
{
$a = $this->classifications($text);
arsort($a);
return array_shift(array_keys($a));
}
// ----
/**
* Return an array of strings => ints. Each word in the string is stemmed,
* and indexes to its frequency in the document.
*/
private function _wordArray($word)
{
return $this->_wordArrayForWords(
array_merge(
preg_split('/\s+/', preg_replace('/[^\w\s]/','', $word)),
preg_split('/\s+/', preg_replace('/[\w]/',' ', $word))));
}
private function _wordArrayForWords($words)
{
$d = array();
foreach ($words as $word)
{
if (preg_match('/[\w]+/',$word)) $word = strtolower($word);
$key = PorterStemmer::Stem($word);
if (preg_match('/[^\w]/',$word)
|| !in_array($word, self::$CORPUS_SKIP_WORDS)
&& strlen($word) > 2)
{
if (!isset($d[$key]))
$d[$key] = 0;
$d[$key] += 1;
}
}
return $d;
}
private static $CORPUS_SKIP_WORDS = array(
"a",
"again",
"all",
"along",
"are",
"also",
"an",
"and",
"as",
"at",
"but",
"by",
"came",
"can",
"cant",
"couldnt",
"did",
"didn",
"didnt",
"do",
"doesnt",
"dont",
"ever",
"first",
"from",
"have",
"her",
"here",
"him",
"how",
"i",
"if",
"in",
"into",
"is",
"isnt",
"it",
"itll",
"just",
"last",
"least",
"like",
"most",
"my",
"new",
"no",
"not",
"now",
"of",
"on",
"or",
"should",
"sinc",
"so",
"some",
"th",
"than",
"this",
"that",
"the",
"their",
"then",
"those",
"to",
"told",
"too",
"true",
"try",
"until",
"url",
"us",
"were",
"when",
"whether",
"while",
"with",
"within",
"yes",
"you",
"youll",
);
}