InitCs6sentence.php 3.5 KB

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