UpgradeCommunityTerm.php 6.4 KB

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