Created
December 28, 2019 11:28
-
-
Save hassaku63/a84b63845091dd2c5666219403e02252 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 tempfile | |
import boto3 | |
from uuid import uuid4 | |
# Comprehend constant | |
REGION = 'us-west-2' | |
# Function for detecting the dominant language | |
def detect_dominant_language(text): | |
comprehend = boto3.client('comprehend', region_name=REGION) | |
response = comprehend.detect_dominant_language(Text=text) | |
return response | |
# Function for detecting named entities | |
def detect_entities(text, language_code): | |
comprehend = boto3.client('comprehend', region_name=REGION) | |
response = comprehend.detect_entities(Text=text, LanguageCode=language_code) | |
return response | |
# Function for detecting key phrases | |
def detect_key_phraes(text, language_code): | |
comprehend = boto3.client('comprehend', region_name=REGION) | |
response = comprehend.detect_key_phrases(Text=text, LanguageCode=language_code) | |
return response | |
# 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 get_object_path(event): | |
bucketName = event['Input']['detail']['requestParameters']['bucketName'] | |
objectKey = event['Input']['detail']['requestParameters']['key'] | |
return bucketName, objectKey | |
def get_downloaded_file_path(bucketName, key): | |
s3 = boto3.resource('s3') | |
bucket = s3.Bucket(bucketName) | |
obj = bucket.Object(key) | |
tmpFilePath = '/tmp/'+str(uuid4()) | |
with open(tmpFilePath, 'wb') as fp: | |
obj.download_fileobj(fp) | |
return tmpFilePath | |
def lambda_handler(event, context): | |
print(json.dumps(event)) | |
# text | |
text = "Amazon Comprehend is a natural language processing (NLP) service that uses machine learning to find insights and relationships in text." | |
bucketName, key = get_object_path(event) | |
local_path = get_downloaded_file_path(bucketName, key) | |
with open(local_path) as fp: | |
text = fp.read() | |
print(text) | |
# language code | |
language_code = 'en' | |
# detecting the dominant language | |
result = detect_dominant_language(text) | |
print("Starting detecting the dominant language") | |
print(json.dumps(result, sort_keys=True)) | |
print("End of detecting the dominant language\n") | |
primary_language = max(result['Languages'], key=lambda elm: elm['Score']) | |
print('Primary language detected.') | |
print(json.dumps(primary_language)) | |
primary_language['Text'] = text | |
return json.dumps(primary_language) | |
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/