2
0

UpgradeDictSysRegular.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. <?php
  2. /**
  3. * 生成系统规则变形词典
  4. * 算法: 扫描字典里的所有单词。根据语尾表变形。
  5. * 并在词库中查找是否在三藏中出现。出现的保存。
  6. */
  7. namespace App\Console\Commands;
  8. use App\Http\Api\DictApi;
  9. use App\Models\UserDict;
  10. use App\Tools\CaseMan;
  11. use App\Tools\Tools;
  12. use Illuminate\Console\Command;
  13. use Illuminate\Support\Facades\DB;
  14. use Illuminate\Support\Facades\Log;
  15. class UpgradeDictSysRegular extends Command
  16. {
  17. /**
  18. * The name and signature of the console command.
  19. * php artisan upgrade:regular jāta
  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. if (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')) {
  91. $this->info("{$word->word}:{$word->type}");
  92. }
  93. $newWords = $caseMan->Declension($word->word, $word->type, $word->grammar, 0.5);
  94. if ($this->option('debug')) {
  95. $this->info("{$word->word}:".count($newWords));
  96. }
  97. foreach ($newWords as $newWord) {
  98. if (isset($newWord['type'])) {
  99. $type = $newWord['type'];
  100. } else {
  101. $type = \str_replace(':base', '', $word->type);
  102. }
  103. $new = UserDict::firstOrNew(
  104. [
  105. 'word' => $newWord['word'],
  106. 'type' => $type,
  107. 'grammar' => $newWord['grammar'],
  108. 'parent' => $word->word,
  109. 'factors' => $newWord['factors'],
  110. 'dict_id' => $dict_id,
  111. ],
  112. [
  113. 'id' => app('snowflake')->id(),
  114. 'source' => '_ROBOT_',
  115. 'create_time' => (int) (microtime(true) * 1000),
  116. ]
  117. );
  118. $new->confidence = 80;
  119. $new->language = 'cm';
  120. $new->creator_id = 1;
  121. $new->flag = 1;
  122. $new->save();
  123. }
  124. $bar->advance();
  125. }
  126. $bar->finish();
  127. if (! empty($this->argument('word'))) {
  128. $declensions = UserDict::where('dict_id', $dict_id)
  129. ->where('parent', $this->argument('word'))
  130. ->select('word')
  131. ->groupBy('word')
  132. ->get();
  133. foreach ($declensions as $key => $word) {
  134. Log::debug($word->word);
  135. }
  136. }
  137. // 删除旧数据
  138. $table = UserDict::where('dict_id', $dict_id);
  139. if (! empty($this->argument('word'))) {
  140. $table = $table->where('parent', $this->argument('word'));
  141. }
  142. $table->where('flag', 0)->delete();
  143. // DB::enableQueryLog();
  144. $newRecord = UserDict::where('dict_id', $dict_id);
  145. if (! empty($this->argument('word'))) {
  146. $newRecord = $newRecord->where('parent', $this->argument('word'));
  147. }
  148. $newRecord->where('flag', 1)->update(['flag' => 0]);
  149. // print_r(DB::getQueryLog());
  150. return 0;
  151. }
  152. }