我现在系统中要用到中文分词,从网上找到了scws中文分词系统,用的PSCWS4版本,下载之后写了个测试程序如下:
// 加入头文件
require 'pscws/pscws4.class.php';
header("Content-Type:text/html;charset=utf-8");    
// 建立分词类对像, 参数为字符集, 默认为 gbk, 可在后面调用 set_charset 改变
$pscws = new PSCWS4('utf-8');$pscws->set_ignore('yes');
$pscws->set_dict('E:/scws/etc/dict.utf8.xdb');
$pscws->set_rule('E:/scws/etc/rules_cht.utf8.ini');// 分词调用 send_text() 将待分词的字符串传入, 紧接着循环调用 get_result() 方法取回一系列分好的词
// 直到 get_result() 返回 false 为止
// 返回的词是一个关联数组, 包含: word 词本身, idf 逆词率(重), off 在text中的偏移, len 长度, attr 词性
//
$text = "中国航天官员应邀到美国与太空总署官员开会";
$pscws->send_text($text);
while ($some = $pscws->get_result())
{
   foreach ($some as $word)
   {
       echo $word['word'].'=>'.$word['attr'].'<br>';   }
   flush();
}
$pscws->close();?>网页上的显示结果却是:
航天=>nr
?员庽>nr
?员=>nr 

解决方案 »

  1.   

    帖出
    print_r($some)
    的结果
      

  2.   

    $pscws = new PSCWS4('utf-8');
    =>
    $pscws = new PSCWS4();
    $pscws->set_charset("utf-8");
      

  3.   

    就是这个问题.
    见源码 // 构造函数
    function PSCWS4($charset = 'gbk')
    {
    $this->_xd = false;
    $this->_rs = $this->_rd = array();
    $this->set_charset($charset);
    } // FOR PHP5
    function __construct() { $this->PSCWS4(); }呵呵,明白了吧
      

  4.   

    谢谢大家的回答,还想问一下:PSCWS4这个版本支持文本词典添加自定义词库吗?
      

  5.   

    由说明
    1) 新增功能: 支持载入纯文本词典(TXT), 一次分词可使用多个词典, 以实现不改变核心词库的原则下快速增减词。
    可知,应该是支持的
      

  6.   

    你说的中SCWS-1.1.0 及以上版本,是c版本的,而我用的是PSCWS4,纯php版本的,这个支持吗
      

  7.   

    人家不是提供一个基于PHP的导入导出工具吗?
    中文的资料你都自己不查一下.
    太懒了.
      

  8.   

    资料我看过了,我是想在程序中写一个页面,可以把一些专业的词随时加入到词典中,但是那个基于PHP的导入导出工具不能实现这个吧?
      

  9.   

    导入导出工具包里有个make_xdb_file.php文件,你可以根据里面的xdb的数据格式,把你要增加的词汇,追加到你的xdb文件里去。
      

  10.   

    源码:
    if (!isset($_SERVER['argv'][1]) || !is_file($_SERVER['argv'][1]))
    {
    echo "Usage: {$_SERVER['argv'][0]} <xdb file> [output file]\n";
    exit(0);
    }$output = isset($_SERVER['argv'][2]) ? $_SERVER['argv'][2] : 'php://stdout';'argv' 
    Array of arguments passed to the script. When the script is run on the command line, this gives C-style access to the command line parameters. When called via the GET method, this will contain the query string.