michaeljwright/aws-comprehend

该包已被废弃且不再维护。作者建议使用aws-sdk-php-laravel包。

Laravel的AWS Comprehend包

v1.1 2018-06-13 11:05 UTC

This package is not auto-updated.

Last update: 2023-08-23 08:33:23 UTC


README

这是Amazon Comprehend的Laravel包/facade,它是AWS API PHP SDK的一部分。

Amazon Comprehend是一种自然语言处理(NLP)服务,它使用机器学习在文本中寻找见解和关系。它非常适合对文档批次进行情感分析。

该存储库实现了一个简单的AWS Comprehend客户端Service Provider,并使其通过Laravel >= 5中的Facade轻松访问。

有关更多信息,请参阅AWS Comprehend

要求

AWS上创建账户并记下您的API密钥。

使用Composer安装

在终端应用程序中使用cd命令将您的laravel项目根目录切换到laravel项目的根目录,并使用composer将项目作为依赖项添加。

composer require michaeljwright/aws-comprehend

这将在您的composer.json中添加以下行,并将项目及其依赖项下载到项目的./vendor目录中

//

./composer.json
{
    "name": "michaeljwright/aws-comprehend",
    "description": "A Laravel package for the AWS Comprehend",

    // ...

    "require-dev": {
        "phpunit/phpunit": "~5.7",
        "orchestra/testbench": "~3.0"
        // ...
    },
    "require": {
        "aws/aws-sdk-php":"~3.0"
    },

    //...
}

设置/配置

为了使用静态接口,我们必须自定义应用程序配置,以告诉系统在哪里可以找到新的服务。打开文件config/app.php,并添加以下行([a],[b])

// config/app.php

return [

    // ...

    'providers' => [

        // ...

        /*
         * Package Service Providers...
         */
        MichaelJWright\Comprehend\ComprehendServiceProvider::class, // [a]

        /*
         * Application Service Providers...
         */
        App\Providers\AppServiceProvider::class,
        App\Providers\AuthServiceProvider::class,
        App\Providers\EventServiceProvider::class,
        App\Providers\RouteServiceProvider::class,

    ],

    // ...

    'aliases' => [

        'App' => Illuminate\Support\Facades\App::class,
        'Artisan' => Illuminate\Support\Facades\Artisan::class,

        // ...

        'Comprehend' => 'MichaelJWright\Comprehend\ComprehendFacade', // [b]
        'Hash' => Illuminate\Support\Facades\Hash::class,

        // ...
    ],

];

发布供应商

aws-comprehend需要一个连接配置。要开始,您需要运行以下命令以发布所有供应商资产

php artisan vendor:publish

这将在您的app中创建一个config/comprehend.php文件,您可以修改它来设置配置。确保您添加了在comprehend.php文件中引用的相关环境变量。

重要 非常重要的是要编辑您的AWS IAM,为指定的密钥/密钥/区域包含Amazon Comprehend API。否则,任何都不会工作。

用法

现在您应该在您的应用程序中使用外观。

重要 每个API调用都有一个限制,即25条评论(AWS称为文档)。

例如

$config = [
       'LanguageCode' => 'en',
       'TextList' => ['This is good', 'This is bad'],
   ];

$jobSentiment = \Comprehend::batchDetectSentiment($config);

dd($jobSentiment['ResultList']);

从包含字符串(评论)的给定数组中检测情感示例

// FIRST create a function to call the comprehend facade and parse the results (below will return an array with the overall sentiment as well as positive/negative scores)

public function sentimentAnalysis($comments) {

    $results = array();

    if(count($comments)>0) {
        $config = [
               'LanguageCode' => 'en',
               'TextList' => $comments,
           ];

        $jobSentiment = \Comprehend::batchDetectSentiment($config);

        $positive = array();
        $negative = array();

        if(count($jobSentiment['ResultList'])) {
            foreach($jobSentiment['ResultList'] as $result){
                $positive[] = $result['SentimentScore']['Positive'];
                $negative[] = $result['SentimentScore']['Negative'];
            }
        }

        $results['positive'] = array_sum($positive)/count($positive);
        $results['negative'] = array_sum($negative)/count($negative);
        $results['sentiment'] = ($results['positive'] > $results['negative'] ? 'POSITIVE' : 'NEGATIVE');

        return $results;
    } else {
        return $results['sentiment'] = 'INVALID';
    }
}

// SECOND create an array of comments for the analysis and call the above function

$comments = [
    'I think this is very good considering I created a package/wrapper for Amazon Comprehend. Yay me!',
    'Oh my good this is such a bloody rubbish package/wrapper. I hope the author stops coding immediately.',
    'This is really good, I really love this stand by this',
    'This is sooooo bad'
];

dd($this->sentimentAnalysis($comments));

测试

单元测试使用PHPunit和orchestra/testbench创建,可以使用./vendor/bin/phpunit运行。

贡献

找到你可以帮助的领域并付诸实践。开源是关于协作和开放参与的。尽量让你的代码看起来像已经存在的代码或者更好,并提交一个拉取请求。此外,如果你有任何改进代码或者扩大范围和功能的想法,请联系任何贡献者。

许可证

MIT许可证。