split.php 6.1 KB

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