zoukankan      html  css  js  c++  java
  • 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);
    
    
    ?>
  • 相关阅读:
    Linux下安装maven
    非连续性及反脆弱
    高手是怎么练成的
    思维型大脑
    编写文档五轮模式
    Nginx初识
    ida快捷键
    ida+gdb调试任意平台
    gcc常用命令使用
    ida调试ios应用
  • 原文地址:https://www.cnblogs.com/glory-jzx/p/3509045.html
Copyright © 2011-2022 走看看