paliword_sc.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. switch($op){
  26. case "pre"://预查询
  27. {
  28. $time_start = microtime_float();
  29. $searching=$arrWordList[count($arrWordList)-1];
  30. $dbfile = _FILE_DB_WORD_INDEX_;
  31. PDO_Connect("sqlite:".$dbfile);
  32. if(count($arrWordList)>1){
  33. //echo "<div>";
  34. foreach($arrWordList as $oneword){
  35. //echo $oneword."+";
  36. }
  37. //echo "</div>";
  38. }
  39. $query = "SELECT word,count,bold FROM wordindex WHERE word_en like ? OR word like ? limit 0,20";
  40. $Fetch = PDO_FetchAll($query,array("{$searching}%","{$searching}%"));
  41. echo json_encode($Fetch,JSON_UNESCAPED_UNICODE);
  42. break;
  43. }
  44. case "search":
  45. {
  46. $_pagesize = 20;
  47. if(isset($_GET["page"])){
  48. $_page = (int)$_GET["page"];
  49. }
  50. else{
  51. $_page=0;
  52. }
  53. if(count($arrWordList)>1){
  54. $dictFileName=_FILE_DB_PALITEXT_;
  55. PDO_Connect("sqlite:$dictFileName");
  56. # 首先精确匹配
  57. $words = implode(" ",$arrWordList);
  58. $query = "SELECT book,paragraph, text FROM pali_text WHERE text like ? LIMIT ? , ?";
  59. $Fetch = PDO_FetchAll($query,array("%{$words}%",$_page*$_pagesize,$_pagesize));
  60. #然后查不精确的
  61. $strQuery="";
  62. foreach($arrWordList as $oneword){
  63. $strQuery.="\"text\" like \"% {$oneword} %\" AND";
  64. }
  65. $strQuery = substr($strQuery,0,-3);
  66. $query = "SELECT book,paragraph, html FROM pali_text WHERE {$strQuery} LIMIT 0,20";
  67. $Fetch = PDO_FetchAll($query);
  68. $iFetch=count($Fetch);
  69. foreach($Fetch as $row){
  70. $html = $row["html"];
  71. foreach($arrWordList as $oneword){
  72. $html=str_replace($oneword,"<hl>{$oneword}</hl>",$html);
  73. }
  74. //echo "<div class='dict_word'>{$html}</div>";
  75. }
  76. # 然后查特别不精确的
  77. return;
  78. }
  79. //计算某词在三藏中出现的次数
  80. $time_start = microtime_float();
  81. $arrRealWordList = countWordInPali($word);
  82. $countWord=count($arrRealWordList);
  83. if($countWord==0){
  84. #没查到 模糊查询
  85. $dictFileName=_FILE_DB_PALITEXT_;
  86. PDO_Connect("sqlite:$dictFileName");
  87. $query = "SELECT book,paragraph, text FROM pali_text WHERE text like ? LIMIT ? , ?";
  88. $Fetch = PDO_FetchAll($query,array("%{$word}%",$_page*$_pagesize,$_pagesize));
  89. $result["data"]=$Fetch;
  90. exit;
  91. }
  92. $strQueryWordId="(";//实际出现的单词id查询字串
  93. $aQueryWordList=array();//id 为键 拼写为值的数组
  94. $aInputWordList=array();//id 为键 拼写为值的数组 该词是否被选择
  95. $aShowWordList=array();//拼写为键 个数为值的数组
  96. $aShowWordIdList=array();//拼写为键 值Id的数组
  97. for($i=0;$i<$countWord;$i++){
  98. $value= $arrRealWordList[$i];
  99. $strQueryWordId.="'{$value["id"]}',";
  100. $aQueryWordList["{$value["id"]}"]=$value["word"];
  101. $aInputWordList["{$value["id"]}"]=false;
  102. $aShowWordList[$value["word"]]=$value["count"];
  103. $aShowWordIdList[$value["word"]]=$value["id"];
  104. }
  105. if(isset($_GET["words"])){
  106. $word_selected = json_decode($_GET["words"]);
  107. if(count($word_selected)>0){
  108. $strQueryWordId="(";
  109. foreach ($word_selected as $key => $value) {
  110. $strQueryWordId.="'{$value}',";
  111. $aInputWordList["{$value}"] = true;
  112. }
  113. }
  114. else{
  115. foreach ($aInputWordList as $key => $value) {
  116. //$aInputWordList[$key] = true;
  117. }
  118. }
  119. }
  120. /*
  121. else{
  122. foreach ($aInputWordList as $key => $value) {
  123. $aInputWordList[$key] = true;
  124. }
  125. }
  126. */
  127. $strQueryWordId=mb_substr($strQueryWordId, 0,mb_strlen($strQueryWordId,"UTF-8")-1,"UTF-8");
  128. $strQueryWordId.=")";
  129. $queryTime=(microtime_float()-$time_start)*1000;
  130. //显示单词列表
  131. arsort($aShowWordList);
  132. $out_case = array();
  133. $word_count=0;
  134. foreach($aShowWordList as $x=>$x_value) {
  135. $caseword = array();
  136. $caseword["id"]=$aShowWordIdList[$x];
  137. $caseword["spell"] = $x;
  138. $caseword["count"] = $x_value;
  139. $caseword["selected"] = $aInputWordList["{$aShowWordIdList[$x]}"];
  140. $word_count+=$x_value;
  141. $out_case[] = $caseword;
  142. }
  143. $result["case"]=$out_case;
  144. $result["case_num"]=$countWord;
  145. $result["case_count"]=$word_count;
  146. //查找这些词出现在哪些书中
  147. $booklist=get_new_book_list($strQueryWordId);
  148. $result["book_list"]=$booklist;
  149. $result["book_tag"]=get_book_tag($strQueryWordId);
  150. $wordInBookCounter=0;
  151. $strFirstBookList="(";
  152. foreach($booklist as $onebook){
  153. $wordInBookCounter+=$onebook["count"];
  154. $strFirstBookList.="'".$onebook["book"]."',";
  155. if($wordInBookCounter>=20){
  156. break;
  157. }
  158. }
  159. $strFirstBookList=mb_substr($strFirstBookList, 0,mb_strlen($strFirstBookList,"UTF-8")-1,"UTF-8");
  160. $strFirstBookList.=")";
  161. $strQueryBookId=" ";
  162. if(isset($_GET["book"])){
  163. $book_selected = json_decode($_GET["book"]);
  164. $bookSelected = array();
  165. if(count($book_selected)>0){
  166. $strQueryBookId=" AND book IN (";
  167. foreach ($book_selected as $key => $value) {
  168. $strQueryBookId.="'{$value}',";
  169. $bookSelected[$value]=1;
  170. }
  171. $strQueryBookId=mb_substr($strQueryBookId, 0,mb_strlen($strQueryBookId,"UTF-8")-1,"UTF-8");
  172. $strQueryBookId.=")";
  173. foreach ($result["book_list"] as $bookindex => $bookvalue) {
  174. # code...
  175. $bookid = $bookvalue["book"];
  176. if(isset($bookSelected["{$bookid}"])){
  177. $result["book_list"][$bookindex]["selected"]=true;
  178. }
  179. else{
  180. $result["book_list"][$bookindex]["selected"]=false;
  181. }
  182. }
  183. }
  184. }
  185. //前20条记录
  186. $time_start=microtime_float();
  187. $dictFileName=_FILE_DB_PALI_INDEX_;
  188. PDO_Connect("sqlite:$dictFileName");
  189. $query = "SELECT count(*) from (SELECT book FROM word WHERE \"wordindex\" in $strQueryWordId $strQueryBookId group by book,paragraph) where 1 ";
  190. $result["record_count"]= PDO_FetchOne($query);
  191. $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";
  192. $Fetch = PDO_FetchAll($query);
  193. $out_data = array();
  194. $queryTime=(microtime_float()-$time_start)*1000;
  195. $iFetch=count($Fetch);
  196. if($iFetch>0){
  197. $dictFileName=_FILE_DB_PALITEXT_;
  198. PDO_Connect("sqlite:$dictFileName");
  199. for($i=0;$i<$iFetch;$i++){
  200. $newRecode = array();
  201. $paliwordid=$Fetch[$i]["wordindex"];
  202. $paliword=$aQueryWordList["{$paliwordid}"];
  203. $book=$Fetch[$i]["book"];
  204. $paragraph=$Fetch[$i]["paragraph"];
  205. $bookInfo = _get_book_info($book);
  206. $bookname=$bookInfo->title;
  207. $c1=$bookInfo->c1;
  208. $c2=$bookInfo->c2;
  209. $c3=$bookInfo->c3;
  210. //echo "<div class='dict_word' style='margin: 10px 0;padding: 5px;border-bottom: 1px solid var(--border-line-color);'>";
  211. //echo "<div style='font-size: 130%;font-weight: 700;'>$paliword</div>";
  212. //echo "<div class='dict_word'>";
  213. $path_1 = $c1.">";
  214. if($c2 !== ""){
  215. $path_1=$path_1.$c2.">";
  216. }
  217. if($c3 !== ""){
  218. $path_1=$path_1.$c3.">";
  219. }
  220. $path_1=$path_1."《{$bookname}》>";
  221. $query = "select * from pali_text where \"book\" = '{$book}' and \"paragraph\" = '{$paragraph}' limit 0,1";
  222. $FetchPaliText = PDO_FetchAll($query);
  223. $countPaliText=count($FetchPaliText);
  224. if($countPaliText>0){
  225. $path="";
  226. $parent = $FetchPaliText[0]["parent"];
  227. $deep=0;
  228. $sFirstParentTitle="";
  229. //循环查找父标题 得到整条路径
  230. while($parent>-1){
  231. $query = "select * from pali_text where \"book\" = '{$book}' and \"paragraph\" = '{$parent}' limit 0,1";
  232. $FetParent = PDO_FetchAll($query);
  233. $path="{$FetParent[0]["toc"]}>{$path}";
  234. if($sFirstParentTitle==""){
  235. $sFirstParentTitle = $FetParent[0]["toc"];
  236. }
  237. $parent = $FetParent[0]["parent"];
  238. $deep++;
  239. if($deep>5){
  240. break;
  241. }
  242. }
  243. $path=$path_1.$path."para. ".$paragraph;
  244. $newRecode["title"]=$sFirstParentTitle;
  245. $newRecode["path"] = $path;
  246. $newRecode["book"] = $book;
  247. $newRecode["para"] = $paragraph;
  248. $newRecode["palitext"] = $FetchPaliText[0]["html"];
  249. $newRecode["keyword"] = $paliword;
  250. $newRecode["wt"] = $Fetch[$i]["wt"];
  251. // echo "<div class='mean' style='font-size:120%'><a href='../reader/?view=para&book={$book}&para={$paragraph}' target='_blank'>$path</a></div>";
  252. $out_data[] = $newRecode;
  253. }
  254. }
  255. }
  256. $queryTime=(microtime_float()-$time_start)*1000;
  257. $result["data"] = $out_data;
  258. echo json_encode($result,JSON_UNESCAPED_UNICODE);
  259. break;
  260. }
  261. case "update":
  262. $target=$_GET["target"];
  263. switch($target){
  264. case "bold";
  265. $wordlist=$_GET["wordlist"];
  266. $booklist=$_GET["booklist"];
  267. $aBookList=ltrim($booklist,"(");
  268. $aBookList=rtrim($aBookList,")");
  269. $aBookList=str_replace("'","",$aBookList);
  270. $aBookList=str_getcsv($aBookList);
  271. $arrBookType=json_decode(file_get_contents("../public/book_name/booktype.json"));
  272. //查找这些词出现在哪些书中
  273. $newBookList=render_book_list($wordlist,$aBookList);
  274. //前20条记录
  275. $time_start=microtime_float();
  276. $dictFileName=_FILE_DB_PALI_INDEX_;
  277. PDO_Connect("sqlite:$dictFileName");
  278. $query = "select * from word where \"wordindex\" in $wordlist and \"book\" in $booklist group by book,paragraph limit 0,20";
  279. $Fetch = PDO_FetchAll($query);
  280. $queryTime=(microtime_float()-$time_start)*1000;
  281. //echo "<div >搜索时间:$queryTime </div>";
  282. if($booklist=="()"){
  283. echo "<div >请选择书名</div>";
  284. }
  285. $iFetch=count($Fetch);
  286. if($iFetch>0){
  287. $dictFileName=_FILE_DB_PALITEXT_;
  288. PDO_Connect("sqlite:$dictFileName");
  289. for($i=0;$i<$iFetch;$i++){
  290. $paliword=$Fetch[$i]["wordindex"];
  291. //$paliword=$wordlist["{$paliwordid}"];
  292. $book=$Fetch[$i]["book"];
  293. $bookInfo = _get_book_info($book);
  294. $bookname=$bookInfo->title;
  295. $c1=$bookInfo->c1;
  296. $c2=$bookInfo->c2;
  297. $c3=$bookInfo->c3;
  298. $paragraph=$Fetch[$i]["paragraph"];
  299. $path_1 = $c1.">";
  300. if($c2 !== ""){
  301. $path_1=$path_1.$c2.">";
  302. }
  303. if($c3 !== ""){
  304. $path_1=$path_1.$c3.">";
  305. }
  306. $path_1=$path_1."《{$bookname}》>";
  307. echo "<div class='dict_word'>";
  308. echo "<div class='book' ><span style='font-size:110%;font-weight:700;'>《{$bookname}》</span> <tag>$c1</tag> <tag>$c2</tag> </div>";
  309. echo "<div class='mean'>$paliword</div>";
  310. $query = "select * from pali_text where \"book\" = '{$book}' and \"paragraph\" = '{$paragraph}' limit 0,20";
  311. $FetchPaliText = PDO_FetchAll($query);
  312. $countPaliText=count($FetchPaliText);
  313. if($countPaliText>0){
  314. for($iPali=0;$iPali<$countPaliText;$iPali++){
  315. $path="";
  316. $parent = $FetchPaliText[0]["parent"];
  317. $deep=0;
  318. $sFirstParentTitle="";
  319. while($parent>-1){
  320. $query = "select * from pali_text where \"book\" = '{$book}' and \"paragraph\" = '{$parent}' limit 0,1";
  321. $FetParent = PDO_FetchAll($query);
  322. if($sFirstParentTitle==""){
  323. $sFirstParentTitle = $FetParent[0]["toc"];
  324. }
  325. $path="{$FetParent[0]["toc"]}>{$path}";
  326. $parent = $FetParent[0]["parent"];
  327. $deep++;
  328. if($deep>5){
  329. break;
  330. }
  331. }
  332. $path=$path."No. ".$paragraph;
  333. echo "<div class='mean' style='font-size:120%;'><a href='../reader/?view=para&book={$book}&para={$paragraph}' target='_blank' >$path</a></div>";
  334. if(substr($paliword,-1)=="n"){
  335. $paliword=substr($paliword,0,-1);
  336. }
  337. $light_text=str_replace($paliword,"<hl>{$paliword}</hl>",$FetchPaliText[$iPali]["html"]);
  338. echo "<div class='wizard_par_div'>{$light_text}</div>";
  339. echo "<div class='search_para_tools'></div>";
  340. }
  341. }
  342. echo "</div>";
  343. }
  344. }
  345. break;
  346. }
  347. break;
  348. }
  349. ?>