]>
git.wh0rd.org - tt-rss.git/blob - plugins/af_sort_bayes/lib/class.naivebayesian.php
4a4ffa7eba521ba3f674106911a683a8e9cfafc0
3 ***** BEGIN LICENSE BLOCK *****
4 This file is part of PHP Naive Bayesian Filter.
6 The Initial Developer of the Original Code is
7 Loic d'Anterroches [loic_at_xhtml.net].
8 Portions created by the Initial Developer are Copyright (C) 2003
9 the Initial Developer. All Rights Reserved.
14 PHP Naive Bayesian Filter is free software; you can redistribute it
15 and/or modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation; either version 2 of
17 the License, or (at your option) any later version.
19 PHP Naive Bayesian Filter is distributed in the hope that it will
20 be useful, but WITHOUT ANY WARRANTY; without even the implied
21 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
22 See the GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with Foobar; if not, write to the Free Software
26 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 Alternatively, the contents of this file may be used under the terms of
29 the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 in which case the provisions of the LGPL are applicable instead
33 ***** END LICENSE BLOCK *****
37 /** min token length for it to be taken into consideration */
38 var $min_token_length = 3;
39 /** max token length for it to be taken into consideration */
40 var $max_token_length = 15;
41 /** list of token to ignore
44 var $ignore_list = array();
46 @see class NaiveBayesianStorage
50 function NaiveBayesian($nbs) {
56 /** categorize a document.
57 Get list of categories in which the document can be categorized
58 with a score for each category.
60 @return array keys = category ids, values = scores
61 @param string document
63 function categorize($document) {
65 $categories = $this->nbs
->getCategories();
66 $tokens = $this->_getTokens($document);
68 // calculate the score in each category
72 while (list($category, $data) = each($categories)) {
73 $total_words +
= $data['word_count'];
79 while (list($category, $data) = each($categories)) {
80 $scores[$category] = $data['probability'];
81 // small probability for a word not in the category
82 // maybe putting 1.0 as a 'no effect' word can also be good
84 if ($data['word_count'] > 0)
85 $small_proba = 1.0 / ($data['word_count'] * 2);
91 while (list($token, $count) = each($tokens)) {
93 if ($this->nbs
->wordExists($token)) {
94 $word = $this->nbs
->getWord($token, $category);
97 $proba = $word['count'] / $data['word_count'];
100 $proba = $small_proba;
103 $scores[$category] *= pow($proba, $count) * pow($total_words / $ncat, $count);
104 // pow($total_words/$ncat, $count) is here to avoid underflow.
110 return $this->_rescale($scores);
113 /** training against a document.
114 Set a document as being in a specific category. The document becomes a reference
115 and is saved in the table of references. After a set of training is done
116 the updateProbabilities() function must be run.
118 @see updateProbabilities()
121 @param string document id, must be unique
122 @param string category_id the category id in which the document should be
123 @param string content of the document
125 function train($doc_id, $category_id, $content) {
129 // if this doc_id already trained, no trained
130 if (!$this->nbs
->getReference($doc_id, false)) {
132 $tokens = $this->_getTokens($content);
134 while (list($token, $count) = each($tokens)) {
135 $this->nbs
->updateWord($token, $count, $category_id);
138 $this->nbs
->saveReference($doc_id, $category_id, $content);
149 /** untraining of a document.
150 To remove just one document from the references.
152 @see updateProbabilities()
155 @param string document id, must be unique
157 function untrain($doc_id) {
158 $ref = $this->nbs
->getReference($doc_id);
160 if (isset($ref['content'])) {
162 $tokens = $this->_getTokens($ref['content']);
164 while (list($token, $count) = each($tokens)) {
165 $this->nbs
->removeWord($token, $count, $ref['category_id']);
168 $this->nbs
->removeReference($doc_id);
176 /** rescale the results between 0 and 1.
178 @author Ken Williams, ken@mathforum.org
180 @return array normalized scores (keys => category, values => scores)
181 @param array scores (keys => category, values => scores)
183 function _rescale($scores) {
184 // Scale everything back to a reasonable area in
185 // logspace (near zero), un-loggify, and normalize
190 while (list($cat, $score) = each($scores)) {
196 while (list($cat, $score) = each($scores)) {
197 $scores[$cat] = (float) exp($score - $max);
198 $total +
= (float) pow($scores[$cat], 2);
201 $total = (float) sqrt($total);
204 while (list($cat, $score) = each($scores)) {
205 $scores[$cat] = (float) $scores[$cat] / $total;
212 /** update the probabilities of the categories and word count.
213 This function must be run after a set of training
219 function updateProbabilities() {
220 // this function is really only database manipulation
221 // that is why all is done in the NaiveBayesianStorage
222 return $this->nbs
->updateProbabilities();
225 /** Get the list of token to ignore.
226 @return array ignore list
228 function getIgnoreList() {
229 //return array('the', 'that', 'you', 'for', 'and');
231 // https://en.wikipedia.org/wiki/Most_common_words_in_English
232 return array('the', 'be', 'to', 'of', 'and', 'a', 'in', 'that', 'have', 'I', 'it', 'for', 'not', 'on', 'with',
233 'he', 'as', 'you', 'do', 'at', 'this', 'but', 'his', 'by', 'from', 'they', 'we', 'say', 'her',
234 'she', 'or', 'an', 'will', 'my', 'one', 'all', 'would', 'there', 'their', 'what', 'so', 'up',
235 'out', 'if', 'about', 'who', 'get', 'which', 'go', 'me', 'when', 'make', 'can', 'like', 'time',
236 'no', 'just', 'him', 'know', 'take', 'people', 'into', 'year', 'your', 'good', 'some', 'could',
237 'them', 'see', 'other', 'than', 'then', 'now', 'look', 'only', 'come', 'its', 'over', 'think',
238 'also', 'back', 'after', 'use', 'two', 'how', 'our', 'work', 'first', 'well', 'way', 'even',
239 'new', 'want', 'because', 'any', 'these', 'give', 'day', 'most', 'us', 'read', 'more');
243 /** get the tokens from a string
245 @author James Seng. [http://james.seng.cc/] (based on his perl version)
248 @param string the string to get the tokens from
250 function _getTokens($string) {
251 $rawtokens = array();
253 //$string = $this->_cleanString($string);
255 if (count(0 >= $this->ignore_list
)) {
256 $this->ignore_list
= $this->getIgnoreList();
259 $rawtokens = preg_split("/[\(\),:\.;\t\r\n ]/", $string, -1, PREG_SPLIT_NO_EMPTY
);
261 // remove some tokens
262 while (list(, $token) = each($rawtokens)) {
263 $token = trim($token);
264 if (!(('' == $token) ||
(mb_strpos($token, "&") !== FALSE) ||
(mb_strlen($token) < $this->min_token_length
) ||
(mb_strlen($token) > $this->max_token_length
) ||
(preg_match('/^[0-9]+$/', $token)) ||
(in_array($token, $this->ignore_list
)))) {
272 /** clean a string from the diacritics
274 @author Antoine Bajolet [phpdig_at_toiletoine.net]
275 @author SPIP [http://uzine.net/spip/]
277 @return string clean string
278 @param string string with accents
280 function _cleanString($string) {
281 $diac = /* A */ chr(192) . chr(193) . chr(194) . chr(195) . chr(196) . chr(197) .
282 /* a */ chr(224) . chr(225) . chr(226) . chr(227) . chr(228) . chr(229) .
283 /* O */ chr(210) . chr(211) . chr(212) . chr(213) . chr(214) . chr(216) .
284 /* o */ chr(242) . chr(243) . chr(244) . chr(245) . chr(246) . chr(248) .
285 /* E */ chr(200) . chr(201) . chr(202) . chr(203) .
286 /* e */ chr(232) . chr(233) . chr(234) . chr(235) .
287 /* Cc */ chr(199) . chr(231) .
288 /* I */ chr(204) . chr(205) . chr(206) . chr(207) .
289 /* i */ chr(236) . chr(237) . chr(238) . chr(239) .
290 /* U */ chr(217) . chr(218) . chr(219) . chr(220) .
291 /* u */ chr(249) . chr(250) . chr(251) . chr(252) .
292 /* yNn */ chr(255) . chr(209) . chr(241);
294 return strtolower(strtr($string, $diac, 'AAAAAAaaaaaaOOOOOOooooooEEEEeeeeCcIIIIiiiiUUUUuuuuyNn'));