UpgradePaliText.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Models\PaliText;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Facades\Log;
  7. class UpgradePaliText extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'upgrade:palitext {from?} {to?}';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = 'Command description';
  21. /**
  22. * Create a new command instance.
  23. *
  24. * @return void
  25. */
  26. public function __construct()
  27. {
  28. parent::__construct();
  29. }
  30. /**
  31. * Execute the console command.
  32. *
  33. * @return int
  34. */
  35. public function handle()
  36. {
  37. $this->info("upgrade pali text");
  38. $startTime = time();
  39. $_from = $this->argument('from');
  40. $_to = $this->argument('to');
  41. if(empty($_from) && empty($_to)){
  42. $_from = 1;
  43. $_to = 217;
  44. }else if(empty($_to)){
  45. $_to = $_from;
  46. }
  47. #载入文件列表
  48. $fileListFileName = config("app.path.palitext_filelist");
  49. $filelist = array();
  50. if (($handle = fopen($fileListFileName, 'r')) !== false) {
  51. while (($filelist[] = fgetcsv($handle, 0, ',')) !== false) {
  52. }
  53. }
  54. $bar = $this->output->createProgressBar($_to-$_from+1);
  55. for ($from=$_from; $from <= $_to; $from++) {
  56. $inputRow = 0;
  57. $arrInserString = array();
  58. #载入csv数据
  59. $FileName = $filelist[$from-1][1];
  60. $csvFile = config("app.path.palicsv") .'/'. $FileName .'/'. $FileName.'_pali.csv';
  61. if (($fp = fopen($csvFile, "r")) !== false) {
  62. while (($data = fgetcsv($fp, 0, ',')) !== false) {
  63. if ($inputRow > 0) {
  64. array_push($arrInserString, $data);
  65. }
  66. $inputRow++;
  67. }
  68. fclose($fp);
  69. Log::info("csv load:" . $csvFile);
  70. } else {
  71. $this->error( "can not open csv file. filename=" . $csvFile. PHP_EOL) ;
  72. Log::error( "can not open csv file. filename=" . $csvFile) ;
  73. continue;
  74. }
  75. $title_data = PaliText::where('book',$from)->orderby('paragraph','asc')->get();
  76. DB::transaction(function ()use($from,$arrInserString,$title_data) {
  77. $paragraph_count = count($title_data);
  78. $paragraph_info = array();
  79. $paragraph_info[] = array($from, -1, $paragraph_count, -1, -1, -1);
  80. for ($iPar = 0; $iPar < count($title_data); $iPar++) {
  81. $title_data[$iPar]["level"] = $arrInserString[$iPar][3];
  82. }
  83. for ($iPar = 0; $iPar < count($title_data); $iPar++) {
  84. $book = $from ;
  85. $paragraph = $title_data[$iPar]["paragraph"];
  86. if ((int) $title_data[$iPar]["level"] == 8) {
  87. $title_data[$iPar]["level"] = 100;
  88. }
  89. $curr_level = (int) $title_data[$iPar]["level"];
  90. # 计算这个chapter的段落数量
  91. $length = -1;
  92. for ($iPar1 = $iPar + 1; $iPar1 < count($title_data); $iPar1++) {
  93. $thislevel = (int) $title_data[$iPar1]["level"];
  94. if ($thislevel <= $curr_level) {
  95. $length = (int) $title_data[$iPar1]["paragraph"] - $paragraph;
  96. break;
  97. }
  98. }
  99. if ($length == -1) {
  100. $length = $paragraph_count - $paragraph + 1;
  101. }
  102. $prev = -1;
  103. if ($iPar > 0) {
  104. for ($iPar1 = $iPar - 1; $iPar1 >= 0; $iPar1--) {
  105. if ($title_data[$iPar1]["level"] == $curr_level) {
  106. $prev = $title_data[$iPar1]["paragraph"];
  107. break;
  108. }
  109. }
  110. }
  111. $next = -1;
  112. if ($iPar < count($title_data) - 1) {
  113. for ($iPar1 = $iPar + 1; $iPar1 < count($title_data); $iPar1++) {
  114. if ($title_data[$iPar1]["level"] == $curr_level) {
  115. $next = $title_data[$iPar1]["paragraph"];
  116. break;
  117. }
  118. }
  119. }
  120. $parent = -1;
  121. if ($iPar > 0) {
  122. for ($iPar1 = $iPar - 1; $iPar1 >= 0; $iPar1--) {
  123. if ($title_data[$iPar1]["level"] < $curr_level) {
  124. $parent = $title_data[$iPar1]["paragraph"];
  125. break;
  126. }
  127. }
  128. }
  129. //计算章节包含总字符数
  130. $iChapter_strlen = 0;
  131. for ($i = $iPar; $i < $iPar + $length; $i++) {
  132. $iChapter_strlen += $title_data[$i]["lenght"];
  133. }
  134. $newData = [
  135. 'level' => $arrInserString[$iPar][3],
  136. 'toc' => $arrInserString[$iPar][5],
  137. 'chapter_len' => $length,
  138. 'next_chapter' => $next,
  139. 'prev_chapter' => $prev,
  140. 'parent' => $parent,
  141. 'chapter_strlen'=> $iChapter_strlen,
  142. ];
  143. PaliText::where('book',$book)
  144. ->where('paragraph',$paragraph)
  145. ->update($newData);
  146. if ($curr_level > 0 && $curr_level < 8) {
  147. $paragraph_info[] = array($book, $paragraph, $length, $prev, $next, $parent);
  148. }
  149. }
  150. });
  151. $bar->advance();
  152. }
  153. $bar->finish();
  154. $this->info("instert pali text finished. in ". time()-$startTime . "s" .PHP_EOL);
  155. Log::info("instert pali text finished. in ". time()-$startTime . "s");
  156. return 0;
  157. }
  158. }