UpgradeDictSysRegular.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * 生成系统规则变形词典
  4. * 算法: 扫描字典里的所有单词。根据语尾表变形。
  5. * 并在词库中查找是否在三藏中出现。出现的保存。
  6. */
  7. namespace App\Console\Commands;
  8. use App\Models\UserDict;
  9. use App\Models\WbwTemplate;
  10. use Illuminate\Console\Command;
  11. use Illuminate\Support\Facades\Cache;
  12. use Illuminate\Support\Facades\Log;
  13. use Illuminate\Support\Facades\DB;
  14. use App\Http\Api\DictApi;
  15. use App\Tools\CaseMan;
  16. class UpgradeDictSysRegular extends Command
  17. {
  18. /**
  19. * The name and signature of the console command.
  20. *
  21. * @var string
  22. */
  23. protected $signature = 'upgrade:regular {word?} {--debug}';
  24. /**
  25. * The console command description.
  26. *
  27. * @var string
  28. */
  29. protected $description = 'upgrade regular';
  30. /**
  31. * Create a new command instance.
  32. *
  33. * @return void
  34. */
  35. public function __construct()
  36. {
  37. parent::__construct();
  38. }
  39. /**
  40. * Execute the console command.
  41. *
  42. * @return int
  43. */
  44. public function handle()
  45. {
  46. $dict_id = DictApi::getSysDict('system_regular');
  47. if(!$dict_id){
  48. $this->error('没有找到 system_regular 字典');
  49. return 1;
  50. }else{
  51. $this->info("dict id:{$dict_id}");
  52. }
  53. if(empty($this->argument('word'))){
  54. $words = UserDict::where('type','.n:base.')
  55. ->orWhere('type','.v:base.')
  56. ->orWhere('type','.adj:base.')
  57. ->orWhere('type','.ti:base.');
  58. }else{
  59. $words = UserDict::where('word',$this->argument('word'))
  60. ->where(function($query) {
  61. $query->where('type','.n:base.')
  62. ->orWhere('type','.v:base.')
  63. ->orWhere('type','.adj:base.')
  64. ->orWhere('type','.ti:base.');
  65. });
  66. }
  67. $words = $words->select(['word','type','grammar'])
  68. ->groupBy(['word','type','grammar'])
  69. ->orderBy('word');
  70. $query = "
  71. select count(*) from (select count(*) from user_dicts ud where
  72. \"type\" = '.v:base.' or
  73. \"type\" = '.n:base.' or
  74. \"type\" = '.ti:base.' or
  75. \"type\" = '.adj:base.'
  76. group by word,type,grammar) as t;
  77. ";
  78. $count = DB::select($query);
  79. $bar = $this->output->createProgressBar($count[0]->count);
  80. $caseMan = new CaseMan();
  81. foreach ($words->cursor() as $word) {
  82. if($this->option('debug')){$this->info("{$word->word}:{$word->type}");}
  83. $newWords = $caseMan->Declension($word->word,$word->type,$word->grammar,0.5);
  84. if($this->option('debug')){$this->info("{$word->word}:".count($newWords));}
  85. foreach ($newWords as $newWord) {
  86. $new = UserDict::firstOrNew(
  87. [
  88. 'word' => $newWord['word'],
  89. 'type' => \str_replace(':base','',$word->type),
  90. 'grammar' => $newWord['grammar'],
  91. 'parent' => $word->word,
  92. 'factors' => "{$word->word}+[{$newWord['ending']}]",
  93. 'dict_id' => $dict_id,
  94. ],
  95. [
  96. 'id' => app('snowflake')->id(),
  97. 'source' => '_ROBOT_',
  98. 'create_time'=>(int)(microtime(true)*1000)
  99. ]
  100. );
  101. $new->confidence = 80;
  102. $new->language = 'cm';
  103. $new->creator_id = 1;
  104. $new->flag = 1;
  105. $new->save();
  106. }
  107. $bar->advance();
  108. }
  109. $bar->finish();
  110. //删除旧数据
  111. $table = UserDict::where('dict_id',$dict_id);
  112. if(!empty($this->argument('word'))){
  113. $table = $table->where('word',$this->argument('word'));
  114. }
  115. $table->where('flag',0)->delete();
  116. //DB::enableQueryLog();
  117. $newRecord = UserDict::where('dict_id',$dict_id);
  118. if(!empty($this->argument('word'))){
  119. $newRecord = $newRecord->where('parent',$this->argument('word'));
  120. }
  121. $newRecord->where('flag',1)->update(['flag'=>0]);
  122. //print_r(DB::getQueryLog());
  123. return 0;
  124. }
  125. }