Comprehend#
Amazon Comprehend is a natural language processing (NLP) service that uses machine learning to find insights and relationships in text. ... Customer emails, support tickets, product reviews, social media, even advertising copy represents insights into customer sentiment that can be put to work for your business.
Using Comprehend with boto3 (Python)
If your EC Data Platform account/DSL component has Textract access, you can use the Comprehend API via a lambda function dataplatform-comprehend-api
. The arguments that are required for the execution are based on the boto3 API. You can use Amazon WorkSpaces (after initiating an AWS role, 5.1.1) any DSL component that has access to boto3 and has associated permissions.
Following actions are supported:
detect_dominant_language()
detect_entities()
detect_key_phrases()
detect_sentiment()
detect_syntax()
Detect Dominant Language#
The following example demonstrates using the DetectDominantLanguage operation with Python.
import boto3
import json
# setup lambda client and specify which comprehend function to execute
# Method defines the comprehend API to call
# Args expects a dict with a key-value pair per argument
client = boto3.client('lambda', region_name='eu-west-1')
payload = {
"Method" : "detect_dominant_language",
"Args" : {
"Text" : "This is an example text that we will analyze with AWS Comprehend"
}
}
response = client.invoke(
FunctionName='dataplatform-comprehend-api',
Payload=json.dumps(payload)
)
comprehend_result = response['Payload'].read()
print(comprehend_result)
Detect Named Entities#
The following example uses the DetectEntities operation with Python. You must specify the language of the input text.
import boto3
import json
# setup lambda client and specify which comprehend function to execute
# Method defines the comprehend API to call
# Args expects a dict with a key-value pair per argument
client = boto3.client('lambda', region_name='eu-west-1')
payload = {
"Method" : "detect_entities",
"Args" : {
"Text" : "This is an example text that we will analyze with AWS Comprehend",
"LanguageCode" : "en"
}
}
response = client.invoke(
FunctionName='dataplatform-comprehend-api',
Payload=json.dumps(payload)
)
comprehend_result = response['Payload'].read()
print(comprehend_result)
Detect key phrases#
The following example uses the DetectKeyPhrases operation with Python. You must specify the language of the input text.
import boto3
import json
# setup lambda client and specify which comprehend function to execute
# Method defines the comprehend API to call
# Args expects a dict with a key-value pair per argument
client = boto3.client('lambda', region_name='eu-west-1')
payload = {
"Method" : "detect_key_phrases",
"Args" : {
"Text" : "This is an example text that we will analyze with AWS Comprehend",
"LanguageCode" : "en"
}
}
response = client.invoke(
FunctionName='dataplatform-comprehend-api',
Payload=json.dumps(payload)
)
comprehend_result = response['Payload'].read()
Detect Sentiment#
The following Python program detects the sentiment of input text with DetectSentiment. You must specify the language of the input text.
import boto3
import json
# setup lambda client and specify which comprehend function to execute
# Method defines the comprehend API to call
# Args expects a dict with a key-value pair per argument
client = boto3.client('lambda', region_name='eu-west-1')
payload = {
"Method" : "detect_sentiment",
"Args" : {
"Text" : "This is an example text that we will analyze with AWS Comprehend",
"LanguageCode" : "en"
}
}
response = client.invoke(
FunctionName='dataplatform-comprehend-api',
Payload=json.dumps(payload)
)
comprehend_result = response['Payload'].read()
print(comprehend_result)
Detect Syntax#
The following Python program detects the parts of speech in the input text with DetectSyntax. You must specify the language of the input text.
import boto3
import json
# setup lambda client and specify which comprehend function to execute
# Method defines the comprehend API to call
# Args expects a dict with a key-value pair per argument
client = boto3.client('lambda', region_name='eu-west-1')
payload = {
"Method" : "detect_syntax",
"Args" : {
"Text" : "This is an example text that we will analyze with AWS Comprehend",
"LanguageCode" : "en"
}
}
response = client.invoke(
FunctionName='dataplatform-comprehend-api',
Payload=json.dumps(payload)
)
comprehend_result = response['Payload'].read()
print(comprehend_result)