arthurkushman / detox
Detox是一个用于检测不同模式、不同长度的毒性文本(评论/帖子等)的库
1.1.5
2018-07-03 07:11 UTC
Requires
- php: ^7.1
Requires (Dev)
- fzaninotto/faker: ^1.7
- mockery/mockery: ~1.0
- phpunit/phpunit: >=6.5
This package is auto-updated.
Last update: 2024-09-20 16:54:39 UTC
README
该项目受到一个仅提供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
欢迎所有贡献