Detox是一个用于检测不同模式、不同长度的毒性文本(评论/帖子等)的库

安装次数: 16,272

依赖项: 0

建议者: 0

安全性: 0

星标: 12

关注者: 3

分支: 1

公开问题: 2

类型:detox

1.1.5 2018-07-03 07:11 UTC

This package is auto-updated.

Last update: 2024-09-20 16:54:39 UTC


README

Scrutinizer Code Quality Build Status Total Downloads Code Intelligence Status codecov License: MIT

该项目受到一个仅提供PHP实现(无需在C/Python中设置多个库或导入数据库转储等)的简单评分/过滤工具的启发。

安装

composer require arthurkushman/detox

使用单词和单词模式

获取任何文本的毒性分数

    $text  = new Text('Some text');   
    $words = new Words(new EnglishSet(), $text);
    $words->processWords();
    if ($words->getScore() >= 0.5) {
        echo 'Toxic text detected';
    }

在星号模式出现上测试输入字符串

    $words->processPatterns();
    if ($words->getScore() >= 0.5) {
        echo 'Toxic text detected';
    }    

使用短语

另一种选择是检查短语

    $phrases = new Phrases(new EnglishSet(), $text);
    $phrases->processPhrases();
    if ($words->getScore() >= 0.5) {
        echo 'Toxic text detected';
    }

没有限制同时使用所有选项,因此可以这样做

    // Phrases object extends Words - just use all inherited methods 
    $detox = new Phrases(new EnglishSet(), $text);
    $detox->processWords();
    // change string in Text object
    $text->setString('Another text');
    // inject Text object to Phrases 
    $detox->setText($text);
    $detox->processPhrases();
    $text->setString('Yet another text');
    $detox->setText($text);
    $detox->processPatterns();
    if ($detox->getScore() >= 0.5) {
        echo 'Toxic text detected';
    }

使用自定义模板和前缀/后缀预设进行替换

在某些特定情况下可能需要的一个附加选项是将单词/短语替换为预设模板

    $this->text->setPrefix('[');
    $this->text->setPostfix(']');
    $this->text->setReplaceChars('____');
    $this->text->setString('Just piss off dude');
    $this->text->setReplaceable(true);
    $this->phrases->setText($this->text);

    $this->phrases->processPhrases();
    echo $this->phrases->getText()->getString(); // output: Just [____] dude 

默认模式为5个破折号,因此可以在任何处理器之前调用$this->text->setReplaceable(true);以实现默认设置的替换。

创建自定义数据集

    $customSet = new CustomSet();
    $customSet->setWords([
        '0.9' => ['weird']
    ]);
    $this->text->setString('This weird text should be detected');
    $this->words->setText($this->text);
    $this->words->setDataSet($customSet);
    $this->words->processWords();
    echo $this->words->getScore(); // output: 0.9

运行测试

在根目录(在控制台)运行以下命令

phpunit

请确保全局安装phpunit,或从vendor运行它

vendor/bin/phpunit

欢迎所有贡献