paliword_sc.php 9.5 KB

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