dict_find_auto.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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(isset($one["part_id"])){
  166. $part_id = $one["part_id"];
  167. }
  168. else{
  169. $part_id = "";
  170. }
  171. if(isset($one["status"])){
  172. $status = $one["status"];
  173. }
  174. else{
  175. $status = "";
  176. }
  177. if(isset($one["dict_name"])){
  178. $dict_name = $one["dict_name"];
  179. }
  180. else{
  181. $dict_name = "";
  182. }
  183. if(isset($one["language"])){
  184. $language = $one["language"];
  185. }
  186. else{
  187. $language = "en";
  188. }
  189. array_push($output,array(
  190. "id"=>$id,
  191. "guid"=>$guid,
  192. "pali"=>$pali,
  193. "type"=>$type,
  194. "gramma"=>$gramma,
  195. "parent"=>$parent,
  196. "mean"=>$mean,
  197. "note"=>$note,
  198. "parts"=>$parts,
  199. "part_id"=>$part_id,
  200. "partmean"=>$partmean,
  201. "status"=>$status,
  202. "dict_name"=>$dict_name,
  203. "language"=>$language
  204. ));
  205. if(!empty($parent)){
  206. if($pali != $parent){
  207. $parent_list[$one["parent"]]=1;
  208. }
  209. }
  210. if($type!="part"){
  211. if(isset($one["factors"])){
  212. $parts=str_getcsv($one["factors"],'+');
  213. foreach($parts as $x){
  214. if(!empty($x)){
  215. if($x != $pali){
  216. $parent_list[$x]=1;
  217. }
  218. }
  219. }
  220. }
  221. }
  222. }
  223. }
  224. $PDO = null;
  225. }
  226. /*
  227. if($i==0){
  228. //自动查找单词词干
  229. $word_base=getPaliWordBase($in_word);
  230. foreach($word_base as $x=>$infolist){
  231. foreach($infolist as $gramma){
  232. array_push($output,
  233. array("pali"=>$in_word,
  234. "type"=>$gramma["type"],
  235. "gramma"=>$gramma["gramma"],
  236. "mean"=>"",
  237. "parent"=>$x,
  238. "parts"=>$gramma["parts"],
  239. "partmean"=>"",
  240. "language"=>"en",
  241. "dict_name"=>"auto",
  242. "status"=>128
  243. ));
  244. $part_list=str_getcsv($gramma["parts"],"+");
  245. foreach($part_list as $part){
  246. $parent_list[$part]=1;
  247. }
  248. }
  249. }
  250. }
  251. */
  252. if($debug){
  253. echo "parent:".count($parent_list)."<br>";
  254. //print_r($parent_list)."<br>";
  255. }
  256. if(count($parent_list)==0){
  257. break;
  258. }
  259. else{
  260. $word_list=array();
  261. foreach($parent_list as $x=>$value){
  262. array_push($word_list,$x);
  263. }
  264. }
  265. }
  266. //查询结束
  267. //删除无效数据
  268. $newOutput = array();
  269. foreach($output as $value){
  270. if($value["dict_name"]=="auto"){
  271. if(isset($dict_word_spell["{$value["parent"]}"])){
  272. array_push($newOutput,$value);
  273. }
  274. }
  275. else
  276. {
  277. array_push($newOutput,$value);
  278. }
  279. }
  280. if($debug){
  281. echo "<textarea width=\"100%\" >";
  282. echo json_encode($newOutput, JSON_UNESCAPED_UNICODE);
  283. echo "</textarea>";
  284. }
  285. if($debug){
  286. echo "生成:".count($output)."<br>";
  287. echo "有效:".count($newOutput)."<br>";
  288. }
  289. //开始匹配
  290. $counter=0;
  291. $output=array();
  292. foreach($FetchAllWord as $word){
  293. $pali=$word["real"];
  294. $type="";
  295. $gramma="";
  296. $mean="";
  297. $parent="";
  298. $parts="";
  299. $partmean="";
  300. foreach($newOutput as $dictword){
  301. if($dictword["pali"]==$pali){
  302. if($type=="" && $gramma==""){
  303. $type=$dictword["type"];
  304. $gramma=$dictword["gramma"];
  305. }
  306. if(trim($mean)=="" ){
  307. $mean=str_getcsv($dictword["mean"],"$")[0];
  308. }
  309. if($parent=="" ){
  310. $parent=$dictword["parent"];
  311. }
  312. if($parts=="" ){
  313. $parts=$dictword["parts"];
  314. }
  315. if($partmean==""){
  316. $partmean=$dictword["partmean"];
  317. }
  318. }
  319. }
  320. if($mean=="" && $parent!=""){
  321. foreach($newOutput as $parentword){
  322. if($parentword["pali"]==$parent){
  323. if($parentword["mean"]!=""){
  324. $mean=trim(str_getcsv($parentword["mean"],"$")[0]);
  325. if($mean!=""){
  326. break;
  327. }
  328. }
  329. }
  330. }
  331. }
  332. if( $type!="" ||
  333. $gramma!="" ||
  334. $mean!="" ||
  335. $parent!="" ||
  336. $parts!="" ||
  337. $partmean!=""){
  338. $counter++;
  339. }
  340. array_push($output,
  341. array("book"=>$in_book,
  342. "paragraph"=>$word["paragraph"],
  343. "num"=>$word["wid"],
  344. "pali"=>$word["real"],
  345. "type"=>$type,
  346. "gramma"=>$gramma,
  347. "mean"=>$mean,
  348. "parent"=>$parent,
  349. "parts"=>$parts,
  350. "partmean"=>$partmean,
  351. "status"=>3
  352. ));
  353. }
  354. if($debug){
  355. echo "<textarea width=\"100%\" >";
  356. }
  357. echo json_encode($output, JSON_UNESCAPED_UNICODE);
  358. if($debug){
  359. echo "</textarea>";
  360. }
  361. if($debug){
  362. echo "匹配".(($counter/count($FetchAllWord))*100)."<br>";
  363. foreach($output as $result){
  364. //echo "{$result["pali"]}-{$result["mean"]}-{$result["parent"]}<br>";
  365. }
  366. $queryTime=(microtime_float()-$time_start)*1000;
  367. echo "<div >搜索时间:$queryTime 毫秒</div>";
  368. }
  369. ?>