简体   繁体   中英

Trouble getting AWS AmazonComprehend example working in .net C#

Amazon Comprehend should pretty much do exactly what I am trying to accomplish. Unfotunately their .NET SDK example code doesn't seem to work.

Here is the code directly from their online help files:

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

After setting up the SDK correctly, The error I have is on the last line before the console output.

DetectSentimentResponse detectSentimentResponse = comprehendClient.DetectSentiment(detectSentimentRequest);

The error it throws is:

Error CS0122 'AmazonComprehendClient.DetectSentiment(DetectSentimentRequest)' is inaccessible due to its protection level

How can I fix this?

I was running into the same issue. If you haven't got your answer yet, then this might help.

DetectSentiment doesn't work for .NET Core. This operation is only available in asynchronous form and you could use DetectSentimentAsync for the same purpose. Below is the code that I tried and it works fine. But note that I have used it within a aws lambda function you could try out the same in your function.

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

It now works in .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");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM