UpgradeWbwTemplate.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. *
  14. * @var string
  15. */
  16. protected $signature = 'upgrade:wbw.template {book?} {para?}';
  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. $start = time();
  40. $pali = new PaliSentence;
  41. if(!empty($this->argument('book'))){
  42. $pali = $pali->where('book',$this->argument('book'));
  43. }
  44. if(!empty($this->argument('para'))){
  45. $pali = $pali->where('paragraph',$this->argument('para'));
  46. }
  47. $bar = $this->output->createProgressBar($pali->count());
  48. $channelId = ChannelApi::getSysChannel('_System_Wbw_VRI_');
  49. if($channelId===false){
  50. $this->error('no channel');
  51. return 1;
  52. }
  53. $pali = $pali->select('book','paragraph','word_begin','word_end')->cursor();
  54. foreach ($pali as $value) {
  55. # code...
  56. $wbwContent=[];
  57. $words = WbwTemplate::where("book",$value->book)
  58. ->where("paragraph",$value->paragraph)
  59. ->where("wid",">=",$value->word_begin)
  60. ->where("wid","<=",$value->word_end)
  61. ->orderBy('wid','asc')
  62. ->get();
  63. $sent = '';
  64. foreach ($words as $wbw_word) {
  65. # code...
  66. $type = $wbw_word->type=='?'? '':$wbw_word->type;
  67. $grammar = $wbw_word->gramma=='?'? '':$wbw_word->gramma;
  68. $part = $wbw_word->part=='?'? '':$wbw_word->part;
  69. $wbwContent[] = [
  70. 'word'=>['value'=>$wbw_word->word,'status'=>0],
  71. 'real'=> ['value'=>$wbw_word->word,'status'=>0],
  72. 'meaning'=> ['value'=>[],'status'=>0],
  73. 'type'=> ['value'=>$type,'status'=>0],
  74. 'grammar'=> ['value'=>$grammar,'status'=>0],
  75. 'case'=> ['value'=>[],'status'=>0],
  76. 'style'=> ['value'=>$wbw_word->style,'status'=>0],
  77. 'factors'=> ['value'=>$part,'status'=>0],
  78. 'factorMeaning'=> ['value'=>'','status'=>0],
  79. 'confidence'=> 0.5
  80. ];
  81. }
  82. $sent = \json_encode($wbwContent,JSON_UNESCAPED_UNICODE);
  83. $newRow = Sentence::firstOrNew(
  84. [
  85. "book_id" => $value->book,
  86. "paragraph" => $value->paragraph,
  87. "word_start" => $value->word_begin,
  88. "word_end" => $value->word_end,
  89. "channel_uid" => $channelId,
  90. ],
  91. [
  92. 'id' =>app('snowflake')->id(),
  93. 'uid' =>Str::uuid(),
  94. ]
  95. );
  96. $newRow->editor_uid = config("app.admin.root_uuid");
  97. $newRow->content = trim($sent);
  98. $newRow->strlen = mb_strlen($sent,"UTF-8");
  99. $newRow->status = 30;
  100. $newRow->create_time = time()*1000;
  101. $newRow->modify_time = time()*1000;
  102. $newRow->language = 'en';
  103. $newRow->save();
  104. $bar->advance();
  105. }
  106. $bar->finish();
  107. $this->info("finished ".(time()-$start)."s");
  108. return 0;
  109. }
  110. }