]> git.wh0rd.org - tt-rss.git/blob - plugins/af_sort_bayes/lib/class.naivebayesian_ngram.php
cee2bb1d7bfeeb4b734be7a883260e63ae3e586a
[tt-rss.git] / plugins / af_sort_bayes / lib / class.naivebayesian_ngram.php
1 <?php
2
3 class NaiveBayesianNgram extends NaiveBayesian {
4 var $N = 2;
5
6 /**
7 * add Parameter for ngram
8 *
9 * @param NaiveBayesianStorage $nbs
10 * @param ngram's N $n
11 * @return boolean
12 */
13 function __construct($nbs, $n = 2) {
14 parent::__construct($nbs);
15
16 $this->N = $n;
17
18 return true;
19 }
20
21 /**
22 * override method for ngram
23 *
24 * @param string $string
25 * @return multiple
26 */
27 function _getTokens($string) {
28 $tokens = array();
29
30 if (mb_strlen($string)) {
31 for ($i = 0; $i < mb_strlen($string) - $this->N; $i++) {
32 $wd = mb_substr($string, $i, $this->N);
33
34 if (mb_strlen($wd) == $this->N) {
35 if (!array_key_exists($wd, $tokens)) {
36 $tokens[$wd] = 0;
37 }
38
39 $tokens[$wd]++;
40 }
41 }
42 }
43
44 if (count($tokens)) {
45 // remove empty value
46 $tokens = array_filter($tokens);
47 }
48
49 return $tokens;
50 }
51
52 }