split.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. <?php
  2. //强力拆分复合词
  3. /*
  4. function: split compound word
  5. step 1 : split at diphthong . ~aa~ -> ~a-a~
  6. 第一步:先切开双元音
  7. step 2 : every part use sandhi rule
  8. 第二步:用$sandhi的方法切分(套用连音规则)
  9. algorithm:
  10. 算法:
  11. f(word){
  12. 1. cut one letter from the end of word by sandhi rule in array($sandhi)
  13. 1. 从单词尾部切去一个字母
  14. 2. lookup first part .
  15. 2. 查询剩余部分
  16. if confidence value>0.8
  17. 如果有结果
  18. - get the confidence value
  19. 获取该部分的信心指数
  20. - process the remaining part at same way
  21. 用同样的方法处理剩余部分
  22. - f(stack.first element)
  23. else
  24. apply other sandhi rule
  25. back to 1
  26. }
  27. this is a recursion, depth=16
  28. 此为递归算法,深度=16
  29. */
  30. require_once "../dict/turbo_split.php";
  31. //check input
  32. if (isset($_POST["word"])) {
  33. $input_word = mb_strtolower(trim($_POST["word"]), 'UTF-8');
  34. if (trim($input_word) == "") {
  35. echo "Empty";
  36. exit;
  37. }
  38. $arrWords = str_getcsv($input_word, "\n"); //支持批量拆分
  39. } else {
  40. ?>
  41. <!--debug only-->
  42. <form action="split.php" method="post">
  43. Words: <br>
  44. <textarea type="text" name="word" style="width:50em;height:20em;"></textarea><br>
  45. <input name="debug" type="hidden" />批量查询,单词之间用换行分隔。 input word. between two words insert 'enter'
  46. <div>
  47. <input type="checkbox" name = "express" checked /> 快速搜索(遇到第一个连音规则成功就返回) return when get first result
  48. </div>
  49. <input type="submit">
  50. </form>
  51. <?php
  52. return;
  53. }
  54. if (isset($_POST["express"])) {
  55. if ($_POST["express"] === "on") {
  56. $_express = true;
  57. } else {
  58. $_express = false;
  59. }
  60. } else {
  61. $_express = false;
  62. }
  63. //main
  64. $allword = array();
  65. foreach ($arrWords as $currword) {
  66. $t1 = microtime_float();
  67. $output = array();
  68. if (isset($_POST["debug"])) {
  69. echo "Look up:{$currword}<br>";
  70. }
  71. //预处理
  72. //将双元音拆开
  73. //step 1 : split at diphthong . ~aa~ -> ~a-a~
  74. //按连字符拆开处理
  75. $arrword = split_diphthong($currword);
  76. foreach ($arrword as $oneword) {
  77. $result = array(); //全局变量,递归程序的输出容器
  78. mySplit2($oneword, 0, $_express, 0, 0.1, 0.01, true, true);
  79. if (isset($_POST["debug"])) {
  80. echo "正切:" . count($result) . "\n";
  81. }
  82. mySplit2($oneword, 0, $_express, 0, 0.1, 0.01, false, true);
  83. if (isset($_POST["debug"])) {
  84. echo "反切:" . count($result) . "\n";
  85. }
  86. /*
  87. if (count($result) < 5) {
  88. #sandhi advance
  89. mySplit2($oneword, 0, $_express, 0, 0.8, 0.8, false, true);
  90. if (isset($_POST["debug"])) {
  91. echo "反切:" . count($result) . "\n";
  92. }
  93. }
  94. if (count($result) < 5) {
  95. #反向
  96. mySplit2($oneword, 0, $_express, 0, 0.8, 0.8, false);
  97. }
  98. if (count($result) < 5) {
  99. #正向
  100. mySplit2($oneword, 0, $_express, 0, 0.8, 0, true);
  101. }
  102. if (count($result) < 5) {
  103. #反向
  104. mySplit2($oneword, 0, $_express, 0, 0.8, 0, false);
  105. }
  106. */
  107. arsort($result); //按信心指数排序
  108. #输出结果 ouput to json
  109. $wordlist = array();
  110. $iMax = 5;
  111. $iCount = 0;
  112. foreach ($result as $row => $value) {
  113. $iCount++;
  114. $word_part = array();
  115. $word_part["word"] = $row;
  116. $word_part["confidence"] = $value;
  117. $wordlist[] = $word_part;
  118. if ($iCount >= $iMax) {
  119. break;
  120. }
  121. }
  122. $output[] = $wordlist;
  123. if (isset($_POST["debug"])) {
  124. echo "<h2>{$oneword}</h2>";
  125. echo "<h4>" . count($result) . "</h4>";
  126. }
  127. $iCount = 0;
  128. foreach ($result as $row => $value) {
  129. if ($iCount > 10) {
  130. break;
  131. }
  132. $iCount++;
  133. $level = $value * 90;
  134. if (isset($_POST["debug"])) {
  135. echo $row . "-[" . $value . "]<br>";
  136. }
  137. }
  138. /*
  139. 后处理
  140. -ssāpi=-[ssa]-api
  141. */
  142. }
  143. $t2 = microtime_float();
  144. $one_split["data"] = $output;
  145. $one_split["time"] = $auto_split_times;
  146. $one_split["second"] = $t2 - $t1;
  147. $allword[] = $one_split;
  148. if (isset($_POST["debug"])) {
  149. echo "<div>";
  150. echo "<br>查询【{$auto_split_times}】次";
  151. echo "time:" . ($t2 - $t1);
  152. echo "</div>";
  153. }
  154. }
  155. if (isset($_POST["debug"])) {
  156. echo "<pre style='margin:2em;padding:1em;background-color:#e9e9e9;'>";
  157. print_r($allword);
  158. echo "</pre>";
  159. }
  160. echo json_encode($allword, JSON_UNESCAPED_UNICODE);
  161. ?>