split.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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. #输出结果 ouput to json
  79. $wordlist = array();
  80. $needDeep = false;
  81. $preSandhi = preSandhi($oneword);
  82. if($preSandhi!==$oneword){
  83. $word_part["word"] = $preSandhi;
  84. $word_part["confidence"] = 1.0;
  85. $wordlist[] = $word_part;
  86. $new = split2($preSandhi);
  87. if($new!==$row){
  88. $word_part["word"] = $new;
  89. $word_part["confidence"] = $value;
  90. $wordlist[] = $word_part;
  91. #再处理一次
  92. $new2 = split2($new);
  93. if($new2!==$new){
  94. $word_part["word"] = $new2;
  95. $word_part["confidence"] = $value;
  96. $wordlist[] = $word_part;
  97. }
  98. $needDeep = false;
  99. }
  100. else{
  101. $needDeep = true;
  102. }
  103. }
  104. else{
  105. $new = split2($oneword);
  106. if($new!==$row){
  107. $word_part["word"] = $new;
  108. $word_part["confidence"] = $value;
  109. $wordlist[] = $word_part;
  110. #再处理一次
  111. $new2 = split2($new);
  112. if($new2!==$new){
  113. $word_part["word"] = $new2;
  114. $word_part["confidence"] = $value;
  115. $wordlist[] = $word_part;
  116. }
  117. $needDeep = false;
  118. }
  119. $needDeep = true;
  120. }
  121. if($needDeep){
  122. mySplit2($oneword, 0, false, 0, 0.2, 0.9, true, false);
  123. if(count($result) < 2){
  124. mySplit2($oneword, 0, $_express, 0, 0.2, 0.8, true, true);
  125. }
  126. if (isset($_POST["debug"])) {
  127. echo "正切:" . count($result) . "<br>\n";
  128. }
  129. if(count($result) < 2){
  130. mySplit2($oneword, 0, $_express, 0, 0.2, 0.8, false, true);
  131. }
  132. if (isset($_POST["debug"])) {
  133. echo "反切:" . count($result) . "<br>\n";
  134. }
  135. /*
  136. if (count($result) < 5) {
  137. #sandhi advance
  138. mySplit2($oneword, 0, $_express, 0, 0.8, 0.8, false, true);
  139. if (isset($_POST["debug"])) {
  140. echo "反切:" . count($result) . "\n";
  141. }
  142. }
  143. if (count($result) < 5) {
  144. #反向
  145. mySplit2($oneword, 0, $_express, 0, 0.8, 0.8, false);
  146. }
  147. if (count($result) < 5) {
  148. #正向
  149. mySplit2($oneword, 0, $_express, 0, 0.8, 0, true);
  150. }
  151. if (count($result) < 5) {
  152. #反向
  153. mySplit2($oneword, 0, $_express, 0, 0.8, 0, false);
  154. }
  155. */
  156. arsort($result); //按信心指数排序
  157. $iMax = 5;
  158. $iCount = 0;
  159. foreach ($result as $row => $value) {
  160. $iCount++;
  161. $word_part = array();
  162. $word_part["word"] = $row;
  163. $word_part["confidence"] = $value;
  164. $wordlist[] = $word_part;
  165. //后处理 进一步切分没有意思的长词
  166. $new = split2($row);
  167. if($new!==$row){
  168. $word_part["word"] = $new;
  169. $word_part["confidence"] = $value;
  170. $wordlist[] = $word_part;
  171. #再处理一次
  172. $new2 = split2($new);
  173. if($new2!==$new){
  174. $word_part["word"] = $new2;
  175. $word_part["confidence"] = $value;
  176. $wordlist[] = $word_part;
  177. }
  178. }
  179. if ($iCount >= $iMax) {
  180. break;
  181. }
  182. }
  183. }
  184. $output[] = $wordlist;
  185. if (isset($_POST["debug"])) {
  186. echo "<h2>{$oneword}</h2>";
  187. echo "<h4>" . count($result) . "</h4>";
  188. }
  189. $iCount = 0;
  190. foreach ($result as $row => $value) {
  191. if ($iCount > 100) {
  192. break;
  193. }
  194. $iCount++;
  195. $level = $value * 90;
  196. if (isset($_POST["debug"])) {
  197. echo $row . "-[" . $value . "]<br>";
  198. }
  199. }
  200. /*
  201. 后处理
  202. -ssāpi=-[ssa]-api
  203. */
  204. }
  205. $t2 = microtime_float();
  206. $one_split["data"] = $output;
  207. $one_split["time"] = $auto_split_times;
  208. $one_split["second"] = $t2 - $t1;
  209. $allword[] = $one_split;
  210. if (isset($_POST["debug"])) {
  211. echo "<div>";
  212. echo "<br>查询【{$auto_split_times}】次";
  213. echo "time:" . ($t2 - $t1);
  214. echo "</div>";
  215. }
  216. }
  217. if (isset($_POST["debug"])) {
  218. echo "<pre style='margin:2em;padding:1em;background-color:#e9e9e9;'>";
  219. print_r($allword);
  220. echo "</pre>";
  221. }
  222. echo json_encode($allword, JSON_UNESCAPED_UNICODE);
  223. ?>