public function init() { scws_set_charset($this->tool, "utf8"); scws_set_dict($this->tool, self::ROOT . "etc/dict.utf8.xdb"); scws_set_rule($this->tool, self::ROOT . "etc/rules.utf8.ini"); scws_set_ignore($this->tool, true); //清楚标点 scws_set_multi($this->tool, self::SCWS_MULTI_DUALITY); #scws_set_multi($this->tool,self::SCWS_MULTI_SHORT); scws_set_duality($this->tool, false); }
/** * get tops content of the input, get number of word from return * @param string $content input content * @param int $tops number of words */ public static function keywords($content, $type = '', $tops = 10) { $obj = scws_open(); //load dic path and rules $dicpath = empty($type) ? C('dictionay.defaultdic') : C('dictionay.' . $type); $rules = C('dictionay.defaultrule'); if (empty($dicpath) || empty($rules)) { throw_exception("Load dictionary node: dic file Error"); } //var_dump($type,$dicpath); scws_set_charset($obj, 'utf8'); scws_set_dict($obj, $dicpath, SCWS_XDICT_TXT); scws_set_duality($obj, true); scws_set_rule($obj, $rules); scws_send_text($obj, $content); return scws_get_tops($obj, $tops); }
public static function segments($arr, $num = 10) { $list = array(); if (empty($text)) { return $list; } $words = array(); //检测是否已安装php_scws扩展 if (function_exists("scws_open")) { $sh = scws_open(); scws_set_charset($sh, 'utf8'); scws_set_dict($sh, APP_ROOT_PATH . 'system/scws/dict.utf8.xdb'); scws_set_rule($sh, APP_ROOT_PATH . 'system/scws/rules.utf8.ini'); scws_set_ignore($sh, true); foreach ($arr as $key => $text) { scws_send_text($sh, $text); $words[] = scws_get_tops($sh, $num); } scws_close($sh); } else { require_once APP_ROOT_PATH . 'system/scws/pscws4.class.php'; $pscws = new PSCWS4(); $pscws->set_dict(APP_ROOT_PATH . 'system/scws/dict.utf8.xdb'); $pscws->set_rule(APP_ROOT_PATH . 'system/scws/rules.utf8.ini'); $pscws->set_ignore(true); foreach ($arr as $key => $text) { $pscws->send_text($text); $words[] = $pscws->get_tops($num); } $pscws->close(); } for ($i = 0; $i < $num; $i++) { foreach ($words as $item) { if (isset($item[$i])) { $word = $item[$i]['word']; if (isset($list[$word])) { $list[$word]++; } else { $list[$word] = 1; } } } } $list = array_slice($list, 0, $num); return array_keys($list); }
/** * 文本分词 * @param string $text 需要分词的文本 * @return array */ public static function segmentAll($text) { $list = array(); if (empty($text)) { return $list; } //检测是否已安装php_scws扩展 if (function_exists("scws_open")) { $sh = scws_open(); scws_set_charset($sh, 'utf8'); scws_set_dict($sh, APP_ROOT_PATH . 'system/scws/dict.utf8.xdb'); scws_set_rule($sh, APP_ROOT_PATH . 'system/rules.utf8.ini'); scws_set_ignore($sh, true); scws_send_text($sh, $text); while ($words = scws_get_result($sh)) { foreach ($words as $word) { $list[] = $word['word']; } } scws_close($sh); } else { require_once APP_ROOT_PATH . 'system/scws/pscws4.class.php'; $pscws = new PSCWS4(); $pscws->set_dict(APP_ROOT_PATH . 'system/scws/dict.utf8.xdb'); $pscws->set_rule(APP_ROOT_PATH . 'system/scws/rules.utf8.ini'); $pscws->set_ignore(true); $pscws->send_text($text); while ($words = $pscws->get_result()) { foreach ($words as $word) { $list[] = $word['word']; } } $pscws->close(); } return $list; }
$str = "Module {$module} is not compiled into PHP"; } echo "{$str}\n\n"; $text = <<<EOF 陈凯歌并不是《无极》的唯一著作权人,一部电影的整体版权归电影制片厂所有。 一部电影的作者包括导演、摄影、编剧等创作人员,这些创作人员对他们的创作是有版权的。不经过制片人授权,其他人不能对电影做拷贝、发行、反映,不能通过网络来传播,既不能把电影改编成小说、连环画等其他艺术形式发表,也不能把一部几个小时才能放完的电影改编成半个小时就能放完的短片。 著作权和版权在我国是同一个概念,是法律赋予作品创作者的专有权利。所谓专有权利就是没有经过权利人许可又不是法律规定的例外,要使用这个作品,就必须经过作者授权,没有授权就是侵权。 一九八零年春天 EOF; $cws = scws_open(); scws_set_charset($cws, "utf8"); scws_set_dict($cws, ini_get('scws.default.fpath') . '/dict.utf8.xdb'); scws_set_rule($cws, ini_get('scws.default.fpath') . '/rules.utf8.ini'); //scws_set_ignore($cws, true); //scws_set_multi($cws, true); scws_send_text($cws, $text); echo "\n"; // top words printf("No. WordString Attr Weight(times)\n"); printf("-------------------------------------------------\n"); $list = scws_get_tops($cws, 10, "~v"); $cnt = 1; foreach ($list as $tmp) { printf("%02d. %-24.24s %-4.2s %.2f(%d)\n", $cnt, $tmp['word'], $tmp['attr'], $tmp['weight'], $tmp['times']); $cnt++; } echo "\n\n-------------------------------------------------\n"; // segment