2
0

UpgradeWbwTemplate.php 4.7 KB

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