Sep
18
2024
调用 scws 分词插件返回分词数组的php函数
作者:
绝缘体.. 发布:
2024-09-18 17:06 分类:
未分类 阅读:
抢沙发
调用 scws 分词插件返回分词数组的php函数。
/**
* 调用 scws 分词插件返回分词数组
* @param string $string 如:go的安装和卸载
* @param array $removeAttrList 根据词性移除,c:连词;uj:结构助词;
* @param int $minLen 词最小的长度
* @param bool $removeSame 是否去重
* @return array 结果如:['go', '安装', '卸载']
*/
function getWordListByScws($string, $removeAttrList = [], $minLen = 0, $removeSame = true): array {
$so = scws_new();
$so->set_charset('utf8');
$so->set_ignore(true);
$so->set_multi(0);
$so->set_duality(true);
$so->send_text($string);
$result = $so->get_words(null);
$so->close();
if ($result && $removeSame) {
$result = array_column($result, NULL, 'word');
//echo '<pre>';print_r($result);exit;
}
if ($result && $removeAttrList) {
foreach ($result as $key => &$value) {
if (in_array($value['attr'], $removeAttrList)) {
unset($result[$key]);
}
}
}
if ($result && $minLen) {
foreach ($result as $key => &$value) {
if (mb_strlen($value['word']) < $minLen) {
unset($result[$key]);
}
}
}
$result = $result ? array_keys($result) : [];
return $result;
}
微信扫一扫,打赏作者吧~