TipitakaWordParagraphService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. namespace App\Services\V3;
  3. use App\Models\PageNumber;
  4. use App\Models\PaliText;
  5. use App\Models\WbwTemplate;
  6. use App\Services\PaliSeriesesService;
  7. use Illuminate\Contracts\Database\Eloquent\Builder;
  8. use Illuminate\Pagination\LengthAwarePaginator;
  9. use Illuminate\Support\Collection;
  10. /**
  11. * 按词形找三藏段落(逐词表 `wbw_templates`:连音已拆开,每个词形精确匹配)。
  12. *
  13. * 与 OpenSearch 全文检索(`/v3/search`)互补:这里召回精确到词形、能区分黑体(注释书里
  14. * 被解释的词),但不做模糊、不做多词共现。
  15. *
  16. * v2 `search-pali-wbw` 每行另查 3~4 次库(段落、标题、页码、丛书缩写),这里整页批量取。
  17. */
  18. class TipitakaWordParagraphService
  19. {
  20. /** page_numbers.type 代号 → 对外的版本名(与 v2 ref.type 相同,插件按它拼出处) */
  21. private const EDITIONS = ['M' => 'My', 'P' => 'PTS', 'V' => 'VRI', 'T' => 'Thai', 'O' => 'Other'];
  22. public function __construct(
  23. private readonly PaliSeriesesService $serieses,
  24. private readonly TipitakaBookService $books,
  25. ) {}
  26. /**
  27. * 命中段落,按命中权重降序(同分按经文顺序)。
  28. *
  29. * @param array{words: list<string>, bold: string|null, book_ids: list<int>} $criteria
  30. * @return LengthAwarePaginator<int, array{
  31. * book: int, para: int, score: int, hits: int, title: string, level: int,
  32. * path: list<array{book: int, para: int, title: string, level: int}>,
  33. * text: string,
  34. * refs: list<array{type: string, volume: int|null, page: int, title: string|null}>
  35. * }>
  36. */
  37. public function paragraphs(array $criteria, int $perPage): LengthAwarePaginator
  38. {
  39. $page = $this->matches($criteria)
  40. ->when($criteria['book_ids'] !== [], fn (Builder $q) => $q->whereIn('pcd_book_id', $criteria['book_ids']))
  41. ->groupBy(['book', 'paragraph'])
  42. ->selectRaw('book, paragraph, sum(weight) as score, count(*) as hits')
  43. ->orderByDesc('score')
  44. ->orderBy('book')
  45. ->orderBy('paragraph')
  46. ->paginate($perPage);
  47. $rows = collect($page->items());
  48. $texts = $this->paliTexts($rows);
  49. $refs = $this->pageNumbers($rows);
  50. $highlighter = $this->highlighter($criteria['words']);
  51. return $page->setCollection($rows->map(function ($row) use ($texts, $refs, $highlighter) {
  52. $book = (int) $row->book;
  53. $para = (int) $row->paragraph;
  54. $text = $texts->get("{$book}-{$para}");
  55. $path = self::path($text?->path);
  56. $series = $this->serieses->find($book, $para);
  57. return [
  58. 'book' => $book,
  59. 'para' => $para,
  60. 'score' => (int) $row->score,
  61. 'hits' => (int) $row->hits,
  62. // 标题段(level < 100)用自己的标题,正文段用所在章节的标题
  63. 'title' => $text && $text->level < 100 ? (string) $text->toc : (string) (end($path)['title'] ?? ''),
  64. 'level' => (int) ($text?->level ?? 100),
  65. 'path' => $path,
  66. 'text' => $highlighter((string) ($text?->html ?? '')),
  67. 'refs' => [
  68. ...array_map(fn (array $r) => $r + ['title' => match ($r['type']) {
  69. 'My' => $series['abbr_my'] ?? null,
  70. 'PTS' => $series['abbr_pts'] ?? null,
  71. default => null,
  72. }], $refs->get("{$book}-{$para}", [])),
  73. ['type' => 'WP', 'volume' => null, 'page' => $para, 'title' => $series['abbr_wp'] ?? null],
  74. ],
  75. ];
  76. }));
  77. }
  78. /**
  79. * 按书统计:每本书(pcd_book_id)命中多少段、多少词次。按段落数降序。
  80. *
  81. * @param array{words: list<string>, bold: string|null} $criteria
  82. * @return array{
  83. * items: list<array{book_id: int, book: int, para: int, title: string, layer: string|null, tags: list<string>, paragraphs: int, occurrences: int}>,
  84. * paragraphs: int,
  85. * occurrences: int
  86. * }
  87. */
  88. public function books(array $criteria): array
  89. {
  90. $groups = $this->matches($criteria)
  91. ->groupBy('pcd_book_id')
  92. ->selectRaw('pcd_book_id, count(*) as occurrences, count(distinct (book, paragraph)) as paragraphs')
  93. ->orderByDesc('paragraphs')
  94. ->orderBy('pcd_book_id')
  95. ->get();
  96. $books = $this->books->find($groups->pluck('pcd_book_id'));
  97. $items = $groups->map(function ($group) use ($books) {
  98. $book = $books->get((int) $group->pcd_book_id);
  99. return [
  100. 'book_id' => (int) $group->pcd_book_id,
  101. 'book' => $book['book'] ?? 0,
  102. 'para' => $book['para'] ?? 0,
  103. 'title' => $book['title'] ?? '',
  104. 'layer' => $book['layer'] ?? null,
  105. 'tags' => $book['tags'] ?? [],
  106. 'paragraphs' => (int) $group->paragraphs,
  107. 'occurrences' => (int) $group->occurrences,
  108. ];
  109. })->values()->all();
  110. return [
  111. 'items' => $items,
  112. 'paragraphs' => array_sum(array_column($items, 'paragraphs')),
  113. 'occurrences' => array_sum(array_column($items, 'occurrences')),
  114. ];
  115. }
  116. /**
  117. * 词形与黑体条件。
  118. *
  119. * @param array{words: list<string>, bold: string|null} $criteria
  120. */
  121. private function matches(array $criteria): Builder
  122. {
  123. return WbwTemplate::query()
  124. ->whereIn('real', $criteria['words'])
  125. ->when($criteria['bold'] === 'only', fn (Builder $q) => $q->where('style', 'bld'))
  126. ->when($criteria['bold'] === 'exclude', fn (Builder $q) => $q->where(
  127. fn (Builder $q) => $q->where('style', '<>', 'bld')->orWhereNull('style'),
  128. ));
  129. }
  130. /**
  131. * 本页段落的正文与路径,一次查完。
  132. *
  133. * @param Collection<int, mixed> $rows
  134. * @return Collection<string, PaliText>
  135. */
  136. private function paliTexts(Collection $rows): Collection
  137. {
  138. if ($rows->isEmpty()) {
  139. return collect();
  140. }
  141. return PaliText::query()
  142. ->where(function (Builder $q) use ($rows) {
  143. foreach ($rows as $row) {
  144. $q->orWhere(fn (Builder $q) => $q->where('book', $row->book)->where('paragraph', $row->paragraph));
  145. }
  146. })
  147. ->get(['book', 'paragraph', 'level', 'toc', 'path', 'html'])
  148. ->keyBy(fn (PaliText $t) => "{$t->book}-{$t->paragraph}");
  149. }
  150. /**
  151. * 本页段落在各印本里的页码,每种版本取段落里第一个页码标记。一次查完。
  152. *
  153. * @param Collection<int, mixed> $rows
  154. * @return Collection<string, list<array{type: string, volume: int|null, page: int}>>
  155. */
  156. private function pageNumbers(Collection $rows): Collection
  157. {
  158. if ($rows->isEmpty()) {
  159. return collect();
  160. }
  161. return PageNumber::query()
  162. ->where(function (Builder $q) use ($rows) {
  163. foreach ($rows as $row) {
  164. $q->orWhere(fn (Builder $q) => $q->where('book', $row->book)->where('paragraph', $row->paragraph));
  165. }
  166. })
  167. ->orderBy('wid')
  168. ->get(['type', 'volume', 'page', 'book', 'paragraph'])
  169. ->groupBy(fn (PageNumber $p) => "{$p->book}-{$p->paragraph}")
  170. ->map(fn (Collection $pages) => $pages->unique('type')->map(fn (PageNumber $p) => [
  171. 'type' => self::EDITIONS[$p->type] ?? (string) $p->type,
  172. 'volume' => $p->volume === null ? null : (int) $p->volume,
  173. 'page' => (int) $p->page,
  174. ])->values()->all());
  175. }
  176. /**
  177. * `pali_texts.path`(JSON 字符串,level 有时是字符串)→ 规整的数组。
  178. *
  179. * @return list<array{book: int, para: int, title: string, level: int}>
  180. */
  181. private static function path(mixed $raw): array
  182. {
  183. $items = is_string($raw) ? json_decode($raw, true) : $raw;
  184. if (! is_array($items)) {
  185. return [];
  186. }
  187. return array_values(array_map(fn (array $p) => [
  188. 'book' => (int) ($p['book'] ?? 0),
  189. 'para' => (int) ($p['paragraph'] ?? 0),
  190. 'title' => (string) ($p['title'] ?? ''),
  191. 'level' => (int) ($p['level'] ?? 0),
  192. ], array_filter($items, 'is_array')));
  193. }
  194. /**
  195. * 正文 HTML → 只含 `<b>`(黑体)与 `<mark>`(命中词形)的安全 HTML。
  196. *
  197. * 其余标签全部去掉、正文全部转义,客户端可以直接作为 HTML 渲染。命中词按整词匹配
  198. * (前后不是字母),不区分首字母大小写;词形以 ti / nti 结尾的,正文里常写成
  199. * 「词’’ti」,所以也匹配去掉 ti 后的部分(同 v2)。
  200. *
  201. * @param list<string> $words
  202. * @return \Closure(string): string
  203. */
  204. private function highlighter(array $words): \Closure
  205. {
  206. $variants = [];
  207. foreach ($words as $word) {
  208. $variants[] = $word;
  209. if (str_ends_with($word, 'nti')) {
  210. $variants[] = mb_substr($word, 0, -3, 'UTF-8');
  211. } elseif (str_ends_with($word, 'ti')) {
  212. $variants[] = mb_substr($word, 0, -2, 'UTF-8');
  213. }
  214. }
  215. $variants = array_filter(array_unique($variants), fn (string $v) => mb_strlen($v, 'UTF-8') > 1);
  216. usort($variants, fn (string $a, string $b) => mb_strlen($b, 'UTF-8') <=> mb_strlen($a, 'UTF-8'));
  217. $alternatives = implode('|', array_map(fn (string $v) => preg_quote(htmlspecialchars($v, ENT_QUOTES, 'UTF-8'), '/'), $variants));
  218. $pattern = $alternatives === '' ? null : '/(?<![\p{L}\p{M}])('.$alternatives.')(?![\p{L}\p{M}])/iu';
  219. return function (string $html) use ($pattern): string {
  220. // 黑体先换成占位符,去标签、解实体、整体转义,再换回 <b>
  221. $text = preg_replace('/<span class=["\']bld["\']>(.*?)<\/span>/su', "\u{E000}$1\u{E001}", $html) ?? '';
  222. $text = htmlspecialchars(trim(html_entity_decode(strip_tags($text), ENT_QUOTES | ENT_HTML5, 'UTF-8')), ENT_QUOTES, 'UTF-8');
  223. if ($pattern !== null) {
  224. $text = preg_replace($pattern, '<mark>$1</mark>', $text) ?? $text;
  225. }
  226. return str_replace(["\u{E000}", "\u{E001}"], ['<b>', '</b>'], $text);
  227. };
  228. }
  229. }