调用 scws 分词插件返回分词数组的php函数

 
更多

调用 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;
}
打赏

本文固定链接: https://www.cxy163.net/archives/2924 | 绝缘体

该日志由 绝缘体.. 于 2024年09月18日 发表在 未分类 分类下, 你可以发表评论,并在保留原文地址及作者的情况下引用到你的网站或博客。
原创文章转载请注明: 调用 scws 分词插件返回分词数组的php函数 | 绝缘体
关键字: , , , ,

调用 scws 分词插件返回分词数组的php函数:等您坐沙发呢!

发表评论


快捷键:Ctrl+Enter