Amazon Comprehend 自然语言处理,PHP使用教程,调用示例

Amazon Comprehend 是一项自然语言处理 (NLP) 服务,使用机器学习来发现文本中的洞察信息。Amazon Comprehend 提供关键词提取、情绪分析、实体识别、主题建模和语言检测 API,因此您可以轻松地将自然语言处理集成到您的应用程序中。您只需在应用程序中调用相应 Amazon Comprehend API 并提供源文档或文本的位置即可。这些 API 将以 JSON 格式输出实体、关键词、情绪和语言,以供您在应用程序中使用。

一、通过Composer安装SDK(需要安装aws/aws-sdk-php软件包)

composer require aws/aws-sdk-php

https://packagist.org/packages/aws/aws-sdk-php

或下载独立的AWS来开始使用。 zip 或aws.phar文件。
https://docs.aws.amazon.com/aws-sdk-php/v3/download/aws.zip
二、非框架安装需要引用/vendor/autoload.php
use Aws\Comprehend\ComprehendClient;
use Aws\Exception\AwsException;

完整demo

<?php
namespace app\controller;

use app\BaseController;
use Aws\Comprehend\ComprehendClient;
use Aws\Textract\TextractClient;
use Aws\Exception\AwsException;
class Index extends BaseController
{
	/**
	 * doc:https://docs.aws.amazon.com/aws-sdk-php/v3/api/api-comprehend-2017-11-27.html#detectsentiment
	 */
    public function aws(){
	    $client=new ComprehendClient([
		    'version' => 'latest',
            //'version' => '2018-06-27',
		    'region' => 'us-west-2',
		    'credentials' => [
			    'key' => 'AKIAQHKSE4HRGDDDDDDD', //IAM user key
			    'secret' => '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', //IAM user secret
		    ]]);

	    /**
	     * 情绪分析
	     * 情绪分析 API 会返回文本的整体情绪(正面、负面、中性或混合)。
	     */

	    $result = $client->detectSentiment([
//	    'LanguageCode'=>'en | es | fr | de | it | pt | ar | hi | ja | ko | zh | zh-TW',//必填
	    'LanguageCode'=>'en',//必填
        'Text'=>'Your service attitude is really poor',//必填
		]);
	    print_r($result->toArray());
	    /*
		Array
		(
		    [Sentiment] => NEGATIVE
		    [SentimentScore] => Array
		        (
		            [Positive] => 6.2582264945377E-5
		            [Negative] => 0.99910479784012
		            [Neutral] => 0.00083179638022557
		            [Mixed] => 8.1932506645899E-7
		        )

		    [@metadata] => Array
		        (
		            [statusCode] => 200
		            [effectiveUri] => https://comprehend.us-west-2.amazonaws.com
		            [headers] => Array
		                (
		                    [x-amzn-requestid] => 7436bbaf-73c1-4462-a814-d29efe81522f
		                    [content-type] => application/x-amz-json-1.1
		                    [content-length] => 164
		                    [date] => Thu, 21 May 2020 08:46:28 GMT
		                )

		            [transferStats] => Array
		                (
		                    [http] => Array
		                        (
		                            [0] => Array
		                                (
		                                )

		                        )

		                )

		        )

		)
	    */
	    /**
	     * 语言检测
	     * 语言检测 API 可自动识别出 100 多种语言编写的文本,并返回主导语言以及证明其占据主导地位的置信度得分。
	     */

	    $result =$client->detectDominantLanguage([
		    'Text'=>'Your service attitude is really poor',//必填
	    ]);
	    print_r($result->toArray());
	    /*
	    Array
	    (
		    [Languages] => Array
		    (
			    [0] => Array
			    (
				    [LanguageCode] => en
				    [Score] => 0.98973816633224
                )

            )

		    [@metadata] => Array
			    (
				    [statusCode] => 200
		            [effectiveUri] => https://comprehend.us-west-2.amazonaws.com
		            [headers] => Array
			    (
				    [x-amzn-requestid] => e4a6f3cd-5678-46b7-abd3-51d81e28b69f
			    [content-type] => application/x-amz-json-1.1
		                    [content-length] => 64
		                    [date] => Thu, 21 May 2020 08:42:48 GMT
		                )

		            [transferStats] => Array
			    (
				    [http] => Array
				    (
					    [0] => Array
					    (
					    )

				    )

			    )

		        )

		)
	     */
	    /**
	     * 关键词提取
	     * 关键词提取 API 会返回关键词或谈话要点以及证明此为关键词的置信度。
	     */

	    $result =$client->detectKeyPhrases([
		    'LanguageCode'=>'en',//必填
		    'Text'=>'Your service attitude is really poor',//必填
	    ]);
	    print_r($result->toArray());
        /*
	    Array
	    (
		    [KeyPhrases] => Array
		    (
			    [0] => Array
			    (
				    [Score] => 1
                    [Text] => Your service attitude
	    [BeginOffset] => 0
                    [EndOffset] => 21
                )

        )

		    [@metadata] => Array
			    (
				    [statusCode] => 200
		            [effectiveUri] => https://comprehend.us-west-2.amazonaws.com
		            [headers] => Array
			    (
				    [x-amzn-requestid] => 18657c2a-28d9-432c-9e4b-b403ac597299
			    [content-type] => application/x-amz-json-1.1
		                    [content-length] => 92
		                    [date] => Thu, 21 May 2020 09:01:37 GMT
		                )

		            [transferStats] => Array
			    (
				    [http] => Array
				    (
					    [0] => Array
					    (
					    )

				    )

			    )

		        )

		)
        */


	    /**
	     * 实体识别
	     * 实体识别 API 会返回根据提供的文本自动分类的命名实体(“人物”、“地点”和“位置”等)。
	     */

	    $result =$client->detectEntities([
		    'LanguageCode'=>'en',//必填
		    'Text'=>'Your service attitude is really poor',//必填
	    ]);
	    print_r($result->toArray());
	    /*
	    Array
	    (
		    [Entities] => Array
		    (
		    )

		    [@metadata] => Array
			    (
				    [statusCode] => 200
		            [effectiveUri] => https://comprehend.us-west-2.amazonaws.com
		            [headers] => Array
			    (
				    [x-amzn-requestid] => 78db83bb-7bca-4381-82f8-06deb3e18546
			    [content-type] => application/x-amz-json-1.1
		                    [content-length] => 15
		                    [date] => Thu, 21 May 2020 09:04:33 GMT
		                )

		            [transferStats] => Array
			    (
				    [http] => Array
				    (
					    [0] => Array
					    (
					    )

				    )

			    )

		        )

		)
	     */

	    /**
	     * 语法分析
	     * 借助 Amazon Comprehend Syntax API,客户能够使用分词断句和词性 (PoS) 分析文本,识别文本中的名词和形容词等单词边界和标签。
	     */
	    $result =$client->detectSyntax([
		    'LanguageCode'=>'en',//必填
		    'Text'=>'Your service attitude is really poor',//必填
	    ]);
	    print_r($result->toArray());

	    /*
	    Array
	    (
		    [SyntaxTokens] => Array
		    (
			    [0] => Array
			    (
				    [TokenId] => 1
                    [Text] => Your
	    [BeginOffset] => 0
                    [EndOffset] => 4
                    [PartOfSpeech] => Array
				    (
					    [Tag] => PRON
					    [Score] => 0.99996387958527
			                        )

			                )

			            [1] => Array
				    (
					    [TokenId] => 2
			                    [Text] => service
				    [BeginOffset] => 5
			                    [EndOffset] => 12
			                    [PartOfSpeech] => Array
				    (
					    [Tag] => NOUN
					    [Score] => 0.9985853433609
			                        )

			                )

			            [2] => Array
				    (
					    [TokenId] => 3
			                    [Text] => attitude
				    [BeginOffset] => 13
			                    [EndOffset] => 21
			                    [PartOfSpeech] => Array
				    (
					    [Tag] => NOUN
					    [Score] => 0.99978679418564
			                        )

			                )

			            [3] => Array
				    (
					    [TokenId] => 4
			                    [Text] => is
				    [BeginOffset] => 22
			                    [EndOffset] => 24
			                    [PartOfSpeech] => Array
				    (
					    [Tag] => VERB
					    [Score] => 0.99935394525528
			                        )

			                )

			            [4] => Array
				    (
					    [TokenId] => 5
			                    [Text] => really
				    [BeginOffset] => 25
			                    [EndOffset] => 31
			                    [PartOfSpeech] => Array
				    (
					    [Tag] => ADV
					    [Score] => 0.9999988079071
			                        )

			                )

			            [5] => Array
				    (
					    [TokenId] => 6
			                    [Text] => poor
				    [BeginOffset] => 32
			                    [EndOffset] => 36
			                    [PartOfSpeech] => Array
				    (
					    [Tag] => ADJ
					    [Score] => 0.9997541308403
			                        )

			                )

			        )

			    [@metadata] => Array
				    (
					    [statusCode] => 200
			            [effectiveUri] => https://comprehend.us-west-2.amazonaws.com
			            [headers] => Array
				    (
					    [x-amzn-requestid] => 0eb35b66-d36a-43e8-94ee-b5b0c3e786eb
				    [content-type] => application/x-amz-json-1.1
			                    [content-length] => 722
			                    [date] => Thu, 21 May 2020 09:08:51 GMT
			                )

			            [transferStats] => Array
				    (
					    [http] => Array
					    (
						    [0] => Array
						    (
						    )

					    )

				    )

			        )

			)
	    */

    }
}

https://docs.aws.amazon.com/comprehend/latest/dg/comprehend-general.html

https://aws.amazon.com/cn/comprehend/features/

https://docs.aws.amazon.com/general/latest/gr/rande.html

https://docs.aws.amazon.com/comprehend/latest/dg/supported-languages.html

linux(centos)安装python

下载pthon安装包
https://www.python.org/downloads/source/

wget https://www.python.org/ftp/python/3.8.3/Python-3.8.3.tgz

创建安装目录

mkdir -p /usr/local/python3.8.3

解压

tar -zxvf Python-3.8.3.tgz

 编译安装 首先需要gcc环境和zlib库为了方向键等不出现乱码还需要 readline-devel 包

yum -y install gcc
yum -y install zlib*
yum install readline-devel
cd Python-3.8.3
./configure --prefix=/usr/local/python3.8.3
#编译安装
make && make install
建立软链接
ln -s /usr/local/python3.8.3/bin/python3.8 /usr/bin/python
ln -s /usr/local/python3.8.3/bin/pip3.8 /usr/bin/pip
[root@fd07bda1fc63 bin]# python --version
Python 3.8.3