cledilsonweb/badwords

Badwords PHP 是一个用于检测内容中“不良”词汇的小型轻量级 PHP 库,例如粗俗用语。

安装: 9

依赖者: 0

建议者: 0

安全: 0

星标: 0

关注者: 0

分支: 2

类型:

v0.0.1 2022-09-16 21:42 UTC

This package is auto-updated.

Last update: 2024-09-04 00:27:02 UTC


README

Badwords PHP 是一个用于检测 "不良" 词汇(例如粗俗用语)的 小型轻量级 PHP 库。这是从 mioga-technik/badwords 分支出来的,以便进行定制。

除了明显匹配字符串中是否存在某个词外,过滤器还试图检测与列表中相似的词,例如 gl@dglad

该库旨在 高度可配置,从使用的词汇列表到过滤器核心的字符替换配置。

注意: 目前提供的默认配置 不是一个万无一失的解决方案,但它可以捕获大多数变化。随着时间的推移,这会变得更加健壮。

要求

  • 该库仅支持 PHP 5.6 及以上版本。
  • 需要 Composer。

安装

要将它包含在你的项目 composer.json 中。

    $ composer require cledilsonweb/badwords
    $ 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
  • 使用 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

致谢

以前的作者