⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 中文分词算法.txt

📁 中文分词分割程序Chinese word segmentation procedure for the carving
💻 TXT
字号:
http://www.yuanma.org/data/2006/0718/article_1167.htm

http://scholar.ilib.cn/abstract.aspx?A=huncjgdzkxxxb200501024
<?php
/* ----------------------------------------------------------------------- *\
   PHP-简易中文分词 (SCWS) ver 3.1/2.1 (实例演示)
   
   (*) 基于词频词典逐点搜索最长词, 双向根据词频取较高之分法

   -----------------------------------------------------------------------
   作者: 马明练(hightman) (MSN: MingL_Mar@msn.com) (php-QQ群: 17708754)
   网站: http://php.twomice.net
   时间: 2006/03/05
   目的: 学习研究交流用, 希望有好的建议及用途希望能进一步交流.
   -----------------------------------------------------------------------
   运行环境: PHP 4.x.* (x>0) (编译需要 --enable-dba --with-[cdb|gdbm])
\* ----------------------------------------------------------------------- */

/**
 * 查看源码的参数 <*.php?source>
 */
if (isset($_SERVER['QUERY_STRING']) 
    && !strncasecmp($_SERVER['QUERY_STRING'], 'source', 6))
{
    /* hightman: 代码继续开放 */
    /*
    $im = imagecreatefromgif("logo.gif");
    $co = imagecolorallocate($im, 255, 255, 5);
    imagestring($im, 1, 40, 2, date('Y/m/d'), $co);
    //imagestring($im, 2, 0, 60, 'IP/' . $_SERVER['REMOTE_ADDR'], $co);

    header ("Content-Type: image/gif");
    imagepng($im);    
    imagedestroy($im);

    exit(0);
    */

    $qlen = strlen($_SERVER['QUERY_STRING']);    
    switch ($qlen)
    {
        case 9 :
            highlight_file('my_Dictionary.class.php');
            break;        
        case 8 :
            highlight_file('my_SimpledCWS3.class.php');
            break;
        case 7 :
            highlight_file('my_SimpledCWS2.class.php');
            break;
        case 6 :
        default :
            highlight_file(__FILE__);
            break;
    }
    exit(0);
}

/**
 * 实例进行
 */

// 尝试计算实列运算时间
function get_microtime()
{
    list($usec, $sec) = explode(' ', microtime()); 
    return ((float)$usec + (float)$sec); 
}
$time_start = get_microtime();

// 分词结果之回调函数 (param: 分好的词组成的数组)
function words_cb($ar)
{
    foreach ($ar as $tmp)
    {
        if ($tmp == "\n")
        {
            echo $tmp;
            continue;
        }
        echo $tmp . ' ';
    }
    flush();
}

// 实例化前的参数指定与读取
$dict = 'dict/dict.cdb';
$mydata  = NULL;    // 待切数据
$version = 3;        // 采用版本
$autodis = false;    // 是否识别名字
$ignore  = false;    // 是否忽略标点
$debug   = false;    // 是否为除错模式
$stats     = false;    // 是否查看统计结果
$is_cli  = (php_sapi_name() == 'cli');    // 是否为 cli 运行环境

// 根据不同版本的环境读取参数设置
if ($is_cli)
{
    $argc = $_SERVER['argc'];
    for ($i = 1; $i < $argc; $i++)
    {
        $optarg = $_SERVER['argv'][$i];
        if (!strncmp($optarg, "--", 2))
        {
            $cmp = substr($optarg, 2);
            if (!strcasecmp($cmp, "help"))
            {
                $mydata = NULL;
                break;
            }
            else if (!strcasecmp($cmp, "autodis"))
                $autodis = true;
            else if (!strcasecmp($cmp, "ignore"))
                $ignore = true;
            else if (!strcasecmp($cmp, "v2"))
                $version = 2;
            else if (!strcasecmp($cmp, "debug"))
                $debug = true;
            else if (!strcasecmp($cmp, "stats"))
                $stats = true;
            else if (!strcasecmp($cmp, "dict"))
            {
                $i++;
                $dict = $_SERVER['argv'][$i];
            }
        }
        else if (is_null($mydata))
            $mydata = trim($optarg);

        if (!is_null($mydata) && is_file($mydata))
            $mydata = @file_get_contents($mydata);
    }
}
else
{
    // 部分参数选项
    $checked_ignore = $checked_autodis = $checked_v2 = '';
    
    // 是否指定有第 2 版
    if (isset($_REQUEST['version']) && $_REQUEST['version'] == 2)
    {
        $version = 2;
        $checked_v2 = ' selected';
    }

    // 是否指定一个词典格式
    $selected_gdbm = $selected_text = $selected_sqlite = '';
    if (isset($_REQUEST['dict']))
    {
        if ($_REQUEST['dict'] == 'gdbm')
        {
            $dict = 'dict/dict.gdbm';
            $selected_gdbm = ' selected';
        }
        else if ($_REQUEST['dict'] == 'text')
        {
            $dict = 'dict/cwords.txt';
            $selected_text = ' selected';
        }
        else if ($_REQUEST['dict'] == 'sqlite')
        {
            $dict = 'dict/dict.sqlite';
            $selected_sqlite = ' selected';
        }
        else
        {
            $_REQUEST['dict'] = 'cdb';
        }
    }

    // 是否开启人名识别 (缺省关闭)
    if (isset($_REQUEST['autodis']) && !strcmp($_REQUEST['autodis'], 'yes'))
    {
        $autodis = true;
        $checked_autodis = ' checked';
    }

    // 是否清除标点符号
    if (isset($_REQUEST['ignore']) && !strcmp($_REQUEST['ignore'], 'yes'))
    {
        $ignore = true;
        $checked_ignore = ' checked';
    }

    // 是否开启debug
    if (isset($_REQUEST['debug']) && !strcmp($_REQUEST['debug'], 'yes'))
    {
        $debug = true;
        $checked_debug = ' checked';
    }

    // 是否看统计表
    if (isset($_REQUEST['stats']) && !strcmp($_REQUEST['stats'], 'yes'))
    {
        $stats = true;
        $checked_stats = ' checked';
    }

    // 切分数据
    if (!isset($_REQUEST['mydata']) || empty($_REQUEST['mydata']))
    {
        $mydata = @file_get_contents('sample.txt');
    }
    else
    {
        $mydata = & $_REQUEST['mydata'];
        if (get_magic_quotes_gpc())
            $mydata = stripslashes($mydata);
    }
}

// 清除最后的 \r\n\t
if (!is_null($mydata)) 
    $mydata = trim($mydata);

// 实例化分词对像(mydata非空)
$object = 'my_SimpledCWS' . $version;
require ($object . '.class.php');
    
$cws = new $object($dict);
$cws->set_ignore_mark($ignore);
$cws->set_autodis($autodis);
$cws->set_debug($debug);
// hightman.060330: 强行开启统计
$cws->set_statistics($stats);

?>
<?php if (!$is_cli) { ?>
<html>
<head>
<title>PHP简易中文分词(SCWS) 第<?php echo $version; ?>版在线演示 (by hightman)</title>
<meta http-equiv="Content-type" content="text/html; charset=gbk">
<style type="text/css">
<!--
td, body    { background-color: #efefef; font-family: tahoma; font-size: 14px; }
.demotx        { font-size: 12px; width: 100%; height: 100px; }
small        { font-size: 12px; }
//-->
</style>
</head>
<body>
<h3>
  <font color=red>PHP简易中文分词(SCWS)</font>
  <font color=blue>第<?php echo $version; ?>版</font> - 在线演示 (by hightman)
</h3>  
基本功能: 根据词频词典进行机械分词、中外人名智能辨认 (词典格式: gdbm/cdb等)
<hr />

<table width=100% border=0>
  <tr>
    <form method=post>
    <td width=100%>
      <strong>请输入文字点击提交尝试分词: </strong> <br />
      <textarea name=mydata cols=60 rows=8 class=demotx><?php echo $mydata; ?></textarea>
      <small>
        <input type=checkbox name=autodis value="yes"<?php echo $checked_autodis;?>> 智能识别人名
        &nbsp;
        <input type=checkbox name=ignore value="yes"<?php echo $checked_ignore;?>> 清除标点符号
        &nbsp;
        <input type=checkbox name=stats value="yes"<?php echo $checked_stats;?>> <font color=red>统计结果</font>
        &nbsp;
        <input type=checkbox name=debug value="yes"<?php echo $checked_debug;?>> debug
        &nbsp;
        <br />
        词典格式: 
        <select name=dict size=1>        
        <option value=cdb>CDB</option>
        <option value=gdbm<?php echo $selected_gdbm; ?>>GDBM</option>
        <option value=text<?php echo $selected_text; ?>>Text</option>
        <option value=sqlite<?php echo $selected_sqlite; ?>>SQLite2.x</option>
        </select>
        &nbsp;
        尝试采用第
        <select name=version size=1 style="color: red; font-weight: bold;">
          <option value=3>3</option>
          <option value=2<?php echo $checked_v2;?>>2</option>
        </select>
        版分词算法
        &nbsp;&nbsp;
      </small>
      <input type=submit>
      </td>
      </form>
    </tr>
    <tr>
      <td><hr /></td>
    </tr>
    <tr>
      <td width=100%>
        <strong>分词结果(原文总长度 <?php echo strlen($mydata); ?> 字符) </strong>
        <br />
        <textarea cols=60 rows=8 class=demotx readonly style="color:#888888;">
<?php } else { ?>
_____________________________________________________________________

PHP简易中文分词程序(SCWS) - 第<?php echo $version; ?>版 - by hightman
_____________________________________________________________________
1.基于词频词典逐点搜索最长词, 双向根据词频取较高之分法
2.用法: <?php echo $_SERVER['argv'][0]; ?> [选项] <string|file>
3.选项: --autodis    打开人名识别
        --ignore     清除结果中的标点符号
        --v2         使用第2版分词算法(缺省第3版)
        --stats      分析统计分词结果 
        --dict <file>直接指定词典文件, 后缀(.cdb|.gdbm|.txt|.sqlite)
        --help       显示本页帮助文件
4.结果: 直接输出分词结果, 词之间以空格分隔
_____________________________________________________________________

<?php } ?>
<?php
// 执行切分, 分词结果数组执行 words_cb()
$cws->segment($mydata, 'words_cb');

// 切分时间统计
$time_end = get_microtime();
$time = $time_end - $time_start;

// hightman.060330: 分词结果报表
$stat_string  = '';
if ($stats)
{
    $stat_string .= sprintf("%-16s  %-8s  %s\n", '字或词', '次数', '出现位置');
    $stat_string .= str_repeat('-', 70) . "\n";
    
    $list = &$cws->get_statistics();
    $word_num = 0;
    foreach ($list as $k => $v)
    {
        $stat_string .= sprintf("%-16s  %-8d  %s\n", $k, $v['times'], implode(', ',$v['poses']));
        $word_num += $v['times'];
    }

    $stat_string .= str_repeat('-', 70) . "\n";
    $stat_string .= sprintf("合计:%4d 词      %-8d\n", count($list), $word_num);
}

// 以下显示结果
?>
<?php if (!$is_cli) { ?>
      </textarea>        
      <small>
        分词耗时: <?php echo $time; ?>秒, 
        词典查询次数: <?php echo $cws->_dict->query_times; ?>次
      </small>
    </td>
  </tr>
</table>
<hr />
<pre>
<?php echo $stat_string; ?>
</pre>
<hr />
<small>
  注: 本程序代码及相关词典免费开放下载, 供研究学习交流.
  参见网页 <a href=http://php.twomice.net target=_blank>http://php.twomice.net</a>
  或直接 <a href="?source" target="_blank">查看源码</a>
</small>
</body>
</html>
<?php } else if (!empty($mydata)) { ?>

_____________________________________________________________________
总长: <?php echo strlen($mydata); ?>字符, 耗时: <?php echo $time; ?>秒, 查词次数: <?php echo $cws->_dict->query_times; ?>次

<?php echo $stat_string; } ?>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -