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