UpgradeDictSysPreference.php 4.6 KB

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