2 Star 5 Fork 4

anxu / sensitive-words

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

Sensitive word

基于DFA算法,实现敏感词的判断 和 过滤

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist anxu/sensitive "*"

or add

"anxu/sensitive": "*"

to the require section of your composer.json file.

Usage

1.将敏感词转为二维节点数组

$words=[
    '苹果',
    '香蕉'
];
$nodes =  Utils::words2Node($words);

//[
//    [
//        Node('苹', false),
//        Node('果', true)
//    ],
//    [
//        Node('香', false),
//        Node('蕉', true)
//    ],
//]

2.构建字典树

$tree = new Tree($nodes);
// $tree = new Tree();
// $tree->appendMultiple($nodes)

3.创建敏感词处理类,处理文本

$sensitive = new SensitiveWords($tree);
//$sensitive = new SensitiveWords();
//$sensitive->setTree($tree);

$text = '苹果正好吃,我不爱吃香蕉, 香梨才是王道,香水有毒';
//如果文本包含敏感词,则返回包含的敏感词数组;否则返回false
$res = $sensitive->has($text);

//用指定字符,替换文本中敏感词
$string = $sensitive->filter($text,'#');

例子

//敏感词数组;
$words=['苹果','香蕉'];

$nodes =  Utils::words2Node($words);
$tree = new Tree($nodes);
//如果敏感词数量比较大,字典树构建会比较慢,建议将构建好的字典树进行缓存
$sensitive = new SensitiveWords($tree);

$text = '苹果正好吃,我不爱吃香蕉, 香梨才是王道,香水有毒';
$res = $sensitive->has($text);
$string = $sensitive->filter($text,'#');
MIT License Copyright (c) 2019 anxu Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

基于DFA算法,实现敏感词的判断 和 过滤 展开 收起
PHP
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
PHP
1
https://gitee.com/anxu/sensitive.git
git@gitee.com:anxu/sensitive.git
anxu
sensitive
sensitive-words
master

搜索帮助