UpgradeCommunityTerm.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Tools\Tools;
  5. use App\Models\DhammaTerm;
  6. use App\Models\UserOperationDaily;
  7. use App\Models\Sentence;
  8. use App\Http\Api\ChannelApi;
  9. use Illuminate\Support\Str;
  10. class UpgradeCommunityTerm extends Command
  11. {
  12. /**
  13. * The name and signature of the console command.
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'upgrade:community.term {lang} {word?}';
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = 'Command 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. if (\App\Tools\Tools::isStop()) {
  41. return 0;
  42. }
  43. $lang = strtolower($this->argument('lang'));
  44. $langFamily = explode('-', $lang)[0];
  45. $localTerm = ChannelApi::getSysChannel("_community_term_{$lang}_");
  46. if (!$localTerm) {
  47. return 1;
  48. }
  49. $channelId = ChannelApi::getSysChannel('_System_Pali_VRI_');
  50. if ($channelId === false) {
  51. $this->error('no channel');
  52. return 1;
  53. }
  54. $table = DhammaTerm::select(['word', 'tag'])
  55. ->whereIn('language', [$this->argument('lang'), $lang, $langFamily])
  56. ->groupBy(['word', 'tag']);
  57. if ($this->argument('word')) {
  58. $table = $table->where('word', $this->argument('word'));
  59. }
  60. $words = $table->get();
  61. $bar = $this->output->createProgressBar(count($words));
  62. foreach ($words as $key => $word) {
  63. /**
  64. * 最优算法
  65. * 1. 找到最常见的意思
  66. * 2. 找到分数最高的
  67. */
  68. $bestNote = "";
  69. $allTerm = DhammaTerm::where('word', $word->word)
  70. ->where('tag', $word->tag)
  71. ->whereIn('language', [$this->argument('lang'), $lang, $langFamily])
  72. ->get();
  73. $score = [];
  74. //$term_exp = [];
  75. foreach ($allTerm as $key => $term) {
  76. //获取经验值
  77. $exp = UserOperationDaily::where('user_id', $term->editor_id)
  78. ->where('date_int', '<=', date_timestamp_get(date_create($term->updated_at)) * 1000)
  79. ->sum('duration');
  80. $iExp = (int)($exp / 1000);
  81. $noteStrLen = $term->note ? mb_strlen($term->note, 'UTF-8') : 0;
  82. $paliStrLen = 0;
  83. $tranStrLen = 0;
  84. $noteWithoutPali = "";
  85. if ($term->note && !empty(trim($term->note))) {
  86. //计算note得分
  87. //查找句子模版
  88. $pattern = "/\{\{[0-9].+?\}\}/";
  89. //获取去掉句子模版的剩余部分
  90. $noteWithoutPali = preg_replace($pattern, "", $term->note);
  91. $sentences = [];
  92. $iSent = preg_match_all($pattern, $term->note, $sentences);
  93. if ($iSent > 0) {
  94. foreach ($sentences[0] as $sentence) {
  95. $sentId = explode("-", trim($sentence, "{}"));
  96. if (count($sentId) === 4) {
  97. $hasTran = Sentence::where('book_id', $sentId[0])
  98. ->where('paragraph', $sentId[1])
  99. ->where('word_start', $sentId[2])
  100. ->where('word_end', $sentId[3])
  101. ->exists();
  102. $sentLen = Sentence::where('book_id', $sentId[0])
  103. ->where('paragraph', $sentId[1])
  104. ->where('word_start', $sentId[2])
  105. ->where('word_end', $sentId[3])
  106. ->where("channel_uid", $channelId)
  107. ->value('strlen');
  108. if ($sentLen) {
  109. $paliStrLen += $sentLen;
  110. if ($hasTran) {
  111. $tranStrLen += $sentLen;
  112. }
  113. }
  114. }
  115. }
  116. }
  117. }
  118. //计算该术语note的总得分
  119. $score["{$key}"] = $iExp * $noteStrLen;
  120. //$term_exp["{$key}"] = $iExp;
  121. //$updated_time["{$key}"] = $term->updated_at 先提取,具体如何使用待定
  122. }
  123. //需要过滤掉system_term的数量,把count(*)替换为经验值加合作为基础得分,(基础经验得分之和转化为标准活动周,再加上最近的更新时间,为最终得分)
  124. $hotMeaning = DhammaTerm::selectRaw('meaning,count(*) as co')
  125. ->where('word', $word->word)
  126. ->whereIn('language', [$this->argument('lang'), $lang, $langFamily])
  127. ->groupBy('meaning')
  128. ->orderBy('co', 'desc')
  129. ->first();
  130. if ($hotMeaning) {
  131. $bestNote = "";
  132. if (count($score) > 0) {
  133. arsort($score);
  134. $bestNote = $allTerm[(int)key($score)]->note;
  135. }
  136. $term = DhammaTerm::where('channal', $localTerm)->firstOrNew(
  137. [
  138. "word" => $word->word,
  139. "tag" => $word->tag,
  140. "channal" => $localTerm,
  141. ],
  142. [
  143. 'id' => app('snowflake')->id(),
  144. 'guid' => Str::uuid(),
  145. 'word_en' => Tools::getWordEn($word->word),
  146. 'meaning' => '',
  147. 'language' => $this->argument('lang'),
  148. 'owner' => config("mint.admin.root_uuid"),
  149. 'editor_id' => 0,
  150. 'create_time' => time() * 1000,
  151. ]
  152. );
  153. $term->tag = $word->tag;
  154. $term->meaning = $hotMeaning->meaning;
  155. $term->note = $bestNote;
  156. $term->modify_time = time() * 1000;
  157. $term->save();
  158. }
  159. $bar->advance();
  160. }
  161. $bar->finish();
  162. return 0;
  163. }
  164. }