在 .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

神经网络反向传播代码不工作

我需要编写一个简单的由1个输出节点、1个包含3个节点的…

值错误:y 包含先前未见过的标签:

我使用了 决策树分类器,我想将我的 输入 作为 字符串…

使用不平衡数据集进行特征选择时遇到的问题

我正在使用不平衡数据集(54:38:7%)进行特征选择…

广义随机森林/因果森林在Python上的应用

我在寻找Python上的广义随机森林/因果森林算法,但…

如何用PyTorch仅用标量损失来训练神经网络?

假设我们有一个神经网络,我们希望它能根据输入预测三个值…

什么是RNN中间隐藏状态的良好用途?

我已经以三种不同的方式使用了RNN/LSTM: 多对多…

发表回复

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