SearchPaliDataService.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. namespace App\Services;
  3. use App\Models\BookTitle;
  4. use App\Models\WbwTemplate;
  5. use App\Models\PaliText;
  6. use App\Models\PaliSentence;
  7. class SearchPaliDataService
  8. {
  9. /**
  10. * Retrieve paginated Pali data for search.
  11. *
  12. * @param int $book
  13. * @param int $start
  14. * @param int $pageSize
  15. * @return array
  16. */
  17. public function getPaliData($book, $start = 1, $pageSize = null)
  18. {
  19. $maxParagraph = WbwTemplate::where('book', $book)->max('paragraph');
  20. $output = [];
  21. $pageSize = $pageSize === null ? $maxParagraph : $pageSize;
  22. // Calculate end paragraph for pagination
  23. $endOfPara = min($start + $pageSize, $maxParagraph + 1);
  24. for ($iPara = $start; $iPara < $endOfPara; $iPara++) {
  25. $content = $this->getParaContent($book, $iPara);
  26. // Retrieve bold words
  27. $words = WbwTemplate::where('book', $book)
  28. ->where('paragraph', $iPara)
  29. ->orderBy('wid')
  30. ->get();
  31. $bold1 = [];
  32. $bold2 = [];
  33. $bold3 = [];
  34. $currBold = [];
  35. foreach ($words as $word) {
  36. if ($word->style === 'bld') {
  37. $currBold[] = $word->real;
  38. } else {
  39. $countBold = count($currBold);
  40. if ($countBold === 1) {
  41. $bold1[] = $currBold[0];
  42. } elseif ($countBold === 2) {
  43. $bold2 = array_merge($bold2, $currBold);
  44. } elseif ($countBold > 0) {
  45. $bold3 = array_merge($bold3, $currBold);
  46. }
  47. $currBold = [];
  48. }
  49. }
  50. // Handle any remaining bold words
  51. $countBold = count($currBold);
  52. if ($countBold === 1) {
  53. $bold1[] = $currBold[0];
  54. } elseif ($countBold === 2) {
  55. $bold2 = array_merge($bold2, $currBold);
  56. } elseif ($countBold > 0) {
  57. $bold3 = array_merge($bold3, $currBold);
  58. }
  59. // Retrieve book ID
  60. $pcd_book = BookTitle::where('book', $book)
  61. ->where('paragraph', '<=', $iPara)
  62. ->orderBy('paragraph', 'desc')
  63. ->first();
  64. $pcd_book_id = $pcd_book ? $pcd_book->sn : BookTitle::where('book', $book)
  65. ->orderBy('paragraph')
  66. ->value('sn');
  67. $output[] = [
  68. 'uid' => PaliText::where('book', $book)->where('paragraph', $iPara)->value('uid'),
  69. 'book' => $book,
  70. 'paragraph' => $iPara,
  71. 'bold1' => implode(' ', $bold1),
  72. 'bold2' => implode(' ', $bold2),
  73. 'bold3' => implode(' ', $bold3),
  74. 'content' => $content['markdown'],
  75. 'markdown' => $content['markdown'],
  76. 'text' => $content['text'],
  77. 'pcd_book_id' => $pcd_book_id
  78. ];
  79. }
  80. return ['rows' => $output, 'count' => $maxParagraph];
  81. }
  82. /**
  83. * Generate content string for a given book and paragraph.
  84. *
  85. * @param int $book
  86. * @param int $para
  87. * @return string
  88. */
  89. private function getContent($book, $para)
  90. {
  91. $words = WbwTemplate::where('book', $book)
  92. ->where('paragraph', $para)
  93. ->where('type', '<>', '.ctl.')
  94. ->orderBy('wid')
  95. ->get();
  96. $content = '';
  97. foreach ($words as $word) {
  98. if ($word->style === 'bld') {
  99. if (strpos($word->word, '{') === false) {
  100. $content .= "**{$word->word}** ";
  101. } else {
  102. $content .= str_replace(['{', '}'], ['**', '** '], $word->word);
  103. }
  104. } elseif ($word->style === 'note') {
  105. $content .= " _{$word->word}_ ";
  106. } else {
  107. $content .= $word->word . ' ';
  108. }
  109. }
  110. return trim($content);
  111. }
  112. /**
  113. * Generate paragraph sentence list for a given book and paragraph.
  114. *
  115. * @param int $book
  116. * @param int $para
  117. * @return array $sentences
  118. */
  119. public function getParaContent($book, $para)
  120. {
  121. $sentences = PaliSentence::where('book', $book)
  122. ->where('paragraph', $para)
  123. ->orderBy('word_begin')
  124. ->get();
  125. if (!$sentences) {
  126. return null;
  127. }
  128. $markdown = [];
  129. $text = [];
  130. foreach ($sentences as $key => $sentence) {
  131. $content = $this->getSentenceText($book, $para, $sentence->word_begin, $sentence->word_end);
  132. $id = "{$book}-{$para}-{$sentence->word_begin}-{$sentence->word_end}";
  133. $markdown[] = $content['markdown'];
  134. $text[] = $content['text'];
  135. }
  136. return [
  137. 'markdown' => implode("\n", $markdown),
  138. 'text' => implode("", $text),
  139. ];
  140. }
  141. /**
  142. * Generate paragraph sentence list for a given book and paragraph.
  143. *
  144. * @param int $book
  145. * @param int $para
  146. * @return array $sentence
  147. */
  148. private function getSentenceText($book, $para, $start, $end)
  149. {
  150. $words = WbwTemplate::where('book', $book)
  151. ->where('paragraph', $para)
  152. ->where('type', '<>', '.ctl.')
  153. ->whereBetween('wid', [$start, $end])
  154. ->orderBy('wid')
  155. ->get();
  156. $text = [];
  157. $markdown = '';
  158. foreach ($words as $word) {
  159. $text[] = str_replace(['{', '}'], ['', ''], $word->word);
  160. if ($word->style === 'bld') {
  161. if (strpos($word->word, '{') === false) {
  162. $markdown .= "**{$word->word}** ";
  163. } else {
  164. $markdown .= str_replace(['{', '}'], ['**', '** '], $word->word);
  165. }
  166. } elseif ($word->style === 'note') {
  167. $markdown .= " ~~{$word->word}~~ ";
  168. } else {
  169. $markdown .= $word->word . ' ';
  170. }
  171. }
  172. return [
  173. 'markdown' => $this->abbrReplace(trim(str_replace(['~~ ~~', '** **'], [' ', ' '], $markdown))),
  174. 'text' => $this->abbrReplace(implode(' ', $text)),
  175. ];
  176. }
  177. private function abbrReplace($input)
  178. {
  179. $abbr = ['sī .', 'syā .', 'kaṃ .', 'pī .'];
  180. $abbrTo = ['sī.', 'syā.', 'kaṃ.', 'pī.'];
  181. return str_replace($abbr, $abbrTo, $input);
  182. }
  183. }