-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProfanus.php
139 lines (114 loc) · 4.2 KB
/
Profanus.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
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
<?php
/**
* developed by Tadiwanashe Mataruse 2019-04-08
* email: [email protected] / [email protected]
* tested on PHP 7
**/
class Profanus
{
/* this is an array of blacklisted words, can be generated from a database or file or direct in the script
* for best result, just make sure you add the profanity words in smallcase eg 'bitch','pussy'
*/
private function black_list()
{
$bad_words = array('fck', 'fuck', 'ass', 'babe', 'sexy', 'bitch');
$more_bad_words = $this->read_dictionary();
$bad_words = array_merge($bad_words,$more_bad_words);
return $bad_words;
}
/** the following function uses a text file as its dictionary. so instead of putting the badwords into an array above
* you can add the words in small caps into the text file separating each word with a new line for example:
* bitch
* ass
* dick
* nigger
* just as words and do not put quotes or anything around the words.
**/
function read_dictionary()
{
$bad_word_lines = file('blacklist.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
return $bad_word_lines;
}
// the below function focuses on the censorship of the whole sentence but then automatically skips the first word hence the need for the later functions
function censor_sentence($sentence)
{
$bad_words = $this->black_list();
$bad_words_count = count($bad_words);
for ($i = 0; $i < $bad_words_count; $i++) {
if (strpos(strtolower($sentence), $bad_words[$i])) {
$dirtyWordLength = strlen($bad_words[$i]);
$clean_sentence = str_ireplace($bad_words[$i], str_repeat('*', $dirtyWordLength), $sentence);
$clean = false;
$sentence = $clean_sentence;
} else {
$clean = true;
}
}
if ($clean) {
return $sentence;
} else {
return $sentence;
}
}
// the below function focuses only on the censorship of the first word of the sentence, this was my chosen approach
function censor_first_word($sentence)
{
$first_word = strtok($sentence, " ");
$bad_words = $this->black_list();
$bad_words_count = count($bad_words);
for ($i = 0; $i < $bad_words_count; $i++) {
if (strpos(strtolower($sentence), $bad_words[$i]) === 0) {
$first_word_len = strlen($first_word);
$bad_word_len = strlen($bad_words[$i]);
if (strcasecmp($first_word, $bad_words[$i]) == 0) {
$char_count = $first_word_len;
}
if (strcasecmp($first_word, $bad_words[$i]) !== 0 && $first_word_len > $bad_word_len) {
$char_count = $bad_word_len;
}
if (strcasecmp($first_word, $bad_words[$i]) !== 0 && $first_word_len < $bad_word_len) {
$char_count = $first_word_len;
}
$clean_first_wd = array();
for ($j = 0; $j < $char_count; $j++) {
array_push($clean_first_wd, "*");
}
$clean_first_word = implode("", $clean_first_wd);
$sentence = str_replace($first_word, $clean_first_word, $sentence);
for ($i = 0; $i < $bad_words_count; $i++) {
if (strpos($sentence, $bad_words[$i])) {
$dirtyWordLength = strlen($bad_words[$i]);
$clean_sentence = str_ireplace($bad_words[$i], str_repeat('*', $dirtyWordLength), $sentence);
$clean = false;
$sentence = $clean_sentence;
} else {
$clean = true;
}
}
}
}
if ($clean) {
return strtok($sentence, " ");
} else {
return strtok($sentence, " ");
}
}
// combining the censorship functions into one method:
function censor($sentence)
{
$censored_first_word = $this->censor_first_word($sentence);
$sentence = $this->remove_first_word($sentence);
$censored_sentence = $this->censor_sentence($sentence);
$final_censored = $censored_first_word . $censored_sentence;
return $final_censored;
}
// removing the first word from the uncensored sentence so that later this function output will be joined to the censor_first_word() output
private function remove_first_word($sentence)
{
$first_word = strtok($sentence, " ");
$first_word_len = strlen($first_word);
$trimmed = substr($sentence, $first_word_len);
return $trimmed;
}
}
?>