UpgradePaliText.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 = 'upgrade pali_texts paragraph infomation';
  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.pali_title") .'/'. $from.'_pali.csv';
  61. if (($fp = fopen($csvFile, "r")) !== false) {
  62. Log::info("csv load:" . $csvFile);
  63. while (($data = fgetcsv($fp, 0, ',')) !== false) {
  64. if ($inputRow > 0) {
  65. array_push($arrInserString, $data);
  66. }
  67. $inputRow++;
  68. }
  69. fclose($fp);
  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(
  77. //function ()use($from,$arrInserString,$title_data)
  78. {
  79. $paragraph_count = count($title_data);
  80. $paragraph_info = array();
  81. $paragraph_info[] = array($from, -1, $paragraph_count, -1, -1, -1);
  82. for ($iPar = 0; $iPar < count($title_data); $iPar++) {
  83. $title_data[$iPar]["level"] = $arrInserString[$iPar][3];
  84. }
  85. for ($iPar = 0; $iPar < count($title_data); $iPar++) {
  86. $book = $from ;
  87. $paragraph = $title_data[$iPar]["paragraph"];
  88. if ((int) $title_data[$iPar]["level"] == 8) {
  89. $title_data[$iPar]["level"] = 100;
  90. }
  91. $curr_level = (int) $title_data[$iPar]["level"];
  92. # 计算这个chapter的段落数量
  93. $length = -1;
  94. for ($iPar1 = $iPar + 1; $iPar1 < count($title_data); $iPar1++) {
  95. $thislevel = (int) $title_data[$iPar1]["level"];
  96. if ($thislevel <= $curr_level) {
  97. $length = (int) $title_data[$iPar1]["paragraph"] - $paragraph;
  98. break;
  99. }
  100. }
  101. if ($length == -1) {
  102. $length = $paragraph_count - $paragraph + 1;
  103. }
  104. /*
  105. 上一个段落
  106. 算法:查找上一个标题段落。而且该标题段落的下一个段落不是标题段落
  107. */
  108. $prev = -1;
  109. if ($iPar > 0) {
  110. for ($iPar1 = $iPar - 1; $iPar1 >= 0; $iPar1--) {
  111. if ($title_data[$iPar1]["level"] < 8 && $title_data[$iPar1+1]["level"]==100) {
  112. $prev = $title_data[$iPar1]["paragraph"];
  113. break;
  114. }
  115. }
  116. }
  117. /*
  118. 下一个段落
  119. 算法:查找下一个标题段落。而且该标题段落的下一个段落不是标题段落
  120. */
  121. $next = -1;
  122. if ($iPar < count($title_data) - 1) {
  123. for ($iPar1 = $iPar + 1; $iPar1 < count($title_data)-1; $iPar1++) {
  124. if ($title_data[$iPar1]["level"] <8 && $title_data[$iPar1+1]["level"]==100) {
  125. $next = $title_data[$iPar1]["paragraph"];
  126. break;
  127. }
  128. }
  129. }
  130. $parent = -1;
  131. if ($iPar > 0) {
  132. for ($iPar1 = $iPar - 1; $iPar1 >= 0; $iPar1--) {
  133. if ($title_data[$iPar1]["level"] < $curr_level) {
  134. $parent = $title_data[$iPar1]["paragraph"];
  135. break;
  136. }
  137. }
  138. }
  139. //计算章节包含总字符数
  140. $iChapter_strlen = 0;
  141. for ($i = $iPar; $i < $iPar + $length; $i++) {
  142. $iChapter_strlen += $title_data[$i]["lenght"];
  143. }
  144. $newData = [
  145. 'level' => $arrInserString[$iPar][3],
  146. 'toc' => $arrInserString[$iPar][5],
  147. 'chapter_len' => $length,
  148. 'next_chapter' => $next,
  149. 'prev_chapter' => $prev,
  150. 'parent' => $parent,
  151. 'chapter_strlen'=> $iChapter_strlen,
  152. ];
  153. $path = [];
  154. $title_data[$iPar]["level"] = $newData["level"];
  155. $title_data[$iPar]["toc"] = $newData["toc"];
  156. $title_data[$iPar]["parent"] = $newData["parent"];
  157. /*
  158. *获取路径
  159. */
  160. $currParent = $parent;
  161. $iLoop = 0;
  162. while ($currParent != -1 && $iLoop<7) {
  163. # code...
  164. $pathTitle = $title_data[$currParent-1]["toc"];
  165. $pathLevel = $title_data[$currParent-1]['level'];
  166. $path[] = ["book"=>$book,"paragraph"=>$currParent,"title"=>$pathTitle,"level"=>$pathLevel];
  167. $currParent = $title_data[$currParent-1]["parent"];
  168. $iLoop++;
  169. }
  170. # 将路径反向
  171. $path1 = [];
  172. for ($i=count($path)-1; $i >=0 ; $i--) {
  173. # code...
  174. $path1[] = $path[$i];
  175. }
  176. $newData['path'] = $path1;
  177. PaliText::where('book',$book)
  178. ->where('paragraph',$paragraph)
  179. ->update($newData);
  180. if ($curr_level > 0 && $curr_level < 8) {
  181. $paragraph_info[] = array($book, $paragraph, $length, $prev, $next, $parent);
  182. }
  183. }
  184. }
  185. //);
  186. $bar->advance();
  187. }
  188. $bar->finish();
  189. $this->info("instert pali text finished. in ". time()-$startTime . "s" .PHP_EOL);
  190. Log::info("instert pali text finished. in ". time()-$startTime . "s");
  191. return 0;
  192. }
  193. }