PHP自动提取文章关键字的实现方法

来源:互联网 发布:ubuntu 16.04中文版 编辑:程序博客网 时间:2024/06/05 03:11

<?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); }?>



<html>    <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>        现在, 很多web系统都用到了不少的自然语言处理技术来提高客户体验. 主要技术: 1. 文章关键字提取. 2. 相关文章(产品)推荐. 最近有不少网友问道, 这里以php为例子讲解下php的"关键字提取"的实现, 同时这个也是实现"相关文章推荐"的前提. 基本分以下几个步骤: 一. 对文章进行分词: php的中文分词程序还是有不少的, 从前辈的scws, 到用纯php实现的phpAnalysis, phpcws(phpcws)以及本人开发的robbe扩展. 这里的讲解是使用"robbe分词扩展"来进行分词, robbe兴许不是最好的, 但一定是最快的. 选择的分词器需要支持停止词过滤. 二. 统计词条词频并且排序: 对一篇文章分词后, 统计每个词条出现的次数. 然后按照词频降序排序下, 你想要的结果在前面几个词中. 前提是去除了出现词频很高的停止词, 要不然得到的都是一些无用的停止词. (类似于TF-IDF算法) 完整的过程代码如下:         <div id="box">            <div>                请输入文章内容:            </div>            <form name="robbe" method="post" action="" id="robbe">                <div>                    <textarea name="text" id="text"> <?=$__text__?> </textarea>                </div> <input type="hidden" name="_act" value="split">                <div>                    <a><input type="radio" name="mode" value="1" <?=$__mode__==1?'checked="checked"':''?>>简易模式</a> <a><input type="radio" name="mode" value="2" <?=$__mode__==2?'checked="checked"':''?>>复杂模式</a> <a href="javascript:;" onclick="do_submit();return false;" id="submit-link" name="submit-link">robbe分词</a>                </div>            </form> <?php            if ( $_act == 'split' ) {            ?>            <div>                关键字相关排序:            </div>            <div>                <textarea> <?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></html>

0 0