dict_find_one.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. <?php
  2. require_once "../path.php";
  3. require_once "../public/_pdo.php";
  4. require_once "../public/function.php";
  5. require_once '../ucenter/setting_function.php';
  6. if(isset($_GET["book"])){
  7. $in_book=$_GET["book"];
  8. }
  9. if(isset($_GET["paragraph"])){
  10. $in_para=$_GET["paragraph"];
  11. }
  12. if(isset($_GET["sn"])){
  13. $in_sn=$_GET["sn"];
  14. }
  15. if(isset($_GET["type"])){
  16. $type=$_GET["type"];
  17. }
  18. else{
  19. $type="wbw";
  20. }
  21. if(isset($_GET["dict_name"])){
  22. $dict_name=$_GET["dict_name"];
  23. }
  24. else{
  25. $dict_name="";
  26. }
  27. if($type=="part"){
  28. $lookup_loop=3;
  29. }
  30. else{
  31. $lookup_loop=3;
  32. }
  33. if(isset($_GET["deep"])){
  34. $lookup_loop=$_GET["deep"];
  35. }
  36. else{
  37. $lookup_loop=3;
  38. }
  39. $in_word=$_GET["word"];
  40. if(isset($_GET["debug"])){
  41. $debug=true;;
  42. }
  43. else{
  44. $debug=false;
  45. }
  46. if(mb_strlen($in_word)==0){
  47. exit;
  48. }
  49. function microtime_float()
  50. {
  51. list($usec, $sec) = explode(" ", microtime());
  52. return ((float)$usec + (float)$sec);
  53. }
  54. $time_start = microtime_float();
  55. $user_setting = get_setting();
  56. //open database
  57. global $PDO;
  58. $word_list=str_getcsv($in_word);
  59. $dict_word_spell=array();
  60. $output=array();
  61. $db_file_list=array();
  62. //用户词典
  63. if($dict_name==""){
  64. array_push($db_file_list , _FILE_DB_WBW_);
  65. array_push($db_file_list , _DIR_DICT_SYSTEM_."/sys_regular.db");
  66. array_push($db_file_list , _DIR_DICT_SYSTEM_."/sys_irregular.db");
  67. array_push($db_file_list , _DIR_DICT_SYSTEM_."/union.db");
  68. array_push($db_file_list , _DIR_DICT_SYSTEM_."/comp.db");
  69. array_push($db_file_list , _DIR_DICT_3RD_."/pm.db");
  70. array_push($db_file_list , _DIR_DICT_3RD_."/bhmf.db");
  71. array_push($db_file_list , _DIR_DICT_3RD_."/shuihan.db");
  72. array_push($db_file_list , _DIR_DICT_3RD_."/concise.db");
  73. array_push($db_file_list , _DIR_DICT_3RD_."/uhan_en.db");
  74. }
  75. else{
  76. $dict_list=str_getcsv($dict_name,',');
  77. foreach($dict_list as $dict){
  78. array_push($db_file_list , $dict);
  79. }
  80. }
  81. $_dict_db = array();
  82. foreach($db_file_list as $db_file){
  83. try {
  84. $dbh = new PDO("sqlite:".$db_file, "", "");
  85. $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
  86. $_dict_db[] = array("file"=>$db_file,"dbh"=>$dbh);
  87. } catch (PDOException $e) {
  88. if($debug){
  89. print "Error!: " . $e->getMessage() . "<br/>";
  90. }
  91. }
  92. }
  93. for($i=0;$i<$lookup_loop;$i++)
  94. {
  95. $parent_list=array();
  96. $strQueryWord="(";//单词查询字串
  97. foreach($word_list as $word){
  98. $word=str_replace("'","’",$word);
  99. $strQueryWord.="'{$word}',";
  100. }
  101. $strQueryWord=mb_substr($strQueryWord, 0,mb_strlen($strQueryWord,"UTF-8")-1,"UTF-8");
  102. $strQueryWord.=")";
  103. if($debug){
  104. echo "<h2>第".($i+1)."轮查询:".count($word_list)."</h2>";
  105. }
  106. foreach($_dict_db as $db_file){
  107. if($debug){
  108. echo "dict connect:{$db_file["file"]}<br>";
  109. }
  110. //PDO_Connect("sqlite:".$db_file);
  111. if($i==0){
  112. $query = "select * from dict where \"pali\" in $strQueryWord ORDER BY rowid DESC";
  113. }
  114. else{
  115. $query = "select * from dict where \"pali\" in $strQueryWord AND ( type <> '.n.' AND type <> '.ti.' AND type <> '.adj.' AND type <> '.pron.' AND type <> '.v.' ) ORDER BY rowid DESC";
  116. }
  117. if($debug){
  118. echo $query."<br>";
  119. }
  120. if($db_file["dbh"]){
  121. $stmt = $db_file["dbh"]->query($query);
  122. $Fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
  123. }
  124. else{
  125. $Fetch = array();
  126. if($debug){
  127. echo "无效的数据库句柄";
  128. }
  129. }
  130. //$Fetch = PDO_FetchAll($query);
  131. $iFetch=count($Fetch);
  132. if($debug){
  133. echo "count:$iFetch<br>";
  134. }
  135. if($iFetch>0){
  136. foreach($Fetch as $one){
  137. $id = $one["id"];
  138. if(isset($one["guid"])){
  139. $guid = $one["guid"];
  140. }
  141. else{
  142. $guid = "";
  143. }
  144. if(isset($one["lang"])){
  145. $language = $one["lang"];
  146. }
  147. else if(isset($one["language"])){
  148. $language = $one["language"];
  149. }
  150. else{
  151. $language = "en";
  152. }
  153. $pali = $one["pali"];
  154. $dict_word_spell["{$pali}"]=1;
  155. $type = $one["type"];
  156. $gramma = $one["gramma"];
  157. $parent = $one["parent"];
  158. //$mean = $one["mean"];
  159. if(inLangSetting($language,$user_setting["dict.lang"])){
  160. $mean = $one["mean"];
  161. }
  162. else{
  163. $mean = "";
  164. }
  165. $note = $one["note"];
  166. if(isset($one["factors"])){
  167. $parts = $one["factors"];
  168. }
  169. else if(isset($one["parts"])){
  170. $parts = $one["parts"];
  171. }
  172. else{
  173. $parts = "";
  174. }
  175. if(isset($one["factormean"])){
  176. $partmean = $one["factormean"];
  177. }
  178. else if(isset($one["partmean"])){
  179. $partmean = $one["partmean"];
  180. }
  181. else{
  182. $partmean = "";
  183. }
  184. if(inLangSetting($language,$user_setting["dict.lang"])==false){
  185. $partmean = "";
  186. }
  187. $status = $one["status"];
  188. if(isset($one["confidence"])){
  189. $confidence = $one["confidence"];
  190. }
  191. else{
  192. $confidence = 100;
  193. }
  194. if(isset($one["dict_name"])){
  195. $dict_name = $one["dict_name"];
  196. }
  197. else{
  198. $dict_name = "";
  199. }
  200. array_push($output,array(
  201. "id"=>$id,
  202. "guid"=>$guid,
  203. "pali"=>$pali,
  204. "type"=>$type,
  205. "gramma"=>$gramma,
  206. "parent"=>$parent,
  207. "mean"=>$mean,
  208. "note"=>$note,
  209. "parts"=>$parts,
  210. "partmean"=>$partmean,
  211. "status"=>$status,
  212. "confidence"=>$confidence,
  213. "dict_name"=>$dict_name,
  214. "language"=>$language
  215. ));
  216. //将语基插入下次查询的列表
  217. if(!empty($parent)){
  218. if($pali != $parent){
  219. $parent_list[$parent]=1;
  220. }
  221. }
  222. //将拆分插入下次查询的列表
  223. if($type!=".part."){
  224. if(!empty($parts)){
  225. $wordparts=str_getcsv($parts,'+');
  226. foreach($wordparts as $x){
  227. if(!empty($x)){
  228. if($x != $pali){
  229. $parent_list[$x]=1;
  230. }
  231. }
  232. }
  233. }
  234. }
  235. }
  236. }
  237. }
  238. /*
  239. if($i==0){
  240. //自动查找单词词干
  241. $word_base=getPaliWordBase($in_word);
  242. foreach($word_base as $x=>$infolist){
  243. foreach($infolist as $gramma){
  244. array_push($output,
  245. array("pali"=>$in_word,
  246. "parent"=>$x,
  247. "type"=>$gramma["type"],
  248. "gramma"=>$gramma["gramma"],
  249. "parts"=>$gramma["parts"],
  250. "partmean"=>"",
  251. "mean"=>"",
  252. "language"=>"en",
  253. "dict_name"=>"auto",
  254. "status"=>128
  255. ));
  256. $part_list=str_getcsv($gramma["parts"],"+");
  257. foreach($part_list as $part){
  258. $parent_list[$part]=1;
  259. }
  260. }
  261. }
  262. }
  263. */
  264. if($debug){
  265. echo "parent:".count($parent_list)."<br>";
  266. print_r($parent_list)."<br>";
  267. }
  268. if(count($parent_list)==0){
  269. break;
  270. }
  271. else{
  272. $word_list=array();
  273. foreach($parent_list as $x=>$value){
  274. array_push($word_list,$x);
  275. }
  276. }
  277. }
  278. //删除无效数据
  279. $newOutput = array();
  280. foreach($output as $value){
  281. if($value["dict_name"]=="auto"){
  282. if(isset($dict_word_spell["{$value["parent"]}"])){
  283. array_push($newOutput,$value);
  284. }
  285. }
  286. else
  287. {
  288. array_push($newOutput,$value);
  289. }
  290. }
  291. if($debug){
  292. echo "<textarea width=\"100%\" >";
  293. }
  294. echo json_encode($newOutput, JSON_UNESCAPED_UNICODE);
  295. if($debug){
  296. echo "</textarea>";
  297. }
  298. if($debug){
  299. echo "生成:".count($output)."<br>";
  300. echo "有效:".count($newOutput)."<br>";
  301. foreach($newOutput as $result){
  302. echo "{$result["pali"]}-{$result["parent"]}-{$result["mean"]}<br>";
  303. }
  304. $queryTime=(microtime_float()-$time_start)*1000;
  305. echo "<div >搜索时间:$queryTime 毫秒</div>";
  306. }
  307. ?>