util.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 public function is_character($word)
  15. {
  16. $ar = str_split($word);
  17. return count($ar) == 1 ? true : false;
  18. }
  19. static public function filter($word)
  20. {
  21. if(empty($word)) {
  22. return false;
  23. }
  24. if(self::is_character($word))
  25. {
  26. if(ctype_space($word)) {
  27. return false;
  28. }
  29. elseif (ctype_graph($word)) {
  30. return false;
  31. }
  32. elseif (ctype_cntrl($word)) {
  33. return false;
  34. }
  35. else {
  36. return false;
  37. }
  38. }
  39. else
  40. {
  41. if(in_array($word,self::special_character)) {
  42. return false;
  43. }
  44. else {
  45. return true;
  46. }
  47. }
  48. }
  49. }
  50. //通过字找到key,通过key 找到词
  51. class words
  52. {
  53. protected $mDict;
  54. protected $mContainer;
  55. public function __construct()
  56. {
  57. $this->mDict = array();
  58. $this->mContainer = array();
  59. }
  60. public function parase($words,$value)
  61. {
  62. foreach (mb_str_split($words) as $word)
  63. {
  64. if(filter::filter($word)) {
  65. $this->add($word,$value);
  66. }
  67. }
  68. $this->mContainer[$value] = $words;
  69. }
  70. protected function add($key,$value)
  71. {
  72. if(isset($this->mDict[$key]))
  73. {
  74. $datas = &$this->mDict[$key];
  75. if(algorithm::binary_search($datas,$value) == false) {
  76. $pos = algorithm::lower_bonud($datas,$value);
  77. algorithm::array_insert($datas,$pos,$value);
  78. }
  79. }
  80. else {
  81. $this->mDict[$key] = array($value);
  82. }
  83. }
  84. public function find($key)
  85. {
  86. if(isset($this->mDict[$key])) {
  87. return $this->mDict[$key];
  88. } else {
  89. return array();
  90. }
  91. }
  92. public function name($val)
  93. {
  94. if(isset($this->mContainer[$val])) {
  95. return $this->mContainer[$val];
  96. } else {
  97. return false;
  98. }
  99. }
  100. }
  101. class one_multi
  102. {
  103. private $mContainer;
  104. public function __construct()
  105. {
  106. $this->mContainer = array();
  107. }
  108. public function add($key,$val)
  109. {
  110. if(isset($this->mContainer[$key]))
  111. {
  112. $values = &$this->mContainer[$key];
  113. if(algorithm::binary_search($values,$val) == false) {
  114. $pos = algorithm::lower_bonud($values,$val);
  115. algorithm::array_insert($values,$pos,$val);
  116. }
  117. }
  118. else {
  119. $this->mContainer[$key] = [];
  120. $this->mContainer[$key][] = $val;
  121. }
  122. }
  123. public function get($key)
  124. {
  125. if(isset($this->mContainer[$key]))
  126. {
  127. return $this->mContainer[$key];
  128. }
  129. else {
  130. return array();
  131. }
  132. }
  133. }
  134. class one_one
  135. {
  136. private $mContainer;
  137. public function __construct()
  138. {
  139. $this->mContainer = array();
  140. }
  141. public function add($key,$val)
  142. {
  143. $this->mContainer[$key] = $val;
  144. }
  145. public function get($key)
  146. {
  147. if(isset($this->mContainer[$key]))
  148. {
  149. return $this->mContainer[$key];
  150. }
  151. else {
  152. return false;
  153. }
  154. }
  155. }
  156. class array_tree
  157. {
  158. private $mTree;
  159. public function __construct()
  160. {
  161. $this->mTree = [];
  162. }
  163. public function add($id,$pid)
  164. {
  165. $id = intval($id);
  166. $pid = intval($pid);
  167. if(isset($this->mTree[$pid]) == false) {
  168. $this->mTree[$pid] = [];
  169. $this->mTree[$pid]['pid'] = 0;
  170. $this->mTree[$pid]['subids'] = [];
  171. $this->mTree[$pid]['subids'][] = $id;
  172. } else {
  173. $sub_ids = &$this->mTree[$pid]['subids'];
  174. $this->add_sub($sub_ids,$id);
  175. }
  176. if(isset($this->mTree[$id]) == false) {
  177. $this->mTree[$id] = [];
  178. $this->mTree[$id]['pid'] = $pid;
  179. $this->mTree[$id]['subids'] = [];
  180. }
  181. }
  182. private function add_sub(&$values,$val)
  183. {
  184. if(algorithm::binary_search($values,$val) == false) {
  185. $pos = algorithm::lower_bonud($values,$val);
  186. algorithm::array_insert($values,$pos,$val);
  187. }
  188. }
  189. public function is_parent($hot)
  190. {
  191. if (isset($this->mTree[$hot]) == false) {
  192. return false;
  193. }
  194. return (count($this->mTree[$hot]['subids']) > 0);
  195. }
  196. public function subs($hot)
  197. {
  198. if (isset($this->mTree[$hot]) == false) {
  199. return array();
  200. }
  201. return $this->mTree[$hot]['subids'];
  202. }
  203. }
  204. class valtokey
  205. {
  206. private $mKeys;
  207. private $mValMap;
  208. private $mKeyMap;
  209. private $mCount;
  210. public function __construct()
  211. {
  212. $this->mKeys = [];
  213. $this->mValMap = [];
  214. $this->mKeyMap = [];
  215. $this->mCount = 0;
  216. }
  217. public function add($key,$val)
  218. {
  219. if(algorithm::binary_search($this->mKeyMap,$key) == true) {
  220. return false;
  221. } else {
  222. $pos = algorithm::lower_bonud($this->mKeyMap,$key);
  223. algorithm::array_insert($this->mKeyMap,$pos,$key);
  224. }
  225. $pos = algorithm::lower_bonud($this->mValMap,$val);
  226. algorithm::array_insert($this->mValMap,$pos,$val);
  227. algorithm::array_insert($this->mKeys,$pos,$key);
  228. return true;
  229. }
  230. public function finish() {
  231. $this->mKeyMap = [];
  232. $this->mCount = count($this->mKeys);
  233. }
  234. public function findless($val,$start,$length)
  235. {
  236. $pos = algorithm::upper_bound($this->mValMap,$val);
  237. if($pos < 0) {
  238. return false;
  239. }
  240. elseif ($pos >= $this->mCount) {
  241. $pos = $this->mCount - 1;
  242. }
  243. else {
  244. $data = $this->mValMap[$pos];
  245. if($data > $val) {
  246. $pos -= 1;
  247. }
  248. }
  249. $pos = $pos - $start;
  250. if($pos < 0) {
  251. return false;
  252. }
  253. $result = [];
  254. for ($i = $pos; $i >= 0 && $length > 0; $i--,$length--) {
  255. $result[] = $this->mKeys[$i];
  256. }
  257. return array('total' => $pos + 1,'cids' => $result);
  258. }
  259. public function findall($val)
  260. {
  261. $pos = algorithm::upper_bound($this->mValMap,$val);
  262. if($pos < 0) {
  263. return false;
  264. }
  265. elseif ($pos >= $this->mCount) {
  266. $pos = $this->mCount - 1;
  267. }
  268. else {
  269. $data = $this->mValMap[$pos];
  270. if($data > $val) {
  271. $pos -= 1;
  272. }
  273. }
  274. $result = [];
  275. for ($i = $pos; $i >= 0; $i--) {
  276. $result[] = $this->mKeys[$i];
  277. }
  278. return $result;
  279. }
  280. }