dict_find_auto.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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. $user_setting = get_setting();
  7. if(isset($_GET["book"])){
  8. $in_book=$_GET["book"];
  9. }
  10. if(isset($_GET["para"])){
  11. $in_para=$_GET["para"];
  12. }
  13. $para_list=str_getcsv($in_para);
  14. $strQueryPara="(";//单词查询字串
  15. foreach($para_list as $para){
  16. $strQueryPara.="'{$para}',";
  17. }
  18. $strQueryPara=mb_substr($strQueryPara, 0,mb_strlen($strQueryPara,"UTF-8")-1,"UTF-8");
  19. $strQueryPara.=")";
  20. if(isset($_GET["debug"])){
  21. $debug=true;;
  22. }
  23. else{
  24. $debug=false;
  25. }
  26. function microtime_float()
  27. {
  28. list($usec, $sec) = explode(" ", microtime());
  29. return ((float)$usec + (float)$sec);
  30. }
  31. $time_start = microtime_float();
  32. //open database
  33. global $PDO;
  34. //查询单词表
  35. $db_file = _DIR_PALICANON_TEMPLET_."/p".$in_book."_tpl.db3";
  36. PDO_Connect("sqlite:$db_file");
  37. $query="SELECT paragraph,wid,real FROM \"main\" WHERE (\"paragraph\" in ".$strQueryPara." ) and \"real\"<>\"\" and \"type\"<>'.ctl.' ";
  38. if($debug){
  39. echo "filename:".$db_file."<br>";
  40. echo $query."<br>";
  41. }
  42. $FetchAllWord = PDO_FetchAll($query);
  43. $iFetch=count($FetchAllWord);
  44. if($iFetch==0){
  45. echo json_encode(array(), JSON_UNESCAPED_UNICODE);
  46. exit;
  47. }
  48. $voc_list=array();
  49. foreach($FetchAllWord as $word){
  50. $voc_list[$word["real"]]=1;
  51. }
  52. if($debug){
  53. echo "单词表共计:".count($voc_list)."词<br>";
  54. }
  55. //查询单词表结束
  56. $word_list=array();
  57. foreach($voc_list as $word=>$value){
  58. array_push($word_list,$word);
  59. }
  60. $lookup_loop=2;
  61. $dict_word_spell=array();
  62. $output=array();
  63. $db_file_list=array();
  64. //用户词典
  65. array_push($db_file_list , array(_FILE_DB_WBW_," ORDER BY rowid DESC"));
  66. array_push($db_file_list , array(_DIR_DICT_SYSTEM_."/sys_regular.db"," ORDER BY confidence DESC"));
  67. array_push($db_file_list , array(_DIR_DICT_SYSTEM_."/sys_irregular.db",""));
  68. array_push($db_file_list , array(_DIR_DICT_SYSTEM_."/union.db",""));
  69. array_push($db_file_list , array(_DIR_DICT_SYSTEM_."/comp.db",""));
  70. array_push($db_file_list , array(_DIR_DICT_3RD_."/pm.db",""));
  71. array_push($db_file_list , array(_DIR_DICT_3RD_."/bhmf.db",""));
  72. array_push($db_file_list , array(_DIR_DICT_3RD_."/shuihan.db",""));
  73. array_push($db_file_list , array(_DIR_DICT_3RD_."/concise.db",""));
  74. array_push($db_file_list , array(_DIR_DICT_3RD_."/uhan_en.db",""));
  75. for($i=0;$i<$lookup_loop;$i++)
  76. {
  77. $parent_list=array();
  78. $strQueryWord="(";//单词查询字串
  79. foreach($word_list as $word){
  80. $word=str_replace("'","’",$word);
  81. $strQueryWord.="'{$word}',";
  82. }
  83. $strQueryWord=mb_substr($strQueryWord, 0,mb_strlen($strQueryWord,"UTF-8")-1,"UTF-8");
  84. $strQueryWord.=")";
  85. if($debug){
  86. echo "<h2>第{$i}轮查询:$strQueryWord</h2>";
  87. }
  88. foreach($db_file_list as $db){
  89. $db_file=$db[0];
  90. $db_sort=$db[1];
  91. if($debug){
  92. echo "dict:$db_file<br>";
  93. }
  94. PDO_Connect("sqlite:{$db_file}");
  95. PDO_Execute("PRAGMA synchronous = OFF");
  96. PDO_Execute("PRAGMA journal_mode = WAL");
  97. PDO_Execute("PRAGMA foreign_keys = ON");
  98. PDO_Execute("PRAGMA busy_timeout = 5000");
  99. $strOrderby=$db[1];
  100. if($i==0){
  101. $query = "select * from dict where \"pali\" in {$strQueryWord} AND ( type <> '.n:base.' AND type <> '.ti:base.' AND type <> '.adj:base.' AND type <> '.pron:base.' AND type <> '.v:base.' AND type <> '.part.' ) ".$strOrderby;
  102. }
  103. else{
  104. $query = "select * from dict where \"pali\" in {$strQueryWord} ".$strOrderby;
  105. }
  106. if($debug){
  107. echo $query."<br>";
  108. }
  109. try {
  110. $Fetch = PDO_FetchAll($query);
  111. } catch (Exception $e) {
  112. if($debug){
  113. echo 'Caught exception: ', $e->getMessage(), "\n";
  114. }
  115. continue;
  116. }
  117. $iFetch=count($Fetch);
  118. if($debug){
  119. echo "count:{$iFetch}<br>";
  120. }
  121. if($iFetch>0){
  122. foreach($Fetch as $one){
  123. $id = $one["id"];
  124. if(isset($one["guid"])){
  125. $guid = $one["guid"];
  126. }
  127. else{
  128. $guid = "";
  129. }
  130. $pali = $one["pali"];
  131. $dict_word_spell["{$pali}"]=1;
  132. $type = $one["type"];
  133. $gramma = $one["gramma"];
  134. $parent = $one["parent"];
  135. if(inLangSetting($one["lang"],$user_setting["dict.lang"])){
  136. $mean = $one["mean"];
  137. }
  138. else{
  139. $mean = "";
  140. }
  141. if(isset($one["note"])){
  142. $note = $one["note"];
  143. }
  144. else{
  145. $note = "";
  146. }
  147. if(isset($one["parts"])){
  148. $parts = $one["parts"];
  149. }
  150. else if(isset($one["factors"])){
  151. $parts = $one["factors"];
  152. }
  153. else{
  154. $parts = "";
  155. }
  156. if(isset($one["partmean"])){
  157. $partmean = $one["partmean"];
  158. }
  159. else if(isset($one["factormean"])){
  160. $partmean = $one["factormean"];
  161. }
  162. else{
  163. $partmean = "";
  164. }
  165. if(inLangSetting($one["lang"],$user_setting["dict.lang"])==false){
  166. $partmean = "";
  167. }
  168. if(isset($one["part_id"])){
  169. $part_id = $one["part_id"];
  170. }
  171. else{
  172. $part_id = "";
  173. }
  174. if(isset($one["status"])){
  175. $status = $one["status"];
  176. }
  177. else{
  178. $status = "";
  179. }
  180. if(isset($one["dict_name"])){
  181. $dict_name = $one["dict_name"];
  182. }
  183. else{
  184. $dict_name = "";
  185. }
  186. if(isset($one["language"])){
  187. $language = $one["language"];
  188. }
  189. else{
  190. $language = "en";
  191. }
  192. array_push($output,array(
  193. "id"=>$id,
  194. "guid"=>$guid,
  195. "pali"=>$pali,
  196. "type"=>$type,
  197. "gramma"=>$gramma,
  198. "parent"=>$parent,
  199. "mean"=>$mean,
  200. "note"=>$note,
  201. "parts"=>$parts,
  202. "part_id"=>$part_id,
  203. "partmean"=>$partmean,
  204. "status"=>$status,
  205. "dict_name"=>$dict_name,
  206. "language"=>$language
  207. ));
  208. if(!empty($parent)){
  209. if($pali != $parent){
  210. $parent_list[$one["parent"]]=1;
  211. }
  212. }
  213. if($type!="part"){
  214. if(isset($one["factors"])){
  215. $parts=str_getcsv($one["factors"],'+');
  216. foreach($parts as $x){
  217. if(!empty($x)){
  218. if($x != $pali){
  219. $parent_list[$x]=1;
  220. }
  221. }
  222. }
  223. }
  224. }
  225. }
  226. }
  227. $PDO = null;
  228. }
  229. /*
  230. if($i==0){
  231. //自动查找单词词干
  232. $word_base=getPaliWordBase($in_word);
  233. foreach($word_base as $x=>$infolist){
  234. foreach($infolist as $gramma){
  235. array_push($output,
  236. array("pali"=>$in_word,
  237. "type"=>$gramma["type"],
  238. "gramma"=>$gramma["gramma"],
  239. "mean"=>"",
  240. "parent"=>$x,
  241. "parts"=>$gramma["parts"],
  242. "partmean"=>"",
  243. "language"=>"en",
  244. "dict_name"=>"auto",
  245. "status"=>128
  246. ));
  247. $part_list=str_getcsv($gramma["parts"],"+");
  248. foreach($part_list as $part){
  249. $parent_list[$part]=1;
  250. }
  251. }
  252. }
  253. }
  254. */
  255. if($debug){
  256. echo "parent:".count($parent_list)."<br>";
  257. //print_r($parent_list)."<br>";
  258. }
  259. if(count($parent_list)==0){
  260. break;
  261. }
  262. else{
  263. $word_list=array();
  264. foreach($parent_list as $x=>$value){
  265. array_push($word_list,$x);
  266. }
  267. }
  268. }
  269. //查询结束
  270. //删除无效数据
  271. $newOutput = array();
  272. foreach($output as $value){
  273. if($value["dict_name"]=="auto"){
  274. if(isset($dict_word_spell["{$value["parent"]}"])){
  275. array_push($newOutput,$value);
  276. }
  277. }
  278. else
  279. {
  280. array_push($newOutput,$value);
  281. }
  282. }
  283. if($debug){
  284. echo "<textarea width=\"100%\" >";
  285. echo json_encode($newOutput, JSON_UNESCAPED_UNICODE);
  286. echo "</textarea>";
  287. }
  288. if($debug){
  289. echo "生成:".count($output)."<br>";
  290. echo "有效:".count($newOutput)."<br>";
  291. }
  292. //开始匹配
  293. $counter=0;
  294. $output=array();
  295. foreach($FetchAllWord as $word){
  296. $pali=$word["real"];
  297. $type="";
  298. $gramma="";
  299. $mean="";
  300. $parent="";
  301. $parts="";
  302. $partmean="";
  303. foreach($newOutput as $dictword){
  304. if($dictword["pali"]==$pali){
  305. if($type=="" && $gramma==""){
  306. $type=$dictword["type"];
  307. $gramma=$dictword["gramma"];
  308. }
  309. if(trim($mean)=="" ){
  310. $mean=str_getcsv($dictword["mean"],"$")[0];
  311. }
  312. if($parent=="" ){
  313. $parent=$dictword["parent"];
  314. }
  315. if($parts=="" ){
  316. $parts=$dictword["parts"];
  317. }
  318. if($partmean==""){
  319. $partmean=$dictword["partmean"];
  320. }
  321. }
  322. }
  323. if($mean=="" && $parent!=""){
  324. foreach($newOutput as $parentword){
  325. if($parentword["pali"]==$parent){
  326. if($parentword["mean"]!=""){
  327. $mean=trim(str_getcsv($parentword["mean"],"$")[0]);
  328. if($mean!=""){
  329. break;
  330. }
  331. }
  332. }
  333. }
  334. }
  335. if( $type!="" ||
  336. $gramma!="" ||
  337. $mean!="" ||
  338. $parent!="" ||
  339. $parts!="" ||
  340. $partmean!=""){
  341. $counter++;
  342. }
  343. array_push($output,
  344. array("book"=>$in_book,
  345. "paragraph"=>$word["paragraph"],
  346. "num"=>$word["wid"],
  347. "pali"=>$word["real"],
  348. "type"=>$type,
  349. "gramma"=>$gramma,
  350. "mean"=>$mean,
  351. "parent"=>$parent,
  352. "parts"=>$parts,
  353. "partmean"=>$partmean,
  354. "status"=>3
  355. ));
  356. }
  357. if($debug){
  358. echo "<textarea width=\"100%\" >";
  359. }
  360. echo json_encode($output, JSON_UNESCAPED_UNICODE);
  361. if($debug){
  362. echo "</textarea>";
  363. }
  364. if($debug){
  365. echo "匹配".(($counter/count($FetchAllWord))*100)."<br>";
  366. foreach($output as $result){
  367. //echo "{$result["pali"]}-{$result["mean"]}-{$result["parent"]}<br>";
  368. }
  369. $queryTime=(microtime_float()-$time_start)*1000;
  370. echo "<div >搜索时间:$queryTime 毫秒</div>";
  371. }
  372. ?>