SCWS (Simple Chinese Word Segmentation) 是高效能中文分詞引擎,支援專有名詞、人名、地名、數字年代等規則集,準確率 90-95%,分詞速度 1.2MB/秒 (C 核心) / 250KB/秒 (PHP 擴展)。
php_scws.dll (Windows) 或編譯 scws.so (Linux)dict.utf8.xdb 與規則 rules.utf8.iniphp.ini:extension=php_scws.dll
scws.default.charset = utf-8
scws.default.fpath = /path/to/dict.utf8.xdb
scws.default.rpath = /path/to/rules.utf8.ini
<?php
// 1. 實例化
$scws = scws_new();
// 2. 設定編碼 (需與詞典/規則/輸入文字一致)
$scws->set_charset('utf-8');
// 3. 載入詞典與規則
$scws->set_dict('/path/dict.utf8.xdb');
$scws->set_rule('/path/rules.utf8.ini');
// 4. 參數調優
$scws->set_ignore(true); // 去除標點
$scws->set_multi(true); // 複式分割:中國人 → 中國 + 人 + 中國人
$scws->set_duality(true); // 雙字聚合
// 5. 送入文字
$scws->send_text("歡迎來到火星時代IT開發");
// 6. 取得分詞結果
while ($token = $scws->get_result()) {
print_r($token);
/* 輸出結構範例:
Array (
[word] => 火星
[idf] => 5.23
[attr] => n
[off] => 6
[len] => 4
)
*/
}
// 7. 關閉釋放資源
$scws->close();
?>
$scws->send_text($long_text);
$top_words = $scws->get_tops(10); // 取前 10 高頻詞
foreach ($top_words as $word) {
echo "{$word['word']} (權重: {$word['weight']})<br>";
}
| 版本 | 類型 | 平台 | PHP 版本 | 備註 |
|---|---|---|---|---|
| SCWS-1.1.x | C 原始碼 | Unix/Linux | - | 核心庫,需自行編譯 |
| php_scws.dll(1) | PHP 擴展 | Windows | 4.4.x | |
| php_scws.dll(2) | PHP 擴展 | Windows | 5.2.x | |
| php_scws.dll(3) | PHP 擴展 | Windows | 5.3.x | |
| php_scws.dll(4) | PHP 擴展 | Windows | 5.4.x | 修正 5.4 相容性 |
| PSCWS23 | PHP 原始碼 | 跨平台 | - | 不支援 UTF-8 |
| PSCWS4 | PHP 原始碼 | 跨平台 | - | 純 PHP 實作,效能較低 |
keywords TEXT)MATCH(keywords) AGAINST(...) 或 LIKE 條件組合dict.utf8.txt 後用 scws-gen-dict 重新生成 .xdb