From cc7b34f64d2b169fba7c823bf4f6d51c231a13c7 Mon Sep 17 00:00:00 2001 From: mirzaev Date: Wed, 3 Jan 2024 23:38:44 +0700 Subject: [PATCH] levenshtein marina --- mirzaev/marina/system/controllers/index.php | 38 ++++++++++++++++--- .../system/controllers/traits/converters.php | 30 +++++++++++++++ 2 files changed, 63 insertions(+), 5 deletions(-) create mode 100644 mirzaev/marina/system/controllers/traits/converters.php diff --git a/mirzaev/marina/system/controllers/index.php b/mirzaev/marina/system/controllers/index.php index 5abd181..e28736f 100644 --- a/mirzaev/marina/system/controllers/index.php +++ b/mirzaev/marina/system/controllers/index.php @@ -5,7 +5,8 @@ declare(strict_types=1); namespace mirzaev\marina\controllers; // Файлы проекта -use mirzaev\marina\controllers\core; +use mirzaev\marina\controllers\core, + mirzaev\marina\controllers\traits\converters; // Discord framework use Discord\Discord as discord, @@ -31,6 +32,10 @@ use exception; */ final class index extends core { + use converters { + utf8_to_extended_ascii as protected ascii; + } + /** * Главная страница * @@ -44,7 +49,7 @@ final class index extends core // Запись в буфер вывода (терминал) echo "Marina is ready!", PHP_EOL; - // Сообщение: "марина" + // Сообщение: создано $discord->on(event::MESSAGE_CREATE, function (message $message, discord $discord) { // Игнорирование чат-роботов if ($message->author->bot) return; @@ -52,12 +57,35 @@ final class index extends core // Запись в буфер вывода (терминал) echo "{$message->author->username}: {$message->content}", PHP_EOL; - if (mb_stristr($message->content, 'марина') !== false) { - $message->reply(_message::new()->setContent('ЗДАРОВА')); + foreach (explode(' ', $message->content) as $word) { + // Перебор слов из текста сообщения + + // Инициализация буфера символов для конвертации: UTF-8 -> ASCII+ + $buffer = []; + + // Конвертация проверяемых слов + $marina = self::ascii('марина', $buffer); + $marishka = self::ascii('маришка', $buffer); + $marinochka = self::ascii('мариночка', $buffer); + $marinushka = self::ascii('маринушка', $buffer); + $marya = self::ascii('маря', $buffer); + + // Конвертация слова + $_word = self::ascii(mb_strtolower($word), $buffer); + + if ( + levenshtein($_word, $marina, 2, 2, 1) < 3 + || levenshtein($_word, $marishka, 2, 1, 2) < 4 + || levenshtein($_word, $marinochka, 2, 1, 2) < 4 + || levenshtein($_word, $marinushka, 2, 1, 2) < 4 + || (levenshtein($_word, $marya, 3, 3, 1) < 3 && $word !== 'мария') + ) { + $message->reply(_message::new()->setContent('ЗДАРОВА')); + } } }); - // Пользователь: обновление роли + // Пользователь: обновление $discord->on(event::GUILD_MEMBER_UPDATE, function (member $new, discord $discord, ?member $old = null) { // Запись в буфер вывода (терминал) diff --git a/mirzaev/marina/system/controllers/traits/converters.php b/mirzaev/marina/system/controllers/traits/converters.php new file mode 100644 index 0000000..bb4d17d --- /dev/null +++ b/mirzaev/marina/system/controllers/traits/converters.php @@ -0,0 +1,30 @@ + + */ +trait converters +{ + private static function utf8_to_extended_ascii(string $target, &$map) + { + // find all multibyte characters (cf. utf-8 encoding specs) + $matches = array(); + if (!preg_match_all('/[\xC0-\xF7][\x80-\xBF]+/', $target, $matches)) + return $target; // plain ascii string + + // update the encoding map with the characters not already met + foreach ($matches[0] as $mbc) + if (!isset($map[$mbc])) + $map[$mbc] = chr(128 + count($map)); + + // finally remap non-ascii characters + return strtr($target, $map); + } +}