PaliSearch.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace App\Tools;
  3. use Illuminate\Support\Str;
  4. use Illuminate\Support\Facades\Log;
  5. class PaliSearch
  6. {
  7. public static function search($words,$books,$matchMode='case',$index=0,$size=10){
  8. $host = config('mint.server.rpc.tulip');
  9. Log::debug('tulip host='.$host);
  10. $client = new \Mint\Tulip\V1\SearchClient($host, [
  11. 'credentials' => \Grpc\ChannelCredentials::createInsecure(),
  12. ]);
  13. $request = new \Mint\Tulip\V1\SearchRequest();
  14. $request->setKeywords($words);
  15. $request->setBooks($books);
  16. $request->setMatchMode($matchMode);
  17. $page = new \Mint\Tulip\V1\SearchRequest\Page;
  18. $page->setIndex($index);
  19. $page->setSize($size);
  20. $request->setPage($page);
  21. list($response, $status) = $client->Pali($request)->wait();
  22. if ($status->code !== \Grpc\STATUS_OK) {
  23. Log::error("ERROR: " . $status->code . ", " . $status->details);
  24. return false;
  25. }
  26. $output = [];
  27. $output['total'] = $response->getTotal();
  28. $output['rows'] = [];
  29. foreach ($response->getItems() as $key => $value) {
  30. $output['rows'][] = (object)[
  31. 'rank' => $value->getRank(),
  32. 'highlight' => $value->getHighlight(),
  33. 'book' => $value->getBook(),
  34. 'paragraph' => $value->getParagraph(),
  35. 'content' => $value->getContent(),
  36. ];
  37. }
  38. return $output;
  39. }
  40. public static function book_list($words,$books,$matchMode='case',$index=0,$size=10){
  41. $host = config('mint.server.rpc.tulip');
  42. Log::debug('tulip host='.$host);
  43. $client = new \Mint\Tulip\V1\SearchClient($host, [
  44. 'credentials' => \Grpc\ChannelCredentials::createInsecure(),
  45. ]);
  46. $request = new \Mint\Tulip\V1\SearchRequest();
  47. $request->setKeywords($words);
  48. $request->setBooks($books);
  49. $request->setMatchMode($matchMode);
  50. $page = new \Mint\Tulip\V1\SearchRequest\Page;
  51. $page->setIndex($index);
  52. $page->setSize($size);
  53. $request->setPage($page);
  54. list($response, $status) = $client->BookList($request)->wait();
  55. if ($status->code !== \Grpc\STATUS_OK) {
  56. Log::error("ERROR: " . $status->code . ", " . $status->details);
  57. return false;
  58. }
  59. $output = [];
  60. $output['rows'] = [];
  61. foreach ($response->getItems() as $key => $value) {
  62. $output['rows'][] = (object)[
  63. 'pcd_book_id' => $value->getBook(),
  64. 'co' => $value->getCount(),
  65. ];
  66. }
  67. return $output;
  68. }
  69. public static function upload_dict($data){
  70. $host = config('mint.server.rpc.tulip');
  71. Log::debug('tulip host='.$host);
  72. $client = new \Mint\Tulip\V1\SearchClient($host, [
  73. 'credentials' => \Grpc\ChannelCredentials::createInsecure(),
  74. ]);
  75. $request = new \Mint\Tulip\V1\UploadDictionaryRequest();
  76. $request->setData($data);
  77. list($response, $status) = $client->UploadDictionary($request)->wait();
  78. if ($status->code !== \Grpc\STATUS_OK) {
  79. Log::error("ERROR: " . $status->code . ", " . $status->details);
  80. return false;
  81. }
  82. return $response->getError();
  83. }
  84. public static function update($book,$paragraph,
  85. $bold1,$bold2,$bold3,
  86. $content,$pcd_book_id){
  87. $host = config('mint.server.rpc.tulip');
  88. Log::debug('tulip host='.$host);
  89. $client = new \Mint\Tulip\V1\SearchClient($host, [
  90. 'credentials' => \Grpc\ChannelCredentials::createInsecure(),
  91. ]);
  92. $request = new \Mint\Tulip\V1\UpdateRequest();
  93. $request->setBook($book);
  94. $request->setParagraph($paragraph);
  95. $request->setLevel(0);
  96. $request->setBold1($bold1);
  97. $request->setBold2($bold2);
  98. $request->setBold3($bold3);
  99. $request->setContent($content);
  100. $request->setPcdBookId($pcd_book_id);
  101. list($response, $status) = $client->Update($request)->wait();
  102. if ($status->code !== \Grpc\STATUS_OK) {
  103. Log::error("ERROR: " . $status->code . ", " . $status->details);
  104. return false;
  105. }
  106. return $response->getCount();
  107. }
  108. }