• php 建立 搜索 分词树


    <?php 
    /**
     * @author: xiaojiang 20140107
     * php 建立分词树
     * */
    class Tree{
    
        public $w = '';
        public $subT = array();
        public $isEnd = false;
        
        public function __construct($w= '' , $isEnd = false){
            if(!empty($w)){
                $this->w = $w;
                $this->isEnd = $isEnd;
            }
        }
        public function insert( $str ){
        
            $len = strlen($str);
            if(!$len) return ;
            $scope = $this;
            for( $i = 0; $i< $len; $i++ ){
                //判断汉字
                $cStr = $str[$i];
                if( ord( $cStr ) > 127 ){
                    $cStr = substr($str, $i, 3);
                    $i += 2;
                }
                $scope = $scope->insertNode( $cStr );
            }
            $scope->isEnd = true;
        }
        
        private function &insertNode(  $w ){
            $t = $this->hasTree( $w );
            if( !$t ){
                $t =  new Tree( $w );
                array_push($this->subT, $t );
            }
            return $t;
        }
        
        private function &hasTree($w){
            foreach ($this->subT as $t){
                if($t->w == $w)
                    return $t;
            }
            return false;
        }
    
    }
    $tIns = new Tree();
    $tIns->insert('啊啊');
    $tIns->insert('啊你妹');
    $tIns->insert('你妹');
    print_r($tIns);
    
    
    ?>
  • 相关阅读:
    [PHP] Layui + jquery 实现 实用的文章自定义标签
    个人总结第五周
    个人总结第四周
    个人总结第三周
    个人总结第二周
    个人总结第一周
    用户体验评价
    第二阶段scrum冲刺
    单词统计
    用户模块和用户场景
  • 原文地址:https://www.cnblogs.com/glory-jzx/p/3509045.html
Copyright © 2020-2023  润新知