UpgradeDictSysPreference.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Http\Api\DictApi;
  4. use App\Models\UserDict;
  5. use App\Models\WordIndex;
  6. use App\Tools\Tools;
  7. use Carbon\Carbon;
  8. use Illuminate\Console\Command;
  9. class UpgradeDictSysPreference extends Command
  10. {
  11. /**
  12. * The name and signature of the console command.
  13. *
  14. * @var string
  15. * php artisan upgrade:dict.sys.preference
  16. */
  17. protected $signature = 'upgrade:dict.sys.preference';
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = 'upgrade dict system preference';
  24. /**
  25. * Create a new command instance.
  26. *
  27. * @return void
  28. */
  29. public function __construct()
  30. {
  31. parent::__construct();
  32. }
  33. /**
  34. * Execute the console command.
  35. *
  36. * @return int
  37. */
  38. public function handle()
  39. {
  40. if (Tools::isStop()) {
  41. return 0;
  42. }
  43. $this->info('start');
  44. $dictList = [
  45. 'community_extract',
  46. 'robot_compound',
  47. 'system_regular',
  48. 'system_preference',
  49. ];
  50. $dict_id = [];
  51. foreach ($dictList as $key => $value) {
  52. $dict_id[$value] = DictApi::getSysDict($value);
  53. if (! $dict_id[$value]) {
  54. $this->error("没有找到 {$value} 字典");
  55. return 1;
  56. } else {
  57. $this->info("{$value} :{$dict_id[$value]}");
  58. }
  59. }
  60. if (! $this->confirm('继续吗?')) {
  61. return 0;
  62. }
  63. // 搜索顺序
  64. $order = [
  65. '4d3a0d92-0adc-4052-80f5-512a2603d0e8', /* system irregular */
  66. $dict_id['community_extract'], /* 社区字典 */
  67. $dict_id['robot_compound'],
  68. $dict_id['system_regular'],
  69. ];
  70. $words = WordIndex::orderBy('count', 'desc')->cursor();
  71. $rows = 0;
  72. $found = 0;
  73. foreach ($words as $key => $word) {
  74. if (preg_match('/\d/', $word->word)) {
  75. // 不处理带数字的
  76. continue;
  77. }
  78. $rows++;
  79. $factors = null;
  80. $parent = null;
  81. $confidence = 0;
  82. foreach ($order as $key => $dict) {
  83. // 找到第一个有拆分的
  84. $preWords = UserDict::where('word', $word->word)
  85. ->where('dict_id', $dict)
  86. ->orderBy('confidence', 'desc')
  87. ->get();
  88. foreach ($preWords as $key => $value) {
  89. if (! $factors && ! empty($value->factors)) {
  90. $factors = $value->factors;
  91. if ($value->confidence > $confidence) {
  92. $confidence = $value->confidence;
  93. }
  94. }
  95. if (! $parent && ! empty($value->parent)) {
  96. $parent = $value->parent;
  97. if ($value->confidence > $confidence) {
  98. $confidence = $value->confidence;
  99. }
  100. }
  101. if ($parent && $factors) {
  102. break;
  103. }
  104. }
  105. }
  106. if ($parent || $factors) {
  107. $prefWord = UserDict::where('word', $word->word)
  108. ->where('dict_id', $dict_id['system_preference'])
  109. ->first();
  110. if (! $prefWord) {
  111. $prefWord = new UserDict;
  112. $prefWord->word = $word->word;
  113. $prefWord->dict_id = $dict_id['system_preference'];
  114. $prefWord->id = app('snowflake')->id();
  115. $prefWord->source = '_ROBOT_';
  116. $prefWord->create_time = (int) (microtime(true) * 1000);
  117. $prefWord->language = 'cm';
  118. $prefWord->creator_id = 1;
  119. } else {
  120. if (Carbon::parse($prefWord->updated_at) > Carbon::parse($prefWord->created_at)) {
  121. // 跳过已经被编辑过的。
  122. $this->info('跳过已经被编辑过的'.$word->word);
  123. continue;
  124. }
  125. }
  126. $prefWord->factors = $factors;
  127. $prefWord->parent = $parent;
  128. $prefWord->confidence = $confidence;
  129. $prefWord->save();
  130. $found++;
  131. }
  132. if ($rows % 100 == 0) {
  133. $output = "[{$rows}] {$word->word} found:{$found}";
  134. $this->info($output);
  135. $found = 0;
  136. }
  137. }
  138. return 0;
  139. }
  140. }