Created
December 28, 2019 11:29
-
-
Save hassaku63/e19f87ae48e5ecb7aaafdf6fcb8fc7c1 to your computer and use it in GitHub Desktop.
lambdaの中でcomprehend apiを呼び出す
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 | |
# Comprehend constant | |
REGION = 'us-west-2' | |
# Function for detecting sentiment | |
def detect_sentiment(text, language_code): | |
comprehend = boto3.client('comprehend', region_name=REGION) | |
response = comprehend.detect_sentiment(Text=text, LanguageCode=language_code) | |
return response | |
def lambda_handler(event, context): | |
# TODO implement | |
print(json.dumps(event)) | |
# | |
# 'data' expects a object has 3 key: | |
# - LanguageCode, Score, Text | |
# | |
data = json.loads(event['Input']['Payload']) | |
print(data) | |
# detecting sentiment | |
result = detect_sentiment(data['Text'], data['LanguageCode']) | |
print("Starting detecting sentiment") | |
print(json.dumps(result, sort_keys=True)) | |
print("End of detecting sentiment\n") | |
return json.dumps({ | |
'event': result | |
}) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
参考
https://dev.classmethod.jp/cloud/comprehend-operations-using-python-boto3-ja/