VicWord.php 6.6 KB

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