php自动文章关键字提取


现在, 很多web系统都用到了不少的自然语言处理技术来提高客户体验. 

主要技术:

1. 文章关键字提取.

2. 相关文章(产品)推荐.

最近有不少网友问道, 这里以php为例子讲解下php的"关键字提取"的实现, 同时这个也是实现"相关文章推荐"的前提.

基本分以下几个步骤: 

一. 对文章进行分词: 

    php的中文分词程序还是有不少的, 从前辈的scws, 到用纯php实现的phpAnalysis, phpcws(phpcws)以及本人开发的robbe扩展.

    这里的讲解是使用"robbe分词扩展"来进行分词, robbe兴许不是最好的, 但一定是最快的.

    选择的分词器需要支持停止词过滤.

二. 统计词条词频并且排序: 

    对一篇文章分词后, 统计每个词条出现的次数. 然后按照词频降序排序下, 你想要的结果在前面几个词中.

    前提是去除了出现词频很高的停止词, 要不然得到的都是一些无用的停止词. (类似于TF-IDF算法)

完整的过程代码如下:

<?php
header('content-type:text/html;charset:utf-8');

$__text__ = '';
$__mode__ = 2;
$__timer__ = 0;
$_act = '';
if ( isset($_POST['_act']) ) {
	$_act = $_POST['_act'];
	if ( $_act == 'split'  ) {
		$__text__ = $_POST['text'];
		$__mode__ = intval( $_POST['mode'] );

		$s_time = timer();
		$_result = rb_split($__text__, $__mode__);
		
		$_keywords = array();
		foreach ( $_result as $_value ) {
			if ( is_numeric($_value) ) continue;
			if ( ord($_value) > 127 && strlen($_value) == 3 ) {
				//$w = rb_dic_get(__RB_LEX_CJK_WORDS__, $_value);
				//if ( $w['freq'] > 58023 ) continue;
				continue;
			}
			if ( ! isset($_keywords[$_value]) ) $_keywords[$_value] = 1;
			else $_keywords[$_value] = $_keywords[$_value] + 1;
		}
		//Sort
		arsort($_keywords, SORT_NUMERIC);
		unset($_result);
		$_result = array();
		foreach( $_keywords as $_key => $_value ) {
			//if ( $_value <= 2 ) continue;
			$_result[$_key] = $_value;
		}
		unset($_keywords);
		$__timer__ = timer() - $s_time;
	}
}

function timer() {
	list($msec, $sec) = explode(' ', microtime());	
	return ((float)$msec + (float)$sec);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
	<title>robbe分词测试程序</title>
	<meta http-equiv="content-type" content="text/html;charset=utf-8" />
	<style type="text/css">
		#box {width: 1000px;}
		.input-text {border: 1px solid #CCC;width: 1000px;height: 200px;background-color: #FFF;
			color: #555;font-size: 14px;}
		.link-box {overflow: hidden;zoom:1;padding-top:10px;}
		#submit-link {float:right;width:150px;height: 26px;line-height: 26px;
			background-color: #A50100;color: #FFF;font-weight: bold;text-align: center;
			text-decoration: none;font-size: 14px;}
		#info-link {float:right;width:300px;height: 26px;line-height: 26px;
			background-color: #A50100;color: #FFF;font-weight: bold;text-align: center;
			text-decoration: none;font-size: 14px;}
		.link-item {float: left;font-size: 14px;font-weight: bold;
			height: 26px;line-height: 26px;width: 100px;color: #A50100;}
		.title-item {height:30px;line-height: 30px;font-size: 14px;font-weight: bold;}
	</style>
</head>

<body>
	<div id="box">
		<div class="title-item">请输入文章内容:</div>
		<form name="robbe" method="post" action="robbe.keywords.php">
			<div class="r-item"><textarea name="text" class="input-text" id="text"><?=$__text__?></textarea></div>
			<input type="hidden" name="_act" value="split"/>
			<div class="link-box">
			<a class="link-item">
				<input type="radio" name="mode" value="1" <?=$__mode__==1?'checked="checked"':''?>/>简易模式</a>
			<a class="link-item">
				<input type="radio" name="mode" value="2" <?=$__mode__==2?'checked="checked"':''?>/>复杂模式</a>
			<a href="javascript:;" onclick="do_submit();return false;" id="submit-link">robbe分词</a>
		</div>
		</form>

		<?php
		if ( $_act == 'split' ) {
		?>
		<div class="title-item">关键字相关排序:</div>
		<div><textarea class="input-text">
			<?php foreach ( $_result as $_key => $_val ) echo $_key.'['.$_val.'] ';?>
		</textarea></div>
		<?php
		}
		?>
	</div>

<script type="text/javascript">
String.prototype.trim = function() {return this.replace(/^\s+|\s+$/g, '');}
function do_submit() {
	var text = document.getElementById('text');
	if ( text.value.trim() == '' ) return; 
	document.robbe.submit();
}
</script>
</body>

安装robbe扩展后, 可以直接运行该php页面了.


测试一下: 

以本人的一篇博客为测试例子: http://my.oschina.net/jcseg/blog/124173,  

统计排序词频超过3的词条如下:

缓存[13] web[9] 静态[6] 服务器[6] 负载[5] 数据库[4] 浏览器[4] 内容[4] 使用[4] 组件[4] js[4] css[4] 均衡[4] 分离[4] 压缩[4] 域名[4] 文件[4] 文案[3] 系统[3] 图片[3] 分布式[3] 方案[3] dns[3]

效果基本还可以吧, 这篇博客确实关注的是"缓存", "数据库", "负载", "浏览器", "web". 主要关键字都提取出来了.

想要达到更好的效果, 可能需要维护停止词库.



你可能感兴趣的:(PHP,关键字提取,robbe)