UpgradeDictSysPreference.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. class UpgradeDictSysPreference extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. * php artisan upgrade:dict.sys.preference
  14. */
  15. protected $signature = 'upgrade:dict.sys.preference';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = 'upgrade dict system preference';
  22. /**
  23. * Create a new command instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. }
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return int
  35. */
  36. public function handle()
  37. {
  38. if(\App\Tools\Tools::isStop()){
  39. return 0;
  40. }
  41. $this->info("start");
  42. $dictList = [
  43. 'community_extract',
  44. 'robot_compound',
  45. 'system_regular',
  46. 'system_preference',
  47. ];
  48. $dict_id = array();
  49. foreach ($dictList as $key => $value) {
  50. $dict_id[$value] = DictApi::getSysDict($value);
  51. if(!$dict_id[$value]){
  52. $this->error("没有找到 {$value} 字典");
  53. return 1;
  54. }else{
  55. $this->info("{$value} :{$dict_id[$value]}");
  56. }
  57. }
  58. //搜索顺序
  59. $order = [
  60. '4d3a0d92-0adc-4052-80f5-512a2603d0e8',/* system irregular */
  61. $dict_id['community_extract'],/* 社区字典*/
  62. $dict_id['robot_compound'],
  63. $dict_id['system_regular'],
  64. ];
  65. $words = WordIndex::orderBy('count', 'desc')->cursor();
  66. $rows = 0;
  67. $found = 0;
  68. foreach ($words as $key => $word) {
  69. if (preg_match('/\d/', $word->word)) {
  70. continue;
  71. }
  72. $rows++;
  73. $preference = null;
  74. foreach ($order as $key => $dict) {
  75. $preference = UserDict::where('word', $word->word)
  76. ->where('dict_id', $dict)
  77. ->whereNotNull('factors')
  78. ->where('factors','<>','')
  79. ->orderBy('confidence', 'desc')
  80. ->first(); # code...
  81. if($preference){
  82. break;
  83. }
  84. }
  85. if($preference){
  86. $userDict = UserDict::firstOrNew([
  87. 'word'=>$word->word,
  88. 'dict_id'=>$dict_id['system_preference']
  89. ],
  90. [
  91. 'id' => app('snowflake')->id(),
  92. 'source' => '_ROBOT_',
  93. 'create_time'=>(int)(microtime(true)*1000)
  94. ]);
  95. $userDict->factors = $preference->factors;
  96. $userDict->parent = $preference->parent;
  97. $userDict->confidence = $preference->confidence;
  98. $userDict->language = 'cm';
  99. $userDict->creator_id = 1;
  100. $userDict->save();
  101. $found++;
  102. }
  103. if($rows % 100 == 0){
  104. $output = "[{$rows}] {$word->word} found:{$found}";
  105. $this->info($output);
  106. $found=0;
  107. }
  108. }
  109. return 0;
  110. }
  111. }