UpgradeDictSysRegular.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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("system_regular :{$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. $init = UserDict::where('dict_id',$dict_id)
  59. ->update(['flag'=>0]);
  60. }else{
  61. $words = UserDict::where('word',$this->argument('word'))
  62. ->where(function($query) {
  63. $query->where('type','.n:base.')
  64. ->orWhere('type','.v:base.')
  65. ->orWhere('type','.adj:base.')
  66. ->orWhere('type','.ti:base.');
  67. });
  68. $init = UserDict::where('dict_id',$dict_id)
  69. ->where('word',$this->argument('word'))
  70. ->update(['flag'=>0]);
  71. }
  72. $words = $words->select(['word','type','grammar'])
  73. ->groupBy(['word','type','grammar'])
  74. ->orderBy('word');
  75. $query = "
  76. select count(*) from (select count(*) from user_dicts ud where
  77. \"type\" = '.v:base.' or
  78. \"type\" = '.n:base.' or
  79. \"type\" = '.ti:base.' or
  80. \"type\" = '.adj:base.'
  81. group by word,type,grammar) as t;
  82. ";
  83. $count = DB::select($query);
  84. $bar = $this->output->createProgressBar($count[0]->count);
  85. $caseMan = new CaseMan();
  86. foreach ($words->cursor() as $word) {
  87. if($this->option('debug')){$this->info("{$word->word}:{$word->type}");}
  88. $newWords = $caseMan->Declension($word->word,$word->type,$word->grammar,0.5);
  89. if($this->option('debug')){$this->info("{$word->word}:".count($newWords));}
  90. foreach ($newWords as $newWord) {
  91. if(isset($newWord['type'])){
  92. $type = $newWord['type'];
  93. }else{
  94. $type = \str_replace(':base','',$word->type);
  95. }
  96. $new = UserDict::firstOrNew(
  97. [
  98. 'word' => $newWord['word'],
  99. 'type' => $type,
  100. 'grammar' => $newWord['grammar'],
  101. 'parent' => $word->word,
  102. 'factors' => $newWord['factors'],
  103. 'dict_id' => $dict_id,
  104. ],
  105. [
  106. 'id' => app('snowflake')->id(),
  107. 'source' => '_ROBOT_',
  108. 'create_time'=>(int)(microtime(true)*1000)
  109. ]
  110. );
  111. $new->confidence = 80;
  112. $new->language = 'cm';
  113. $new->creator_id = 1;
  114. $new->flag = 1;
  115. $new->save();
  116. }
  117. $bar->advance();
  118. }
  119. $bar->finish();
  120. //删除旧数据
  121. $table = UserDict::where('dict_id',$dict_id);
  122. if(!empty($this->argument('word'))){
  123. $table = $table->where('parent',$this->argument('word'));
  124. }
  125. $table->where('flag',0)->delete();
  126. //DB::enableQueryLog();
  127. $newRecord = UserDict::where('dict_id',$dict_id);
  128. if(!empty($this->argument('word'))){
  129. $newRecord = $newRecord->where('parent',$this->argument('word'));
  130. }
  131. $newRecord->where('flag',1)->update(['flag'=>0]);
  132. //print_r(DB::getQueryLog());
  133. return 0;
  134. }
  135. }