paliword_sc.php 9.4 KB

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