@@ -46,6 +46,7 @@ class SpamDetector
4646 'увеличение прибыли в интернете ' , 'инвестирование в акции ' ,
4747 'финансовая безопасность ' , 'нужен только телефон ' , 'стабильный доход ' ,
4848 'бесплатное обучение ' , '18+ ' , '18 лет ' , 'hamsterkombat ' , 'hamster ' ,
49+ 'покупать тут ' ,
4950 ];
5051
5152 /**
@@ -66,6 +67,55 @@ public function containsStopWords(): bool
6667 }
6768
6869 /**
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
69119 * Checks if the message contains an excessive amount of special characters.
70120 * For example, the proportion of special characters should not exceed a given threshold (default is 2%).
71121 *
@@ -104,6 +154,45 @@ public function hasExcessiveUnicodeCharacters(float $threshold = 0.4): bool
104154 return $ unicodePercentage > $ threshold ;
105155 }
106156
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+
107196 /**
108197 * Check if the message is spam using a Naive Bayes classifier.
109198 *
@@ -151,13 +240,13 @@ private function trainClassifier(Classifier $classifier, string $fileName, strin
151240 *
152241 * @return bool True if classified as spam, otherwise false
153242 */
154- public function isSpam ()
243+ public function isSpam (): bool
155244 {
156- if ($ this ->hasExcessiveUnicodeCharacters ()) {
245+ if ($ this ->containsStopWords ()) {
157246 return true ;
158247 }
159248
160- if ($ this ->containsStopWords ()) {
249+ if ($ this ->hasTooManySpecialCharacters ()) {
161250 return true ;
162251 }
163252
0 commit comments