<?php
/**
*提取关键字类
*dict 词典目录 base基础词典 my.txt用户自定义词典
*
* Copyright 雷日锦 362606856@qq.com
**/
class keyword{
	public $run_time;//运行时间
	public $dict;//词典内容
	public $dir;//词典文件夹
	public function __construct($dir="dict/"){
		$this->dir=$dir?$dir:"dict/";
		$this->set(); 
	}
	/**
	*编译全部词典
	*/
	public function  save_all(){
		set_time_limit(0);
		$my=$this->save_my();
		$base=$this->save_base();
		
		$my=$my."\r\n".$base;
		$x=explode("\r\n",$my);
		$temp=array();
		foreach($x as $v){
			$v=trim($v);
			$temp[strlen($v)][]=$v;
		}
		$arr=array();
		krsort($temp);
		if($temp){
			foreach($temp as $d){
				foreach($d as $c){
					$arr[]=$c;
				}
			}
		}
		$arr=array_unique($arr);
		$data="";
		$data.="<?php\r\n";
		$data.='$dict_all=array('."\r\n";
		foreach($arr as $k=>$v){
			if($k==0){
				$data.="'$v'";
			}else{
				$data.=",'$v'";
			}
		}
		$data.="\r\n);\r\n?>";
		file_put_contents($this->dir."dict_all.php",$data);
	}
	/*
	*编译基础词典
	*/
	public function save_base(){
		set_time_limit(0);
		$my=file_get_contents($this->dir."base.txt");
		$x=explode("\r\n",$my);
		$temp=array();
		foreach($x as $v){
			$v=trim($v);
			$temp[strlen($v)][]=$v;
		}
		$arr=array();
		krsort($temp);
		if($temp){
			foreach($temp as $d){
				foreach($d as $c){
					$arr[]=$c;
				}
			}
		}
		 
		$arr=array_unique($arr);
		$data="";
		$data.="<?php\r\n";
		$data.='$dict_base=array('."\r\n";
		foreach($arr as $k=>$v){
			if($k==0){
				$data.="'$v'";
			}else{
				$data.=",'$v'";
			}
		}
		$data.="\r\n);\r\n?>";
		file_put_contents($this->dir."dict_base.php",$data);
		return $my;
	}
	/*
	*编译用户自定义词典
	*/
	public function save_my(){
		set_time_limit(0);
		$my=file_get_contents($this->dir."my.txt");
		$x=explode("\r\n",$my);
		$temp=array();
		foreach($x as $v){
			$v=trim($v);
			$temp[strlen($v)][]=$v;
		}
		$arr=array();
		krsort($temp);
		if($temp){
			foreach($temp as $d){
				foreach($d as $c){
					$arr[]=$c;
				}
			}
		}
		$arr=array_unique($arr);
		$data="";
		$data.="<?php\r\n";
		$data.='$dict_my=array('."\r\n";
		foreach($arr as $k=>$v){
			if($k==0){
				$data.="'$v'";
			}else{
				$data.=",'$v'";
			}
		}
		$data.="\r\n);\r\n?>";
		file_put_contents($this->dir."dict_my.php",$data);
		return $my;
	}
	/*
	*设置要使用的词典
	*/
	public function set($type='base'){
		
		switch($type){
			case "my":
					if(file_exists($this->dir."dict_my.php")){
						require($this->dir."dict_my.php");
						$this->dict=$dict_my;
					}
				break;
			case 'all':
					if(file_exists($this->dir."dict_all.php")){
						require($this->dir."dict_all.php");
						$this->dict=$dict_all;
					}
				break;
			default :
					if(file_exists($this->dir."dict_base.php")){
						require($this->dir."dict_base.php");
						$this->dict=$dict_base;
					}
				break;
		}
		return $this;
	}
	/*
	*获取关键词
	*$str 文本内容
	*$limit 关键词数量 
	*/
	public function get($str,$limit=10000){
		$start=microtime(true);
		$i=0;
		$temp=array();
		if(empty($this->dict)) return false;
		foreach($this->dict as $k=>$v){ 
			if(strpos($str,$v)!==false){
				$temp[]=$v;
				$i++;
				if($i>$limit) break;
			}
		}
		$this->run_time=microtime(true)-$start;
		return $temp;
	}
}


?>