-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
PersianSwear.php
56 lines (47 loc) · 1.02 KB
/
PersianSwear.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
<?php
class PersianSwear
{
private $words;
public function __construct()
{
if (file_exists("data.json")) {
$data = file_get_contents('data.json');
$data_encoded = json_decode($data, true);
$this->words = array_flip($data_encoded['word']);
} else {
die("Dataset file not found!");
}
}
public function is_empty()
{
return empty($this->words);
}
public function filter_words($text, $symbol = "*")
{
if ($this->is_empty()) {
return $text;
}
$pattern = '/\b(' . implode('|', array_keys($this->words)) . ')\b/i';
return preg_replace($pattern, str_repeat($symbol, strlen('$0')), $text);
}
public function add_word($text)
{
$this->words[ltrim(rtrim($text))] = true;
}
public function remove_word($text)
{
unset($this->words[ltrim(rtrim($text))]);
}
public function is_bad($text)
{
return empty($this->filter_words($text, ''));
}
public function has_swear($text)
{
return $this->is_bad($text);
}
public function tostring()
{
return implode(" ", array_keys($this->words));
}
}