mioga-technik / badwords
Badwords PHP 是一个用于检测内容中的“不良”词汇(例如粗话)的小型轻量级 PHP 库。
dev-master
2017-11-06 10:04 UTC
Requires
- php: >=5.3
Requires (Dev)
- phpunit/phpunit: 3.7.0
This package is not auto-updated.
Last update: 2024-09-25 07:17:38 UTC
README
Badwords PHP 是一个 小型轻量级 PHP 库,用于检测内容中的“不良”词汇,例如粗话。
除了检测字符串中是否存在词汇之外,过滤器还会尝试检测与列表中词汇相似的词汇,例如 gl@d 和 glad。
该库设计为 高度可配置,从使用的词汇列表到过滤器核心中的字符替换配置。
注意:目前提供的默认配置不是一个万无一失的解决方案,但它可以捕获大多数变体。这将在未来变得更加稳健。
需求
- 该库仅支持 PHP 5.3.0 及以上版本。
- 需要 Composer。
安装
要将库包含在项目的 composer.json 中。
$ composer require mioga-brian/badwords-php
$ composer update
此包运行不需要其他依赖。
使用方法
文件方法
使用该库最简单的方法如下,
$dictionary = new \Badword\Dictionary\Php('path/to/dictionary_list.php'); $config = new \Badword\Filter\Config\Standard(); $filter = new \Badword\Filter($dictionary, $config); $result = $filter->filter('My content...'); $result->getRiskLevel(); $result->getMatches(); $result->getMatchesAndRiskLevels(); $result->getHighlightedContent();
解释如下,
- 首先,使用
Dictionary对象加载您的“不良”词汇列表,或者创建自己的并实现Dictionary接口。 - 为过滤器定义一个配置(提供了一个默认的
Standard配置)。 - 创建一个
Filter,传递您的字典(s)和配置。 - 使用
filter()方法过滤内容。 - 使用
Result对象分析您的内容。
数组方法
// An example moderate dictionary. $dictionaryWords = array( 'some', 'bad', 'words' ); $dictionary = new \Badword\Dictionary\PhpArray($dictionaryWords, 1); $config = new \Badword\Filter\Config\Standard(); $filter = new \Badword\Filter($dictionary, $config); $result = $filter->filter('My content...'); $result->getRiskLevel(); $result->getMatches(); $result->getMatchesAndRiskLevels(); $result->getHighlightedContent();
对象层方法 [新]
$dictionaryWords = array( 'reject' => array( 'maecenas', 'mauris', 'luctus' ), 'moderate' => array( 'consectetur', 'neque', 'velit' ) ); $Badwords = new \Badword\Badwords($dictionaryWords); $result = $Badwords->Filter()->filter('My content...')); $result->getRiskLevel(); $result->getMatches(); $result->getMatchesAndRiskLevels(); $result->getHighlightedContent();
测试
要运行此包的单元测试,只需从包目录中运行 vendor/bin/phpunit。
致谢
- 通过 Brian Schäffner 更新仓库并将其推送到 Packagist。
- 由 Stephen Melrose 编写和开发。
- 原始概念由 Paul Lemon 提出。