mrurlwin/aws-comprehend
最新稳定版本:v1.2.0
Composer 安装命令:
composer require mrurlwin/aws-comprehend
包简介
A Laravel package for AWS Comprehend
README 文档
README
A Laravel package/facade for Amazon Comprehend, part of AWS API PHP SDK.
Amazon Comprehend is a natural language processing (NLP) service that uses machine learning to find insights and relationships in text. It's great for running sentiment analysis on batches of documents.
This repository implements a simple Service Provider of the AWS Comprehend client, and makes it easily accessible via a Facade in Laravel >= 5.
- See docs for usage (https://docs.aws.amazon.com/comprehend/latest/dg/API_Reference.html)
Also see AWS Comprehend for more information.
Requirements
Create an account at AWS and take note of your API keys.
Installation using Composer
In your terminal application move to the root directory of your laravel project using the cd command and require the project as a dependency using composer.
composer require michaeljwright/aws-comprehend
This will add the following lines to your composer.json and download the project and its dependencies to your projects ./vendor directory:
// ./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" }, //... }
Setup / Configuration
In order to use the static interface we must customize the application configuration to tell the system where it can find the new service. Open the file config/app.php and add the following lines ([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, // ... ], ];
Publish Vendor
aws-comprehend requires a connection configuration. To get started, you'll need to publish all vendor assets running:
php artisan vendor:publish
This will create a config/comprehend.php file in your app that you can modify to set your configuration. Make sure you add relevant environment variables which are referenced in the comprehend.php file.
IMPORTANT It's very important to edit your AWS IAM for the specified key/secret/region to include the Amazon Comprehend API. Otherwise nothing will work.
Usage
Now you should be able to use the facade within your application.
IMPORTANT There is a limit of 25 comments (what AWS calls documents) for each API call.
For example:
$config = [ 'LanguageCode' => 'en', 'TextList' => ['This is good', 'This is bad'], ]; $jobSentiment = \Comprehend::batchDetectSentiment($config); dd($jobSentiment['ResultList']);
Example to detect Sentiment from a given array containing strings (comments)
// 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));
Testing
Unit Tests are created with PHPunit and orchestra/testbench, they can be ran with ./vendor/bin/phpunit.
Contributing
Find an area you can help with and do it. Open source is about collaboration and open participation. Try to make your code look like what already exists or better and submit a pull request. Also, if you have any ideas on how to make the code better or on improving the scope and functionality please contact any of the contributors.
License
MIT License.
mrurlwin/aws-comprehend 适用场景与选型建议
mrurlwin/aws-comprehend 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 11.29k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2020 年 08 月 11 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 mrurlwin/aws-comprehend 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mrurlwin/aws-comprehend 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 11.29k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-08-11