UpgradeCommunityTerm.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 = $term->note? mb_strlen($term->note,'UTF-8'):0;
  79. $paliStrLen = 0;
  80. $tranStrLen = 0;
  81. $noteWithoutPali = "";
  82. if($term->note && !empty(trim($term->note))){
  83. //计算note得分
  84. //查找句子模版
  85. $pattern = "/\{\{[0-9].+?\}\}/";
  86. //获取去掉句子模版的剩余部分
  87. $noteWithoutPali = preg_replace($pattern,"",$term->note);
  88. $sentences = [];
  89. $iSent = preg_match_all($pattern,$term->note,$sentences);
  90. if($iSent>0){
  91. foreach ($sentences[0] as $sentence) {
  92. $sentId = explode("-",trim($sentence,"{}"));
  93. if(count($sentId) === 4){
  94. $hasTran = Sentence::where('book_id',$sentId[0])
  95. ->where('paragraph',$sentId[1])
  96. ->where('word_start',$sentId[2])
  97. ->where('word_end',$sentId[3])
  98. ->exists();
  99. $sentLen = Sentence::where('book_id',$sentId[0])
  100. ->where('paragraph',$sentId[1])
  101. ->where('word_start',$sentId[2])
  102. ->where('word_end',$sentId[3])
  103. ->where("channel_uid", $channelId)
  104. ->value('strlen');
  105. if($sentLen){
  106. $paliStrLen += $sentLen;
  107. if($hasTran){
  108. $tranStrLen += $sentLen;
  109. }
  110. }
  111. }
  112. }
  113. }
  114. }
  115. //计算该术语总得分
  116. $score["{$key}"] = $iExp*$noteStrLen;
  117. }
  118. $hotMeaning = DhammaTerm::selectRaw('meaning,count(*) as co')
  119. ->where('word',$word->word)
  120. ->whereIn('language',[$this->argument('lang'),$lang,$langFamily])
  121. ->groupBy('meaning')
  122. ->orderBy('co','desc')
  123. ->first();
  124. if($hotMeaning){
  125. $bestNote = "";
  126. if(count($score)>0){
  127. arsort($score);
  128. $bestNote = $allTerm[(int)key($score)]->note;
  129. }
  130. $term = DhammaTerm::where('channal',$localTerm)->firstOrNew(
  131. [
  132. "word" => $word->word,
  133. "channal" => $localTerm,
  134. ],
  135. [
  136. 'id' =>app('snowflake')->id(),
  137. 'guid' =>Str::uuid(),
  138. 'word_en' =>Tools::getWordEn($word->word),
  139. 'meaning' => '',
  140. 'language' => $this->argument('lang'),
  141. 'owner' => config("mint.admin.root_uuid"),
  142. 'editor_id' => 0,
  143. 'create_time' => time()*1000,
  144. ]
  145. );
  146. $term->meaning = $hotMeaning->meaning;
  147. $term->note = $bestNote;
  148. $term->modify_time = time()*1000;
  149. $term->save();
  150. }
  151. $bar->advance();
  152. }
  153. $bar->finish();
  154. return 0;
  155. }
  156. }