SearchPaliDataService.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 book ID
  27. $pcd_book = BookTitle::where('book', $book)
  28. ->where('paragraph', '<=', $iPara)
  29. ->orderBy('paragraph', 'desc')
  30. ->first();
  31. $pcd_book_id = $pcd_book ? $pcd_book->sn : BookTitle::where('book', $book)
  32. ->orderBy('paragraph')
  33. ->value('sn');
  34. $output[] = [
  35. 'uid' => PaliText::where('book', $book)->where('paragraph', $iPara)->value('uid'),
  36. 'book' => $book,
  37. 'paragraph' => $iPara,
  38. 'bold1' => implode(' ', $content['bold1']),
  39. 'bold2' => implode(' ', $content['bold2']),
  40. 'bold3' => implode(' ', $content['bold3']),
  41. 'content' => $content['markdown'],
  42. 'markdown' => $content['markdown'],
  43. 'text' => $content['text'],
  44. 'pcd_book_id' => $pcd_book_id
  45. ];
  46. }
  47. return ['rows' => $output, 'count' => $maxParagraph];
  48. }
  49. /**
  50. * Generate content string for a given book and paragraph.
  51. *
  52. * @param int $book
  53. * @param int $para
  54. * @return string
  55. */
  56. private function getContent($book, $para)
  57. {
  58. $words = WbwTemplate::where('book', $book)
  59. ->where('paragraph', $para)
  60. ->where('type', '<>', '.ctl.')
  61. ->orderBy('wid')
  62. ->get();
  63. $content = '';
  64. foreach ($words as $word) {
  65. if ($word->style === 'bld') {
  66. if (strpos($word->word, '{') === false) {
  67. $content .= "**{$word->word}** ";
  68. } else {
  69. $content .= str_replace(['{', '}'], ['**', '** '], $word->word);
  70. }
  71. } elseif ($word->style === 'note') {
  72. $content .= " _{$word->word}_ ";
  73. } else {
  74. $content .= $word->word . ' ';
  75. }
  76. }
  77. return trim($content);
  78. }
  79. /**
  80. * Generate paragraph sentence list for a given book and paragraph.
  81. *
  82. * @param int $book
  83. * @param int $para
  84. * @return array $sentences
  85. */
  86. public function getParaContent($book, $para)
  87. {
  88. $sentences = PaliSentence::where('book', $book)
  89. ->where('paragraph', $para)
  90. ->orderBy('word_begin')
  91. ->get();
  92. if (!$sentences) {
  93. return null;
  94. }
  95. $markdown = [];
  96. $text = [];
  97. $wordList = [];
  98. foreach ($sentences as $key => $sentence) {
  99. $content = $this->getSentenceText($book, $para, $sentence->word_begin, $sentence->word_end);
  100. $id = "{$book}-{$para}-{$sentence->word_begin}-{$sentence->word_end}";
  101. $markdown[] = "`id:{$id}`" . $content['markdown'];
  102. $text[] = $content['text'];
  103. $wordList = array_merge($wordList, $content['words']);
  104. }
  105. // Retrieve bold words
  106. $words = WbwTemplate::where('book', $book)
  107. ->where('paragraph', $para)
  108. ->orderBy('wid')
  109. ->get();
  110. $bold1 = [];
  111. $bold2 = [];
  112. $bold3 = [];
  113. $currBold = [];
  114. foreach ($words as $word) {
  115. if ($word->type === '.ctl.') {
  116. //检测义注段落号
  117. if (preg_match('/^para\d+_[a-zA-Z].*$/', $word->real)) {
  118. $commentary = $word->real;
  119. }
  120. } else {
  121. if ($word->style === 'bld') {
  122. $currBold[] = $word->real;
  123. } else {
  124. $countBold = count($currBold);
  125. if ($countBold === 1) {
  126. $bold1[] = $currBold[0];
  127. } elseif ($countBold === 2) {
  128. $bold2 = array_merge($bold2, $currBold);
  129. } elseif ($countBold > 0) {
  130. $bold3 = array_merge($bold3, $currBold);
  131. }
  132. $currBold = [];
  133. }
  134. }
  135. }
  136. $data = [
  137. 'markdown' => implode("\n", $markdown),
  138. 'text' => implode(" ", $text),
  139. 'words' => $wordList,
  140. 'bold1' => $bold1,
  141. 'bold2' => $bold2,
  142. 'bold3' => $bold3,
  143. ];
  144. if (isset($commentary)) {
  145. $data['commentary'] = $commentary;
  146. }
  147. return $data;
  148. }
  149. /**
  150. * Generate paragraph sentence list for a given book and paragraph.
  151. *
  152. * @param int $book
  153. * @param int $para
  154. * @return array $sentence
  155. */
  156. public function getSentenceText($book, $para, $start, $end)
  157. {
  158. $words = WbwTemplate::where('book', $book)
  159. ->where('paragraph', $para)
  160. ->where('type', '<>', '.ctl.')
  161. ->whereBetween('wid', [$start, $end])
  162. ->orderBy('wid')
  163. ->get();
  164. $arrText = [];
  165. $markdown = '';
  166. $wordList = [];
  167. foreach ($words as $word) {
  168. $arrText[] = str_replace(['{', '}'], ['', ''], $word->word);
  169. $wordList[] = $word->real;
  170. if ($word->style === 'bld') {
  171. if (strpos($word->word, '{') === false) {
  172. $markdown .= "**{$word->word}** ";
  173. } else {
  174. $markdown .= str_replace(['{', '}'], ['**', '**'], $word->word) . ' ';
  175. }
  176. } elseif ($word->style === 'note') {
  177. $markdown .= " ~~{$word->word}~~ ";
  178. } else {
  179. $markdown .= $word->word . ' ';
  180. }
  181. }
  182. $markdown = str_replace([' ti', ' ,', ' .', ' ?', '‘ ‘ ', ' ’ ’'], ['ti', ',', '.', '?', '‘‘', '’’'], $markdown);
  183. $markdown = str_replace(['~~ ~~', '** **'], [' ', ' '], $markdown);
  184. $text = implode(' ', $arrText);
  185. $text = str_replace([' ti', ' ,', ' .', ' ?'], ['ti', ',', '.', '?'], $text);
  186. return [
  187. 'markdown' => $this->abbrReplace(trim($markdown)),
  188. 'text' => $this->abbrReplace($text),
  189. 'words' => $wordList,
  190. ];
  191. }
  192. private function abbrReplace($input)
  193. {
  194. $abbr = ['sī .', 'syā .', 'kaṃ .', 'pī .'];
  195. $abbrTo = ['sī.', 'syā.', 'kaṃ.', 'pī.'];
  196. return str_replace($abbr, $abbrTo, $input);
  197. }
  198. }