UpgradeWbwTemplate.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Models\PaliSentence;
  5. use App\Models\WbwTemplate;
  6. use App\Models\Sentence;
  7. use App\Http\Api\ChannelApi;
  8. use Illuminate\Support\Str;
  9. class UpgradeWbwTemplate extends Command
  10. {
  11. /**
  12. * The name and signature of the console command.
  13. * php artisan upgrade:wbw.template 129 509
  14. * @var string
  15. */
  16. protected $signature = 'upgrade:wbw.template {book?} {para?} {--debug=false}';
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = 'upgrade wbw template by sentence';
  23. /**
  24. * Create a new command instance.
  25. *
  26. * @return void
  27. */
  28. public function __construct()
  29. {
  30. parent::__construct();
  31. }
  32. /**
  33. * Execute the console command.
  34. *
  35. * @return int
  36. */
  37. public function handle()
  38. {
  39. if(\App\Tools\Tools::isStop()){
  40. return 0;
  41. }
  42. $start = time();
  43. $pali = new PaliSentence;
  44. if(!empty($this->argument('book'))){
  45. $pali = $pali->where('book',$this->argument('book'));
  46. }
  47. if(!empty($this->argument('para'))){
  48. $pali = $pali->where('paragraph',$this->argument('para'));
  49. }
  50. $bar = $this->output->createProgressBar($pali->count());
  51. $channelId = ChannelApi::getSysChannel('_System_Wbw_VRI_');
  52. if($channelId===false){
  53. $this->error('no channel');
  54. return 1;
  55. }
  56. $this->info('channel id='.$channelId);
  57. $pali = $pali->select('book','paragraph','word_begin','word_end')->cursor();
  58. foreach ($pali as $value) {
  59. # code...
  60. $wbwContent=[];
  61. $words = WbwTemplate::where("book",$value->book)
  62. ->where("paragraph",$value->paragraph)
  63. ->where("wid",">=",$value->word_begin)
  64. ->where("wid","<=",$value->word_end)
  65. ->orderBy('wid','asc')
  66. ->get();
  67. $sent = '';
  68. $sentId = $value->book.'-'.$value->paragraph.'-'.$value->word_begin.'-'.$value->word_end;
  69. if(count($words)===0){
  70. $this->error('no word data in'.$sentId);
  71. }
  72. foreach ($words as $wbw_word) {
  73. # code...
  74. $type = $wbw_word->type=='?'? '':$wbw_word->type;
  75. $grammar = $wbw_word->gramma=='?'? '':$wbw_word->gramma;
  76. $part = $wbw_word->part=='?'? '':$wbw_word->part;
  77. if(!empty($type) || !empty($grammar)){
  78. $case = "{$type}#$grammar";
  79. }else{
  80. $case = "";
  81. }
  82. $wbwContent[] = [
  83. 'sn'=>[$wbw_word->wid],
  84. 'word'=>['value'=>$wbw_word->word,'status'=>0],
  85. 'real'=> ['value'=>$wbw_word->real,'status'=>0],
  86. 'meaning'=> ['value'=>'','status'=>0],
  87. 'type'=> ['value'=>$type,'status'=>0],
  88. 'grammar'=> ['value'=>$grammar,'status'=>0],
  89. 'case'=> ['value'=>$case,'status'=>0],
  90. 'style'=> ['value'=>$wbw_word->style,'status'=>0],
  91. 'factors'=> ['value'=>$part,'status'=>0],
  92. 'factorMeaning'=> ['value'=>'','status'=>0],
  93. 'confidence'=> 1.0
  94. ];
  95. }
  96. $sent = \json_encode($wbwContent,JSON_UNESCAPED_UNICODE);
  97. if($this->option('debug')==='true'){
  98. $this->info($sentId.'='.$sent);
  99. }
  100. $newRow = Sentence::firstOrNew(
  101. [
  102. "book_id" => $value->book,
  103. "paragraph" => $value->paragraph,
  104. "word_start" => $value->word_begin,
  105. "word_end" => $value->word_end,
  106. "channel_uid" => $channelId,
  107. ],
  108. [
  109. 'id' =>app('snowflake')->id(),
  110. 'uid' =>Str::uuid(),
  111. ]
  112. );
  113. $newRow->editor_uid = config("mint.admin.root_uuid");
  114. $newRow->content = trim($sent);
  115. $newRow->content_type = "json";
  116. $newRow->strlen = mb_strlen($sent,"UTF-8");
  117. $newRow->status = 10;
  118. $newRow->create_time = time()*1000;
  119. $newRow->modify_time = time()*1000;
  120. $newRow->language = 'en';
  121. $newRow->save();
  122. $bar->advance();
  123. }
  124. $bar->finish();
  125. $this->info("finished ".(time()-$start)."s");
  126. return 0;
  127. }
  128. }