UpgradeCommunityTerm.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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\Http\Api\ChannelApi;
  7. use Illuminate\Support\Str;
  8. class UpgradeCommunityTerm extends Command
  9. {
  10. /**
  11. * The name and signature of the console command.
  12. *
  13. * @var string
  14. */
  15. protected $signature = 'upgrade:community.term {lang}';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = 'Command description';
  22. /**
  23. * Create a new command instance.
  24. *
  25. * @return void
  26. */
  27. public function __construct()
  28. {
  29. parent::__construct();
  30. }
  31. /**
  32. * Execute the console command.
  33. *
  34. * @return int
  35. */
  36. public function handle()
  37. {
  38. $lang = strtolower($this->argument('lang'));
  39. $langFamily = explode('-',$lang)[0];
  40. $localTerm = ChannelApi::getSysChannel(
  41. "_community_term_{$lang}_"
  42. );
  43. if(!$localTerm){
  44. return 1;
  45. }
  46. $channelId = ChannelApi::getSysChannel('_System_Pali_VRI_');
  47. if($channelId === false){
  48. $this->error('no channel');
  49. return 1;
  50. }
  51. $table = DhammaTerm::select('word')->whereIn('language',[$this->argument('lang'),$lang,$langFamily])
  52. ->groupBy('word');
  53. $words = $table->get();
  54. $bar = $this->output->createProgressBar(count($words));
  55. foreach ($words as $key => $word) {
  56. /**
  57. * 最优算法
  58. * 1. 找到最常见的意思
  59. * 2. 找到分数最高的
  60. */
  61. $bestNote = "" ;
  62. $allTerm = DhammaTerm::where('word',$word->word)
  63. ->whereIn('language',[$this->argument('lang'),$lang,$langFamily])
  64. ->get();
  65. foreach ($allTerm as $term) {
  66. # code..
  67. //经验值
  68. $exp = UserOperationDaily::where('user_id',$this->creator_id)
  69. ->where('date_int','<=',date_timestamp_get(date_create($term->updated_at))*1000)
  70. ->sum('duration');
  71. $iExp = (int)($exp/1000);
  72. $noteStrLen = mb_strlen($term->note);
  73. $paliStrLen = 0;
  74. #查找句子模版
  75. $pattern = "/\{\{([0-9].+?)\}\}/";
  76. $noteWithoutPali = preg_replace($pattern,"",$term->note);
  77. $sentences = [];
  78. $iSent = preg_match_all($pattern,$term->note,$sentences);
  79. foreach ($sentences as $sentence) {
  80. $sentId = explode("-",$sentence);
  81. if(count($sentId) === 4){
  82. $countTran = Sentence::where('book_id',$sentId[0])
  83. ->where('paragraph',$sentId[1])
  84. ->where('word_start',$sentId[2])
  85. ->where('word_end',$sentId[3])
  86. ->count();
  87. $sentLen = Sentence::where('book_id',$sentId[0])
  88. ->where('paragraph',$sentId[1])
  89. ->where('word_start',$sentId[2])
  90. ->where('word_end',$sentId[3])
  91. ->where("channel_uid", $channelId)
  92. ->value('strlen');
  93. if($sentLen){
  94. $paliStrLen += $sentLen;
  95. }
  96. }
  97. }
  98. //计算得分
  99. }
  100. $hotMeaning = DhammaTerm::selectRaw('meaning,count(*) as co')
  101. ->where('word',$word->word)
  102. ->whereIn('language',[$this->argument('lang'),$lang,$langFamily])
  103. ->groupBy('meaning')
  104. ->orderBy('co','desc')
  105. ->first();
  106. if($hotMeaning){
  107. $term = DhammaTerm::where('channal',$localTerm)->firstOrNew(
  108. [
  109. "word" => $word->word,
  110. "channal" => $localTerm,
  111. ],
  112. [
  113. 'id' =>app('snowflake')->id(),
  114. 'guid' =>Str::uuid(),
  115. 'word_en' =>Tools::getWordEn($word->word),
  116. 'meaning' => '',
  117. 'language' => $this->argument('lang'),
  118. 'owner' => config("app.admin.root_uuid"),
  119. 'editor_id' => 0,
  120. 'create_time' => time()*1000,
  121. ]
  122. );
  123. $term->meaning = $hotMeaning->meaning;
  124. $term->modify_time = time()*1000;
  125. $term->save();
  126. }
  127. $bar->advance();
  128. }
  129. $bar->finish();
  130. return 0;
  131. }
  132. }