paliword_sc.php 8.8 KB

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