UpgradePaliText.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. /**
  3. * 计算章节的父子,前后关系
  4. * 输入: csv文件
  5. */
  6. namespace App\Console\Commands;
  7. use Illuminate\Console\Command;
  8. use App\Models\PaliText;
  9. use App\Models\BookTitle;
  10. use Illuminate\Support\Facades\DB;
  11. use Illuminate\Support\Facades\Log;
  12. class UpgradePaliText extends Command
  13. {
  14. /**
  15. * The name and signature of the console command.
  16. *
  17. * @var string
  18. */
  19. protected $signature = 'upgrade:palitext {from?} {to?}';
  20. /**
  21. * The console command description.
  22. *
  23. * @var string
  24. */
  25. protected $description = 'upgrade pali_texts paragraph infomation';
  26. /**
  27. * Create a new command instance.
  28. *
  29. * @return void
  30. */
  31. public function __construct()
  32. {
  33. parent::__construct();
  34. }
  35. /**
  36. * Execute the console command.
  37. *
  38. * @return int
  39. */
  40. public function handle()
  41. {
  42. $this->info("upgrade pali text");
  43. $startTime = time();
  44. $_from = $this->argument('from');
  45. $_to = $this->argument('to');
  46. if(empty($_from) && empty($_to)){
  47. $_from = 1;
  48. $_to = 217;
  49. }else if(empty($_to)){
  50. $_to = $_from;
  51. }
  52. #载入文件列表
  53. $fileListFileName = config("app.path.palitext_filelist");
  54. $filelist = array();
  55. if (($handle = fopen($fileListFileName, 'r')) !== false) {
  56. while (($filelist[] = fgetcsv($handle, 0, ',')) !== false) {
  57. }
  58. }
  59. $bar = $this->output->createProgressBar($_to-$_from+1);
  60. for ($from=$_from; $from <= $_to; $from++) {
  61. $inputRow = 0;
  62. $arrInserString = array();
  63. #载入csv数据
  64. $FileName = $filelist[$from-1][1];
  65. $csvFile = config("app.path.pali_title") .'/'. $from.'_pali.csv';
  66. if (($fp = fopen($csvFile, "r")) !== false) {
  67. Log::info("csv load:" . $csvFile);
  68. while (($data = fgetcsv($fp, 0, ',')) !== false) {
  69. if ($inputRow > 0) {
  70. array_push($arrInserString, $data);
  71. }
  72. $inputRow++;
  73. }
  74. fclose($fp);
  75. } else {
  76. $this->error( "can not open csv file. filename=" . $csvFile. PHP_EOL) ;
  77. Log::error( "can not open csv file. filename=" . $csvFile) ;
  78. continue;
  79. }
  80. $title_data = PaliText::select(['book','paragraph','level','parent','toc','lenght'])
  81. ->where('book',$from)->orderby('paragraph','asc')->get();
  82. $paragraph_count = count($title_data);
  83. $paragraph_info = array();
  84. $paragraph_info[] = array($from, -1, $paragraph_count, -1, -1, -1);
  85. for ($iPar = 0; $iPar < count($title_data); $iPar++) {
  86. $title_data[$iPar]["level"] = $arrInserString[$iPar][3];
  87. }
  88. for ($iPar = 0; $iPar < count($title_data); $iPar++) {
  89. $this->info($iPar);
  90. $book = $from ;
  91. $paragraph = $title_data[$iPar]["paragraph"];
  92. $true_level = (int) $title_data[$iPar]["level"];
  93. if ((int) $title_data[$iPar]["level"] == 8) {
  94. $title_data[$iPar]["level"] = 100;
  95. }
  96. $curr_level = (int) $title_data[$iPar]["level"];
  97. # 计算这个chapter的段落数量
  98. $length = -1;
  99. for ($iPar1 = $iPar + 1; $iPar1 < count($title_data); $iPar1++) {
  100. $thislevel = (int) $title_data[$iPar1]["level"];
  101. if ($thislevel <= $curr_level) {
  102. $length = (int) $title_data[$iPar1]["paragraph"] - $paragraph;
  103. break;
  104. }
  105. }
  106. if ($length == -1) {
  107. $length = $paragraph_count - $paragraph + 1;
  108. }
  109. /*
  110. 上一个段落
  111. 算法:查找上一个标题段落。而且该标题段落的下一个段落不是标题段落
  112. */
  113. $prev = -1;
  114. if ($iPar > 0) {
  115. for ($iPar1 = $iPar - 1; $iPar1 >= 0; $iPar1--) {
  116. if ($title_data[$iPar1]["level"] < 8 && $title_data[$iPar1+1]["level"]==100) {
  117. $prev = $title_data[$iPar1]["paragraph"];
  118. break;
  119. }
  120. }
  121. }
  122. /*
  123. 下一个段落
  124. 算法:查找下一个标题段落。而且该标题段落的下一个段落不是标题段落
  125. */
  126. $next = -1;
  127. if ($iPar < count($title_data) - 1) {
  128. for ($iPar1 = $iPar + 1; $iPar1 < count($title_data)-1; $iPar1++) {
  129. if ($title_data[$iPar1]["level"] <8 && $title_data[$iPar1+1]["level"]==100) {
  130. $next = $title_data[$iPar1]["paragraph"];
  131. break;
  132. }
  133. }
  134. }
  135. //查找parent
  136. $parent = -1;
  137. if ($iPar > 0) {
  138. for ($iPar1 = $iPar - 1; $iPar1 >= 0; $iPar1--) {
  139. if ($title_data[$iPar1]["level"] < $true_level) {
  140. $parent = $title_data[$iPar1]["paragraph"];
  141. break;
  142. }
  143. }
  144. }
  145. //计算章节包含总字符数
  146. $iChapter_strlen = 0;
  147. for ($i = $iPar; $i < $iPar + $length; $i++) {
  148. $iChapter_strlen += $title_data[$i]["lenght"];
  149. }
  150. $newData = [
  151. 'level' => $arrInserString[$iPar][3],
  152. 'toc' => $arrInserString[$iPar][5],
  153. 'chapter_len' => $length,
  154. 'next_chapter' => $next,
  155. 'prev_chapter' => $prev,
  156. 'parent' => $parent,
  157. 'chapter_strlen'=> $iChapter_strlen,
  158. ];
  159. $path = [];
  160. $title_data[$iPar]["level"] = $newData["level"];
  161. $title_data[$iPar]["toc"] = $newData["toc"];
  162. $title_data[$iPar]["parent"] = $newData["parent"];
  163. /*
  164. *获取路径
  165. */
  166. $currParent = $parent;
  167. $iLoop = 0;
  168. while ($currParent != -1 && $iLoop<7) {
  169. # code...
  170. $pathTitle = $title_data[$currParent-1]["toc"];
  171. $pathLevel = $title_data[$currParent-1]['level'];
  172. $path[] = ["book"=>$book,"paragraph"=>$currParent,"title"=>$pathTitle,"level"=>$pathLevel];
  173. $currParent = $title_data[$currParent-1]["parent"];
  174. $iLoop++;
  175. }
  176. if(count($path)>0){
  177. //插入书名
  178. $bookTitle = BookTitle::where('book',$book)
  179. ->where('paragraph',end($path)['paragraph'])
  180. ->value('title');
  181. $path[] = ["book"=>0,"paragraph"=>0,"title"=>$bookTitle,"level"=>0];
  182. }
  183. # 将路径反向
  184. $path1 = [];
  185. for ($i=count($path)-1; $i >=0 ; $i--) {
  186. # code...
  187. $path1[] = $path[$i];
  188. }
  189. $newData['path'] = $path1;
  190. PaliText::where('book',$book)
  191. ->where('paragraph',$paragraph)
  192. ->update($newData);
  193. if ($curr_level > 0 && $curr_level < 8) {
  194. $paragraph_info[] = array($book, $paragraph, $length, $prev, $next, $parent);
  195. }
  196. }
  197. $bar->advance();
  198. }
  199. $bar->finish();
  200. $this->info("instert pali text finished. in ". time()-$startTime . "s" );
  201. Log::info("instert pali text finished. in ". time()-$startTime . "s");
  202. return 0;
  203. }
  204. }