paliword_sc.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. PDO_Connect(_FILE_DB_PALITEXT_);
  40. # 首先精确匹配
  41. $words = implode(" ", $arrWordList);
  42. $query = "SELECT book,paragraph, text FROM "._TABLE_PALI_TEXT_." WHERE text like ? LIMIT ? OFFSET ?";
  43. $Fetch1 = PDO_FetchAll($query, array("%{$words}%", $_pagesize, $_page * $_pagesize));
  44. foreach ($Fetch1 as $key => $value) {
  45. # code...
  46. $newRecode["title"] = $_dbPaliText->getTitle($value["book"], $value["paragraph"]);
  47. $newRecode["path"] = _get_para_path($value["book"], $value["paragraph"]);
  48. $newRecode["book"] = $value["book"];
  49. $newRecode["para"] = $value["paragraph"];
  50. $newRecode["palitext"] = $value["text"];
  51. $newRecode["keyword"] = $arrWordList;
  52. $newRecode["wt"] = 0;
  53. $out_data[] = $newRecode;
  54. }
  55. $result["time"][] = array("event" => "精确匹配结束", "time" => microtime(true)-$_start);
  56. /*
  57. #然后查分散的
  58. $strQuery = "";
  59. foreach ($arrWordList as $oneword) {
  60. $strQuery .= "\"text\" like \"% {$oneword} %\" AND";
  61. }
  62. $strQuery = substr($strQuery, 0, -3);
  63. $query = "SELECT book,paragraph, html FROM pali_text WHERE {$strQuery} LIMIT 0,20";
  64. $Fetch2 = PDO_FetchAll($query);
  65. foreach ($Fetch2 as $key => $value) {
  66. # code...
  67. $newRecode["title"] = $_dbPaliText->getTitle($value["book"], $value["paragraph"]);
  68. $newRecode["path"] = _get_para_path($value["book"], $value["paragraph"]);
  69. $newRecode["book"] = $value["book"];
  70. $newRecode["para"] = $value["paragraph"];
  71. $newRecode["palitext"] = $value["text"];
  72. $newRecode["keyword"] = $arrWordList;
  73. $newRecode["wt"] = 0;
  74. $out_data[] = $newRecode;
  75. }
  76. $result["time"][] = array("event" => "查分散的结束", "time" => microtime(true)-$_start);
  77. */
  78. $result["data"] = $out_data;
  79. echo json_encode($result, JSON_UNESCAPED_UNICODE);
  80. # 然后查特别不精确的
  81. exit;
  82. }
  83. //计算某词在三藏中出现的次数
  84. $time_start = microtime_float();
  85. $arrRealWordList = countWordInPali($word);
  86. $countWord = count($arrRealWordList);
  87. $result["time"][] = array("event" => "计算某词在三藏中出现的次数", "time" => microtime(true) - $_start);
  88. if ($countWord == 0) {
  89. #没查到 模糊查询
  90. PDO_Connect(_FILE_DB_PALITEXT_);
  91. $query = "SELECT book,paragraph, text FROM "._TABLE_PALI_TEXT_." WHERE text like ? LIMIT ? OFFSET ?";
  92. $Fetch = PDO_FetchAll($query, array("%{$word}%", $_pagesize, $_page * $_pagesize));
  93. $result["data"] = $Fetch;
  94. exit;
  95. }
  96. $strQueryWordId = "("; //实际出现的单词id查询字串
  97. $aQueryWordList = array(); //id 为键 拼写为值的数组
  98. $aInputWordList = array(); //id 为键 拼写为值的数组 该词是否被选择
  99. $aShowWordList = array(); //拼写为键 个数为值的数组
  100. $aShowWordIdList = array(); //拼写为键 值Id的数组
  101. for ($i = 0; $i < $countWord; $i++) {
  102. $value = $arrRealWordList[$i];
  103. $strQueryWordId .= "'{$value["id"]}',";
  104. $aQueryWordList["{$value["id"]}"] = $value["word"];
  105. $aInputWordList["{$value["id"]}"] = false;
  106. $aShowWordList[$value["word"]] = $value["count"];
  107. $aShowWordIdList[$value["word"]] = $value["id"];
  108. }
  109. if (isset($_GET["words"])) {
  110. $word_selected = json_decode($_GET["words"]);
  111. if (count($word_selected) > 0) {
  112. $strQueryWordId = "(";
  113. foreach ($word_selected as $key => $value) {
  114. $strQueryWordId .= "'{$value}',";
  115. $aInputWordList["{$value}"] = true;
  116. }
  117. }
  118. }
  119. $strQueryWordId = mb_substr($strQueryWordId, 0, mb_strlen($strQueryWordId, "UTF-8") - 1, "UTF-8");
  120. $strQueryWordId .= ")";
  121. $queryTime = (microtime_float() - $time_start) * 1000;
  122. //显示单词列表
  123. arsort($aShowWordList);
  124. $result["time"][] = array("event" => "单词列表排序结束", "time" => microtime(true) - $_start);
  125. $out_case = array();
  126. $word_count = 0;
  127. foreach ($aShowWordList as $x => $x_value) {
  128. $caseword = array();
  129. $caseword["id"] = $aShowWordIdList[$x];
  130. $caseword["spell"] = $x;
  131. $caseword["count"] = $x_value;
  132. $caseword["selected"] = $aInputWordList["{$aShowWordIdList[$x]}"];
  133. $word_count += $x_value;
  134. $out_case[] = $caseword;
  135. }
  136. $result["case"] = $out_case;
  137. $result["case_num"] = $countWord;
  138. $result["case_count"] = $word_count;
  139. //查找这些词出现在哪些书中
  140. $booklist = get_new_book_list($strQueryWordId);
  141. $result["book_list"] = $booklist;
  142. $result["book_tag"] = get_book_tag($strQueryWordId);
  143. $result["time"][] = array("event" => "查找书结束", "time" => microtime(true) - $_start);
  144. $wordInBookCounter = 0;
  145. $strFirstBookList = "(";
  146. foreach ($booklist as $onebook) {
  147. $wordInBookCounter += $onebook["count"];
  148. $strFirstBookList .= "'" . $onebook["book"] . "',";
  149. if ($wordInBookCounter >= 20) {
  150. break;
  151. }
  152. }
  153. $strFirstBookList = mb_substr($strFirstBookList, 0, mb_strlen($strFirstBookList, "UTF-8") - 1, "UTF-8");
  154. $strFirstBookList .= ")";
  155. $strQueryBookId = " ";
  156. if (isset($_GET["book"])) {
  157. $book_selected = json_decode($_GET["book"]);
  158. $bookSelected = array();
  159. if (count($book_selected) > 0) {
  160. $strQueryBookId = " AND book IN (";
  161. foreach ($book_selected as $key => $value) {
  162. $strQueryBookId .= "'{$value}',";
  163. $bookSelected[$value] = 1;
  164. }
  165. $strQueryBookId = mb_substr($strQueryBookId, 0, mb_strlen($strQueryBookId, "UTF-8") - 1, "UTF-8");
  166. $strQueryBookId .= ")";
  167. foreach ($result["book_list"] as $bookindex => $bookvalue) {
  168. # code...
  169. $bookid = $bookvalue["book"];
  170. if (isset($bookSelected["{$bookid}"])) {
  171. $result["book_list"][$bookindex]["selected"] = true;
  172. } else {
  173. $result["book_list"][$bookindex]["selected"] = false;
  174. }
  175. }
  176. }
  177. }
  178. $result["time"][] = array("event" => "准备查询", "time" => microtime(true) - $_start);
  179. //前20条记录
  180. $time_start = microtime_float();
  181. PDO_Connect(_FILE_DB_PALI_INDEX_);
  182. $query = "SELECT count(*) from (SELECT book FROM word WHERE \"wordindex\" in $strQueryWordId $strQueryBookId group by book,paragraph) where 1 ";
  183. $result["record_count"] = PDO_FetchOne($query);
  184. $result["time"][] = array("event" => "查询记录数", "time" => microtime(true) - $_start);
  185. $query = "SELECT book,paragraph, wordindex, sum(weight) as wt FROM word WHERE \"wordindex\" in $strQueryWordId $strQueryBookId GROUP BY book,paragraph ORDER BY wt DESC LIMIT ? OFFSET ?";
  186. $Fetch = PDO_FetchAll($query,array($_pagesize , $_page * $_pagesize));
  187. $result["time"][] = array("event" => "查询结束", "time" => microtime(true) - $_start);
  188. $out_data = array();
  189. $queryTime = (microtime_float() - $time_start) * 1000;
  190. $iFetch = count($Fetch);
  191. if ($iFetch > 0) {
  192. PDO_Connect(_FILE_DB_PALITEXT_);
  193. for ($i = 0; $i < $iFetch; $i++) {
  194. $newRecode = array();
  195. $paliwordid = $Fetch[$i]["wordindex"];
  196. $paliword = $aQueryWordList["{$paliwordid}"];
  197. $book = $Fetch[$i]["book"];
  198. $paragraph = $Fetch[$i]["paragraph"];
  199. $bookInfo = _get_book_info($book);
  200. $bookname = $bookInfo->title;
  201. $c1 = $bookInfo->c1;
  202. $c2 = $bookInfo->c2;
  203. $c3 = $bookInfo->c3;
  204. $path_1 = $c1 . ">";
  205. if ($c2 !== "") {
  206. $path_1 = $path_1 . $c2 . ">";
  207. }
  208. if ($c3 !== "") {
  209. $path_1 = $path_1 . $c3 . ">";
  210. }
  211. $path_1 = $path_1 . "《{$bookname}》>";
  212. $query = "SELECT * from "._TABLE_PALI_TEXT_." where book = ? and paragraph = ? limit 1";
  213. $FetchPaliText = PDO_FetchAll($query,array($book,$paragraph));
  214. $countPaliText = count($FetchPaliText);
  215. if ($countPaliText > 0) {
  216. $path = "";
  217. $parent = $FetchPaliText[0]["parent"];
  218. $deep = 0;
  219. $sFirstParentTitle = "";
  220. //循环查找父标题 得到整条路径
  221. while ($parent > -1) {
  222. $query = "SELECT * from "._TABLE_PALI_TEXT_." where book = ? and paragraph = ? limit 1";
  223. $FetParent = PDO_FetchAll($query,array($book,$parent));
  224. $path = "{$FetParent[0]["toc"]}>{$path}";
  225. if ($sFirstParentTitle == "") {
  226. $sFirstParentTitle = $FetParent[0]["toc"];
  227. }
  228. $parent = $FetParent[0]["parent"];
  229. $deep++;
  230. if ($deep > 5) {
  231. break;
  232. }
  233. }
  234. $path = $path_1 . $path . "para. " . $paragraph;
  235. $newRecode["title"] = $sFirstParentTitle;
  236. $newRecode["path"] = $path;
  237. $newRecode["book"] = $book;
  238. $newRecode["para"] = $paragraph;
  239. $newRecode["palitext"] = $FetchPaliText[0]["html"];
  240. $newRecode["keyword"] = array($paliword);
  241. $newRecode["wt"] = $Fetch[$i]["wt"];
  242. $out_data[] = $newRecode;
  243. }
  244. }
  245. }
  246. $result["time"][] = array("event" => "查询路径结束", "time" => microtime(true) - $_start);
  247. $result["data"] = $out_data;
  248. echo json_encode($result, JSON_UNESCAPED_UNICODE);