123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- <?php
- /**
- * Created by PhpStorm.
- * User: stanley-king
- * Date: 2016/10/20
- * Time: 下午7:02
- */
- namespace search;
- use algorithm;
- class filter
- {
- const special_character = array(' ','“','”','Ⅰ','、','。','「','」','【','】','!','&','(',')',',',':','’','┊',
- '╭','╮','╯','╰','▔','▽',' ','《','》','の','*','8',';','?','°');
- static public function is_character($word)
- {
- $ar = str_split($word);
- return count($ar) == 1 ? true : false;
- }
- static public function filter($word)
- {
- if(empty($word)) {
- return false;
- }
- if(self::is_character($word))
- {
- if(ctype_space($word)) {
- return false;
- }
- elseif (ctype_graph($word)) {
- return false;
- }
- elseif (ctype_cntrl($word)) {
- return false;
- }
- else {
- return false;
- }
- }
- else
- {
- if(in_array($word,self::special_character)) {
- return false;
- }
- else {
- return true;
- }
- }
- }
- }
- //通过字找到key,通过key 找到词
- class words
- {
- protected $mDict;
- protected $mContainer;
- public function __construct()
- {
- $this->mDict = array();
- $this->mContainer = array();
- }
- public function parase($words,$value)
- {
- foreach (mb_str_split($words) as $word)
- {
- if(filter::filter($word)) {
- $this->add($word,$value);
- }
- }
- $this->mContainer[$value] = $words;
- }
- protected function add($key,$value)
- {
- if(isset($this->mDict[$key]))
- {
- $datas = &$this->mDict[$key];
- if(algorithm::binary_search($datas,$value) == false) {
- $pos = algorithm::lower_bonud($datas,$value);
- algorithm::array_insert($datas,$pos,$value);
- }
- }
- else {
- $this->mDict[$key] = array($value);
- }
- }
- public function find($key)
- {
- if(isset($this->mDict[$key])) {
- return $this->mDict[$key];
- } else {
- return array();
- }
- }
- public function name($val)
- {
- if(isset($this->mContainer[$val])) {
- return $this->mContainer[$val];
- } else {
- return false;
- }
- }
- }
- class one_multi
- {
- private $mContainer;
- public function __construct()
- {
- $this->mContainer = array();
- }
- public function add($key,$val)
- {
- if(isset($this->mContainer[$key]))
- {
- $values = &$this->mContainer[$key];
- if(algorithm::binary_search($values,$val) == false) {
- $pos = algorithm::lower_bonud($values,$val);
- algorithm::array_insert($values,$pos,$val);
- }
- }
- else {
- $this->mContainer[$key] = [];
- $this->mContainer[$key][] = $val;
- }
- }
- public function get($key)
- {
- if(isset($this->mContainer[$key]))
- {
- return $this->mContainer[$key];
- }
- else {
- return array();
- }
- }
- }
- class one_one
- {
- private $mContainer;
- public function __construct()
- {
- $this->mContainer = array();
- }
- public function add($key,$val)
- {
- $this->mContainer[$key] = $val;
- }
- public function get($key)
- {
- if(isset($this->mContainer[$key]))
- {
- return $this->mContainer[$key];
- }
- else {
- return false;
- }
- }
- }
- class array_tree
- {
- private $mTree;
- public function __construct()
- {
- $this->mTree = [];
- }
- public function add($id,$pid)
- {
- $id = intval($id);
- $pid = intval($pid);
- if(isset($this->mTree[$pid]) == false) {
- $this->mTree[$pid] = [];
- $this->mTree[$pid]['pid'] = 0;
- $this->mTree[$pid]['subids'] = [];
- $this->mTree[$pid]['subids'][] = $id;
- } else {
- $sub_ids = &$this->mTree[$pid]['subids'];
- $this->add_sub($sub_ids,$id);
- }
- if(isset($this->mTree[$id]) == false) {
- $this->mTree[$id] = [];
- $this->mTree[$id]['pid'] = $pid;
- $this->mTree[$id]['subids'] = [];
- }
- }
- private function add_sub(&$values,$val)
- {
- if(algorithm::binary_search($values,$val) == false) {
- $pos = algorithm::lower_bonud($values,$val);
- algorithm::array_insert($values,$pos,$val);
- }
- }
- public function is_parent($hot)
- {
- if (isset($this->mTree[$hot]) == false) {
- return false;
- }
- return (count($this->mTree[$hot]['subids']) > 0);
- }
- public function subs($hot)
- {
- if (isset($this->mTree[$hot]) == false) {
- return array();
- }
- return $this->mTree[$hot]['subids'];
- }
- }
|