dict_find.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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("$db_file");
  83. for ($x = 0; $x < $arrlength; $x++) {
  84. if (mb_strlen($wordlist[$x]) > 1) {
  85. //先直接查,查不到再用语尾表查
  86. $query = "select * from dict where \"pali\"='" . $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. $outXml = $outXml . "<word>";
  92. $outXml = $outXml . "<pali>" . $wordlist[$x] . "</pali>";
  93. $outXml = $outXml . "<type>" . $Fetch[$i]["type"] . "</type>";
  94. $outXml = $outXml . "<gramma>" . $Fetch[$i]["gramma"] . "</gramma>";
  95. $outXml = $outXml . "<parent>" . $Fetch[$i]["parent"] . "</parent>";
  96. $outXml = $outXml . "<mean>" . $Fetch[$i]["mean"] . "</mean>";
  97. $outXml = $outXml . "<factors>" . $Fetch[$i]["factors"] . "</factors>";
  98. $outXml = $outXml . "<factormean>" . $Fetch[$i]["factormean"] . "</factormean>";
  99. $outXml = $outXml . "</word>";
  100. }
  101. } /*直接查询结束*/
  102. /*全部查询结束*/
  103. }
  104. }
  105. $outXml = $outXml . "</wordlist>";
  106. echo $outXml;