Last active
April 8, 2021 02:18
-
-
Save ccortezb/d4428710d160a7f4cb4cb5ddec283568 to your computer and use it in GitHub Desktop.
Comprehend App.py Code (go to repo)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import boto3 | |
#Using boto3 to call the Comprehend API | |
client = boto3.client('comprehend') | |
#Lambda function to work with Comprehend | |
def lambda_handler(event, context): | |
print("event puro:", event) | |
#Accessing data | |
puro = json.dumps(event) #intentando cambiar aqui sino vuelvo a la linea 13 | |
text = puro[0] | |
json_object = json.loads(puro) | |
print("texto a analizar: ", json_object["body"]) | |
Body = json.dumps(json_object["body"]) | |
print("Body:", Body) | |
#Sentiment Analysis | |
print("iniciando analisis de sentimiento....") | |
sentiment = client.detect_sentiment(Text = Body, LanguageCode = 'en') #API call for sentiment analysis | |
sentRes = sentiment['Sentiment'] #Positive, Neutral, or Negative | |
sentScore = sentiment['SentimentScore'] #Percentage of Positive, Neutral, and Negative | |
print(sentRes) | |
print(sentScore) | |
print("iniciando extraccion de entidades....") | |
#Entity Extraction | |
entities = client.detect_entities(Text = Body, LanguageCode = 'en') #API call for entity extraction | |
entities = entities['Entities'] #all entities | |
print(entities) | |
textEntities = [dict_item['Text'] for dict_item in entities] #the text that has been identified as entities | |
typeEntities = [dict_item['Type'] for dict_item in entities] #the type of entity the text is | |
print(textEntities) | |
print(typeEntities) | |
return { | |
'statusCode': 200, | |
'body': str(sentiment) + str(entities) #body returned from our function | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment