@@ -46,6 +46,7 @@ class SpamDetector
46
46
'увеличение прибыли в интернете ' , 'инвестирование в акции ' ,
47
47
'финансовая безопасность ' , 'нужен только телефон ' , 'стабильный доход ' ,
48
48
'бесплатное обучение ' , '18+ ' , '18 лет ' , 'hamsterkombat ' , 'hamster ' ,
49
+ 'покупать тут ' ,
49
50
];
50
51
51
52
/**
@@ -66,6 +67,55 @@ public function containsStopWords(): bool
66
67
}
67
68
68
69
/**
70
+ * Checks if the number of special characters (e.g., emojis) in the message exceeds the number of words.
71
+ *
72
+ * If the count of special characters is greater than the word count, it returns true.
73
+ *
74
+ * @return bool True if the number of special characters exceeds the number of words; otherwise, false.
75
+ */
76
+ public function hasTooManySpecialCharacters ()
77
+ {
78
+ // Length of the message including special characters
79
+ $ withSpecialCharacters = Str::of ($ this ->message )
80
+ ->replace ($ this ->getPhpSpecialSymbols (), ' ' )
81
+ ->replaceMatches ('/[\p{P}]+/u ' , '' ) // Removes all punctuation
82
+ ->squish ()
83
+ ->length ();
84
+
85
+ // Length of the message without special characters
86
+ $ withOutSpecialCharacters = Str::of ($ this ->message )
87
+ ->replace ($ this ->getPhpSpecialSymbols (), '' )
88
+ ->replaceMatches ('/[^\p{L}\p{N}\p{Z}\s]/u ' , '' )
89
+ ->squish ()
90
+ ->length ();
91
+
92
+ // Message contains only emojis
93
+ if ($ withOutSpecialCharacters < 1 ) {
94
+ return true ;
95
+ }
96
+
97
+ if ($ withSpecialCharacters === $ withOutSpecialCharacters ) {
98
+ return false ;
99
+ }
100
+
101
+ $ countWords = Str::of ($ this ->message )
102
+ ->slug ()
103
+ ->replace ('- ' , ' ' )
104
+ ->squish ()
105
+ ->wordCount ();
106
+
107
+ $ diff = ($ withSpecialCharacters - $ withOutSpecialCharacters ) / 2 ;
108
+
109
+ // Proportion of special characters in the message
110
+ $ percentage = round ($ diff / $ countWords , 2 );
111
+
112
+ // Check if the proportion of special characters exceeds the given threshold
113
+ return $ percentage > 1 ;
114
+ }
115
+
116
+
117
+ /**
118
+ * @deprecated
69
119
* Checks if the message contains an excessive amount of special characters.
70
120
* For example, the proportion of special characters should not exceed a given threshold (default is 2%).
71
121
*
@@ -104,6 +154,45 @@ public function hasExcessiveUnicodeCharacters(float $threshold = 0.4): bool
104
154
return $ unicodePercentage > $ threshold ;
105
155
}
106
156
157
+ /**
158
+ * Метод для получения специальных символов PHP
159
+ *
160
+ * @return string[]
161
+ */
162
+ private function getPhpSpecialSymbols ()
163
+ {
164
+ return [
165
+ '$ ' , // Переменные
166
+ '-> ' , // Доступ к свойствам и методам объектов
167
+ ':: ' , // Доступ к статическим свойствам и методам
168
+ '[ ' , // Начало массива
169
+ '] ' , // Конец массива
170
+ '( ' , // Начало функции или метода
171
+ ') ' , // Конец функции или метода
172
+ '{ ' , // Начало блока кода
173
+ '} ' , // Конец блока кода
174
+ '=> ' , // Ассоциативные массивы (ключ => значение)
175
+ '&& ' , // Логическое "И"
176
+ '|| ' , // Логическое "ИЛИ"
177
+ '! ' , // Логическое "НЕ"
178
+ '=== ' , // Строгое равенство
179
+ '!== ' , // Строгое неравенство
180
+ '== ' , // Равенство
181
+ '!= ' , // Неравенство
182
+ '< ' , // Меньше
183
+ '> ' , // Больше
184
+ '<= ' , // Меньше или равно
185
+ '>= ' , // Больше или равно
186
+ '+ ' , // Сложение
187
+ '- ' , // Вычитание
188
+ '* ' , // Умножение
189
+ '/ ' , // Деление
190
+ '% ' , // Остаток от деления
191
+ '** ' , // Возведение в степень (с 7.0)
192
+ '= ' ,
193
+ ];
194
+ }
195
+
107
196
/**
108
197
* Check if the message is spam using a Naive Bayes classifier.
109
198
*
@@ -151,13 +240,13 @@ private function trainClassifier(Classifier $classifier, string $fileName, strin
151
240
*
152
241
* @return bool True if classified as spam, otherwise false
153
242
*/
154
- public function isSpam ()
243
+ public function isSpam (): bool
155
244
{
156
- if ($ this ->hasExcessiveUnicodeCharacters ()) {
245
+ if ($ this ->containsStopWords ()) {
157
246
return true ;
158
247
}
159
248
160
- if ($ this ->containsStopWords ()) {
249
+ if ($ this ->hasTooManySpecialCharacters ()) {
161
250
return true ;
162
251
}
163
252
0 commit comments