PaliWordFormService.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. namespace App\Services\V3;
  3. use App\Models\WordIndex;
  4. use App\Tools\CaseEnding;
  5. use App\Tools\CaseMan;
  6. /**
  7. * 巴利词形展开:一个词 → 它可能的词根 → 每个词根在三藏里实际出现过的词形(含连音 iti / api / ca / hi)。
  8. *
  9. * 词根推导用共享的 `CaseMan::WordToBase(..., verify: false)`(纯字符串运算,不查库);
  10. * 由词根生成词形的规则与 v2 `CaseMan::BaseToWord` 相同,但 v2 每个候选词形单独查一次
  11. * `word_indices`(一个词上百次),这里先生成全部候选,再一次 whereIn 取出现次数。
  12. */
  13. class PaliWordFormService
  14. {
  15. /** 词尾表里低于这个置信度的规则不用(与 v2 /case 相同) */
  16. private const CONFIDENCE = 0.2;
  17. public function __construct(private readonly CaseMan $caseMan) {}
  18. /**
  19. * @return list<array{base: string, forms: list<array{word: string, count: int, bold: int}>}>
  20. */
  21. public function groups(string $word): array
  22. {
  23. $word = mb_strtolower(trim($word), 'UTF-8');
  24. // 输入的词本身排第一组,其后是推导出的词根
  25. $bases = array_values(array_unique([$word, ...array_map('strval', array_keys($this->caseMan->WordToBase($word, 1, false)))]));
  26. $candidates = [];
  27. foreach ($bases as $base) {
  28. $candidates[$base] = $this->candidates($base);
  29. }
  30. $all = array_values(array_unique(array_merge(...array_values($candidates))));
  31. $counts = $all === []
  32. ? collect()
  33. : WordIndex::whereIn('word', $all)->get(['word', 'count', 'bold'])->keyBy('word');
  34. $groups = [];
  35. foreach ($candidates as $base => $words) {
  36. $forms = [];
  37. foreach ($words as $form) {
  38. if ($row = $counts->get($form)) {
  39. $forms[] = ['word' => $form, 'count' => (int) $row->count, 'bold' => (int) $row->bold];
  40. }
  41. }
  42. if ($forms === []) {
  43. continue;
  44. }
  45. usort($forms, fn (array $a, array $b) => $b['count'] <=> $a['count'] ?: strcmp($a['word'], $b['word']));
  46. $groups[] = ['base' => (string) $base, 'forms' => $forms];
  47. }
  48. return $groups;
  49. }
  50. /**
  51. * 由词根按词尾表生成候选词形,再对每个词形套连音规则。
  52. *
  53. * @return list<string>
  54. */
  55. private function candidates(string $base): array
  56. {
  57. $table = new CaseEnding;
  58. $words = [];
  59. foreach ($table->ending as [$from, $to, , , $confidence]) {
  60. if ($confidence < self::CONFIDENCE || ! str_ends_with($base, $from)) {
  61. continue;
  62. }
  63. $word = mb_substr($base, 0, mb_strlen($base, 'UTF-8') - mb_strlen($from, 'UTF-8'), 'UTF-8').$to;
  64. $words[$word] = true;
  65. foreach ($table->union as [$end, $joined]) {
  66. if (str_ends_with($word, $end)) {
  67. $words[mb_substr($word, 0, mb_strlen($word, 'UTF-8') - mb_strlen($end, 'UTF-8'), 'UTF-8').$joined] = true;
  68. }
  69. }
  70. }
  71. return array_map('strval', array_keys($words));
  72. }
  73. }