dict_find_one.php 9.3 KB

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