paliword_sc.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. <?php
  2. //全文搜索
  3. require_once '../path.php';
  4. require_once '../public/casesuf.inc';
  5. require_once '../public/union.inc';
  6. require_once "../public/_pdo.php";
  7. require_once "../public/load_lang.php"; //语言文件
  8. require_once "../public/function.php";
  9. require_once "../search/word_function.php";
  10. require_once "../db/pali_text.php";
  11. $_redis = redis_connect();
  12. $_dbPaliText = new PaliText($_redis);
  13. _load_book_index();
  14. $op = $_GET["op"];
  15. $word = mb_strtolower($_GET["key"], 'UTF-8');
  16. $org_word = $word;
  17. $arrWordList = str_getcsv($word, " ");
  18. $count_return = 0;
  19. $dict_list = array();
  20. global $PDO;
  21. function microtime_float()
  22. {
  23. list($usec, $sec) = explode(" ", microtime());
  24. return ((float) $usec + (float) $sec);
  25. }
  26. $result = array();
  27. $result["error"] = "";
  28. $_start = microtime(true);
  29. $result["time"][] = array("event" => "begin", "time" => $_start);
  30. $_pagesize = 20;
  31. if (isset($_GET["page"])) {
  32. $_page = (int) $_GET["page"];
  33. } else {
  34. $_page = 0;
  35. }
  36. if (count($arrWordList) > 1) {
  37. # 查询多个词
  38. $out_data = array();
  39. /*
  40. PDO_Connect(_FILE_DB_PALITEXT_);
  41. # 首先精确匹配
  42. $words = implode(" ", $arrWordList);
  43. $query = "SELECT book,paragraph, text FROM "._TABLE_PALI_TEXT_." WHERE text like ? LIMIT ? OFFSET ?";
  44. $Fetch1 = PDO_FetchAll($query, array("%{$words}%", $_pagesize, $_page * $_pagesize));
  45. */
  46. $dns = _DB_ENGIN_.":host="._DB_HOST_.";port="._DB_PORT_.";dbname="._DB_NAME_.";user="._DB_USERNAME_.";password="._DB_PASSWORD_.";";
  47. PDO_Connect(_FILE_DB_PALITEXT_,_DB_USERNAME_,_DB_PASSWORD_);
  48. $query = "SELECT ts_rank('{0.1, 0.2, 0.4, 1}', full_text_search_weighted, websearch_to_tsquery('pali', ?)) AS rank, book,paragraph,content as text FROM fts WHERE full_text_search_weighted @@ websearch_to_tsquery('pali', ?) ORDER BY rank DESC LIMIT 20";
  49. $Fetch1 = PDO_FetchAll($query, array($word, $word));
  50. foreach ($Fetch1 as $key => $value) {
  51. # code...
  52. $newRecode["title"] = $_dbPaliText->getTitle($value["book"], $value["paragraph"]);
  53. $newRecode["path"] = _get_para_path($value["book"], $value["paragraph"]);
  54. $newRecode["book"] = $value["book"];
  55. $newRecode["para"] = $value["paragraph"];
  56. $newRecode["palitext"] = $value["text"];
  57. $newRecode["keyword"] = $arrWordList;
  58. $newRecode["wt"] = 0;
  59. $out_data[] = $newRecode;
  60. }
  61. $result["time"][] = array("event" => "fts精确匹配结束", "time" => microtime(true)-$_start);
  62. /*
  63. #然后查分散的
  64. $strQuery = "";
  65. foreach ($arrWordList as $oneword) {
  66. $strQuery .= "\"text\" like \"% {$oneword} %\" AND";
  67. }
  68. $strQuery = substr($strQuery, 0, -3);
  69. $query = "SELECT book,paragraph, html FROM pali_text WHERE {$strQuery} LIMIT 0,20";
  70. $Fetch2 = PDO_FetchAll($query);
  71. foreach ($Fetch2 as $key => $value) {
  72. # code...
  73. $newRecode["title"] = $_dbPaliText->getTitle($value["book"], $value["paragraph"]);
  74. $newRecode["path"] = _get_para_path($value["book"], $value["paragraph"]);
  75. $newRecode["book"] = $value["book"];
  76. $newRecode["para"] = $value["paragraph"];
  77. $newRecode["palitext"] = $value["text"];
  78. $newRecode["keyword"] = $arrWordList;
  79. $newRecode["wt"] = 0;
  80. $out_data[] = $newRecode;
  81. }
  82. $result["time"][] = array("event" => "查分散的结束", "time" => microtime(true)-$_start);
  83. */
  84. $result["data"] = $out_data;
  85. echo json_encode($result, JSON_UNESCAPED_UNICODE);
  86. # 然后查特别不精确的
  87. exit;
  88. }
  89. //计算某词在三藏中出现的次数
  90. $time_start = microtime_float();
  91. $arrRealWordList = countWordInPali($word);
  92. $countWord = count($arrRealWordList);
  93. $result["time"][] = array("event" => "计算某词在三藏中出现的次数", "time" => microtime(true) - $_start);
  94. if ($countWord == 0) {
  95. #没查到 模糊查询
  96. PDO_Connect(_FILE_DB_PALITEXT_);
  97. $query = "SELECT book,paragraph, text FROM "._TABLE_PALI_TEXT_." WHERE text like ? LIMIT ? OFFSET ?";
  98. $Fetch = PDO_FetchAll($query, array("%{$word}%", $_pagesize, $_page * $_pagesize));
  99. $result["data"] = $Fetch;
  100. exit;
  101. }
  102. $strQueryWordId = "("; //实际出现的单词id查询字串
  103. $aQueryWordList = array(); //id 为键 拼写为值的数组
  104. $aInputWordList = array(); //id 为键 拼写为值的数组 该词是否被选择
  105. $aShowWordList = array(); //拼写为键 个数为值的数组
  106. $aShowWordIdList = array(); //拼写为键 值Id的数组
  107. $arrQueryId=array();
  108. for ($i = 0; $i < $countWord; $i++) {
  109. $value = $arrRealWordList[$i];
  110. $strQueryWordId .= "'{$value["id"]}',";
  111. $arrQueryId[] = $value["id"];
  112. $aQueryWordList["{$value["id"]}"] = $value["word"];
  113. $aInputWordList["{$value["id"]}"] = false;
  114. $aShowWordList[$value["word"]] = $value["count"];
  115. $aShowWordIdList[$value["word"]] = $value["id"];
  116. }
  117. if (isset($_GET["words"])) {
  118. $word_selected = json_decode($_GET["words"]);
  119. if (count($word_selected) > 0) {
  120. $strQueryWordId = "(";
  121. foreach ($word_selected as $key => $value) {
  122. $strQueryWordId .= "'{$value}',";
  123. $aInputWordList["{$value}"] = true;
  124. $arrQueryId[] = $value;
  125. }
  126. }
  127. }
  128. $strQueryWordId = mb_substr($strQueryWordId, 0, mb_strlen($strQueryWordId, "UTF-8") - 1, "UTF-8");
  129. $strQueryWordId .= ")";
  130. $queryTime = (microtime_float() - $time_start) * 1000;
  131. //显示单词列表
  132. arsort($aShowWordList);
  133. $result["time"][] = array("event" => "单词列表排序结束", "time" => microtime(true) - $_start);
  134. $out_case = array();
  135. $word_count = 0;
  136. foreach ($aShowWordList as $x => $x_value) {
  137. $caseword = array();
  138. $caseword["id"] = $aShowWordIdList[$x];
  139. $caseword["spell"] = $x;
  140. $caseword["count"] = $x_value;
  141. $caseword["selected"] = $aInputWordList["{$aShowWordIdList[$x]}"];
  142. $word_count += $x_value;
  143. $out_case[] = $caseword;
  144. }
  145. $result["case"] = $out_case;
  146. $result["case_num"] = $countWord;
  147. $result["case_count"] = $word_count;
  148. //查找这些词出现在哪些书中
  149. $booklist = get_new_book_list($strQueryWordId);
  150. $result["book_list"] = $booklist;
  151. $result["book_tag"] = get_book_tag($strQueryWordId);
  152. $result["time"][] = array("event" => "查找书结束", "time" => microtime(true) - $_start);
  153. $wordInBookCounter = 0;
  154. $strFirstBookList = "(";
  155. foreach ($booklist as $onebook) {
  156. $wordInBookCounter += $onebook["count"];
  157. $strFirstBookList .= "'" . $onebook["book"] . "',";
  158. if ($wordInBookCounter >= 20) {
  159. break;
  160. }
  161. }
  162. $strFirstBookList = mb_substr($strFirstBookList, 0, mb_strlen($strFirstBookList, "UTF-8") - 1, "UTF-8");
  163. $strFirstBookList .= ")";
  164. $strQueryBookId = " ";
  165. if (isset($_GET["book"])) {
  166. $book_selected = json_decode($_GET["book"]);
  167. $bookSelected = array();
  168. if (count($book_selected) > 0) {
  169. $strQueryBookId = " AND book IN (";
  170. foreach ($book_selected as $key => $value) {
  171. $strQueryBookId .= "'{$value}',";
  172. $bookSelected[$value] = 1;
  173. }
  174. $strQueryBookId = mb_substr($strQueryBookId, 0, mb_strlen($strQueryBookId, "UTF-8") - 1, "UTF-8");
  175. $strQueryBookId .= ")";
  176. foreach ($result["book_list"] as $bookindex => $bookvalue) {
  177. # code...
  178. $bookid = $bookvalue["book"];
  179. if (isset($bookSelected["{$bookid}"])) {
  180. $result["book_list"][$bookindex]["selected"] = true;
  181. } else {
  182. $result["book_list"][$bookindex]["selected"] = false;
  183. }
  184. }
  185. }
  186. }
  187. $result["time"][] = array("event" => "准备查询", "time" => microtime(true) - $_start);
  188. //前20条记录
  189. $time_start = microtime_float();
  190. PDO_Connect(_FILE_DB_PALI_INDEX_);
  191. $query = "SELECT count(*) from (SELECT book FROM "._TABLE_WORD_." WHERE \"wordindex\" in $strQueryWordId $strQueryBookId group by book,paragraph) as qr where true ";
  192. $result["record_count"] = PDO_FetchOne($query);
  193. $result["time"][] = array("event" => "查询记录数", "time" => microtime(true) - $_start);
  194. $query = "SELECT book,paragraph, sum(weight) as wt FROM "._TABLE_WORD_." WHERE \"wordindex\" in $strQueryWordId $strQueryBookId GROUP BY book,paragraph ORDER BY wt DESC LIMIT ? OFFSET ?";
  195. $Fetch = PDO_FetchAll($query,array($_pagesize , $_page * $_pagesize));
  196. $result["time"][] = array("event" => "查询结束", "time" => microtime(true) - $_start);
  197. $out_data = array();
  198. $queryTime = (microtime_float() - $time_start) * 1000;
  199. $iFetch = count($Fetch);
  200. if ($iFetch > 0) {
  201. PDO_Connect(_FILE_DB_PALITEXT_);
  202. for ($i = 0; $i < $iFetch; $i++) {
  203. $newRecode = array();
  204. $paliword = array();
  205. foreach ($arrQueryId as $value) {
  206. # code...
  207. $paliword[] = $aQueryWordList["{$value}"];
  208. }
  209. $book = $Fetch[$i]["book"];
  210. $paragraph = $Fetch[$i]["paragraph"];
  211. $bookInfo = _get_book_info($book);
  212. $bookname = $bookInfo->title;
  213. $c1 = $bookInfo->c1;
  214. $c2 = $bookInfo->c2;
  215. $c3 = $bookInfo->c3;
  216. $path_1 = $c1 . ">";
  217. if ($c2 !== "") {
  218. $path_1 = $path_1 . $c2 . ">";
  219. }
  220. if ($c3 !== "") {
  221. $path_1 = $path_1 . $c3 . ">";
  222. }
  223. $path_1 = $path_1 . "《{$bookname}》>";
  224. $query = "SELECT * from "._TABLE_PALI_TEXT_." where book = ? and paragraph = ? limit 1";
  225. $FetchPaliText = PDO_FetchAll($query,array($book,$paragraph));
  226. $countPaliText = count($FetchPaliText);
  227. if ($countPaliText > 0) {
  228. $path = "";
  229. $parent = $FetchPaliText[0]["parent"];
  230. $deep = 0;
  231. $sFirstParentTitle = "";
  232. //循环查找父标题 得到整条路径
  233. while ($parent > -1) {
  234. $query = "SELECT * from "._TABLE_PALI_TEXT_." where book = ? and paragraph = ? limit 1";
  235. $FetParent = PDO_FetchAll($query,array($book,$parent));
  236. $path = "{$FetParent[0]["toc"]}>{$path}";
  237. if ($sFirstParentTitle == "") {
  238. $sFirstParentTitle = $FetParent[0]["toc"];
  239. }
  240. $parent = $FetParent[0]["parent"];
  241. $deep++;
  242. if ($deep > 5) {
  243. break;
  244. }
  245. }
  246. $path = $path_1 . $path . "para. " . $paragraph;
  247. $newRecode["title"] = $sFirstParentTitle;
  248. $newRecode["path"] = $path;
  249. $newRecode["book"] = $book;
  250. $newRecode["para"] = $paragraph;
  251. $newRecode["palitext"] = $FetchPaliText[0]["html"];
  252. $newRecode["keyword"] = $paliword;
  253. $newRecode["wt"] = $Fetch[$i]["wt"];
  254. $out_data[] = $newRecode;
  255. }
  256. }
  257. }
  258. $result["time"][] = array("event" => "查询路径结束", "time" => microtime(true) - $_start);
  259. $result["data"] = $out_data;
  260. echo json_encode($result, JSON_UNESCAPED_UNICODE);