UpgradeDictSysWbwExtract.php 4.1 KB

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