简体   繁体   English

如何使用 AWS Lambda 通过 S3 触发 Comprehend?

[英]How do I use AWS Lambda to trigger Comprehend with S3?

I'm currently using aws lambda to trigger an amazon comprehend job, but the code is only used to run one piece of text under sentiment analysis.我目前正在使用 aws lambda 来触发亚马逊理解作业,但代码仅用于在情感分析下运行一段文本。

import boto3
def lambda_handler(event, context):
    s3 = boto3.client("s3")
    bucket = "bucketName"
    key = "textName.txt"
    file = s3.get_object(Bucket = bucket, Key = key)
    
    analysisdata = str(file['Body'].read())

    comprehend = boto3.client("comprehend")

    sentiment = comprehend.detect_sentiment(Text = analysisdata, LanguageCode = "en")
    print(sentiment)
    
    return 'Sentiment detected'

I want to run a file where each line in the text file is a new piece of text to analyze with sentiment analysis (it's an option if you manually enter stuff into comprehend), but is there a way to alter this code to do that?我想运行一个文件,其中文本文件中的每一行都是一段新文本,以便通过情感分析进行分析(如果您手动输入内容,这是一个选项),但是有没有办法改变这个代码来做到这一点? And have the output sentiment analysis file be placed into that same S3 bucket?是否将 output 情绪分析文件放入同一个 S3 存储桶中? Thank you in advance.先感谢您。

It looks like you can use start_sentiment_detection_job() :看起来您可以使用start_sentiment_detection_job()

response = client.start_sentiment_detection_job(
    InputDataConfig={
        'S3Uri': 'string',
        'InputFormat': 'ONE_DOC_PER_FILE'|'ONE_DOC_PER_LINE',
        'DocumentReaderConfig': {
            'DocumentReadAction': 'TEXTRACT_DETECT_DOCUMENT_TEXT'|'TEXTRACT_ANALYZE_DOCUMENT',
            'DocumentReadMode': 'SERVICE_DEFAULT'|'FORCE_DOCUMENT_READ_ACTION',
            'FeatureTypes': [
                'TABLES'|'FORMS',
            ]
        }
    },
    OutputDataConfig={
        'S3Uri': 'string',
        'KmsKeyId': 'string'
    },
    ...
)

It can read from an object in Amazon S3 ( S3Uri ) and store the output in an S3 object.它可以读取 Amazon S3 ( S3Uri ) 中的 object 并将 output 存储在 S3 object 中。

It looks like you could use 'InputFormat': 'ONE_DOC_PER_LINE' to meet your requirements.看起来您可以使用'InputFormat': 'ONE_DOC_PER_LINE'来满足您的要求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM