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

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

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