UpgradeDictSysRegular.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. * php artisan upgrade:regular jāta
  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. if(\App\Tools\Tools::isStop()){
  47. return 0;
  48. }
  49. $dict_id = DictApi::getSysDict('system_regular');
  50. if(!$dict_id){
  51. $this->error('没有找到 system_regular 字典');
  52. return 1;
  53. }else{
  54. $this->info("system_regular :{$dict_id}");
  55. }
  56. if(empty($this->argument('word'))){
  57. $words = UserDict::where('type','.n:base.')
  58. ->orWhere('type','.v:base.')
  59. ->orWhere('type','.adj:base.')
  60. ->orWhere('type','.ti:base.');
  61. $init = UserDict::where('dict_id',$dict_id)
  62. ->update(['flag'=>0]);
  63. }else{
  64. $words = UserDict::where('word',$this->argument('word'))
  65. ->where(function($query) {
  66. $query->where('type','.n:base.')
  67. ->orWhere('type','.v:base.')
  68. ->orWhere('type','.adj:base.')
  69. ->orWhere('type','.ti:base.');
  70. });
  71. $init = UserDict::where('dict_id',$dict_id)
  72. ->where('word',$this->argument('word'))
  73. ->update(['flag'=>0]);
  74. }
  75. $words = $words->select(['word','type','grammar'])
  76. ->groupBy(['word','type','grammar'])
  77. ->orderBy('word');
  78. $query = "
  79. select count(*) from (select count(*) from user_dicts ud where
  80. \"type\" = '.v:base.' or
  81. \"type\" = '.n:base.' or
  82. \"type\" = '.ti:base.' or
  83. \"type\" = '.adj:base.'
  84. group by word,type,grammar) as t;
  85. ";
  86. $count = DB::select($query);
  87. $bar = $this->output->createProgressBar($count[0]->count);
  88. $caseMan = new CaseMan();
  89. foreach ($words->cursor() as $word) {
  90. if($this->option('debug')){$this->info("{$word->word}:{$word->type}");}
  91. $newWords = $caseMan->Declension($word->word,$word->type,$word->grammar,0.5);
  92. if($this->option('debug')){$this->info("{$word->word}:".count($newWords));}
  93. foreach ($newWords as $newWord) {
  94. if(isset($newWord['type'])){
  95. $type = $newWord['type'];
  96. }else{
  97. $type = \str_replace(':base','',$word->type);
  98. }
  99. $new = UserDict::firstOrNew(
  100. [
  101. 'word' => $newWord['word'],
  102. 'type' => $type,
  103. 'grammar' => $newWord['grammar'],
  104. 'parent' => $word->word,
  105. 'factors' => $newWord['factors'],
  106. 'dict_id' => $dict_id,
  107. ],
  108. [
  109. 'id' => app('snowflake')->id(),
  110. 'source' => '_ROBOT_',
  111. 'create_time'=>(int)(microtime(true)*1000)
  112. ]
  113. );
  114. $new->confidence = 80;
  115. $new->language = 'cm';
  116. $new->creator_id = 1;
  117. $new->flag = 1;
  118. $new->save();
  119. }
  120. $bar->advance();
  121. }
  122. $bar->finish();
  123. if(!empty($this->argument('word'))){
  124. $declensions = UserDict::where('dict_id',$dict_id)
  125. ->where('parent',$this->argument('word'))
  126. ->select('word')
  127. ->groupBy('word')
  128. ->get();
  129. foreach ($declensions as $key => $word) {
  130. Log::debug($word->word);
  131. }
  132. }
  133. //删除旧数据
  134. $table = UserDict::where('dict_id',$dict_id);
  135. if(!empty($this->argument('word'))){
  136. $table = $table->where('parent',$this->argument('word'));
  137. }
  138. $table->where('flag',0)->delete();
  139. //DB::enableQueryLog();
  140. $newRecord = UserDict::where('dict_id',$dict_id);
  141. if(!empty($this->argument('word'))){
  142. $newRecord = $newRecord->where('parent',$this->argument('word'));
  143. }
  144. $newRecord->where('flag',1)->update(['flag'=>0]);
  145. //print_r(DB::getQueryLog());
  146. return 0;
  147. }
  148. }