在 .net C# 中运行 AWS AmazonComprehend 示例遇到问题

Amazon Comprehend 应该完全能够实现我想要的功能。不幸的是,他们的 .NET SDK 示例代码似乎无法正常工作。

以下是直接从他们的在线帮助文件中提取的代码:

using System;using Amazon.Comprehend;using Amazon.Comprehend.Model;namespace Comprehend{    class Program    {        static void Main(string[] args)        {            String text = "It is raining today in Seattle";            AmazonComprehendClient comprehendClient = new AmazonComprehendClient(Amazon.RegionEndpoint.USWest2);            // Call DetectKeyPhrases API            Console.WriteLine("Calling DetectSentiment");            DetectSentimentRequest detectSentimentRequest = new DetectSentimentRequest()            {                Text = text,                LanguageCode = "en"            };            DetectSentimentResponse detectSentimentResponse = comprehendClient.DetectSentiment(detectSentimentRequest);            Console.WriteLine(detectSentimentResponse.Sentiment);            Console.WriteLine("Done");        }    }}

在正确设置 SDK 后,我在控制台输出前的最后一行遇到了错误。

DetectSentimentResponse detectSentimentResponse = comprehendClient.DetectSentiment(detectSentimentRequest);

抛出的错误是:

错误 CS0122 ‘AmazonComprehendClient.DetectSentiment(DetectSentimentRequest)’ 由于其保护级别而无法访问

我该如何解决这个问题?


回答:

我遇到了同样的问题。如果你还没有找到答案,这可能会对你有帮助。

DetectSentiment 在 .NET Core 中无法使用。此操作仅以异步形式提供,你可以使用 DetectSentimentAsync 来达到同样的目的。以下是我尝试过的代码,运行良好。但请注意,我是在 AWS Lambda 函数中使用的,你也可以在你的函数中尝试同样的方法。

public async Task FunctionHandler(LexEvent lexEvent, ILambdaContext context) {

        String text = "It is raining today in Seattle";        AmazonComprehendClient comprehendClient = new AmazonComprehendClient(Amazon.RegionEndpoint.USEast1);        Console.WriteLine("Calling DetectSentiment");        DetectSentimentRequest detectSentimentRequest = new DetectSentimentRequest()        {            Text = text,            LanguageCode = "en"        };        DetectSentimentResponse detectSentimentResponse = await        comprehendClient.DetectSentimentAsync(detectSentimentRequest);       Console.WriteLine(detectSentimentResponse.Sentiment);        Console.WriteLine("Done");

Related Posts

多维度Top-k评分

例如,在机器学习中的自然语言处理中,通常使用波束搜索来…

R – Caret train() “错误:停止” 与 “在newdata中未找到对象中使用的所有变量名”

我正在尝试为蘑菇数据构建一个简单的朴素贝叶斯分类器。我…

创建训练和测试数据集分割,数据嵌套在多个文件夹中

我正在准备数据以训练一个图像识别模型。目前我有一个文件…

我的神经网络预测出现错误:IndexError: list index out of range

我正在进行一个简单的垃圾邮件/非垃圾邮件文本分类。我的…

python 给出数组是一维的,但索引了两个维度错误

我已经为 miniBatchGradientDesce…

TensorFlow自定义训练步骤使用不同的损失函数

背景 根据TensorFlow文档,可以使用以下方式执…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注