UpgradeCommunityTerm.php 6.1 KB

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