dict_find_one.php 11 KB

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