TurboSplit.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842
  1. <?php
  2. namespace App\Tools;
  3. require_once __DIR__.'/../../public/app/public/casesuf.inc';
  4. use Illuminate\Support\Facades\Cache;
  5. use Illuminate\Support\Facades\Log;
  6. use Illuminate\Support\Facades\DB;
  7. use App\Models\WordPart;
  8. use App\Models\UserDict;
  9. use Illuminate\Support\Arr;
  10. class TurboSplit
  11. {
  12. protected $options = [
  13. "express" => false,
  14. "c_threshhold" => 0.8,
  15. "w_threshhold" => 0.8,
  16. "forward" => true,
  17. "sandhi_advance" => false,
  18. "lookup_express" => true,/**快速查字典-不去尾 */
  19. ];
  20. protected $node = [];
  21. protected $path = array();
  22. protected $isDebug = false;
  23. #当前搜索路径信心指数,如果过低,马上终止这个路径的搜索
  24. protected $currPathCf;
  25. //结果数组
  26. protected $result = array();
  27. //过程中最大结果数量
  28. protected $MAX_RESULT = 100;
  29. //返回值最大结果数量
  30. protected $MAX_RESULT2 = 5;
  31. //最大递归深度
  32. protected $MAX_DEEP = 16;
  33. //连音规则表
  34. protected $sandhi = [
  35. ["a" => "", "b" => "", "c" => "", "len" => 0, "adj_len" => 0, "advance" => false,"cf"=>1.0],
  36. ["a" => "a", "b" => "a", "c" => "ā", "len" => 1, "adj_len" => 0, "advance" => false,"cf"=>0.99999],
  37. ["a" => "ā", "b" => "ā", "c" => "ā", "len" => 1, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  38. ["a" => "a", "b" => "ā", "c" => "ā", "len" => 1, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  39. ["a" => "ā", "b" => "a", "c" => "ā", "len" => 1, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  40. ["a" => "a", "b" => "e", "c" => "e", "len" => 1, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  41. ["a" => "a", "b" => "i", "c" => "i", "len" => 1, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  42. ["a" => "a", "b" => "o", "c" => "o", "len" => 1, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  43. ["a" => "a", "b" => "u", "c" => "o", "len" => 1, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  44. ["a" => "u", "b" => "a", "c" => "o", "len" => 1, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  45. ["a" => "u", "b" => "u", "c" => "ū", "len" => 1, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  46. ["a" => "a", "b" => "u", "c" => "u", "len" => 1, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  47. ["a" => "a", "b" => "ī", "c" => "ī", "len" => 1, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  48. ["a" => "a", "b" => "ū", "c" => "ū", "len" => 1, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  49. ["a" => "a", "b" => "i", "c" => "e", "len" => 1, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  50. ["a" => "e", "b" => "a", "c" => "e", "len" => 1, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  51. ["a" => "i", "b" => "i", "c" => "ī", "len" => 1, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  52. ["a" => "i", "b" => "e", "c" => "e", "len" => 1, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  53. ["a" => "i", "b" => "a", "c" => "ya", "len" => 2, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  54. ["a" => "a", "b" => "atth", "c" => "atth", "len" => 4, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  55. ["a" => "taṃ", "b" => "n", "c" => "tann", "len" => 4, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  56. ["a" => "[ṃ]", "b" => "api", "c" => "mpi", "len" => 3, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  57. ["a" => "[ṃ]", "b" => "eva", "c" => "meva", "len" => 4, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  58. ["a" => "[o]", "b" => "iva", "c" => "ova", "len" => 3, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  59. ["a" => "o", "b" => "a", "c" => "o", "len" => 1, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  60. ["a" => "a", "b" => "ādi", "c" => "ādi", "len" => 3, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  61. ["a" => "a[ānaṃ]", "b" => "a", "c" => "ānama", "len" => 5, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  62. ["a" => "a", "b" => "iti", "c" => "āti", "len" => 3, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  63. ["a" => "[ṃ]", "b" => "ca", "c" => "ñca", "len" => 3, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  64. ["a" => "[ṃ]", "b" => "iti", "c" => "nti", "len" => 3, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  65. ["a" => "[ṃ]", "b" => "a", "c" => "ma", "len" => 2, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  66. ["a" => "ṃ", "b" => "a", "c" => "m", "len" => 1, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  67. ["a" => "[ṃ]", "b" => "ā", "c" => "mā", "len" => 2, "adj_len" => 0, "advance" => false,"cf"=>0.8],
  68. ["a" => "ṃ", "b" => "ā", "c" => "mā", "len" => 2, "adj_len" => 0, "advance" => false,"cf"=>0.9],
  69. ["a" => "[ṃ]", "b" => "u", "c" => "mu", "len" => 2, "adj_len" => 0, "advance" => false,"cf"=>0.8],
  70. ["a" => "[ṃ]", "b" => "h", "c" => "ñh", "len" => 2, "adj_len" => 0, "advance" => false,"cf"=>0.8],
  71. ["a" => "ā", "b" => "[ṃ]", "c" => "am", "len" => 2, "adj_len" => 0, "advance" => false,"cf"=>0.8],
  72. ["a" => "a", "b" => "[ṃ]", "c" => "am", "len" => 2, "adj_len" => 0, "advance" => false,"cf"=>0.8],
  73. ["a" => "ī", "b" => "[ṃ]", "c" => "im", "len" => 2, "adj_len" => 0, "advance" => false,"cf"=>0.8],
  74. ["a" => "ati", "b" => "tabba", "c" => "atabba", "len" => 6, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  75. ["a" => "ati", "b" => "tabba", "c" => "itabba", "len" => 6, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  76. ["a" => "iti", "b" => "a", "c" => "icca", "len" => 4, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  77. ["a" => "uṃ", "b" => "a", "c" => "uma", "len" => 3, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  78. ["a" => "u[ūnaṃ]", "b" => "a", "c" => "ūnama", "len" => 5, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  79. ["a" => "ī[īnaṃ]", "b" => "a", "c" => "īnama", "len" => 5, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  80. ["a" => "su", "b" => "a", "c" => "sva", "len" => 3, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  81. ["a" => "ā", "b" => "iti", "c" => "āti", "len" => 3, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  82. ["a" => "a", "b" => "iti", "c" => "āti", "len" => 3, "adj_len" => 0, "advance" => false,"cf"=>0.99999],
  83. ["a" => "e", "b" => "iti", "c" => "eti", "len" => 3, "adj_len" => 0, "advance" => false,"cf"=>0.99999],
  84. ["a" => "ī", "b" => "iti", "c" => "īti", "len" => 3, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  85. ["a" => "i", "b" => "iti", "c" => "īti", "len" => 3, "adj_len" => 0, "advance" => false,"cf"=>0.99999],
  86. ["a" => "o", "b" => "iti", "c" => "oti", "len" => 3, "adj_len" => 0, "advance" => false,"cf"=>0.99999],
  87. ["a" => "ū", "b" => "iti", "c" => "ūti", "len" => 3, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  88. ["a" => "u", "b" => "iti", "c" => "ūti", "len" => 3, "adj_len" => 0, "advance" => false,"cf"=>0.99999],
  89. ["a" => "ṃ", "b" => "iti", "c" => "nti", "len" => 3, "adj_len" => 0, "advance" => false,"cf"=>0.99999],
  90. ["a" => "ṃ", "b" => "ca", "c" => "ñca", "len" => 3, "adj_len" => 0, "advance" => false,"cf"=>1.0],
  91. ["a" => "ṃ", "b" => "cāti", "c" => "ñcāti", "len" => 5, "adj_len" => 0, "advance" => false,"cf"=>1.0],
  92. ["a" => "ṃ", "b" => "cet", "c" => "ñcet", "len" => 4, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  93. ["a" => "ṃ", "b" => "ev", "c" => "mev", "len" => 3, "adj_len" => 0, "advance" => false,"cf"=>0.99999],
  94. ["a" => "a", "b" => "a", "c" => "a", "len" => 1, "adj_len" => -1, "advance" => true,"cf"=>0.99],
  95. ["a" => "ī", "b" => "", "c" => "i", "len" => 1, "adj_len" => 0, "advance" => true,"cf"=>0.9],
  96. ];
  97. protected $sandhi2 = [
  98. ["a" => "ṃ", "b" => "ca", "c" => "ñca", "len" => 3, "adj_len" => 0, "advance" => false,"cf"=>1.0],
  99. ["a" => "ṃ", "b" => "hi", "c" => "ñhi", "len" => 3, "adj_len" => 0, "advance" => false,"cf"=>1.0],
  100. ["a" => "ena", "b" => "iti", "c" => "enāti", "len" => 5, "adj_len" => 0, "advance" => false,"cf"=>1.0],
  101. ["a" => "a", "b" => "iti", "c" => "āti", "len" => 3, "adj_len" => 0, "advance" => false,"cf"=>0.9],
  102. ["a" => "ā", "b" => "iti", "c" => "āti", "len" => 3, "adj_len" => 0, "advance" => false,"cf"=>0.6],
  103. ["a" => "e", "b" => "iti", "c" => "eti", "len" => 3, "adj_len" => 0, "advance" => false,"cf"=>0.9],
  104. ["a" => "i", "b" => "iti", "c" => "īti", "len" => 3, "adj_len" => 0, "advance" => false,"cf"=>0.9],
  105. ["a" => "o", "b" => "iti", "c" => "oti", "len" => 3, "adj_len" => 0, "advance" => false,"cf"=>0.9],
  106. ["a" => "u", "b" => "iti", "c" => "ūti", "len" => 3, "adj_len" => 0, "advance" => false,"cf"=>0.9],
  107. ["a" => "ṃ", "b" => "iti", "c" => "nti", "len" => 3, "adj_len" => 0, "advance" => false,"cf"=>0.9],
  108. ["a" => "ī", "b" => "eva", "c" => "iyeva", "len" => 5, "adj_len" => 0, "advance" => false,"cf"=>0.9],
  109. ["a" => "ī", "b" => "eva", "c" => "īyeva", "len" => 5, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  110. ["a" => "u", "b" => "eva", "c" => "uyeva", "len" => 5, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  111. ["a" => "ṃ", "b" => "eva", "c" => "ṃyeva", "len" => 5, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  112. ["a" => "i", "b" => "eva", "c" => "yeva", "len" => 4, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  113. ["a" => "o", "b" => "eva", "c" => "ova", "len" => 3, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  114. ["a" => "ṃ", "b" => "eva", "c" => "meva", "len" => 4, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  115. ["a" => "u", "b" => "eva", "c" => "veva", "len" => 4, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  116. ["a" => "a", "b" => "eva", "c" => "eva", "len" => 3, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  117. ["a" => "e", "b" => "eva", "c" => "eva", "len" => 3, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  118. ["a" => "a", "b" => "api", "c" => "āpi", "len" => 3, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  119. ["a" => "ā", "b" => "api", "c" => "āpi", "len" => 3, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  120. ["a" => "e", "b" => "api", "c" => "epi", "len" => 3, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  121. ["a" => "i", "b" => "api", "c" => "īpi", "len" => 3, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  122. ["a" => "ī", "b" => "api", "c" => "īpi", "len" => 3, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  123. ["a" => "o", "b" => "api", "c" => "opi", "len" => 3, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  124. ["a" => "u", "b" => "api", "c" => "ūpi", "len" => 3, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  125. ["a" => "ū", "b" => "api", "c" => "ūpi", "len" => 3, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  126. ["a" => "u", "b" => "api", "c" => "upi", "len" => 3, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  127. ["a" => "ṃ", "b" => "api", "c" => "mpi", "len" => 3, "adj_len" => 0, "advance" => false,"cf"=>0.9999],
  128. ];
  129. /**
  130. * Create a new class instance.
  131. *
  132. * @return void
  133. */
  134. public function __construct()
  135. {
  136. for($i=0;$i<$this->MAX_DEEP;$i++ ){
  137. array_push($this->path, array("", 0));
  138. }
  139. return;
  140. }
  141. /**
  142. * 从双元音处切开
  143. * @param string $word
  144. * @return void
  145. */
  146. public function splitDiphthong($word)
  147. {
  148. //diphthong table双元音表
  149. $search = array('aa', 'ae', 'ai', 'ao', 'au', 'aā', 'aī', 'aū', 'ea', 'ee', 'ei', 'eo', 'eu', 'eā', 'eī', 'eū', 'ia', 'ie', 'ii', 'io', 'iu', 'iā', 'iī', 'iū', 'oa', 'oe', 'oi', 'oo', 'ou', 'oā', 'oī', 'oū', 'ua', 'ue', 'ui', 'uo', 'uu', 'uā', 'uī', 'uū', 'āa', 'āe', 'āi', 'āo', 'āu', 'āā', 'āī', 'āū', 'īa', 'īe', 'īi', 'īo', 'īu', 'īā', 'īī', 'īū', 'ūa', 'ūe', 'ūi', 'ūo', 'ūu', 'ūā', 'ūī', 'ūū');
  150. $replace = array('a-a', 'a-e', 'a-i', 'a-o', 'a-u', 'a-ā', 'a-ī', 'a-ū', 'e-a', 'e-e', 'e-i', 'e-o', 'e-u', 'e-ā', 'e-ī', 'e-ū', 'i-a', 'i-e', 'i-i', 'i-o', 'i-u', 'i-ā', 'i-ī', 'i-ū', 'o-a', 'o-e', 'o-i', 'o-o', 'o-u', 'o-ā', 'o-ī', 'o-ū', 'u-a', 'u-e', 'u-i', 'u-o', 'u-u', 'u-ā', 'u-ī', 'u-ū', 'ā-a', 'ā-e', 'ā-i', 'ā-o', 'ā-u', 'ā-ā', 'ā-ī', 'ā-ū', 'ī-a', 'ī-e', 'ī-i', 'ī-o', 'ī-u', 'ī-ā', 'ī-ī', 'ī-ū', 'ū-a', 'ū-e', 'ū-i', 'ū-o', 'ū-u', 'ū-ā', 'ū-ī', 'ū-ū');
  151. //将双元音拆开
  152. //step 1 : split at diphthong . ~aa~ -> ~a-a~
  153. $word1 = str_replace($search, $replace, $word);
  154. //按连字符拆开处理
  155. $arrword = str_getcsv($word1, "-");
  156. return $arrword;
  157. }
  158. /**
  159. * 查询单词是否存在
  160. * @param string $word
  161. * @return array(int $wordWeight, int $endingLenght)
  162. */
  163. public function dict_lookup($word)
  164. {
  165. global $case; //语尾表
  166. if (strlen($word) <= 1) {
  167. return array(0,0);
  168. }
  169. $search = $word;
  170. //获取单词权重
  171. $row = Cache::remember('palicanon/wordpart/weight/'.$search, 10 , function() use($search) {
  172. return WordPart::where('word',$search)->value('weight');
  173. });
  174. if ($row) {
  175. //找到
  176. return array($row,0);
  177. } else {
  178. if($this->options["lookup_express"]){
  179. return array(0,0);
  180. }
  181. //去除尾查
  182. $newWord = array();
  183. for ($row = 0; $row < count($case); $row++) {
  184. $len = mb_strlen($case[$row][1], "UTF-8");
  185. $end = mb_substr($search, 0 - $len, null, "UTF-8");
  186. if ($end == $case[$row][1]) {
  187. $base = mb_substr($search, 0, mb_strlen($search, "UTF-8") - $len, "UTF-8") . $case[$row][0];
  188. if ($base != $search) {
  189. $newWord[$base] = mb_strlen($case[$row][1],"UTF-8");
  190. }
  191. }
  192. }
  193. #找到最高频的base
  194. $base_weight = 0;
  195. $len = 0;
  196. foreach ($newWord as $x => $x_value) {
  197. $row = Cache::remember('palicanon/wordpart/weight/'.$search, 10 , function() use($x) {
  198. return WordPart::where('word',$x)->value('weight');
  199. });
  200. if ($row) {
  201. if ($row > $base_weight) {
  202. $base_weight = $row;
  203. $len=$x_value;
  204. }
  205. }
  206. }
  207. return array($base_weight,$len);
  208. }
  209. }
  210. /**
  211. * 查找某个单词是否在现有词典出现
  212. * 返回信心指数
  213. * look up single word in dictionary vocabulary
  214. * return the confidence value
  215. *
  216. *
  217. *
  218. */
  219. public function isExsit($word, $adj_len = 0){
  220. $this->log("正在查询:{$word}");
  221. $isFound = false;
  222. $count = 0;
  223. $wordPart = Cache::remember("turbosplit/part/{$word}",10,function() use($word){
  224. return implode(',',$this->dict_lookup($word));
  225. });
  226. $arrWordPart = explode(',',$wordPart);
  227. $word_count = $arrWordPart[0];
  228. $case_len = $arrWordPart[1];
  229. if ($word_count > 0) {
  230. $this->log("查到:{$word}:{$word_count}个");
  231. $isFound = true;
  232. $count = $word_count + 1;
  233. }
  234. //fomular of confidence value 信心值计算公式
  235. if ($isFound) {
  236. $cf = Cache::remember("turbosplit/confidence/".$word,1000,function() use($word,$count,$case_len){
  237. $len = mb_strlen($word, "UTF-8") - $case_len;
  238. $len_correct = 1.2;
  239. $count2 = 1.1 + pow($count, 1.18);
  240. $conf_num = pow(1 / $count2, pow(($len - 0.5), $len_correct));
  241. return round(1 / (1 + 640 * $conf_num), 9);
  242. });
  243. return ($cf);
  244. } else {
  245. return (-1);
  246. }
  247. }
  248. /**
  249. * word
  250. * cf
  251. * children[]
  252. */
  253. /**
  254. * 核心拆分函数
  255. * $strWord, word to be look up 要查询的词
  256. * $deep, 当前递归深度
  257. * $forward 搜索方向
  258. * true 正向
  259. * false 反向
  260. * $express=true, 快速查询
  261. * $adj_len=0 长度校正系数
  262. * $c_threshhold 信心指数阈值
  263. *
  264. *
  265. *
  266. */
  267. function split(&$node, $deep = 0, $express = false, $adj_len = 0, $c_threshhold = 0.8, $w_threshhold = 0.8, $forward = true, $sandhi_advance = false)
  268. {
  269. $strWord = $node["remain"];
  270. $this->log("spliting word={$strWord} deep={$deep}");
  271. $output = array();
  272. #currPathCf是当前搜索路径信心指数,如果过低,马上终止这个路径的搜索
  273. if($deep == 0){
  274. $this->currPathCf = 1;
  275. }
  276. //达到最大搜索深度,返回
  277. if ($deep >= $this->MAX_DEEP) {
  278. return ;
  279. /*
  280. $word = "";
  281. $cf = 1.0;
  282. for ($i = 0; $i < $deep; $i++) {
  283. if (!empty($this->path[$i][0])) {
  284. $word .= $this->path[$i][0] ;
  285. if($this->isDebug) {
  286. $word .= "(" . $this->path[$i][1] . ")";
  287. }
  288. $word .= "+";
  289. $cf = $cf * $this->path[$i][1];
  290. }
  291. }
  292. $len = pow(mb_strlen($strWord, "UTF-8"), 3);
  293. $cf += (0 - $len) / ($len + 150);
  294. $word .= "{$strWord}";
  295. if ($forward == true) {
  296. $this->result[$word] = $cf;
  297. return 0;
  298. } else {
  299. $reverseWord = $this->word_reverse($word);
  300. $this->result[$reverseWord] = $cf;
  301. return 0;
  302. }
  303. */
  304. }
  305. //直接找到
  306. $confidence = $this->isExsit($strWord, $adj_len);
  307. if ($confidence > $c_threshhold) {
  308. array_push($output, array($strWord, "", $confidence));
  309. if(isset($node['sum_cf'])){
  310. $parent_sum_cf = $node['sum_cf'];
  311. }else{
  312. $parent_sum_cf = 1;
  313. }
  314. $sum_cf = $parent_sum_cf * $confidence;
  315. $node['children'][] = ['word'=>$strWord,'remain'=>"",'cf'=>$confidence,"sum_cf"=>$sum_cf];
  316. $this->log("直接找到{$strWord}-{$confidence}");
  317. }
  318. else if(mb_strlen($strWord,"UTF-8")<6){
  319. //按照语尾查询
  320. $search = "[{$strWord}]";
  321. $confidence = $this->isExsit($search);
  322. $this->log("查询:{$search}-信心指数{$confidence}");
  323. if ($confidence > $c_threshhold) {
  324. array_push($output, array($search, "", $confidence));
  325. if(isset($node['sum_cf'])){
  326. $parent_sum_cf = $node['sum_cf'];
  327. }else{
  328. $parent_sum_cf = 1;
  329. }
  330. $sum_cf = $parent_sum_cf * $confidence;
  331. $node['children'][] = ['word'=>$search,'remain'=>"",'cf'=>$confidence,"sum_cf"=>$sum_cf];
  332. $this->log("直接找到{$strWord}-{$confidence}");
  333. }
  334. }
  335. //如果开头有双辅音,去掉第一个辅音。因为巴利语中没有以双辅音开头的单词。
  336. $doubleword = "kkggccjjṭṭḍḍttddppbb";
  337. if (mb_strlen($strWord, "UTF-8") > 2) {
  338. $left2 = mb_substr($strWord, 0, 2, "UTF-8");
  339. if (mb_strpos($doubleword, $left2, 0, "UTF-8") !== false) {
  340. $strWord = mb_substr($strWord, 1, null, "UTF-8");
  341. }
  342. }
  343. $len = mb_strlen($strWord, "UTF-8");
  344. if ($len > 2) {
  345. if ($forward) {
  346. #正向切
  347. $this->log("正向切");
  348. for ($i = $len; $i > 1; $i--) {
  349. //应用连音规则切分单词
  350. foreach ($this->sandhi as $key => $row) {
  351. if ($sandhi_advance == false && $row["advance"] == true) {
  352. //continue;
  353. }
  354. if (mb_substr($strWord, $i - $row["len"], $row["len"], "UTF-8") == $row["c"]) {
  355. $str1 = mb_substr($strWord, 0, $i - $row["len"], "UTF-8") . $row["a"];
  356. $str2 = $row["b"] . mb_substr($strWord, $i, null, "UTF-8");
  357. $confidence = $this->isExsit($str1, $adj_len)*$row["cf"];
  358. if ($confidence > $c_threshhold) {
  359. //信心指数大于预设的阈值,插入
  360. array_push($output, array($str1, $str2, $confidence, $row["adj_len"]));
  361. if(isset($node['sum_cf'])){
  362. $parent_sum_cf = $node['sum_cf'];
  363. }else{
  364. $parent_sum_cf = 1;
  365. }
  366. $sum_cf = $parent_sum_cf * $confidence;
  367. if($sum_cf>$c_threshhold){
  368. $node['children'][] = [
  369. 'word'=>$str1,
  370. 'remain'=>$str2,
  371. 'cf'=>$confidence,
  372. 'sum_cf'=>$sum_cf,
  373. 'children'=>[]
  374. ];
  375. }
  376. $this->log("插入结构数组:{$str1} 剩余{$str2} 应用:{$row["a"]}-{$row["b"]}-{$row["c"]}");
  377. if ($express) {
  378. break;
  379. }
  380. }
  381. }
  382. }
  383. }
  384. } else {
  385. #反向切
  386. for ($i = 1; $i < $len - 1; $i++) {
  387. foreach ($this->sandhi as $key => $row) {
  388. if ($sandhi_advance == false && $row["advance"] == true) {
  389. //continue;
  390. }
  391. if (mb_substr($strWord, $i, $row["len"], "UTF-8") == $row["c"]) {
  392. $str1 = mb_substr($strWord, 0, $i, "UTF-8") . $row["a"];
  393. $str2 = $row["b"] . mb_substr($strWord, $i + $row["len"], null, "UTF-8");
  394. $confidence = $this->isExsit($str2, $adj_len)*$row["cf"];
  395. if ($confidence > $c_threshhold) {
  396. array_push($output, array($str2, $str1, $confidence, $row["adj_len"]));
  397. if(isset($node['sum_cf'])){
  398. $parent_sum_cf = $node['sum_cf'];
  399. }else{
  400. $parent_sum_cf = 1;
  401. }
  402. $sum_cf = $parent_sum_cf * $confidence;
  403. if($sum_cf>$c_threshhold){
  404. $node['children'][] = [
  405. 'word'=>$str2,
  406. 'remain'=>$str1,
  407. 'cf'=>$confidence,
  408. 'sum_cf'=>$sum_cf,
  409. 'children'=>[],
  410. ];
  411. }
  412. $this->log("将此次结果插入结果数组:剩余={$str2}");
  413. if ($express) {
  414. break;
  415. }
  416. }
  417. }
  418. }
  419. }
  420. }
  421. }
  422. $word = "";
  423. $this->log("结果数组个数:".count($output));
  424. //print_r($node);
  425. //遍历children
  426. foreach ($node['children'] as $key => $child) {
  427. # code...
  428. if(isset($child) && !empty($child['remain'])){
  429. $this->split($node['children'][$key], ($deep + 1), $express, $adj_len, $c_threshhold, $w_threshhold, $forward, $sandhi_advance);
  430. }
  431. }
  432. /*
  433. if (count($output) > 0) {
  434. foreach ($output as $part) {
  435. $checked = $part[0];
  436. $remainder = $part[1];
  437. $this->log("剩余部分:{$remainder}");
  438. $this->path[$deep][0] = $checked;
  439. $this->path[$deep][1] = $part[2];
  440. if (empty($remainder)) {
  441. #全切完了
  442. $this->log("全切完了");
  443. $word = "";
  444. $cf = 1.0;
  445. for ($i = 0; $i < $deep; $i++) {
  446. $word .= $this->path[$i][0];
  447. if ($this->isDebug) {
  448. $word .= "(" . $this->path[$i][1] . ")";
  449. }
  450. $word .= "+";
  451. $cf = $cf * $this->path[$i][1];
  452. }
  453. if ($this->isDebug) {
  454. $word .= $checked . "({$part[2]})";
  455. } else {
  456. $word .= $checked;
  457. }
  458. $cf = $cf * $part[2];
  459. if ($cf > $w_threshhold) {
  460. if ($forward == true) {
  461. $this->result[$word] = $cf;
  462. return 0;
  463. } else {
  464. $reverseWord = $this->word_reverse($word);
  465. $this->result[$reverseWord] = $cf;
  466. return 0;
  467. }
  468. }
  469. } else {
  470. //还有剩余部分
  471. #计算当前信心指数
  472. $cf = 1.0;
  473. for ($i = 0; $i < $deep; $i++) {
  474. $cf = $cf * $this->path[$i][1];
  475. }
  476. $this->log("计算当前信心指数:{$cf}");
  477. if($cf<$w_threshhold){
  478. $this->log("信心指数过低,提前返回 {$cf}");
  479. return 0;
  480. }else{
  481. #接着切
  482. $this->log("接着切:{$remainder}");
  483. $this->split($remainder, ($deep + 1), $express, $adj_len, $c_threshhold, $w_threshhold, $forward, $sandhi_advance);
  484. }
  485. }
  486. }
  487. }else {
  488. #尾巴查不到了
  489. $this->log("尾巴查不到了");
  490. $word = "";
  491. $cf = 1.0;
  492. for ($i = 0; $i < $deep; $i++) {
  493. $word .= $this->path[$i][0];
  494. if ($this->isDebug) {
  495. $word .= "(" . $this->path[$i][1] . ")";
  496. }
  497. $word .= "+";
  498. $cf = $cf * $this->path[$i][1];
  499. }
  500. $len = pow(mb_strlen($strWord, "UTF-8"), 3);
  501. if ($forward) {
  502. $cf =(1-$cf) * $len / ($len + 150);
  503. } else {
  504. $cf =(1-$cf) * $len / ($len + 5);
  505. }
  506. if ($this->isDebug) {
  507. $word = $word.$strWord . "(0)";
  508. } else {
  509. $word = $word .$strWord;
  510. }
  511. if ($cf > $w_threshhold) {
  512. if ($forward == true) {
  513. $this->result[$word] = $cf;
  514. return 0;
  515. }
  516. else {
  517. $reverseWord = $this->word_reverse($word);
  518. $this->result[$reverseWord] = $cf;
  519. return 0;
  520. }
  521. }
  522. }
  523. */
  524. }
  525. /**
  526. * 遍历树状结构,获取结果
  527. */
  528. private function get_result($node,&$path){
  529. # code...
  530. $path[] = $node['word'];
  531. if(isset($node['children']) && count($node['children'])>0){
  532. foreach ($node['children'] as $key => $value) {
  533. $this->get_result($value,$path);
  534. }
  535. }else{
  536. if(empty($node['remain'])){
  537. $factors = trim(implode("+",$path),'+');
  538. $this->result[$factors] = $node['sum_cf'];
  539. }else{
  540. }
  541. }
  542. array_pop($path);
  543. }
  544. /**
  545. * 颠倒词序
  546. */
  547. public function word_reverse($word)
  548. {
  549. $reverse = array();
  550. $newword = explode("+", $word);
  551. $len = count($newword);
  552. if ($len > 0) {
  553. for ($i = $len - 1; $i >= 0; $i--) {
  554. # code...
  555. $reverse[] = $newword[$i];
  556. }
  557. $output = implode("+", $reverse);
  558. return $output;
  559. } else {
  560. return $word;
  561. }
  562. }
  563. /**
  564. * 拆分后的处理
  565. */
  566. public function split2($word){
  567. $input = explode("+",$word);
  568. $newword=array();
  569. foreach ($input as $value) {
  570. //去掉带小括号的调试信息
  571. $word = strstr($value,"(",true);
  572. if($word == false){
  573. $word = $value;
  574. }
  575. if(mb_strlen($word,"UTF-8")>4){
  576. # 先看有没有中文意思
  577. //$this->log("先看有没有中文意思");
  578. if(UserDict::where('word',$word)->where('mean','<>','')->where('language','<>','my')->exists()){
  579. $newword[]=$word;
  580. }else{
  581. //$this->log("如果没有查巴缅替换拆分");
  582. #如果没有查巴缅替换拆分
  583. if(UserDict::where('word',$word)->where('dict_id','61f23efb-b526-4a8e-999e-076965034e60')->exists()){
  584. $pmPart = explode("+",UserDict::where('word',$word)->where('dict_id','61f23efb-b526-4a8e-999e-076965034e60')->value('factors')) ;
  585. foreach ($pmPart as $pm) {
  586. # code...
  587. $newword[]=$pm;
  588. }
  589. }
  590. else{
  591. //$this->log("如果没有查规则变形");
  592. #如果没有查规则变形
  593. if(UserDict::where('word',$word)->where('source','_SYS_REGULAR_')->exists()){
  594. $rglPart = explode("+",UserDict::where('word',$word)->where('source','_SYS_REGULAR_')->value('factors')) ;
  595. #看巴缅有没有第一部分
  596. //$this->log("看巴缅有没有第一部分");
  597. if(UserDict::where('word',$rglPart[0])->where('dict_id','61f23efb-b526-4a8e-999e-076965034e60')->exists()){
  598. $pmPart = explode("+",UserDict::where('word',$rglPart[0])->where('dict_id','61f23efb-b526-4a8e-999e-076965034e60')->value('factors')) ;
  599. foreach ($pmPart as $pm) {
  600. # code...
  601. $newword[]=$pm;
  602. }
  603. }
  604. else{
  605. #没有
  606. $newword[]=$rglPart[0];
  607. }
  608. $newword[]=$rglPart[1];
  609. }
  610. else{
  611. #还没有就认命了
  612. //$this->log("还没有就认命了");
  613. $newword[]=$word;
  614. }
  615. }
  616. }
  617. }
  618. else{
  619. $newword[]=$word;
  620. }
  621. }
  622. return implode("+",$newword);
  623. }
  624. /**
  625. * 预处理连音词
  626. */
  627. public function splitSandhi($word){
  628. $newWord = "";
  629. $firstWord=$word;
  630. do {
  631. $isFound = false;
  632. foreach ($this->sandhi2 as $key => $sandhi) {
  633. # code...
  634. $len = $sandhi["len"];
  635. $end = mb_substr($firstWord, 0 - $len, null, "UTF-8");
  636. if ($end == $sandhi["c"]) {
  637. $word1 = mb_substr($firstWord, 0, mb_strlen($firstWord, "UTF-8") - $len, "UTF-8") .$sandhi["a"];
  638. $word2 = $sandhi["b"];
  639. $newWord = $word2 . "-" .$newWord;
  640. $firstWord = $word1;
  641. $isFound=true;
  642. break;
  643. }
  644. }
  645. } while ($isFound);
  646. $newWord = $firstWord . "-" .$newWord;
  647. return mb_substr($newWord,0,-1, "UTF-8");
  648. }
  649. public function splitA($word){
  650. $output = array();
  651. //预处理连音词
  652. $word1 = $this->splitSandhi($word);
  653. # 处理双元音
  654. $this->log("处理双元音");
  655. $arrword = $this->splitDiphthong($word1);
  656. if (count($arrword) > 1) {
  657. array_push($output,['word'=>$word,'type'=>'.un.','grammar'=>'','parent'=>'','factors'=>implode("+", $arrword),'confidence'=>0.9999]);
  658. }
  659. foreach ($arrword as $oneword) {
  660. $this->result = array(); //清空递归程序的输出容器
  661. $node = ['word'=>"",'remain'=>$oneword,'children'=>[]];
  662. if(mb_strlen($oneword)>35){
  663. //长词使用快速切分 正向切分 不使用少见sandi规则
  664. $this->split($node, 0, true, 0.8, 0.9, 0, true, false);
  665. $min_result = 1;
  666. }else{
  667. $this->split($node, 0, false, 0.8, 0.9, 0, true, false);
  668. $min_result=2;
  669. }
  670. $path = [];
  671. $this->log($node);
  672. $this->get_result($node,$path);
  673. $this->log("正向切分结束 结果数量".count($this->result));
  674. if(count($this->result)<$min_result){
  675. //有效结果过少
  676. $node = ['word'=>"",'remain'=>$oneword,'children'=>[]];
  677. $this->split($node, 0, false, 0.2, 0.8, 0, true, true);
  678. $this->log("有效结果过少 再次正切".count($this->result) );
  679. if(count($this->result)<2){
  680. $node = ['word'=>"",'remain'=>$oneword,'children'=>[]];
  681. $this->split($node, 0, false, 0.2, 0.8, 0, false, true);
  682. $this->log("有效结果过少 再次反切:结果数量" . count($this->result));
  683. }
  684. }
  685. $this->log("{$oneword}:" . count($this->result));
  686. if (count($this->result) > 0) {
  687. arsort($this->result); //按信心指数升序排序
  688. $iCount = 0;
  689. foreach ($this->result as $row => $value) {
  690. $factors = $row;
  691. if(strpos($row,']+') !== FALSE){
  692. $type = '.un.';
  693. $factors = \str_replace(['+[ṃ]+','[ṃ]+'],'ṃ+',$row);
  694. }else{
  695. $type = '.cp.';
  696. }
  697. $newword = ['word'=>$oneword,'type'=>$type,'grammar'=>'','parent'=>'','factors'=>$factors,'confidence'=>$value];
  698. array_push($output,$newword);
  699. if($iCount==0){
  700. //对于最优结果进行处理 找到base
  701. $wordWithType = ['word'=>$oneword,'type'=>'','grammar'=>'','parent'=>'','factors'=>$factors,'confidence'=>$value];
  702. $this->log("查找base");
  703. $factors = explode('+',$row);
  704. $endOfFactor = end($factors);
  705. if(strpos($endOfFactor,"[") !== FALSE){
  706. if(count($factors)>=2){
  707. $endOfFactor = $factors[count($factors)-2];
  708. }
  709. }
  710. $this->log("结尾词:".$endOfFactor);
  711. $caseman = new CaseMan();
  712. //猜测单词的base
  713. $parents = $caseman->WordToBase($oneword,1,false);
  714. //找到结尾单词的base
  715. $end_parents = $caseman->WordToBase($endOfFactor);
  716. if(count($parents)>0){
  717. foreach ($parents as $base=>$case) {
  718. # code...
  719. if(count($end_parents)>0){
  720. foreach ($end_parents as $base2=>$case2) {
  721. if(\mb_substr($base2,-2)===\mb_substr($base,-2)){
  722. $this->log("{$base} ok");
  723. foreach ($case as $value) {
  724. # code...
  725. foreach ($case2 as $value2) {
  726. //验证语法信息是否正确
  727. if($value['type'] == $value2['type'] &&
  728. substr($value['grammar'],0,3) === substr($value2['grammar'],0,3) &&
  729. $value['confidence']>0.5){
  730. $wordWithType['type'] = $value['type'];
  731. $wordWithType['grammar'] = $value['grammar'];
  732. $wordWithType['factors'] = $value['factors'];
  733. $wordWithType['parent'] = $base;
  734. $wordWithType['confidence'] = $value2['confidence'];
  735. $this->log("word:{$wordWithType['word']} ; type:{$wordWithType['type']}; grammar:{$wordWithType['grammar']};parent:{$wordWithType['parent']}");
  736. array_push($output,$wordWithType);
  737. }
  738. }
  739. }
  740. }
  741. }
  742. }else{
  743. foreach ($case as $value) {
  744. $wordWithType['type'] = $value['type'];
  745. $wordWithType['grammar'] = $value['grammar'];
  746. $wordWithType['factors'] = $value['factors'];
  747. $wordWithType['parent'] = $base;
  748. $wordWithType['confidence'] = 0.1;
  749. array_push($output,$wordWithType);
  750. }
  751. }
  752. }
  753. }
  754. }
  755. //后处理 进一步切分没有意思的长词
  756. $this->log("后处理 进一步切分没有意思的长词");
  757. $new = $this->split2($row);
  758. if($new !== $row){
  759. $newword['factors'] = $new;
  760. array_push($output,$newword);
  761. #再处理一次
  762. $new2 = $this->split2($new);
  763. if($new2!==$new){
  764. $newword['factors'] = $new2;
  765. array_push($output,$newword);
  766. }
  767. }
  768. $iCount++;
  769. if ($iCount > $this->MAX_RESULT2) {
  770. break;
  771. }
  772. }
  773. } else {
  774. Log::error("{$oneword} 切分失败");
  775. }
  776. }
  777. return $output;
  778. }
  779. public function setting($param=null){
  780. }
  781. public function getResult(){
  782. return $this->result;
  783. }
  784. public function debug($debug){
  785. $this->isDebug = $debug;
  786. }
  787. private function log($message){
  788. if ($this->isDebug) {
  789. Log::info($message);
  790. }
  791. }
  792. private function pushResult($word,$cf){
  793. array_push($this->result,array($word=>$cf));
  794. }
  795. }