Using Amazon Comprehend with PHP

Using Amazon Comprehend with PHP

Amazon Comprehend offers its functionalities through the AWS SDK for PHP. Here’s a step-by-step guide on how to use Comprehend’s built-in analysis features with PHP, including sample code and output:

Prerequisites:

Steps:

Install the AWS SDK for PHP: Use Composer to install the aws/aws-sdk-php library:

composer require aws/aws-sdk-php

Configure AWS Credentials: There are several ways to configure your AWS credentials in PHP. Here are two common approaches:

  • Environment Variables:

putenv('AWS_ACCESS_KEY_ID=YOUR_ACCESS_KEY_ID'); 
putenv('AWS_SECRET_ACCESS_KEY=YOUR_SECRET_ACCESS_KEY'); 
putenv('REGION=us-east-1'); # Choose your desired region

Replace the placeholders with your actual AWS credentials and desired region.

  • Shared Credentials File:
Create a file named ~/.aws/credentials (Linux/macOS) or C:\Users\<username>\.aws\credentials (Windows) with the following content:

[default] 
aws_access_key_id = YOUR_ACCESS_KEY_ID 
aws_secret_access_key = YOUR_SECRET_ACCESS_KEY 
region = us-east-1 # Choose your desired region

Connect to Comprehend with PHP: Here’s a code snippet demonstrating how to connect to Comprehend using the AWS SDK for PHP:

require 'vendor/autoload.php'; 
use Aws\Comprehend\ComprehendClient; 
$comprehendClient = new ComprehendClient([ 
                   'version' => '2017-11-27', 
                   'region' => 'REGION' // Replace with your chosen region ]);</code> 

This code requires the autoloader (vendor/autoload.php) generated by Composer. It then creates a ComprehendClient object, specifying the API version and region.

Analyzing Text with Built-in Functions: Similar to the Python example, let’s explore two popular Comprehend features:

  • DetectSentiment:

$text = "This movie was absolutely fantastic! The acting was superb, and the plot kept me engaged the entire time. Highly recommend!"; 
$sentimentAnalysisResult = $comprehendClient->detectSentiment([ 'Text' => $text, 'LanguageCode' => 'en' // Specify the language of the text ]); 
var_dump($sentimentAnalysisResult);

This code defines a sample text and uses the detectSentiment method. The var_dump function displays the resulting JSON object containing sentiment information. The output will be similar to the Python example:

object(Aws\Result)#22 (3) { 
    ["@metadata"]=>array(2) 
    { 
        ["statusCode"]=>int(200) 
        ["effectiveUri"]=>string(73) "https://comprehend.us-east-1.amazonaws.com/" 
    } 
    ["Sentiment"]=>string(7) "POSITIVE" 
    ["SentimentScore"]=>array(4) 
    { 
        ["Positive"]=>float(0.99) 
        ["Negative"]=>float(0.01) 
        ["Neutral"]=>float(0) 
        ["Mixed"]=>float(0) 
    } 
}

  • DetectEntities:
See also  Day 8: Cloud Databases: Setting Up and Querying AWS RDS

$text = "Barack Obama, the former president of the United States, visited Paris, France last week."; 
$entityAnalysisResult = $comprehendClient->detectEntities([ 'Text' => $text, 'LanguageCode' => 'en' ]); 
var_dump($entityAnalysisResult);

This code analyzes a text containing entities and uses the detectEntities method. The output will be a PHP object containing details about the identified entities.

Additional Considerations:

  • You can analyze text from various sources: strings, local files, or S3 buckets using appropriate functions provided by the AWS SDK for PHP.
  • Comprehend offers more advanced features like key phrase detection and syntax analysis. Refer to the official documentation for details

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.