UpgradeCommunityTerm.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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}';
  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. $lang = strtolower($this->argument('lang'));
  41. $langFamily = explode('-',$lang)[0];
  42. $localTerm = ChannelApi::getSysChannel(
  43. "_community_term_{$lang}_"
  44. );
  45. if(!$localTerm){
  46. return 1;
  47. }
  48. $channelId = ChannelApi::getSysChannel('_System_Pali_VRI_');
  49. if($channelId === false){
  50. $this->error('no channel');
  51. return 1;
  52. }
  53. $table = DhammaTerm::select('word')
  54. ->whereIn('language',[$this->argument('lang'),$lang,$langFamily])
  55. ->groupBy('word');
  56. $words = $table->get();
  57. $bar = $this->output->createProgressBar(count($words));
  58. foreach ($words as $key => $word) {
  59. /**
  60. * 最优算法
  61. * 1. 找到最常见的意思
  62. * 2. 找到分数最高的
  63. */
  64. $bestNote = "" ;
  65. $allTerm = DhammaTerm::where('word',$word->word)
  66. ->whereIn('language',[$this->argument('lang'),$lang,$langFamily])
  67. ->get();
  68. $score = [];
  69. foreach ($allTerm as $key => $term) {
  70. //经验值
  71. $exp = UserOperationDaily::where('user_id',$term->editor_id)
  72. ->where('date_int','<=',date_timestamp_get(date_create($term->updated_at))*1000)
  73. ->sum('duration');
  74. $iExp = (int)($exp/1000);
  75. $noteStrLen = mb_strlen($term->note);
  76. $paliStrLen = 0;
  77. $tranStrLen = 0;
  78. $noteWithoutPali = "";
  79. if(!empty(trim($term->note))){
  80. #查找句子模版
  81. $pattern = "/\{\{[0-9].+?\}\}/";
  82. //获取去掉句子模版的剩余部分
  83. $noteWithoutPali = preg_replace($pattern,"",$term->note);
  84. $sentences = [];
  85. $iSent = preg_match_all($pattern,$term->note,$sentences);
  86. if($iSent>0){
  87. foreach ($sentences[0] as $sentence) {
  88. $sentId = explode("-",trim($sentence,"{}"));
  89. if(count($sentId) === 4){
  90. $hasTran = Sentence::where('book_id',$sentId[0])
  91. ->where('paragraph',$sentId[1])
  92. ->where('word_start',$sentId[2])
  93. ->where('word_end',$sentId[3])
  94. ->exists();
  95. $sentLen = Sentence::where('book_id',$sentId[0])
  96. ->where('paragraph',$sentId[1])
  97. ->where('word_start',$sentId[2])
  98. ->where('word_end',$sentId[3])
  99. ->where("channel_uid", $channelId)
  100. ->value('strlen');
  101. if($sentLen){
  102. $paliStrLen += $sentLen;
  103. if($hasTran){
  104. $tranStrLen += $sentLen;
  105. }
  106. }
  107. }
  108. }
  109. }
  110. }
  111. //计算该术语总得分
  112. $score["{$key}"] = $iExp*$noteStrLen;
  113. }
  114. $hotMeaning = DhammaTerm::selectRaw('meaning,count(*) as co')
  115. ->where('word',$word->word)
  116. ->whereIn('language',[$this->argument('lang'),$lang,$langFamily])
  117. ->groupBy('meaning')
  118. ->orderBy('co','desc')
  119. ->first();
  120. if($hotMeaning){
  121. $bestNote = "";
  122. if(count($score)>0){
  123. arsort($score);
  124. $bestNote = $allTerm[(int)key($score)]->note;
  125. }
  126. $term = DhammaTerm::where('channal',$localTerm)->firstOrNew(
  127. [
  128. "word" => $word->word,
  129. "channal" => $localTerm,
  130. ],
  131. [
  132. 'id' =>app('snowflake')->id(),
  133. 'guid' =>Str::uuid(),
  134. 'word_en' =>Tools::getWordEn($word->word),
  135. 'meaning' => '',
  136. 'language' => $this->argument('lang'),
  137. 'owner' => config("mint.admin.root_uuid"),
  138. 'editor_id' => 0,
  139. 'create_time' => time()*1000,
  140. ]
  141. );
  142. $term->meaning = $hotMeaning->meaning;
  143. $term->note = $bestNote;
  144. $term->modify_time = time()*1000;
  145. $term->save();
  146. }
  147. $bar->advance();
  148. }
  149. $bar->finish();
  150. return 0;
  151. }
  152. }