本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
DetectSentiment
搭配使用 AWS SDK或 CLI
以下代码示例演示如何使用 DetectSentiment
。
操作示例是大型程序的代码摘录,必须在上下文中运行。在以下代码示例中,您可以查看此操作的上下文:
- .NET
-
- AWS SDK for .NET
-
注意
还有更多相关信息 GitHub。查找完整的示例,学习如何设置和运行 AWS 代码示例存储库
。 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. /// </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"); } }
-
有关API详细信息,请参阅DetectSentiment中的 AWS SDK for .NET API参考。
-
- CLI
-
- AWS CLI
-
检测输入文本的情绪
以下
detect-sentiment
示例分析输入文本,并返回占主导地位的情绪(POSITIVE
、NEUTRAL
、MIXED
或NEGATIVE
)的推断。aws compreh
en
d detect-sentiment \ --language-code en \ --text"It is a beautiful day in Seattle"
输出:
{ "Sentiment": "POSITIVE", "SentimentScore": { "Positive": 0.9976957440376282, "Negative": 9.653854067437351e-05, "Neutral": 0.002169104292988777, "Mixed": 3.857641786453314e-05 } }
有关更多信息,请参阅《Amazon Comprehend 开发人员指南》中的情绪。
-
有关API详细信息,请参阅DetectSentiment
中的 AWS CLI 命令参考。
-
- Java
-
- SDK适用于 Java 2.x
-
注意
还有更多相关信息 GitHub。查找完整的示例,学习如何设置和运行 AWS 代码示例存储库
。 import software.amazon.awssdk.regions.Region; import software.amazon.awssdk.services.comprehend.ComprehendClient; import software.amazon.awssdk.services.comprehend.model.ComprehendException; import software.amazon.awssdk.services.comprehend.model.DetectSentimentRequest; import software.amazon.awssdk.services.comprehend.model.DetectSentimentResponse; /** * Before running this Java V2 code example, set up your development * environment, including your credentials. * * For more information, see the following documentation topic: * * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html */ public class DetectSentiment { public static void main(String[] args) { String text = "Amazon.com, Inc. is located in Seattle, WA and was founded July 5th, 1994 by Jeff Bezos, allowing customers to buy everything from books to blenders. Seattle is north of Portland and south of Vancouver, BC. Other notable Seattle - based companies are Starbucks and Boeing."; Region region = Region.US_EAST_1; ComprehendClient comClient = ComprehendClient.builder() .region(region) .build(); System.out.println("Calling DetectSentiment"); detectSentiments(comClient, text); comClient.close(); } public static void detectSentiments(ComprehendClient comClient, String text) { try { DetectSentimentRequest detectSentimentRequest = DetectSentimentRequest.builder() .text(text) .languageCode("en") .build(); DetectSentimentResponse detectSentimentResult = comClient.detectSentiment(detectSentimentRequest); System.out.println("The Neutral value is " + detectSentimentResult.sentimentScore().neutral()); } catch (ComprehendException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
-
有关API详细信息,请参阅DetectSentiment中的 AWS SDK for Java 2.x API参考。
-
- Python
-
- SDK适用于 Python (Boto3)
-
注意
还有更多相关信息 GitHub。查找完整的示例,学习如何设置和运行 AWS 代码示例存储库
。 class ComprehendDetect: """Encapsulates Comprehend detection functions.""" def __init__(self, comprehend_client): """ :param comprehend_client: A Boto3 Comprehend client. """ self.comprehend_client = comprehend_client def detect_sentiment(self, text, language_code): """ Detects the overall sentiment expressed in a document. Sentiment can be positive, negative, neutral, or a mixture. :param text: The document to inspect. :param language_code: The language of the document. :return: The sentiments along with their confidence scores. """ try: response = self.comprehend_client.detect_sentiment( Text=text, LanguageCode=language_code ) logger.info("Detected primary sentiment %s.", response["Sentiment"]) except ClientError: logger.exception("Couldn't detect sentiment.") raise else: return response
-
有关API详细信息,请参阅DetectSentiment中的 AWS SDK供参考 Python (Boto3) API。
-
有关完整列表 AWS SDK开发者指南和代码示例,请参阅将 Amazon Comprehend 与 SDK 配合 AWS 使用。本主题还包括有关入门的信息以及有关先前SDK版本的详细信息。
DetectPiiEntities
DetectSyntax