From c7aa0026251c884ce6ad5ab92e7889aeec827cb6 Mon Sep 17 00:00:00 2001 From: shibafu Date: Thu, 30 Jul 2020 00:46:33 +0900 Subject: [PATCH] =?UTF-8?q?=E3=81=AA=E3=82=93=E3=82=82=E3=82=8F=E3=81=8B?= =?UTF-8?q?=E3=82=89=E3=82=93=E3=82=8F=E3=83=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Console/Commands/NormalizeTags.php | 50 ++++++++++++++++++++++++++ app/Utilities/Formatter.php | 11 ++++++ 2 files changed, 61 insertions(+) create mode 100644 app/Console/Commands/NormalizeTags.php diff --git a/app/Console/Commands/NormalizeTags.php b/app/Console/Commands/NormalizeTags.php new file mode 100644 index 0000000..4e9d8ed --- /dev/null +++ b/app/Console/Commands/NormalizeTags.php @@ -0,0 +1,50 @@ +formatter = $formatter; + } + + /** + * Execute the console command. + * + * @return mixed + */ + public function handle() + { + foreach (Tag::query()->orderBy('name')->cursor() as $tag) { + $normalizedName = $this->formatter->normalizeToSearchIndex($tag->name); + $this->line("{$tag->name} : {$normalizedName}"); + } + } +} diff --git a/app/Utilities/Formatter.php b/app/Utilities/Formatter.php index 7ac108b..aa09c78 100644 --- a/app/Utilities/Formatter.php +++ b/app/Utilities/Formatter.php @@ -132,4 +132,15 @@ class Formatter return $bytes . 'B'; } + + public function normalizeToSearchIndex(string $text): string + { + $text = \Normalizer::normalize($text, \Normalizer::FORM_KC); +// $text = \Transliterator::create('Katakana-Hiragana')->transliterate($text); + $text = mb_convert_kana($text, 'c'); + $text = preg_replace('/[^\p{L}\p{N}]/u', '', $text); + $text = mb_strtolower($text); + + return $text; + } }