在.net C#中获取AWS AmazonComprehend示例时遇到问题

vuktfyat  于 2022-12-24  发布在  .NET
关注(0)|答案(2)|浏览(151)

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 '亚马逊客户端. DetectSentiment(DetectSentimentRequest)'由于其保护级别无法访问
我该怎么补救呢?

o8x7eapl

o8x7eapl1#

我也遇到了同样的问题。如果你还没有得到答案,那么这个可能会有帮助。
DetectSentiment不适用于.NET Core。此操作只能以异步形式使用,您可以将DetectSentimentAsync用于相同目的。以下是我尝试的代码,它工作正常。但请注意,我在aws lambda函数中使用了它,您可以在您的函数中尝试相同的操作。
公共异步任务函数处理程序(LexEvent lexEvent,ILambdaContext上下文){

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");
hk8txs48

hk8txs482#

它现在可以在.NET Core 6.0 AWS Text Sentiment中运行

using System;
using System.Threading.Tasks;
using Amazon.Comprehend;
using Amazon.Comprehend.Model;

/// <summary>
/// This example shows how to detect the overall sentiment of the supplied
/// text using the Amazon Comprehend service. The example was writing using
/// the AWS SDK for .NET version 3.7 and .NET Core 5.0.
/// </summary>
public static class DetectSentiment
{
    /// <summary>
    /// This method calls the DetetectSentimentAsync method to analyze the
    /// supplied text and determine the overal sentiment.
    /// </summary>
    public static async Task Main()
    {
        string text = "It is raining today in Seattle";

        var comprehendClient = new AmazonComprehendClient(Amazon.RegionEndpoint.USWest2);

        // Call DetectKeyPhrases API
        Console.WriteLine("Calling DetectSentiment");
        var detectSentimentRequest = new DetectSentimentRequest()
        {
            Text = text,
            LanguageCode = "en",
        };
        var detectSentimentResponse = await comprehendClient.DetectSentimentAsync(detectSentimentRequest);
        Console.WriteLine($"Sentiment: {detectSentimentResponse.Sentiment}");
        Console.WriteLine("Done");
    }
}

相关问题