UpgradeWbwTemplate.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 107
  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. 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. $pali = $pali->select('book','paragraph','word_begin','word_end')->cursor();
  57. foreach ($pali as $value) {
  58. # code...
  59. $wbwContent=[];
  60. $words = WbwTemplate::where("book",$value->book)
  61. ->where("paragraph",$value->paragraph)
  62. ->where("wid",">=",$value->word_begin)
  63. ->where("wid","<=",$value->word_end)
  64. ->orderBy('wid','asc')
  65. ->get();
  66. $sent = '';
  67. foreach ($words as $wbw_word) {
  68. # code...
  69. $type = $wbw_word->type=='?'? '':$wbw_word->type;
  70. $grammar = $wbw_word->gramma=='?'? '':$wbw_word->gramma;
  71. $part = $wbw_word->part=='?'? '':$wbw_word->part;
  72. if(!empty($type) || !empty($grammar)){
  73. $case = "{$type}#$grammar";
  74. }else{
  75. $case = "";
  76. }
  77. $wbwContent[] = [
  78. 'sn'=>[$wbw_word->wid],
  79. 'word'=>['value'=>$wbw_word->word,'status'=>0],
  80. 'real'=> ['value'=>$wbw_word->real,'status'=>0],
  81. 'meaning'=> ['value'=>'','status'=>0],
  82. 'type'=> ['value'=>$type,'status'=>0],
  83. 'grammar'=> ['value'=>$grammar,'status'=>0],
  84. 'case'=> ['value'=>$case,'status'=>0],
  85. 'style'=> ['value'=>$wbw_word->style,'status'=>0],
  86. 'factors'=> ['value'=>$part,'status'=>0],
  87. 'factorMeaning'=> ['value'=>'','status'=>0],
  88. 'confidence'=> 1.0
  89. ];
  90. }
  91. $sent = \json_encode($wbwContent,JSON_UNESCAPED_UNICODE);
  92. $newRow = Sentence::firstOrNew(
  93. [
  94. "book_id" => $value->book,
  95. "paragraph" => $value->paragraph,
  96. "word_start" => $value->word_begin,
  97. "word_end" => $value->word_end,
  98. "channel_uid" => $channelId,
  99. ],
  100. [
  101. 'id' =>app('snowflake')->id(),
  102. 'uid' =>Str::uuid(),
  103. ]
  104. );
  105. $newRow->editor_uid = config("mint.admin.root_uuid");
  106. $newRow->content = trim($sent);
  107. $newRow->content_type = "json";
  108. $newRow->strlen = mb_strlen($sent,"UTF-8");
  109. $newRow->status = 10;
  110. $newRow->create_time = time()*1000;
  111. $newRow->modify_time = time()*1000;
  112. $newRow->language = 'en';
  113. $newRow->save();
  114. $bar->advance();
  115. }
  116. $bar->finish();
  117. $this->info("finished ".(time()-$start)."s");
  118. return 0;
  119. }
  120. }