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