dict_getwordlist.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. include "./_pdo.php";
  3. $mode= $_GET["mode"];
  4. switch($mode){
  5. case "xml":
  6. $xmlFileName = $_GET["filename"];
  7. break;
  8. case "word":
  9. $word=$_GET["word"];
  10. break;
  11. case "words":
  12. break;
  13. }
  14. $dictFileName = $_GET["dict"];
  15. if($mode==""){
  16. $mode="xml";
  17. }
  18. $countInsert=0;
  19. $wordlist=array();
  20. switch($mode){
  21. case "xml":
  22. $xml = simplexml_load_file($xmlFileName);
  23. //get word list from xml documnt
  24. $wordsSutta = $xml->xpath('//word');
  25. /*prepare words from xmldoc and remove same words*/
  26. foreach($wordsSutta as $ws){
  27. $pali = $ws->pali;
  28. $arrlength=count($wordlist);
  29. $found=false;
  30. for($x=0;$x<$arrlength;$x++) {
  31. if(strcmp($pali,$wordlist[$x])==0){
  32. $found=true;
  33. break;
  34. }
  35. }
  36. if(!$found){
  37. $srcWord = str_replace("-","",lcfirst($pali));
  38. $srcWord = str_replace("’","",lcfirst($pali));
  39. $srcWord = str_replace(".","",lcfirst($pali));
  40. $srcWord = str_replace("ŋ","ṃ",lcfirst($pali));
  41. $wordlist[$countInsert]=str_replace("-","",lcfirst($pali));/*首字符转为小写 first letter convert to lowercase */
  42. $countInsert++;
  43. }
  44. }
  45. $arrlength=count($wordlist);/*size of word list*/
  46. break;
  47. case "word":
  48. $wordlist[0]=$word;
  49. $arrlength=1;
  50. break;
  51. case "words":
  52. break;
  53. default:
  54. }
  55. /*
  56. prepare case ending table
  57. in the csv file,one record have one kinds of case
  58. so we combine the same form of case ending
  59. */
  60. $caseRowCounter=0;
  61. $casetable = array();
  62. if(($handle=fopen('caseend.csv','r'))!==FALSE){
  63. while(($data=fgetcsv($handle,0,','))!==FALSE){
  64. $find=false;
  65. for($i=0;$i<count($casetable);$i++){
  66. if($casetable[$i][0]==$data[0] && $casetable[$i][1]==$data[1]){
  67. $casetable[$i][2]=$casetable[$i][2]."$".$data[2];
  68. $find=true;
  69. break;
  70. }
  71. }
  72. if($find==false){
  73. $casetable[$caseRowCounter]=$data;
  74. $caseRowCounter++;
  75. }
  76. }
  77. }
  78. $outXml = "<wordlist>";
  79. $db_path="dict/";
  80. $db_file = $db_path.$dictFileName;
  81. //open database
  82. PDO_Connect("sqlite:$db_file");
  83. for($x=0;$x<$arrlength;$x++){
  84. if(mb_strlen($wordlist[$x])>1){
  85. //先直接查,查不到再用语尾表查
  86. $query = "select * from tptdict where \"word\"='".$wordlist[$x]."' ORDER BY rowid DESC ";
  87. $Fetch = PDO_FetchAll($query);
  88. $iFetch=count($Fetch);
  89. if($iFetch>0){
  90. for($i=0;$i<$iFetch;$i++){
  91. if(strcmp($Fetch[$i]["org"],'?')==0 && strcmp($Fetch[$i]["mean"],'?')==0 && strcmp($Fetch[$i]["gramma"],'?')==0 ){
  92. continue;
  93. }
  94. $outXml = $outXml."<word>";
  95. $outXml = $outXml."<pali>".$wordlist[$x]."</pali>";
  96. $outXml = $outXml."<org>".$Fetch[$i]["org"]."</org>";
  97. $outXml = $outXml."<mean>".$Fetch[$i]["mean"]."</mean>";
  98. $outXml = $outXml."<case>".$Fetch[$i]["gramma"]."</case>";
  99. $outXml = $outXml."</word>";
  100. }
  101. }/*直接查询结束*/
  102. else{/*没查到,用语尾表查*/
  103. for($iCase=0;$iCase<count($casetable);$iCase++){
  104. if(substr($wordlist[$x],0-strlen($casetable[$iCase][0]))==$casetable[$iCase][0]){
  105. $pali=substr($wordlist[$x],0,strlen($wordlist[$x])-strlen($casetable[$iCase][0]));
  106. $pali=$pali.$casetable[$iCase][1];
  107. $query = "select * from tptdict where \"word\"='".$pali."' ORDER BY rowid DESC ";
  108. $Fetch = PDO_FetchAll($query);
  109. $iFetch=count($Fetch);
  110. if($iFetch>0){
  111. for($i=0;$i<$iFetch;$i++){
  112. if(strcmp($Fetch[$i]["org"],'?')==0 && strcmp($Fetch[$i]["mean"],'?')==0 && strcmp($Fetch[$i]["gramma"],'?')==0 ){
  113. continue;
  114. }
  115. $outXml = $outXml."<word>";
  116. $outXml = $outXml."<pali>".$wordlist[$x]."</pali>";
  117. $outXml = $outXml."<org>".$Fetch[$i]["word"]."@".$casetable[$iCase][0]."$".$Fetch[$i]["org"]."</org>";
  118. $outXml = $outXml."<mean>".$Fetch[$i]["mean"]."</mean>";
  119. $outXml = $outXml."<case>".$Fetch[$i]["gramma"]."$".$casetable[$iCase][2]."</case>";
  120. $outXml = $outXml."</word>";
  121. }
  122. }
  123. }
  124. }
  125. }
  126. /*全部查询结束*/
  127. }
  128. }
  129. $outXml = $outXml."</wordlist>";
  130. echo $outXml;
  131. ?>