dict_find_one.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. <?php
  2. include("../log/pref_log.php");
  3. require_once "../config.php";
  4. require_once "../public/_pdo.php";
  5. require_once "../public/function.php";
  6. require_once '../ucenter/setting_function.php';
  7. require_once "../redis/function.php";
  8. $redis = redis_connect();
  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. echo json_encode(array(), JSON_UNESCAPED_UNICODE);
  46. exit;
  47. }
  48. function microtime_float()
  49. {
  50. list($usec, $sec) = explode(" ", microtime());
  51. return ((float) $usec + (float) $sec);
  52. }
  53. $time_start = microtime_float();
  54. $user_setting = get_setting();
  55. //open database
  56. global $PDO;
  57. $word_list = str_getcsv($in_word);
  58. $dict_word_spell = array();
  59. $output = array();
  60. $db_file_list = array();
  61. //词典列表
  62. if ($dict_name == "") {
  63. $db_file_list[] = array(_FILE_DB_TERM_,"dict://term",true);
  64. $db_file_list[] = array(_FILE_DB_WBW1_,"dict://user",true);
  65. $db_file_list[] = array( _DIR_DICT_SYSTEM_ . "/sys_regular.db","dict://regular",true);
  66. $db_file_list[] = array( _DIR_DICT_SYSTEM_ . "/sys_irregular.db","dict://irregular",true);
  67. $db_file_list[] = array( _DIR_DICT_SYSTEM_ . "/union.db","dict://union",true);
  68. $db_file_list[] = array( _DIR_DICT_SYSTEM_ . "/comp.db","dict://comp",true);
  69. $db_file_list[] = array( _DIR_DICT_3RD_ . "/pm.db","dict://pm",true);
  70. $db_file_list[] = array( _DIR_DICT_3RD_ . "/bhmf.db","dict://bhmf",true);
  71. $db_file_list[] = array( _DIR_DICT_3RD_ . "/shuihan.db","dict://shuihan",true);
  72. $db_file_list[] = array( _DIR_DICT_3RD_ . "/concise.db","dict://concise",true);
  73. $db_file_list[] = array( _DIR_DICT_3RD_ . "/uhan_en.db","dict://uhan_en",true);
  74. } else {
  75. $dict_list = str_getcsv($dict_name, ',');
  76. foreach ($dict_list as $dict) {
  77. $db_file_list[] = array( $dict,"",false);
  78. }
  79. }
  80. $_dict_db = array();
  81. foreach ($db_file_list as $db_file) {
  82. try {
  83. if ($redis && !empty($db_file[1])) {
  84. $dbh=null;
  85. }
  86. else{
  87. $dbh = new PDO("sqlite:" . $db_file[0], "", "");
  88. $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
  89. }
  90. $_dict_db[] = array("file" => $db_file[0], "dbh" => $dbh,"redis"=>$db_file[1],"static"=>$db_file[2]);
  91. } catch (PDOException $e) {
  92. if ($debug) {
  93. print "Error!: " . $e->getMessage() . "<br/>";
  94. }
  95. }
  96. }
  97. $lookuped=array();
  98. for ($i = 0; $i < $lookup_loop; $i++) {
  99. $parent_list = array();
  100. # 记录已经查过的词,下次就不查了
  101. $newWordList = array();
  102. foreach ($word_list as $lsWord) {
  103. if(!isset($lookuped[$lsWord]) && !empty($lsWord)){
  104. $newWordList[]=$lsWord;
  105. $lookuped[$lsWord]=1;
  106. }
  107. }
  108. if(count($newWordList)==0){
  109. break;
  110. }
  111. $word_list = $newWordList;
  112. # 记录已经查过的词结束
  113. $strQueryWord = "("; //单词查询字串
  114. foreach ($word_list as $word) {
  115. $word = str_replace("'", "’", $word);
  116. $strQueryWord .= "'{$word}',";
  117. }
  118. $strQueryWord = mb_substr($strQueryWord, 0, mb_strlen($strQueryWord, "UTF-8") - 1, "UTF-8");
  119. $strQueryWord .= ")";
  120. if ($debug) {
  121. echo "<h2>第" . ($i + 1) . "轮查询:" . count($word_list) . "</h2>";
  122. }
  123. foreach ($_dict_db as $db_file) {
  124. if ($debug) {
  125. echo "dict connect:{$db_file["file"]}<br>";
  126. }
  127. if ($i == 0) {
  128. $query = "SELECT * from dict where \"pali\" in $strQueryWord ORDER BY id DESC";
  129. } else {
  130. $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";
  131. }
  132. if ($debug) {
  133. echo $query . "<br>";
  134. }
  135. $Fetch = array();
  136. if ($redis && !empty($db_file["redis"])) {
  137. if ($debug) {
  138. echo "<spen style='color:green;'>redis</spen>:{$db_file["redis"]}<br>";
  139. }
  140. foreach ($word_list as $word) {
  141. $wordData = $redis->hGet($db_file["redis"],$word);
  142. if($wordData){
  143. if(!empty($wordData)){
  144. $arrWord = json_decode($wordData,true);
  145. foreach ($arrWord as $one) {
  146. # code...
  147. if(count($one)==14){
  148. $Fetch[] = array("id"=>$one[0],
  149. "pali"=>$one[1],
  150. "type"=>$one[2],
  151. "gramma"=>$one[3],
  152. "parent"=>$one[4],
  153. "mean"=>$one[5],
  154. "note"=>$one[6],
  155. "parts"=>$one[7],
  156. "partmean"=>$one[8],
  157. "status"=>$one[9],
  158. "confidence"=>$one[10],
  159. "dict_name"=>$one[12],
  160. "lang"=>$one[13],
  161. );
  162. }
  163. else{
  164. $Fetch[] = array("id"=>$one[0],
  165. "pali"=>$one[1],
  166. "type"=>$one[2],
  167. "gramma"=>$one[3],
  168. "parent"=>$one[4],
  169. "mean"=>$one[5],
  170. "note"=>$one[6],
  171. "parts"=>$one[7],
  172. "partmean"=>$one[8],
  173. "status"=>$one[9],
  174. "confidence"=>$one[10],
  175. "dict_name"=>$one[12],
  176. "lang"=>"en"
  177. );
  178. }
  179. }
  180. }
  181. }
  182. else{
  183. # 没找到就不找了
  184. }
  185. }
  186. }
  187. else{
  188. if ($debug) {
  189. echo "<spen style='color:red;'>db query</spen>:{$db_file["file"]}<br>";
  190. }
  191. if ($db_file["dbh"]) {
  192. try {
  193. $stmt = $db_file["dbh"]->query($query);
  194. if ($stmt) {
  195. $Fetch = $stmt->fetchAll(PDO::FETCH_ASSOC);
  196. } else {
  197. $Fetch = array();
  198. if ($debug) {
  199. echo "无效的Statement句柄";
  200. }
  201. }
  202. } catch (PDOException $e) {
  203. if ($debug) {
  204. print "Error!: " . $e->getMessage() . "<br/>";
  205. }
  206. $Fetch = array();
  207. }
  208. } else {
  209. $Fetch = array();
  210. if ($debug) {
  211. echo "无效的数据库句柄";
  212. }
  213. }
  214. }
  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. }
  378. PrefLog();