首页 > 解决方案 > 使用 AWS 进行情​​绪分析的最佳方法是什么?

问题描述

我对文章有 300-500 条不同的评论,我想对每一篇文章进行情绪分析,然后对所有结果进行平均。

我已经使用“无服务器”( https://www.serverless.com/ ) 框架构建了一个 REST 端点,其中包含所有评论。当用户向我的终端节点发送请求时,我想使用 AWS Comprehend: DetectSentiment 服务来分析所有评论。所以我需要将所有评论发送给它并等待回复。

到目前为止,我想出了两种不同的方法:

1. 在我的“无服务器”REST 端点中使用线程。每个线程都在调用这个函数:

def sentiment_analysis(string):
    url = 'https://url_to_my_aws_sentiment_analysis_function'
    x = requests.post(url, json.dumps({'Text': string}))
    return x

这是我在 AWS 服务中的代码:

def lambda_handler(event, context):
    client_sentiment = boto3.client('comprehend')  # create a client object
    text = event['Text']
    response = client_sentiment.detect_sentiment(
        Text=text,
        LanguageCode='en'
    )
    return {
        'statusCode': 200,
        'headers': {
            'Access-Control-Allow-Headers': 'Content-Type',
            'Access-Control-Allow-Origin': '*',
            'Access-Control-Allow-Methods': 'OPTIONS,POST,GET'
        },
        'body': json.dumps('Hello from Lambda!'),
        'response' : response
    }

从我的 REST 端点,将一个 JSON 文件中的所有评论发送到 AWS。然后在我的lambda_handler函数中,我将使用boto3.client('comprehend').start_sentiment_detection_job(...)桶和所有东西来启动情绪分析的异步工作。工作完成后,我lambda_handler将发送回一个包含分析的压缩文件。(免责声明 - 我仍然有这个选项的问题)

所以我的问题是——哪一种方法更好?如果我还没有想出另一种方法,我想知道!谢谢!

标签: amazon-web-servicesserverlesssentiment-analysis

解决方案


推荐阅读