2
0

paliword_sc.php 9.8 KB

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