-
Notifications
You must be signed in to change notification settings - Fork 2
/
BlacklistHelper.php
65 lines (54 loc) · 1.5 KB
/
BlacklistHelper.php
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
<?php
class BlacklistHelper {
private $conn;
private $namespace = 'nbc-ns';
function __construct($conf) {
if(empty($conf))
throw new NaiveBayesClassifierException(3001);
if(empty($conf['db_host']))
throw new NaiveBayesClassifierException(3101);
if(empty($conf['db_port']))
throw new NaiveBayesClassifierException(3102);
if(!empty($conf['namespace']))
$this->namespace = $conf['namespace'];
// Redis connection
$this->conn = new Redis();
$this->conn->connect($conf['db_host'], $conf['db_port']);
$this->conn->select(77);
}
public function generateStopwords($data) {
if(!is_array($data)) {
throw new Exception("Parameter must be an array");
}
$key = "{$this->namespace}-blacklist-temp";
foreach($data as $item) {
$words = $this->cleanKeywords(explode(" ", $item));
foreach($words as $word) {
$this->conn->zIncrBy($key, 1, $word);
}
}
return $this->conn->zSize($key);
}
public function getRange($offset = 0, $row = 100, $minCount = 0, $maxCount = INF) {
$key = "{$this->namespace}-blacklist-temp";
return $this->conn->zRevRangeByScore($key, $maxCount, $minCount, array(
'withscores' => TRUE,
'limit' => array($offset, $row)
));
}
private function cleanKeywords($kw = array()) {
if(!empty($kw)) {
$ret = array();
foreach($kw as $k) {
$k = strtolower($k);
$k = preg_replace("/[^a-z]/i", "", $k);
if(!empty($k) && strlen($k) > 2) {
$k = strtolower($k);
if(!empty($k))
$ret[] = $k;
}
}
return $ret;
}
}
}