Keyword.class.php 10.1 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480
<?php
require_once dirname(__FILE__).'/config/config.php';
class Util_Keyword
{
	/**
	 * 编译后的字典数组
	 * 
	 * @var string
	 */
	private $mDictionaryWords = array();
	
	/**
	 * 特殊字符
	 * @var string
	 */
	private $mSpecialCharacters = array(); 
	
	/**
	 * 初始化
	 */
	public function __construct()
	{
		$this->mSpecialCharacters = array(
									0 => ' ',
									1 => '-',
									2 => '*',
									3 => '',
				);
	}
	
	/**
	 * 替换
	 * 
	 * @param string $article
	 * @return array
	 */
	public function replace($article)
	{
		$this->loadwords();
		$len = strlen($article);
		for($i=0; $i<$len; $i++)
		{
			$node = $this->searchKeyword($article, $this->mDictionaryWords, $i);
			if(is_array($node))
			{
				//禁止词
				if($node['value']== PROHIBITION_STATE)
				{
					//continue;
				}
				//替换词
				if($node['value']== REPLACE_STATE)
				{
					//continue;
				}				
				//审核词
				if($node['value']== CHECK_STATE)
				{
					continue;
				}
				if($node['value']!= CHECK_STATE)
				{
                	$this->convert($article, $i, $node['degree']);
				}	
				//替换词的最后位置
				$i--;	
			}
		}
		return $article;
	}
	
	/**
	 * 包含那种类型词
	 * 
	 * @param string $article
	 * @param array $nodeValue
	 * @return boolean
	 */
	public function comprise($article, $nodeValue = array(1,2,3))
	{
		$this->loadwords();
		$len = strlen($article);
		for($i=0; $i<$len; $i++)
		{
			$node = $this->searchKeyword($article, $this->mDictionaryWords, $i);
			if(is_array($node))
			{
				if(array_search($node['value'], $nodeValue)!==FALSE)
				{
					return true;
				}
				//替换词的最后位置
				$i = $i+$node['degree']-1;	
			}	
		}
		//没有那种类型词
		return false;
	}
	
	/**
	 * 全相同
	 * 
	 * @param string $article
	 * @param array $nodeValue
	 * @return boolean
	 */
	public function allSame($article, $nodeValue = array(1,2,3))
	{
		$this->loadwords();
		$len = strlen($article);
		$node = array(
					'degree'	=> -1,
					'value'		=> 0,	
				);
		for($i=0; $i<$len; $i++)
		{
			$node = $this->searchKeyword($article, $this->mDictionaryWords, $i, 1,false);
			if(is_array($node))
			{
				if(array_search($node['value'], $nodeValue)!==FALSE)
				{
					break;
				}
			}	
		}
		//长度和文章长度相等
		if($len == $node['degree'])
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	
	/**
	 * 搜索关键字
	 * 
	 * @param string $content
	 * @param array $keywords
	 * @param int $pos
	 * @param int $degree
	 * @param boolean $semiMatch
	 * @return array
	 */
	public function searchKeyword($content, $keywords, $pos, $degree=1, $semiMatch=true)
	{
		/*	//过滤特殊字符
		if(array_search($content[$pos], $this->mSpecialCharacters)!==false) 
		{
			$this->searchKeyword($content,$keywords,$pos+1, $degree);
		}*/
		//节点
		if($pos<strlen($content))
		{
			$asc = ord($content{$pos});
		}
		else
		{
			return false;
		}
		if($degree>20)
		{
			return false;
		}
		if(!is_array($keywords) || (count($keywords)==0))
		{
		    return false;
		}
		if(!array_key_exists($asc,$keywords))
		{
			return false;
		}
		else
		{
			if(array_key_exists('val',$keywords[$asc]))
			{
				//半匹配模式
				if(intval($keywords[$asc]['val']) && $semiMatch) 
				{
					return array(
					'degree' => $degree,
					'value'  => $keywords[$asc]['val'],
					);
				}
				else if((strlen($content)==$degree) && intval($keywords[$asc]['val'])) //全匹配模式
				{
					return array(
					'degree' => $degree,
					'value'  => $keywords[$asc]['val'],
					);
				}
			}	
		}	
		if(empty($keywords[$asc]))  
		{	
			return false;
		}
		//深度,累加
		$degree++;
		//下一个节点
		$keywords = $keywords[$asc];
		return $this->searchKeyword($content, $keywords, $pos+1, $degree, $semiMatch);
	}
	
	/**
	 * 生成字典词语
	 * 
	 * @param array $compileWords
	 * @param int $nodeValue
	 * @param string $dictionaryCompileFile
	 * @param string $dictionaryFile
	 * @param int $nodeValue
	 */
	public function compileDictionaryWord($compileWords, $nodeValue, $dictionaryCompileFile = COMPILEFILE, $dictionaryFile = DICTIONARYFILE)
	{
		require_once $dictionaryFile;
		$word = $dictionaryWords;
		$keywords = array(
							$nodeValue => $compileWords,
						);
		$dictionaryWords = $this->compileWord($keywords, $word);
		$content = "<?php \r\n \$dictionaryWords = ".var_export($dictionaryWords,1).";";
		//重新生成字典
		$this->writeOver($dictionaryFile, $content);
		require_once $dictionaryCompileFile;
		foreach($compileWords as $word)
		{
			$words[$nodeValue][] = $word;	
		}
		$content = "<?php \r\n \$words = ".var_export($words,1).";";
		//重新生成编译字典
		$this->writeOver($dictionaryCompileFile, $content);
		$this->mDictionaryWords = $dictionaryWords;
		unset($dictionaryWords, $keywords, $content,$words);
	}
	
	/**
	 * 写到字典文件
	 * 
	 * @param array $dictionaryWords
	 * @param string $dictionaryFile
	 * @return boolean
	 */
	public function writeDictionaryFile($dictionaryWords = array(), $dictionaryFile = DICTIONARYFILE)
	{
		if(!file_exists($dictionaryFile))
		{
			return false;
		}
		if(count($dictionaryWords))
		{
			$content = "<?php \r\n \$dictionaryWords = ".var_export($dictionaryWords,1).";";
			$this->writeOver($dictionaryFile, $content);
			return true;
		}
		else
		{
			return false;
		}
	}
	
	/**
	 * 根据字典词语生成字典
	 * 
	 * @param string $dictionaryCompileFile
	 * @param string $dictionaryFile
	 * @return int
	 */
	public function compileDictionaryByCompileFile($dictionaryCompileFile = COMPILEFILE, $dictionaryFile = DICTIONARYFILE)
	{
		if(!file_exists($dictionaryCompileFile))
		{
			return false;
		}
		require_once $dictionaryCompileFile;
		$keywords = $words;			
		$dictionaryWords = $this->compileWord($keywords);
		$content = "<?php \r\n \$dictionaryWords = ".var_export($dictionaryWords,1).";";
		$this->writeOver($dictionaryFile, $content);
		return $dictionaryWords;
	}
	
	/**
	 * 导入文本字典
	 * 
	 * @param string $dictionaryTxtFile
	 * @param string $dictionaryCompileFile
	 * @return boolean
	 */
	public function importTxtDictionary($dictionaryTxtFile = TXTFILE, $dictionaryCompileFile = COMPILEFILE)
	{
		if (file_exists($dictionaryTxtFile))
		{
			$words = array();
			$content = explode("\n",$this->readOver($dictionaryTxtFile));
			foreach($content as $key => $value)
			{			
				if($value)
				{					
					$word = trim(substr($value, 0, strpos($value,'|')));			
					$type = trim(substr(strrchr($value,'|'), 1));
					$type = intval($type);
					if(is_numeric($type))
					{
						$words[$type][] = $word;
					}
					else
					{
						continue;
					}	
    			}
			}
			$content = "<?php \r\n \$words = ".var_export($words,1).";";
			$this->writeOver($dictionaryCompileFile,$content);
			return true;
		}
		else
		{
			die('Error!');
			return false;
		}
	}	
	
	/**
	 * 设置字典数组
	 * 
	 * @param array $words
	 */
	public function setDictionaryWords($words)
	{
		if(is_array($words))
		{
			$this->mDictionaryWords = $words;
			unset($words);
		}
	}
	
	/**
	 * 生成词库
	 * 
	 * @param array $keywords
	 * @param array $word
	 * @return array
	 */
	public function compileWord($keywords, $word=array())
	{
		foreach($keywords as $key => $words)
		{
			foreach ($words as $keyword)
			{
				$temp = '$word';
				for($i=0;$i<strlen($keyword);$i++)
				{
					$asc = ord($keyword{$i});
					$temp.= "['".$asc."']";
				}
				eval($temp.="['val']=".$key.";");
			}
		}	
		return $word;
	}
	
	private function loadwords()
	{
		//如果有值,就不加载
		if(count($this->mDictionaryWords))
		{
			return;
		}
		$dictionaryFile = DICTIONARYFILE;
		//加载字典
		if(file_exists($dictionaryFile))
		{
			require_once $dictionaryFile;
			$this->mDictionaryWords = $dictionaryWords;
		}
		else
		{
			//编译字典
			$dictionaryCompileFile = COMPILEFILE;
			$this->mDictionaryWords = $this->compileDictionary($dictionaryCompileFile, $dictionaryFile);
			if(!is_array($this->mDictionaryWords))
			{
				$dictionaryTxtFile = TXTFILE;
				$this->importTxtDictionary($dictionaryTxtFile, $dictionaryCompileFile);
				$this->mDictionaryWords = $this->compileDictionary($dictionaryCompileFile, $dictionaryFile);
			}
		}	
	}
	/**
	 * 读取内容
	 * 
	 * @param string $fileName
	 * @param string $method
	 * @return string
	 */
	public function readOver($fileName,$method="rb")
	{  
    	//由于strpos函数返回是整数值,所以使用!==(不全等)符号判断是否$filename是否包含..,包含则退出显示 
    	strpos($fileName,'..')!==false && exit();  
    	if($handle=@fopen($fileName,$method))
    	{ 
        	//轻便的咨询文件锁定,设置共享锁定   
       	 	flock($handle,LOCK_SH);
       	 	//读取所有内容
        	$filedata=@fread($handle,filesize($fileName));
        	fclose($handle); 
   		 }  
    	//返回文件内容  
   	 	return $filedata;   
	}
	
	/**
	 * 转化
	 * 
	 * @param string $article
	 * @param int $pos
	 * @param int $len
	 * @return string 
	 */
	private function convert(&$article, &$pos, $len)
	{
		$temp = $pos+$len;
		for($len;$len>0;$len--)
		{
			//判断是否是汉字
			if((ord($article{$pos})>127) && (ord($article{$pos+1})>127) &&
				(ord($article{$pos+2})>127) && ($pos+2<=$temp))
			{
				$article = substr($article,0, $pos).'*'.substr($article, $pos+3);
				$len-=2;
			}
			else
			{
				$article{$pos} = '*';
			}	
			$pos++;
		}
		unset($temp, $len);
	}
	
	/**
	 * 写入内容
	 * 
	 * @param string $fileName
	 * @param string $method
	 * @return boolean
	 */
	public function writeOver($fileName, $content, $method="wb")
	{  
    	//由于strpos函数返回是整数值,所以使用!==(不全等)符号判断是否$filename是否包含..,包含则退出显示 
    	strpos($fileName,'..')!==false && exit();  
    	if($handle=@fopen($fileName,$method))
    	{ 
        	//轻便的咨询文件锁定,设置独占锁定   
       	 	flock($handle,LOCK_EX);
       	 	//写入所有内容
        	fwrite($handle, $content, strlen($content));
        	fclose($handle); 
        	return true;
   		 } 
   		 else
   		 {
   		 	return false;
   		 } 
    	  
	}
	
	/**
	 * 析构
	 */
	public function __destruct()
	{
		unset($this->mDictionaryWords, $this->mSpecialCharacters);
	}
	
}
?>