| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- <?php
- namespace App\Console\Commands;
- use Illuminate\Console\Command;
- use App\Tools\Tools;
- use App\Models\DhammaTerm;
- use App\Models\UserOperationDaily;
- use App\Models\Sentence;
- use App\Http\Api\ChannelApi;
- use Illuminate\Support\Str;
- class UpgradeCommunityTerm extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'upgrade:community.term {lang}';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = 'Command description';
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- }
- /**
- * Execute the console command.
- *
- * @return int
- */
- public function handle()
- {
- $lang = strtolower($this->argument('lang'));
- $langFamily = explode('-',$lang)[0];
- $localTerm = ChannelApi::getSysChannel(
- "_community_term_{$lang}_"
- );
- if(!$localTerm){
- return 1;
- }
- $channelId = ChannelApi::getSysChannel('_System_Pali_VRI_');
- if($channelId === false){
- $this->error('no channel');
- return 1;
- }
- $table = DhammaTerm::select('word')->whereIn('language',[$this->argument('lang'),$lang,$langFamily])
- ->groupBy('word');
- $words = $table->get();
- $bar = $this->output->createProgressBar(count($words));
- foreach ($words as $key => $word) {
- /**
- * 最优算法
- * 1. 找到最常见的意思
- * 2. 找到分数最高的
- */
- $bestNote = "" ;
- $allTerm = DhammaTerm::where('word',$word->word)
- ->whereIn('language',[$this->argument('lang'),$lang,$langFamily])
- ->get();
- $score = [];
- foreach ($allTerm as $key => $term) {
- //经验值
- $exp = UserOperationDaily::where('user_id',$term->editor_id)
- ->where('date_int','<=',date_timestamp_get(date_create($term->updated_at))*1000)
- ->sum('duration');
- $iExp = (int)($exp/1000);
- $noteStrLen = mb_strlen($term->note);
- $paliStrLen = 0;
- $tranStrLen = 0;
- $noteWithoutPali = "";
- if(!empty(trim($term->note))){
- #查找句子模版
- $pattern = "/\{\{[0-9].+?\}\}/";
- //获取去掉句子模版的剩余部分
- $noteWithoutPali = preg_replace($pattern,"",$term->note);
- $sentences = [];
- $iSent = preg_match_all($pattern,$term->note,$sentences);
- if($iSent>0){
- foreach ($sentences[0] as $sentence) {
- $sentId = explode("-",trim($sentence,"{}"));
- if(count($sentId) === 4){
- $hasTran = Sentence::where('book_id',$sentId[0])
- ->where('paragraph',$sentId[1])
- ->where('word_start',$sentId[2])
- ->where('word_end',$sentId[3])
- ->exists();
- $sentLen = Sentence::where('book_id',$sentId[0])
- ->where('paragraph',$sentId[1])
- ->where('word_start',$sentId[2])
- ->where('word_end',$sentId[3])
- ->where("channel_uid", $channelId)
- ->value('strlen');
- if($sentLen){
- $paliStrLen += $sentLen;
- if($hasTran){
- $tranStrLen += $sentLen;
- }
- }
- }
- }
- }
- }
- //计算该术语总得分
- $score["{$key}"] = $iExp*$noteStrLen;
- }
- $hotMeaning = DhammaTerm::selectRaw('meaning,count(*) as co')
- ->where('word',$word->word)
- ->whereIn('language',[$this->argument('lang'),$lang,$langFamily])
- ->groupBy('meaning')
- ->orderBy('co','desc')
- ->first();
- if($hotMeaning){
- $bestNote = "";
- if(count($score)>0){
- arsort($score);
- $bestNote = $allTerm[(int)key($score)]->note;
- }
- $term = DhammaTerm::where('channal',$localTerm)->firstOrNew(
- [
- "word" => $word->word,
- "channal" => $localTerm,
- ],
- [
- 'id' =>app('snowflake')->id(),
- 'guid' =>Str::uuid(),
- 'word_en' =>Tools::getWordEn($word->word),
- 'meaning' => '',
- 'language' => $this->argument('lang'),
- 'owner' => config("app.admin.root_uuid"),
- 'editor_id' => 0,
- 'create_time' => time()*1000,
- ]
- );
- $term->meaning = $hotMeaning->meaning;
- $term->note = $bestNote;
- $term->modify_time = time()*1000;
- $term->save();
- }
- $bar->advance();
- }
- $bar->finish();
- return 0;
- }
- }
|