UpgradeDictSysWbwExtract.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Models\UserDict;
  5. class UpgradeDictSysWbwExtract extends Command
  6. {
  7. /**
  8. * The name and signature of the console command.
  9. *
  10. * @var string
  11. */
  12. protected $signature = 'upgrade:syswbwextract';
  13. /**
  14. * The console command description.
  15. *
  16. * @var string
  17. */
  18. protected $description = '从社区词典中提取最优结果';
  19. /**
  20. * Create a new command instance.
  21. *
  22. * @return void
  23. */
  24. public function __construct()
  25. {
  26. parent::__construct();
  27. }
  28. /**
  29. * Execute the console command.
  30. *
  31. * @return int
  32. */
  33. public function handle()
  34. {
  35. $dict = UserDict::select('word')->where('word','!=','')->where('dict_id','ef620a93-a55d-4756-89c5-e188ab009e45')->groupBy('word');
  36. $bar = $this->output->createProgressBar($dict->count());
  37. foreach ($dict->cursor() as $word) {
  38. # code...
  39. //case
  40. $wordtype = '';
  41. $wordgrammar = '';
  42. $wordparent = '';
  43. $wordfactors = '';
  44. $case = UserDict::selectRaw('type,grammar, sum(confidence)')
  45. ->where('word',$word->word)
  46. ->where('dict_id','ef620a93-a55d-4756-89c5-e188ab009e45')
  47. ->where('type','!=','.part.')
  48. ->where('type','<>','')
  49. ->whereNotNull('type')
  50. ->groupBy(['type','grammar'])
  51. ->orderBy('sum','desc')
  52. ->first();
  53. if($case){
  54. $wordtype = $case->type;
  55. $wordgrammar = $case->grammar;
  56. }
  57. //parent
  58. $parent = UserDict::selectRaw('parent, sum(confidence)')
  59. ->where('word',$word->word)
  60. ->where('dict_id','ef620a93-a55d-4756-89c5-e188ab009e45')
  61. ->where('type','!=','.part.')
  62. ->where('parent','!=','')
  63. ->whereNotNull('parent')
  64. ->groupBy('parent')
  65. ->orderBy('sum','desc')
  66. ->first();
  67. if($parent){
  68. $wordparent = $parent->parent;
  69. }
  70. //factors
  71. $factor = UserDict::selectRaw('factors, sum(confidence)')
  72. ->where('word',$word->word)
  73. ->where('dict_id','ef620a93-a55d-4756-89c5-e188ab009e45')
  74. ->where('type','!=','.part.')
  75. ->where('factors','<>','')
  76. ->whereNotNull('factors')
  77. ->groupBy('factors')
  78. ->orderBy('sum','desc')
  79. ->first();
  80. if($factor){
  81. $wordfactors = $factor->factors;
  82. }
  83. $new = UserDict::firstOrNew(
  84. [
  85. 'word' => $word->word,
  86. 'type' => $wordtype,
  87. 'grammar' => $wordgrammar,
  88. 'parent' => $wordparent,
  89. 'factors' => $wordfactors,
  90. 'dict_id' => '85dcc61c-c9e1-4ae0-9b44-cd6d9d9f0d01',
  91. ],
  92. [
  93. 'id' => app('snowflake')->id(),
  94. 'source' => '_ROBOT_',
  95. 'create_time'=>(int)(microtime(true)*1000)
  96. ]
  97. );
  98. $new->confidence = 90;
  99. $new->language = 'cm';
  100. $new->creator_id = 1;
  101. $new->flag = 1;
  102. $new->save();
  103. $bar->advance();
  104. }
  105. $bar->finish();
  106. return 0;
  107. }
  108. }