util.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2016/10/20
  6. * Time: 下午7:02
  7. */
  8. namespace search;
  9. use algorithm;
  10. class filter
  11. {
  12. const special_character = array(' ','“','”','Ⅰ','、','。','「','」','【','】','!','&','(',')',',',':','’','┊',
  13. '╭','╮','╯','╰','▔','▽',' ','《','》','の','*','8',';','?','°');
  14. static function mb_str_split( $string ) {
  15. return preg_split('/(?<!^)(?!$)/u', $string );
  16. }
  17. static public function is_character($word)
  18. {
  19. $ar = str_split($word);
  20. return count($ar) == 1 ? true : false;
  21. }
  22. static public function filter($keywords)
  23. {
  24. $words = [];
  25. $keywords = self::mb_str_split(strtolower($keywords));
  26. $word = '';
  27. foreach ($keywords as $ch)
  28. {
  29. if(filter::filter_eword($ch)) {
  30. $word .= $ch;
  31. }
  32. else
  33. {
  34. if(!empty($word)) {
  35. $words[] = $word;
  36. }
  37. }
  38. }
  39. foreach ($keywords as $ch)
  40. {
  41. if(filter::filter_eword($ch)) {
  42. $words[] = $ch;
  43. }
  44. }
  45. if(!empty($word)) {
  46. $words[] = $word;
  47. }
  48. foreach ($keywords as $word)
  49. {
  50. if(filter::filter_ch($word)) {
  51. $words[] = $word;
  52. }
  53. }
  54. return $words;
  55. }
  56. static public function filter_ch($word)
  57. {
  58. if(empty($word)) {
  59. return false;
  60. }
  61. if(self::is_character($word))
  62. {
  63. if(ctype_space($word)) {
  64. return false;
  65. }
  66. elseif (ctype_graph($word)) {
  67. return false;
  68. }
  69. elseif (ctype_cntrl($word)) {
  70. return false;
  71. }
  72. else {
  73. return false;
  74. }
  75. }
  76. else
  77. {
  78. if(in_array($word,self::special_character)) {
  79. return false;
  80. }
  81. else {
  82. return true;
  83. }
  84. }
  85. }
  86. static public function filter_eword($word)
  87. {
  88. if(empty($word)) {
  89. return false;
  90. }
  91. if(self::is_character($word))
  92. {
  93. if(ctype_space($word)) {
  94. return false;
  95. }
  96. elseif (ctype_lower($word)) {
  97. return true;
  98. }
  99. elseif (ctype_cntrl($word)) {
  100. return false;
  101. }
  102. else {
  103. return false;
  104. }
  105. }
  106. else {
  107. return false;
  108. }
  109. }
  110. }
  111. //通过字找到key,通过key 找到词
  112. class words
  113. {
  114. protected $mDict;
  115. protected $mContainer;
  116. public function __construct()
  117. {
  118. $this->mDict = array();
  119. $this->mContainer = array();
  120. }
  121. public function parase($words,$value)
  122. {
  123. $fwords = filter::filter($words);
  124. foreach ($fwords as $word) {
  125. $this->add($word,$value);
  126. }
  127. $this->mContainer[$value] = $words;
  128. }
  129. protected function add($key,$value)
  130. {
  131. if(isset($this->mDict[$key]))
  132. {
  133. $datas = &$this->mDict[$key];
  134. if(algorithm::binary_search($datas,$value) == false) {
  135. $pos = algorithm::lower_bonud($datas,$value);
  136. algorithm::array_insert($datas,$pos,$value);
  137. }
  138. }
  139. else {
  140. $this->mDict[$key] = array($value);
  141. }
  142. }
  143. public function find($key)
  144. {
  145. if(isset($this->mDict[$key])) {
  146. return $this->mDict[$key];
  147. } else {
  148. return array();
  149. }
  150. }
  151. public function name($val)
  152. {
  153. if(isset($this->mContainer[$val])) {
  154. return $this->mContainer[$val];
  155. } else {
  156. return false;
  157. }
  158. }
  159. }
  160. class one_multi
  161. {
  162. private $mContainer;
  163. public function __construct()
  164. {
  165. $this->mContainer = array();
  166. }
  167. public function add($key,$val)
  168. {
  169. if(isset($this->mContainer[$key]))
  170. {
  171. $values = &$this->mContainer[$key];
  172. if(algorithm::binary_search($values,$val) == false) {
  173. $pos = algorithm::lower_bonud($values,$val);
  174. algorithm::array_insert($values,$pos,$val);
  175. }
  176. }
  177. else {
  178. $this->mContainer[$key] = [];
  179. $this->mContainer[$key][] = $val;
  180. }
  181. }
  182. public function get($key)
  183. {
  184. if(isset($this->mContainer[$key]))
  185. {
  186. return $this->mContainer[$key];
  187. }
  188. else {
  189. return array();
  190. }
  191. }
  192. }
  193. class one_one
  194. {
  195. private $mContainer;
  196. public function __construct()
  197. {
  198. $this->mContainer = array();
  199. }
  200. public function add($key,$val)
  201. {
  202. $this->mContainer[$key] = $val;
  203. }
  204. public function get($key)
  205. {
  206. if(isset($this->mContainer[$key]))
  207. {
  208. return $this->mContainer[$key];
  209. }
  210. else {
  211. return false;
  212. }
  213. }
  214. }
  215. class array_tree
  216. {
  217. private $mTree;
  218. public function __construct()
  219. {
  220. $this->mTree = [];
  221. }
  222. public function add($id,$pid)
  223. {
  224. $id = intval($id);
  225. $pid = intval($pid);
  226. if(isset($this->mTree[$pid]) == false) {
  227. $this->mTree[$pid] = [];
  228. $this->mTree[$pid]['pid'] = 0;
  229. $this->mTree[$pid]['subids'] = [];
  230. $this->mTree[$pid]['subids'][] = $id;
  231. } else {
  232. $sub_ids = &$this->mTree[$pid]['subids'];
  233. $this->add_sub($sub_ids,$id);
  234. }
  235. if(isset($this->mTree[$id]) == false) {
  236. $this->mTree[$id] = [];
  237. $this->mTree[$id]['pid'] = $pid;
  238. $this->mTree[$id]['subids'] = [];
  239. }
  240. }
  241. private function add_sub(&$values,$val)
  242. {
  243. if(algorithm::binary_search($values,$val) == false) {
  244. $pos = algorithm::lower_bonud($values,$val);
  245. algorithm::array_insert($values,$pos,$val);
  246. }
  247. }
  248. public function is_parent($hot)
  249. {
  250. if (isset($this->mTree[$hot]) == false) {
  251. return false;
  252. }
  253. return (count($this->mTree[$hot]['subids']) > 0);
  254. }
  255. public function subs($hot)
  256. {
  257. if (isset($this->mTree[$hot]) == false) {
  258. return array();
  259. }
  260. return $this->mTree[$hot]['subids'];
  261. }
  262. }
  263. class valtokey
  264. {
  265. private $mKeys;
  266. private $mValMap;
  267. private $mKeyMap;
  268. private $mCount;
  269. public function __construct()
  270. {
  271. $this->mKeys = [];
  272. $this->mValMap = [];
  273. $this->mKeyMap = [];
  274. $this->mCount = 0;
  275. }
  276. public function add($key,$val)
  277. {
  278. if(algorithm::binary_search($this->mKeyMap,$key) == true) {
  279. return false;
  280. } else {
  281. $pos = algorithm::lower_bonud($this->mKeyMap,$key);
  282. algorithm::array_insert($this->mKeyMap,$pos,$key);
  283. }
  284. $pos = algorithm::lower_bonud($this->mValMap,$val);
  285. algorithm::array_insert($this->mValMap,$pos,$val);
  286. algorithm::array_insert($this->mKeys,$pos,$key);
  287. return true;
  288. }
  289. public function finish() {
  290. $this->mKeyMap = [];
  291. $this->mCount = count($this->mKeys);
  292. }
  293. public function findless($val,$start,$length)
  294. {
  295. $pos = algorithm::upper_bound($this->mValMap,$val);
  296. if($pos < 0) {
  297. return false;
  298. }
  299. elseif ($pos >= $this->mCount) {
  300. $pos = $this->mCount - 1;
  301. }
  302. else {
  303. $data = $this->mValMap[$pos];
  304. if($data > $val) {
  305. $pos -= 1;
  306. }
  307. }
  308. $pos = $pos - $start;
  309. if($pos < 0) {
  310. return false;
  311. }
  312. $result = [];
  313. for ($i = $pos; $i >= 0 && $length > 0; $i--,$length--) {
  314. $result[] = $this->mKeys[$i];
  315. }
  316. return array('total' => $pos + 1,'cids' => $result);
  317. }
  318. public function findall($val)
  319. {
  320. $pos = algorithm::upper_bound($this->mValMap,$val);
  321. if($pos < 0) {
  322. return false;
  323. }
  324. elseif ($pos >= $this->mCount) {
  325. $pos = $this->mCount - 1;
  326. }
  327. else {
  328. $data = $this->mValMap[$pos];
  329. if($data > $val) {
  330. $pos -= 1;
  331. }
  332. }
  333. $result = [];
  334. for ($i = $pos; $i >= 0; $i--) {
  335. $result[] = $this->mKeys[$i];
  336. }
  337. return $result;
  338. }
  339. }