UpgradeDictSysWbwExtract.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * 将用户词典中的数据进行汇总。
  4. * 算法:
  5. * 同样词性的合并为一条记录。意思按照出现的次数排序
  6. */
  7. namespace App\Console\Commands;
  8. use Illuminate\Console\Command;
  9. use App\Models\UserDict;
  10. class UpgradeDictSysWbwExtract extends Command
  11. {
  12. /**
  13. * The name and signature of the console command.
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'upgrade:syswbwextract';
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = '从社区词典中提取最优结果';
  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. $user_dict_id = DictApi::getSysDict('community');
  41. if(!$user_dict_id){
  42. $this->error('没有找到 community 字典');
  43. return 1;
  44. }
  45. $user_dict_extract_id = DictApi::getSysDict('community_extract');
  46. if(!$user_dict_extract_id){
  47. $this->error('没有找到 community_extract 字典');
  48. return 1;
  49. }
  50. $dict = UserDict::select('word')->where('word','!=','')->where('dict_id',$user_dict_id)->groupBy('word');
  51. $bar = $this->output->createProgressBar($dict->count());
  52. foreach ($dict->cursor() as $word) {
  53. # code...
  54. //case
  55. $wordtype = '';
  56. $wordgrammar = '';
  57. $wordparent = '';
  58. $wordfactors = '';
  59. $case = UserDict::selectRaw('type,grammar, sum(confidence)')
  60. ->where('word',$word->word)
  61. ->where('dict_id',$user_dict_id)
  62. ->where('type','!=','.part.')
  63. ->where('type','<>','')
  64. ->whereNotNull('type')
  65. ->groupBy(['type','grammar'])
  66. ->orderBy('sum','desc')
  67. ->first();
  68. if($case){
  69. $wordtype = $case->type;
  70. $wordgrammar = $case->grammar;
  71. }
  72. //parent
  73. $parent = UserDict::selectRaw('parent, sum(confidence)')
  74. ->where('word',$word->word)
  75. ->where('dict_id',$user_dict_id)
  76. ->where('type','!=','.part.')
  77. ->where('parent','!=','')
  78. ->whereNotNull('parent')
  79. ->groupBy('parent')
  80. ->orderBy('sum','desc')
  81. ->first();
  82. if($parent){
  83. $wordparent = $parent->parent;
  84. }
  85. //factors
  86. $factor = UserDict::selectRaw('factors, sum(confidence)')
  87. ->where('word',$word->word)
  88. ->where('dict_id',$user_dict_id)
  89. ->where('type','!=','.part.')
  90. ->where('factors','<>','')
  91. ->whereNotNull('factors')
  92. ->groupBy('factors')
  93. ->orderBy('sum','desc')
  94. ->first();
  95. if($factor){
  96. $wordfactors = $factor->factors;
  97. }
  98. $new = UserDict::firstOrNew(
  99. [
  100. 'word' => $word->word,
  101. 'type' => $wordtype,
  102. 'grammar' => $wordgrammar,
  103. 'parent' => $wordparent,
  104. 'factors' => $wordfactors,
  105. 'dict_id' => $user_dict_extract_id,
  106. ],
  107. [
  108. 'id' => app('snowflake')->id(),
  109. 'source' => '_ROBOT_',
  110. 'create_time'=>(int)(microtime(true)*1000)
  111. ]
  112. );
  113. $new->confidence = 90;
  114. $new->language = 'cm';
  115. $new->creator_id = 1;
  116. $new->flag = 1;
  117. $new->save();
  118. $bar->advance();
  119. }
  120. $bar->finish();
  121. return 0;
  122. }
  123. }