Amazon Comprehend via RESTFUL API and Python

Published by Abhay Rastogi on

This amazon comprehend tutorial covers executing amazon comprehend service via RESTFUL API and Python SDK.

We can use the Amazon Comprehend machine learning service to easily identify the language, extract key phrases, places, people, brands, or events, understand sentiment about products or services, and more from the text.

Amazon Comprehend Using RESTFUL API

we can use Postman for making RESTFUL API call to the Amazon Comprehend service.

POST: https://comprehend.us-east-2.amazonaws.com/

Authorization:
AWS Signature
• AccessKey :<enter access key>
• SecretKey:<enter secret key>
• AWSRegion: us-east-2
• Service Name: comprehend

Detecting Sentiments from Text

Headers:
X-amz-target: Comprehend_20171127.DetectSentiment
Content-Type: application/x-amz-json-1.1

Detect Entities from Text

Headers:
X-amz-target: Comprehend_20171127.DetectEntities
Content-Type: application/x-amz-json-1.1

aws comprehend postman

Body

Raw(Text)

{
“Text”: “Coronavirus Cases in India Today, New Coronavirus Variant Delta Plus Symptoms Live Updates: After a gap of six days, India once again registered less than 40,000 new coronavirus cases today. According to the bulletin released by the Union Health Ministry, India detected 39,476 fresh Covid-19 infections on Monday. In another sign of receding the second wave of the coronavirus, the number of Covid deaths further declined to 723 in the last 24 hours. This was the lowest figure in terms of Covid fatalities recorded in India in the last 88 days. The number of active cases has also slumped below 5 lakh mark. The health bulletin said that India has 4,82,071 active cases right now. The national recovery rate has gone up to 97.11 per cent.\nIf you have questions about your bill, AnyCompany Customer Service is available by phone at 206-555-0199 or email at support@anycompany.com.”,
“LanguageCode”: “en”
}

aws comprehend postman

Amazon Comprehend Using Python SDK

Amazon comprehends can be also integrated into Python by using Boto3 plugin. install this by pip install boto3 in the terminal.

Code

import boto3
from botocore.config import Config

ACCESS_KEY = '<Your Key>'
SECRET_KEY = '<Your Key>'

my_config = Config(
    region_name='us-east-2',
    signature_version='v4',
    retries={
        'max_attempts': 10,
        'mode': 'standard'
    }
)
client = boto3.client(
    'comprehend',
    config=my_config,
    aws_access_key_id=ACCESS_KEY,
    aws_secret_access_key=SECRET_KEY,
)

response_Entity = client.detect_entities(
    LanguageCode="en",
    Text='Asynchronous inference requests are measured in units of 100 characters, with a 3 unit (300 character) minimum charge per request. You are charged $3 per hour​ ...'
)

print(response_Entity)

response_Sentiment = client.detect_sentiment(
    LanguageCode="en",
    Text='Asynchronous inference requests are measured in units of 100 characters, with a 3 unit (300 character) minimum charge per request. You are charged $3 per hour​ ...'
)

print(response_Sentiment)


response_DominantLanguage = client.detect_dominant_language(
    Text='Asynchronous inference requests are measured in units of 100 characters, with a 3 unit (300 character) minimum charge per request. You are charged $3 per hour​ ...'
)

print(response_DominantLanguage)

Categories: ML

1 Comment

actuapy · 4th June 2022 at 12:46 pm

Thanks

Leave a Reply

Avatar placeholder

Your email address will not be published.