InitCs6sentence.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 InitCs6sentence extends Command
  11. {
  12. /**
  13. * The name and signature of the console command.
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'init:cs6sentence {book?} {para?}';
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = '按照分句数据库,填充cs6的巴利原文句子';
  24. /**
  25. * Create a new command instance.
  26. *
  27. * @return void
  28. */
  29. public function __construct()
  30. {
  31. parent::__construct();
  32. }
  33. /**
  34. * Execute the console command.
  35. *
  36. * @return int
  37. */
  38. public function handle()
  39. {
  40. if (Tools::isStop()) {
  41. return 0;
  42. }
  43. $start = time();
  44. $channelId = ChannelApi::getSysChannel('_System_Pali_VRI_');
  45. if ($channelId === false) {
  46. $this->error('no channel');
  47. return 1;
  48. }
  49. $this->info($channelId);
  50. $pali = new PaliSentence;
  51. if (! empty($this->argument('book'))) {
  52. $pali = $pali->where('book', $this->argument('book'));
  53. }
  54. if (! empty($this->argument('para'))) {
  55. $pali = $pali->where('paragraph', $this->argument('para'));
  56. }
  57. $bar = $this->output->createProgressBar($pali->count());
  58. $pali = $pali->select('book', 'paragraph', 'word_begin', 'word_end')->cursor();
  59. $pageHead = ['M', 'P', 'T', 'V', 'O'];
  60. foreach ($pali as $value) {
  61. // code...
  62. $words = WbwTemplate::where('book', $value->book)
  63. ->where('paragraph', $value->paragraph)
  64. ->where('wid', '>=', $value->word_begin)
  65. ->where('wid', '<=', $value->word_end)
  66. ->orderBy('wid', 'asc')
  67. ->get();
  68. $sent = '';
  69. $boldStart = false;
  70. $boldCount = 0;
  71. $lastWord = null;
  72. foreach ($words as $word) {
  73. // code...
  74. // if($word->style != "note" && $word->type != '.ctl.')
  75. if ($word->type != '.ctl.') {
  76. if ($lastWord !== null) {
  77. if ($word->real !== 'ti') {
  78. if (! (empty($word->real) && empty($lastWord->real))) {
  79. // 如果不是标点符号,在词的前面加空格 。
  80. $sent .= ' ';
  81. }
  82. }
  83. }
  84. if (strpos($word->word, '{') !== false) {
  85. // 一个单词里面含有黑体字的
  86. $paliWord = \str_replace('{', '<strong>', $word->word);
  87. $paliWord = \str_replace('}', '</strong>', $paliWord);
  88. $sent .= $paliWord;
  89. } else {
  90. if ($word->style == 'bld') {
  91. $sent .= "<strong>{$word->word}</strong>";
  92. } else {
  93. $sent .= $word->word;
  94. }
  95. }
  96. } else {
  97. $type = substr($word->word, 0, 1);
  98. if (in_array($type, $pageHead)) {
  99. $arrPage = explode('.', $word->word);
  100. if (count($arrPage) === 2) {
  101. $pageNumber = $arrPage[0].'.'.(int) $arrPage[1];
  102. $sent .= "<code>{$pageNumber}</code>";
  103. }
  104. }
  105. }
  106. $lastWord = $word;
  107. }
  108. // 将wikipali风格的引用 改为缅文风格
  109. /*
  110. $sent = \str_replace('n’’’ ti','’’’nti',$sent);
  111. $sent = \str_replace('n’’ ti','’’nti',$sent);
  112. $sent = \str_replace('n’ ti','’nti',$sent);
  113. $sent = \str_replace('**ti**','**ti',$sent);
  114. $sent = \str_replace('‘ ','‘',$sent);
  115. */
  116. $sent = \str_replace(' ti', 'ti', $sent);
  117. $newRow = Sentence::firstOrNew(
  118. [
  119. 'book_id' => $value->book,
  120. 'paragraph' => $value->paragraph,
  121. 'word_start' => $value->word_begin,
  122. 'word_end' => $value->word_end,
  123. 'channel_uid' => $channelId,
  124. ],
  125. [
  126. 'id' => app('snowflake')->id(),
  127. 'uid' => Str::uuid(),
  128. 'create_time' => time() * 1000,
  129. ]
  130. );
  131. $newRow->editor_uid = config('mint.admin.root_uuid');
  132. $newRow->content = "<span>{$sent}</span>";
  133. $newRow->strlen = mb_strlen($sent, 'UTF-8');
  134. $newRow->status = 10;
  135. $newRow->content_type = 'html';
  136. $newRow->modify_time = time() * 1000;
  137. $newRow->language = 'en';
  138. $newRow->save();
  139. $bar->advance();
  140. }
  141. $bar->finish();
  142. $this->info('finished '.(time() - $start).'s');
  143. return 0;
  144. }
  145. }