tags.normalized_name

This commit is contained in:
shibafu 2020-07-30 22:42:10 +09:00
parent c7aa002625
commit e2c43fef80
4 changed files with 61 additions and 11 deletions

View File

@ -4,6 +4,7 @@ namespace App\Console\Commands;
use App\Tag;
use App\Utilities\Formatter;
use DB;
use Illuminate\Console\Command;
class NormalizeTags extends Command
@ -42,9 +43,19 @@ class NormalizeTags extends Command
*/
public function handle()
{
foreach (Tag::query()->orderBy('name')->cursor() as $tag) {
$normalizedName = $this->formatter->normalizeToSearchIndex($tag->name);
$this->line("{$tag->name} : {$normalizedName}");
}
$start = hrtime(true);
DB::transaction(function () {
/** @var Tag $tag */
foreach (Tag::query()->cursor() as $tag) {
$normalizedName = $this->formatter->normalizeTagName($tag->name);
$this->line("{$tag->name} : {$normalizedName}");
$tag->normalized_name = $normalizedName;
$tag->save();
}
});
$elapsed = (hrtime(true) - $start) / 1e+9;
$this->info("Done! ({$elapsed} sec)");
}
}

View File

@ -2,6 +2,7 @@
namespace App;
use App\Utilities\Formatter;
use Illuminate\Database\Eloquent\Model;
class Tag extends Model
@ -15,6 +16,15 @@ class Tag extends Model
'name'
];
protected static function boot()
{
parent::boot();
self::creating(function (Tag $tag) {
$tag->normalized_name = app(Formatter::class)->normalizeTagName($tag->name);
});
}
public function ejaculations()
{
return $this->belongsToMany('App\Ejaculation')->withTimestamps();

View File

@ -133,14 +133,11 @@ class Formatter
return $bytes . 'B';
}
public function normalizeToSearchIndex(string $text): string
public function normalizeTagName(string $name)
{
$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);
$name = \Normalizer::normalize($name, \Normalizer::FORM_KC);
$name = mb_strtolower($name);
return $text;
return $name;
}
}

View File

@ -0,0 +1,32 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class AddNormalizedNameToTags extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('tags', function (Blueprint $table) {
$table->string('normalized_name')->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('tags', function (Blueprint $table) {
$table->dropColumn('normalized_name');
});
}
}