dict_find_one.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. if (isset($_GET["book"])) {
  9. $in_book = $_GET["book"];
  10. }
  11. if (isset($_GET["paragraph"])) {
  12. $in_para = $_GET["paragraph"];
  13. }
  14. if (isset($_GET["sn"])) {
  15. $in_sn = $_GET["sn"];
  16. }
  17. if (isset($_GET["type"])) {
  18. $type = $_GET["type"];
  19. } else {
  20. $type = "wbw";
  21. }
  22. if (isset($_GET["dict_name"])) {
  23. $dict_name = $_GET["dict_name"];
  24. } else {
  25. $dict_name = "";
  26. }
  27. if ($type == "part") {
  28. $lookup_loop = 3;
  29. } else {
  30. $lookup_loop = 3;
  31. }
  32. if (isset($_GET["deep"])) {
  33. $lookup_loop = $_GET["deep"];
  34. } else {
  35. $lookup_loop = 3;
  36. }
  37. $in_word = $_GET["word"];
  38. if (isset($_GET["debug"])) {
  39. $debug = true;
  40. } else {
  41. $debug = false;
  42. }
  43. if (mb_strlen($in_word) == 0) {
  44. echo json_encode(array(), JSON_UNESCAPED_UNICODE);
  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,"",false);
  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. # 记录已经查过的词,下次就不查了
  100. $newWordList = array();
  101. foreach ($word_list as $lsWord) {
  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. # 记录已经查过的词结束
  112. $strQueryWord = "("; //单词查询字串
  113. foreach ($word_list as $word) {
  114. $word = str_replace("'", "’", $word);
  115. $strQueryWord .= "'{$word}',";
  116. }
  117. $strQueryWord = mb_substr($strQueryWord, 0, mb_strlen($strQueryWord, "UTF-8") - 1, "UTF-8");
  118. $strQueryWord .= ")";
  119. if ($debug) {
  120. echo "<h2>第" . ($i + 1) . "轮查询:" . count($word_list) . "</h2>";
  121. }
  122. foreach ($_dict_db as $db_file) {
  123. if ($debug) {
  124. echo "dict connect:{$db_file["file"]}<br>";
  125. }
  126. if ($i == 0) {
  127. $query = "SELECT * from dict where \"pali\" in $strQueryWord ORDER BY id DESC";
  128. } else {
  129. $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";
  130. }
  131. if ($debug) {
  132. echo $query . "<br>";
  133. }
  134. $Fetch = array();
  135. if ($redis && !empty($db_file["redis"])) {
  136. if ($debug) {
  137. echo "<spen style='color:green;'>redis</spen>:{$db_file["redis"]}<br>";
  138. }
  139. foreach ($word_list as $word) {
  140. $wordData = $redis->hGet($db_file["redis"],$word);
  141. if($wordData){
  142. if(!empty($wordData)){
  143. $arrWord = json_decode($wordData,true);
  144. foreach ($arrWord as $one) {
  145. # code...
  146. if(count($one)==14){
  147. $Fetch[] = array("id"=>$one[0],
  148. "pali"=>$one[1],
  149. "type"=>$one[2],
  150. "gramma"=>$one[3],
  151. "parent"=>$one[4],
  152. "mean"=>$one[5],
  153. "note"=>$one[6],
  154. "parts"=>$one[7],
  155. "partmean"=>$one[8],
  156. "status"=>$one[9],
  157. "confidence"=>$one[10],
  158. "dict_name"=>$one[12],
  159. "lang"=>$one[13],
  160. );
  161. }
  162. else{
  163. $Fetch[] = array("id"=>$one[0],
  164. "pali"=>$one[1],
  165. "type"=>$one[2],
  166. "gramma"=>$one[3],
  167. "parent"=>$one[4],
  168. "mean"=>$one[5],
  169. "note"=>$one[6],
  170. "parts"=>$one[7],
  171. "partmean"=>$one[8],
  172. "status"=>$one[9],
  173. "confidence"=>$one[10],
  174. "dict_name"=>$one[12],
  175. "lang"=>"en"
  176. );
  177. }
  178. }
  179. }
  180. }
  181. else{
  182. # 没找到就不找了
  183. }
  184. }
  185. }
  186. else{
  187. if ($debug) {
  188. echo "<spen style='color:red;'>db query</spen>:{$db_file["file"]}<br>";
  189. }
  190. if ($db_file["dbh"]) {
  191. try {
  192. $stmt = $db_file["dbh"]->query($query);
  193. if ($stmt) {
  194. $Fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
  195. } else {
  196. $Fetch = array();
  197. if ($debug) {
  198. echo "无效的Statement句柄";
  199. }
  200. }
  201. } catch (PDOException $e) {
  202. if ($debug) {
  203. print "Error!: " . $e->getMessage() . "<br/>";
  204. }
  205. $Fetch = array();
  206. }
  207. } else {
  208. $Fetch = array();
  209. if ($debug) {
  210. echo "无效的数据库句柄";
  211. }
  212. }
  213. }
  214. //$Fetch = PDO_FetchAll($query);
  215. $iFetch = count($Fetch);
  216. if ($debug) {
  217. echo "count:$iFetch<br>";
  218. }
  219. if ($iFetch > 0) {
  220. foreach ($Fetch as $one) {
  221. $id = $one["id"];
  222. if (isset($one["guid"])) {
  223. $guid = $one["guid"];
  224. } else {
  225. $guid = "";
  226. }
  227. if (isset($one["lang"])) {
  228. $language = $one["lang"];
  229. } else if (isset($one["language"])) {
  230. $language = $one["language"];
  231. } else {
  232. $language = "en";
  233. }
  234. $pali = $one["pali"];
  235. $dict_word_spell["{$pali}"] = 1;
  236. $type = $one["type"];
  237. $gramma = $one["gramma"];
  238. $parent = $one["parent"];
  239. //$mean = $one["mean"];
  240. if (inLangSetting($language, $user_setting["dict.lang"])) {
  241. $mean = $one["mean"];
  242. } else {
  243. $mean = "";
  244. }
  245. $note = $one["note"];
  246. if (isset($one["factors"])) {
  247. $parts = $one["factors"];
  248. } else if (isset($one["parts"])) {
  249. $parts = $one["parts"];
  250. } else {
  251. $parts = "";
  252. }
  253. if (isset($one["factormean"])) {
  254. $partmean = $one["factormean"];
  255. } else if (isset($one["partmean"])) {
  256. $partmean = $one["partmean"];
  257. } else {
  258. $partmean = "";
  259. }
  260. if (inLangSetting($language, $user_setting["dict.lang"]) == false) {
  261. $partmean = "";
  262. }
  263. $status = $one["status"];
  264. if (isset($one["confidence"])) {
  265. $confidence = $one["confidence"];
  266. } else {
  267. $confidence = 100;
  268. }
  269. if (isset($one["dict_name"])) {
  270. $dict_name = $one["dict_name"];
  271. } else {
  272. $dict_name = "";
  273. }
  274. array_push($output, array(
  275. "id" => $id,
  276. "guid" => $guid,
  277. "pali" => $pali,
  278. "type" => $type,
  279. "gramma" => $gramma,
  280. "parent" => $parent,
  281. "mean" => $mean,
  282. "note" => $note,
  283. "parts" => $parts,
  284. "partmean" => $partmean,
  285. "status" => $status,
  286. "confidence" => $confidence,
  287. "dict_name" => $dict_name,
  288. "language" => $language,
  289. ));
  290. //将语基插入下次查询的列表
  291. if (!empty($parent)) {
  292. if ($pali != $parent) {
  293. $parent_list[$parent] = 1;
  294. }
  295. }
  296. //将拆分插入下次查询的列表
  297. if ($type != ".part.") {
  298. if (!empty($parts)) {
  299. $wordparts = str_getcsv($parts, '+');
  300. foreach ($wordparts as $x) {
  301. if (!empty($x)) {
  302. if ($x != $pali) {
  303. $parent_list[$x] = 1;
  304. }
  305. }
  306. }
  307. }
  308. }
  309. }
  310. }
  311. }
  312. /*
  313. if($i==0){
  314. //自动查找单词词干
  315. $word_base=getPaliWordBase($in_word);
  316. foreach($word_base as $x=>$infolist){
  317. foreach($infolist as $gramma){
  318. array_push($output,
  319. array("pali"=>$in_word,
  320. "parent"=>$x,
  321. "type"=>$gramma["type"],
  322. "gramma"=>$gramma["gramma"],
  323. "parts"=>$gramma["parts"],
  324. "partmean"=>"",
  325. "mean"=>"",
  326. "language"=>"en",
  327. "dict_name"=>"auto",
  328. "status"=>128
  329. ));
  330. $part_list=str_getcsv($gramma["parts"],"+");
  331. foreach($part_list as $part){
  332. $parent_list[$part]=1;
  333. }
  334. }
  335. }
  336. }
  337. */
  338. if ($debug) {
  339. echo "parent:" . count($parent_list) . "<br>";
  340. print_r($parent_list) . "<br>";
  341. }
  342. if (count($parent_list) == 0) {
  343. break;
  344. } else {
  345. $word_list = array();
  346. foreach ($parent_list as $x => $value) {
  347. array_push($word_list, $x);
  348. }
  349. }
  350. }
  351. //删除无效数据
  352. $newOutput = array();
  353. foreach ($output as $value) {
  354. if ($value["dict_name"] == "auto") {
  355. if (isset($dict_word_spell["{$value["parent"]}"])) {
  356. array_push($newOutput, $value);
  357. }
  358. } else {
  359. array_push($newOutput, $value);
  360. }
  361. }
  362. if ($debug) {
  363. echo "<textarea width=\"100%\" >";
  364. }
  365. echo json_encode($newOutput, JSON_UNESCAPED_UNICODE);
  366. if ($debug) {
  367. echo "</textarea>";
  368. }
  369. if ($debug) {
  370. echo "生成:" . count($output) . "<br>";
  371. echo "有效:" . count($newOutput) . "<br>";
  372. foreach ($newOutput as $result) {
  373. echo "{$result["pali"]}-{$result["parent"]}-{$result["mean"]}<br>";
  374. }
  375. $queryTime = (microtime_float() - $time_start) * 1000;
  376. echo "<div >搜索时间:$queryTime 毫秒</div>";
  377. }