VicWord.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. <?php
  2. namespace addons\cms\library;
  3. /**
  4. * Created by PhpStorm.
  5. * User: tanszhe
  6. * Date: 2017/12/21
  7. * Time: 下午8:11
  8. */
  9. class VicWord
  10. {
  11. private $dict = [];
  12. private $end = '\\';
  13. private $auto = false;
  14. private $count = 0;
  15. /**
  16. * @var string 词性
  17. */
  18. private $x = '\\x';
  19. public function __construct($type = 'igb')
  20. {
  21. if (!file_exists(_VIC_WORD_DICT_PATH_)) {
  22. return false;
  23. }
  24. if ($type == 'igb') {
  25. $this->dict = igbinary_unserialize(file_get_contents(_VIC_WORD_DICT_PATH_));
  26. } else {
  27. $this->dict = json_decode(file_get_contents(_VIC_WORD_DICT_PATH_), true);
  28. }
  29. }
  30. /**
  31. * @param string $path
  32. */
  33. public function getWord($str)
  34. {
  35. $this->auto = false;
  36. $str = $this->filter($str);
  37. return $this->find($str);
  38. }
  39. /**
  40. * @param string $path
  41. */
  42. public function getShortWord($str)
  43. {
  44. $this->auto = false;
  45. $str = $this->filter($str);
  46. return $this->shortfind($str);
  47. }
  48. /**
  49. * @param string $path
  50. */
  51. public function getAutoWord($str)
  52. {
  53. $this->auto = true;
  54. $str = $this->filter($str);
  55. return $this->autoFind($str, ['long' => 1]);
  56. }
  57. private function filter($str)
  58. {
  59. return strtolower(trim($str));
  60. }
  61. private function getD(&$str, $i)
  62. {
  63. $o = ord($str[$i]);
  64. if ($o < 128) {
  65. $d = $str[$i];
  66. } else {
  67. $o = $o >> 4;
  68. if ($o == 12) {
  69. $d = $str[$i] . $str[++$i];
  70. } elseif ($o === 14) {
  71. $d = $str[$i] . $str[++$i] . $str[++$i];
  72. } elseif ($o == 15) {
  73. $d = $str[$i] . $str[++$i] . $str[++$i] . $str[++$i];
  74. } else {
  75. exit('我不认识的编码');
  76. }
  77. }
  78. return [$d, $i];
  79. }
  80. private function autoFind($str, $auto_info = [])
  81. {
  82. if ($auto_info['long']) {
  83. return $this->find($str, $auto_info);
  84. } else {
  85. return $this->shortfind($str, $auto_info);
  86. }
  87. }
  88. private function reGet(&$r, $auto_info)
  89. {
  90. $auto_info['c'] = isset($auto_info['c']) ? $auto_info['c']++ : 1;
  91. $l = count($r) - 1;
  92. $p = [];
  93. $str = '';
  94. for ($i = $l; $i >= 0; $i--) {
  95. $str = $r[$i][0] . $str;
  96. $f = $r[$i][3];
  97. array_unshift($p, $r[$i]);
  98. unset($r[$i]);
  99. if ($f == 1) {
  100. break;
  101. }
  102. }
  103. $this->count++;
  104. $l = strlen($str);
  105. if (isset($r[$i - 1])) {
  106. $w = $r[$i - 1][1];
  107. } else {
  108. $w = 0;
  109. }
  110. if (isset($auto_info['pl']) && $l == $auto_info['pl']) {
  111. $r = $p;
  112. return false;
  113. } elseif ($str && $auto_info['c'] < 3) {
  114. $auto_info['pl'] = $l;
  115. $auto_info['long'] = !$auto_info['long'];
  116. $sr = $this->autoFind($str, $auto_info);
  117. $sr = array_map(function ($v) use ($w) {
  118. $v[1] += $w;
  119. return $v;
  120. }, $sr);
  121. $r = array_merge($r, $this->getGoodWord($p, $sr));
  122. }
  123. }
  124. private function getGoodWord($old, $new)
  125. {
  126. if (!$new) {
  127. return $old;
  128. }
  129. if ($this->getUnknowCount($old) > $this->getUnknowCount($new)) {
  130. return $new;
  131. } else {
  132. return $old;
  133. }
  134. }
  135. private function getUnknowCount($ar)
  136. {
  137. $i = 0;
  138. foreach ($ar as $v) {
  139. if ($v[3] == 0) {
  140. $i += strlen($v[0]);
  141. }
  142. }
  143. return $i;
  144. }
  145. private function find($str, $auto_info = [])
  146. {
  147. $len = strlen($str);
  148. $s = '';
  149. $n = '';
  150. $j = 0;
  151. $r = [];
  152. for ($i = 0; $i < $len; $i++) {
  153. list($d, $i) = $this->getD($str, $i);
  154. if (isset($wr[$d])) {
  155. $s .= $d;
  156. $wr = $wr[$d];
  157. } else {
  158. if (isset($wr[$this->end])) {
  159. $this->addNotFind($r, $n, $s, $j, $auto_info);
  160. $this->addResult($r, $s, $j, $wr[$this->x]);
  161. $n = '';
  162. }
  163. $wr = $this->dict;
  164. if (isset($wr[$d])) {
  165. $s = $d;
  166. $wr = $wr[$d];
  167. } else {
  168. $s = '';
  169. }
  170. }
  171. $n .= $d;
  172. $j = $i;
  173. }
  174. if (isset($wr[$this->end])) {
  175. $this->addNotFind($r, $n, $s, $i, $auto_info);
  176. $this->addResult($r, $s, $i, $wr[$this->x]);
  177. } else {
  178. $this->addNotFind($r, $n, '', $i, $auto_info);
  179. }
  180. return $r;
  181. }
  182. private function addNotFind(&$r, $n, $s, $i, $auto_info = [])
  183. {
  184. if ($n !== $s) {
  185. $n = str_replace($s, '', $n);
  186. $this->addResult($r, $n, $i - strlen($s), null, 0);
  187. if ($this->auto) {
  188. $this->reGet($r, $auto_info);
  189. }
  190. }
  191. }
  192. private function shortFind($str, $auto_info = [])
  193. {
  194. $len = strlen($str);
  195. $s = '';
  196. $n = '';
  197. $r = [];
  198. for ($i = 0; $i < $len; $i++) {
  199. $j = $i;
  200. list($d, $i) = $this->getD($str, $i);
  201. if (isset($wr[$d])) {
  202. $s .= $d;
  203. $wr = $wr[$d];
  204. } else {
  205. if (isset($wr[$this->end])) {
  206. $this->addNotFind($r, $n, $s, $j, $auto_info);
  207. $this->addResult($r, $s, $j, $wr[$this->x]);
  208. $n = '';
  209. }
  210. $wr = $this->dict;
  211. if (isset($wr[$d])) {
  212. $s = $d;
  213. $wr = $wr[$d];
  214. } else {
  215. $s = '';
  216. }
  217. }
  218. $n .= $d;
  219. if (isset($wr[$this->end])) {
  220. $this->addNotFind($r, $n, $s, $i, $auto_info);
  221. $this->addResult($r, $s, $i, $wr[$this->x]);
  222. $wr = $this->dict;
  223. $s = '';
  224. $n = '';
  225. }
  226. }
  227. if (isset($wr[$this->end])) {
  228. $this->addNotFind($r, $n, $s, $i, $auto_info);
  229. $this->addResult($r, $s, $i, $wr[$this->x]);
  230. } else {
  231. $this->addNotFind($r, $n, '', $i, $auto_info);
  232. }
  233. return $r;
  234. }
  235. private function addResult(&$r, $k, $i, $x, $find = 1)
  236. {
  237. $r[] = [$k, $i, $x, $find];
  238. }
  239. }