dict_find3.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. //查询参考字典
  3. require_once 'casesuf.inc';
  4. require_once "../path.php";
  5. require_once "./_pdo.php";
  6. $op=$_GET["op"];
  7. $word=mb_strtolower($_GET["word"],'UTF-8');
  8. $org_word=$word;
  9. $count_return=0;
  10. $dict_list=array();
  11. global $PDO;
  12. $dictFileName=_FILE_DB_REF_;
  13. PDO_Connect("sqlite:$dictFileName");
  14. function isExsit($word){
  15. global $PDO;
  16. $query = "select count(*) as co from dict where \"word\" = ".$PDO->quote($word);
  17. $row=PDO_FetchOne($query);
  18. /*
  19. $Fetch = PDO_FetchAll($query);
  20. $iFetch=count($Fetch);
  21. $count_return+=$iFetch;
  22. if($iFetch>0){
  23. }
  24. */
  25. if($row[0]==0){
  26. return false;
  27. }
  28. else{
  29. return true;
  30. }
  31. }
  32. function myfunction($v1,$v2)
  33. {
  34. return $v1 . "+" . $v2;
  35. }
  36. function mySplit($strWord){
  37. //echo("<br>".$strWord."<br>");
  38. $doubleword="kkggccjjṭṭḍḍttddppbb";
  39. $len=mb_strlen($strWord,"UTF-8");
  40. if($len>5){
  41. for($i=$len-1;$i>3;$i--){
  42. $str1=mb_substr($strWord,0,$i,"UTF-8");
  43. $str2=mb_substr($strWord,$i,NULL,"UTF-8");
  44. //echo "$str1 + $str2 = ";
  45. if(isExsit($str1)){
  46. //echo "match";
  47. $left2=mb_substr($str2,0,2,"UTF-8");
  48. if(mb_strpos($doubleword,$left2,0,"UTF-8")!==FALSE){
  49. $str2=mb_substr($str2,1,NULL,"UTF-8");
  50. }
  51. return array($str1,$str2);
  52. }
  53. else{
  54. $str1=$str1."a";
  55. if(isExsit($str1)){
  56. //echo "match";
  57. $left2=mb_substr($str2,0,2,"UTF-8");
  58. if(mb_strpos($doubleword,$left2,0,"UTF-8")!==FALSE){
  59. $str2=mb_substr($str2,1,NULL,"UTF-8");
  60. }
  61. return array($str1,$str2);
  62. }
  63. }
  64. }
  65. //not found
  66. if(mb_substr($strWord,0,1,"UTF-8")=="ā"){
  67. $strWord='a'.mb_substr($strWord,1,NULL,"UTF-8");
  68. for($i=$len-1;$i>3;$i--){
  69. $str1=mb_substr($strWord,0,$i,"UTF-8");
  70. $str2=mb_substr($strWord,$i,NULL,"UTF-8");
  71. //echo "$str1 + $str2 = ";
  72. if(isExsit($str1)){
  73. //echo "match";
  74. $left2=mb_substr($str2,0,2,"UTF-8");
  75. if(mb_strpos($doubleword,$left2,0,"UTF-8")!==FALSE){
  76. $str2=mb_substr($str2,1,NULL,"UTF-8");
  77. }
  78. return array($str1,$str2);
  79. }
  80. else{
  81. $str1=$str1."a";
  82. if(isExsit($str1)){
  83. //echo "match";
  84. $left2=mb_substr($str2,0,2,"UTF-8");
  85. if(mb_strpos($doubleword,$left2,0,"UTF-8")!==FALSE){
  86. $str2=mb_substr($str2,1,NULL,"UTF-8");
  87. }
  88. return array($str1,$str2);
  89. }
  90. }
  91. }
  92. }
  93. //not found
  94. if(mb_substr($strWord,0,1,"UTF-8")=="e"){
  95. $strWord='i'.mb_substr($strWord,1,NULL,"UTF-8");
  96. for($i=$len-1;$i>3;$i--){
  97. $str1=mb_substr($strWord,0,$i,"UTF-8");
  98. $str2=mb_substr($strWord,$i,NULL,"UTF-8");
  99. //echo "$str1 + $str2 = ";
  100. if(isExsit($str1)){
  101. //echo "match";
  102. $left2=mb_substr($str2,0,2,"UTF-8");
  103. if(mb_strpos($doubleword,$left2,0,"UTF-8")!==FALSE){
  104. $str2=mb_substr($str2,1,NULL,"UTF-8");
  105. }
  106. return array($str1,$str2);
  107. }
  108. else{
  109. $str1=$str1."a";
  110. if(isExsit($str1)){
  111. //echo "match";
  112. $left2=mb_substr($str2,0,2,"UTF-8");
  113. if(mb_strpos($doubleword,$left2,0,"UTF-8")!==FALSE){
  114. $str2=mb_substr($str2,1,NULL,"UTF-8");
  115. }
  116. return array($str1,$str2);
  117. }
  118. }
  119. }
  120. }
  121. }
  122. return(FALSE);
  123. }
  124. switch($op){
  125. case "pre"://预查询
  126. echo "<wordlist>";
  127. $query = "select word,count(*) as co from dict where \"eword\" like ".$PDO->quote($word.'%')." OR \"word\" like ".$PDO->quote($word.'%')." group by word order by length limit 0,100";
  128. $Fetch = PDO_FetchAll($query);
  129. $iFetch=count($Fetch);
  130. if($iFetch>0){
  131. for($i=0;$i<$iFetch;$i++){
  132. $outXml = "<word>";
  133. $word=$Fetch[$i]["word"];
  134. $outXml = $outXml."<pali>$word</pali>";
  135. $outXml = $outXml."<count>".$Fetch[$i]["co"]."</count>";
  136. $outXml = $outXml."</word>";
  137. echo $outXml;
  138. }
  139. }
  140. echo "</wordlist>";
  141. break;
  142. case "search":
  143. //直接查询
  144. $query = "select dict.dict_id,dict.mean,info.shortname from dict LEFT JOIN info ON dict.dict_id = info.id where \"word\" = ".$PDO->quote($word)." limit 0,30";
  145. $Fetch = PDO_FetchAll($query);
  146. $iFetch=count($Fetch);
  147. $count_return+=$iFetch;
  148. if($iFetch>0){
  149. for($i=0;$i<$iFetch;$i++){
  150. $mean=$Fetch[$i]["mean"];
  151. $dictid=$Fetch[$i]["dict_id"];
  152. $dict_list[$dictid]=$Fetch[$i]["shortname"];
  153. $outXml = "<div class='dict_word'>";
  154. $outXml = $outXml."<a name='ref_dict_$dictid'></a>";
  155. $outXml = $outXml."<div class='dict'>".$Fetch[$i]["shortname"]."</div>";
  156. $outXml = $outXml."<div class='mean'>".$mean."</div>";
  157. $outXml = $outXml."</div>";
  158. echo $outXml;
  159. }
  160. }
  161. //去除尾查
  162. $newWord=array();
  163. for ($row = 0; $row < count($case); $row++) {
  164. $len=mb_strlen($case[$row][1],"UTF-8");
  165. $end=mb_substr($word, 0-$len,NULL,"UTF-8");
  166. if($end==$case[$row][1]){
  167. $base=mb_substr($word, 0,mb_strlen($word,"UTF-8")-$len,"UTF-8").$case[$row][0];
  168. if($base!=$word){
  169. if(isset($newWord[$base])){
  170. $newWord[$base] .= "<br />".$case[$row][2];
  171. }
  172. else{
  173. $newWord[$base] = $case[$row][2];
  174. }
  175. }
  176. }
  177. }
  178. if(count($newWord)>0){
  179. foreach($newWord as $x=>$x_value) {
  180. $query = "select dict.dict_id,dict.mean,info.shortname from dict LEFT JOIN info ON dict.dict_id = info.id where \"word\" = ".$PDO->quote($x)." limit 0,30";
  181. $Fetch = PDO_FetchAll($query);
  182. $iFetch=count($Fetch);
  183. $count_return+=$iFetch;
  184. if($iFetch>0){
  185. echo $x . ":<br />$x_value<br>";
  186. for($i=0;$i<$iFetch;$i++){
  187. $mean=$Fetch[$i]["mean"];
  188. $dictid=$Fetch[$i]["dict_id"];
  189. $dict_list[$dictid]=$Fetch[$i]["shortname"];
  190. $outXml = "<div class='dict_word'>";
  191. $outXml = $outXml."<a name='ref_dict_$dictid'></a>";
  192. $outXml = $outXml."<div class='dict'>".$Fetch[$i]["shortname"]."</div>";
  193. $outXml = $outXml."<div class='mean'>".$mean."</div>";
  194. $outXml = $outXml."</div>";
  195. echo $outXml;
  196. }
  197. }
  198. }
  199. }
  200. //去除尾查结束
  201. //模糊查
  202. //模糊查结束
  203. //查连读词
  204. if($count_return<2){
  205. echo "Junction:<br />";
  206. $newWord=array();
  207. for ($row = 0; $row < count($un); $row++) {
  208. $len=mb_strlen($un[$row][1],"UTF-8");
  209. $end=mb_substr($word, 0-$len,NULL,"UTF-8");
  210. if($end==$un[$row][1]){
  211. $base=mb_substr($word, 0,mb_strlen($word,"UTF-8")-$len,"UTF-8").$un[$row][0];
  212. $arr_un=explode("+",$base);
  213. foreach ($arr_un as $oneword)
  214. {
  215. echo "<a onclick='dict_pre_word_click(\"$oneword\")'>$oneword</a> + ";
  216. }
  217. echo "<br />";
  218. }
  219. }
  220. }
  221. //查内容
  222. if($count_return<2){
  223. $word1=$org_word;
  224. $wordInMean="%$org_word%";
  225. echo "include $org_word:<br />";
  226. $query = "select dict.dict_id,dict.word,dict.mean,info.shortname from dict LEFT JOIN info ON dict.dict_id = info.id where \"mean\" like ".$PDO->quote($wordInMean)." limit 0,30";
  227. $Fetch = PDO_FetchAll($query);
  228. $iFetch=count($Fetch);
  229. $count_return+=$iFetch;
  230. if($iFetch>0){
  231. for($i=0;$i<$iFetch;$i++){
  232. $mean=$Fetch[$i]["mean"];
  233. $pos=mb_stripos($mean,$word,0,"UTF-8");
  234. if($pos){
  235. if($pos>20){
  236. $start=$pos-20;
  237. }
  238. else{
  239. $start=0;
  240. }
  241. $newmean=mb_substr($mean,$start,100,"UTF-8");
  242. }
  243. else{
  244. $newmean=$mean;
  245. }
  246. $pos=mb_stripos($newmean,$word1,0,"UTF-8");
  247. $head=mb_substr($newmean,0,$pos,"UTF-8");
  248. $mid=mb_substr($newmean,$pos,mb_strlen($word1,"UTF-8"),"UTF-8");
  249. $end=mb_substr($newmean,$pos+mb_strlen($word1,"UTF-8"),NULL,"UTF-8");
  250. $heigh_light_mean="$head<hl>$mid</hl>$end";
  251. $outXml = "<div class='dict_word'>";
  252. $outXml = $outXml."<div class='word'>".$Fetch[$i]["word"]."</div>";
  253. $outXml = $outXml."<div class='dict'>".$Fetch[$i]["shortname"]."</div>";
  254. $outXml = $outXml."<div class='mean'>".$heigh_light_mean."</div>";
  255. $outXml = $outXml."</div>";
  256. echo $outXml;
  257. }
  258. }
  259. }
  260. //拆复合词
  261. $splitWord=$word;
  262. $part=array();
  263. if($count_return<2){
  264. echo "Try to split comp:<br>";
  265. while(($split=mySplit($splitWord))!==FALSE){
  266. array_push($part,$split[0]);
  267. $splitWord=$split[1];
  268. }
  269. if(count($part)>0){
  270. array_push($part,$splitWord);
  271. $newPart=ltrim(array_reduce($part,"myfunction"),"+");
  272. echo $newPart;
  273. }
  274. }
  275. echo "<div id='dictlist'>";
  276. foreach($dict_list as $x=>$x_value) {
  277. echo "<a href='#ref_dict_$x'>$x_value</a>";
  278. }
  279. echo "</div>";
  280. break;
  281. }
  282. ?>