UpgradeSystemSummary.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use App\Services\SummaryService;
  5. use App\Models\PaliText;
  6. use Illuminate\Support\Facades\Log;
  7. class UpgradeSystemSummary extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'upgrade:sys.summary {book : The book ID to index data for} {--para= : index paragraph No. omit to all} {--test}';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = 'Command description';
  21. protected $summaryService;
  22. private $isTest = false;
  23. /**
  24. * Create a new command instance.
  25. *
  26. * @return void
  27. */
  28. public function __construct(SummaryService $summaryService)
  29. {
  30. parent::__construct();
  31. $this->summaryService = $summaryService;
  32. }
  33. /**
  34. * Execute the console command.
  35. *
  36. * @return int
  37. */
  38. public function handle()
  39. {
  40. $book = $this->argument('book');
  41. $paragraph = $this->option('para');
  42. if ($this->option('test')) {
  43. $this->isTest = true;
  44. $this->info('test mode');
  45. }
  46. if ((int)$book === 0) {
  47. $maxBookId = PaliText::max('book');
  48. $booksId = range(1, $maxBookId);
  49. } else {
  50. $booksId = [$book];
  51. }
  52. foreach ($booksId as $key => $bookId) {
  53. $this->summarize($bookId, $paragraph);
  54. }
  55. return 0;
  56. }
  57. /**
  58. * Index Pali paragraphs for a given book.
  59. *
  60. * @param int $book
  61. * @return int
  62. */
  63. protected function summarize($book, $paragraph = null)
  64. {
  65. $this->info("Starting to index paragraphs for book: $book");
  66. $total = 0;
  67. if ($paragraph) {
  68. $paragraphs = PaliText::where('book', $book)
  69. ->where('paragraph', $paragraph)
  70. ->orderBy('paragraph')->cursor();
  71. } else {
  72. $paragraphs = PaliText::where('book', $book)
  73. ->orderBy('paragraph')->cursor();
  74. }
  75. $commentaryId = '';
  76. $chapterContent = array();
  77. $chapterStart = 0;
  78. $chapterEnd = 0;
  79. foreach ($paragraphs as $key => $para) {
  80. $total++;
  81. $content = $para->text;
  82. if ($para->level < 8) {
  83. $start = $para->paragraph;
  84. $end = $start + $para->chapter_len - 1;
  85. $subChapter = PaliText::whereBetween('paragraph', [$start, $end])
  86. ->where('level', '<', 8)
  87. ->count();
  88. if ($subChapter === 0) {
  89. $chapterStart = $start;
  90. $chapterEnd = $end;
  91. $chapterContent = [];
  92. } else {
  93. $chapterStart = 0;
  94. $chapterEnd = 0;
  95. }
  96. } else {
  97. $summary = $this->summaryService->summarize($para->text);
  98. $this->info("{$para['book']}-[{$para['paragraph']}]-{$summary}");
  99. if ($chapterStart !== 0) {
  100. $chapterContent[] = $summary;
  101. }
  102. if ($para->paragraph === $chapterEnd) {
  103. $chapterSummary = $this->summaryService->summarize(implode(' ', $chapterContent));
  104. $this->info("{$para['book']}-[{$chapterStart}]-{$chapterSummary}");
  105. $chapterStart = 0;
  106. $chapterEnd = 0;
  107. $chapterContent = [];
  108. }
  109. }
  110. }
  111. $this->info("Successfully indexed $total paragraphs for book: $book");
  112. Log::info("Indexed $total paragraphs for book: $book");
  113. return 0;
  114. }
  115. }