mrurlwin / aws-comprehend
Laravel的AWS Comprehend包
Requires
- aws/aws-sdk-php: ~3.0
Requires (Dev)
- orchestra/testbench: ~3.0
- phpunit/phpunit: ~5.7
This package is not auto-updated.
Last update: 2022-03-22 03:29:00 UTC
README
Amazon Comprehend是AWS API PHP SDK的一部分,是Laravel包/外观。
Amazon Comprehend是一种自然语言处理(NLP)服务,它使用机器学习来在文本中找到洞察力和关系。它非常适合对文档批次的情感分析。
此存储库实现了一个简单的AWS Comprehend客户端服务提供者,并使其通过Laravel >= 5的Facade轻松访问。
有关更多信息,请参阅AWS Comprehend
需求
在AWS上创建一个账户,并记下您的API密钥。
使用Composer安装
在您的终端应用程序中,使用cd命令进入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 许可证。