radiojoe/comment-analyzer

Google Perspective Comment Analyzer API for PHP

dev-master 2023-05-14 21:48 UTC

This package is auto-updated.

Last update: 2024-09-15 00:39:29 UTC


README

CommentAnalyzer 是 Google Perspective Comment Analyzer API 的 API 包装器

此包允许您以编程方式扫描支持语言中的任何文本,并针对与负面情绪相关的各种指标应用结果得分,例如自动隐藏来自博客或社交媒体平台的骚扰或露骨的评论。

安装/使用

从源代码

从 GitHub 克隆存储库或将解压缩到您的 vendor 目录中。CommentAnalyzer 为 PSR-4 自动加载打包。

使用 Composer

composer require radiojoe/comment-analyzer

基本用法

CommentAnalyzer 接受以 Comment 对象的形式的文本,该对象使用单个参数 - 您希望分析的文本 - 构造。

实例化 Analyzer 对象,并向其提供您的 Perspective API 密钥和可选的 PSR-3 兼容的 LoggerInterface。然后通过调用实例的 addAttributeModel() 方法添加您希望评分的属性模型。 注意:在调用 analyze() 方法之前,您必须提供至少一个属性模型进行评分,否则将抛出 AnalyzerException

将 Comment 对象的实例传递给 Analyzer 对象的 analyze 方法。这将启动 API 调用过程。

注意:如果您希望以异步方式使用此库,则 Comment 对象包含状态变量 STATE_CREATEDSTATE_SUBMITTEDSTATE_ANALYZED。您可以通过其 getState() 方法检查实例的当前状态,以确保您不会通过多个线程尝试处理相同的评论。

调用成功完成后,通过引用传递的 Comment 对象将填充 SummaryScoreSpanScore 对象,分别表示 API 返回的摘要和跨度得分数据。通过调用 Comment 对象的 getSummaryScore()getSpanScore() 方法(需要单个必需参数 - 您提供给 Analyzer 实例的属性模型之一)来访问这些对象。

示例

<?php
require './vendor/autoload.php';
use bredmor\CommentAnalyzer\Comment;
use bredmor\CommentAnalyzer\Analyzer;

$key = 'your_api_key';

try {
    // Instantiate API and define an attribute model
    $api = new Analyzer($key);
    $api->addAttributeModel(Analyzer::MODEL_TOXICITY);
} catch (Exception $e) {
    echo 'Error: ' . $e->getMessage();
}

// Build your comments from text
$comment = new Comment('Hello my good sir, how are you this fine evening?');
$comment2 = new Comment('You suck, jerkwad.');


try {
    // Analyze the comments and fetch a score for the attribute model you want
    $api->analyze($comment);
    $scoreObj = $comment->getSummaryScore(Analyzer::MODEL_TOXICITY);
    if($scoreObj) {
        echo 'Comment 1 Toxicity rating: ' . floor($scoreObj->value * 100) . '%';
    }

    $api->analyze($comment2);
    $scoreObj2 = $comment2->getSummaryScore(Analyzer::MODEL_TOXICITY);
    if($scoreObj2) {
        echo "\n" . 'Comment 2 Toxicity rating: ' . floor($scoreObj2->value * 100) . '%';
    }
} catch (Exception $e) {
    echo 'Error: ' . $e->getMessage();
}

此示例代码应输出类似以下内容

Comment 1 Toxicity rating: 2%
Comment 2 Toxicity rating: 95%

其他示例可以在 /tests 目录中找到。

请参阅 Perspective API 文档 以获取有关可用属性模型和得分含义的完整参考。

错误处理

当遇到错误时,库中依赖于输入或适当功能使用的每个部分都将根据需要抛出 CommentExceptionAnalyzerException

Analyzer 对象接受可选的 PSR-3 兼容的 LoggerInterface,当 API 不可达或响应非 200 HTTP 错误代码时,记录一个 critical 错误。

API 支持

CommentAnalyzer 支持以下 Perspective API 功能

  • 通过 Analyzer::addAttributeModel() 分析生产、实验和 NYT 类别中的所有属性模型。
  • 通过 Comment::getSummaryScore() 获取提供的文本的摘要得分。
  • 通过 Comment::getSpanScore() 获取提供的文本的跨度得分。
  • 通过 Analyzer::addLanguage() 支持通过 ISO 631-1 双字母代码提供完整语言支持。
  • 语言自动检测 - 语言可以通过 Analyzer::addLanguage() 显式声明。

要求

CommentAnalyzer 版本 3.x 在 PHP 8.0 及更高版本上进行了测试。

作者

贡献

欢迎提交拉取请求、错误报告和功能请求。

如果您添加了新功能或更改了尚未有测试的现有功能,请在您的PR中添加一个测试!

测试

测试通过PHPUnit进行处理。

您可以使用 composer run test 运行所有当前测试。

许可

CommentAnalyzer 在MIT许可证下授权 - 有关详细信息,请参阅LICENSE 文件