文件 AWS SDK AWS 範例 SDK 儲存庫中有更多可用的
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
DetectSyntax
搭配 a AWS SDK 或 CLI 使用
下列程式碼範例示範如何使用 DetectSyntax
。
動作範例是大型程式的程式碼摘錄,必須在內容中執行。您可以在下列程式碼範例的內容中看到此動作:
- .NET
-
- AWS SDK for .NET
-
注意
還有更多 on GitHub。尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫
中設定和執行。 using System; using System.Threading.Tasks; using Amazon.Comprehend; using Amazon.Comprehend.Model; /// <summary> /// This example shows how to use Amazon Comprehend to detect syntax /// elements by calling the DetectSyntaxAsync method. /// </summary> public class DetectingSyntax { /// <summary> /// This method calls DetectSynaxAsync to identify the syntax elements /// in the sample text. /// </summary> public static async Task Main() { string text = "It is raining today in Seattle"; var comprehendClient = new AmazonComprehendClient(); // Call DetectSyntax API Console.WriteLine("Calling DetectSyntaxAsync\n"); var detectSyntaxRequest = new DetectSyntaxRequest() { Text = text, LanguageCode = "en", }; DetectSyntaxResponse detectSyntaxResponse = await comprehendClient.DetectSyntaxAsync(detectSyntaxRequest); foreach (SyntaxToken s in detectSyntaxResponse.SyntaxTokens) { Console.WriteLine($"Text: {s.Text}, PartOfSpeech: {s.PartOfSpeech.Tag}, BeginOffset: {s.BeginOffset}, EndOffset: {s.EndOffset}"); } Console.WriteLine("Done"); } }
-
如需 API 詳細資訊,請參閱 DetectSyntax AWS SDK for .NET 參考中的 API。
-
- CLI
-
- AWS CLI
-
偵測輸入文字中的語音部分
下列
detect-syntax
範例會分析輸入文字的語法,並傳回語音的不同部分。每個預測也會輸出預先訓練模型的可信度分數。aws compreh
en
d detect-syntax \ --language-code en \ --text"It is a beautiful day in Seattle."
輸出:
{ "SyntaxTokens": [ { "TokenId": 1, "Text": "It", "BeginOffset": 0, "EndOffset": 2, "PartOfSpeech": { "Tag": "PRON", "Score": 0.9999740719795227 } }, { "TokenId": 2, "Text": "is", "BeginOffset": 3, "EndOffset": 5, "PartOfSpeech": { "Tag": "VERB", "Score": 0.999901294708252 } }, { "TokenId": 3, "Text": "a", "BeginOffset": 6, "EndOffset": 7, "PartOfSpeech": { "Tag": "DET", "Score": 0.9999938607215881 } }, { "TokenId": 4, "Text": "beautiful", "BeginOffset": 8, "EndOffset": 17, "PartOfSpeech": { "Tag": "ADJ", "Score": 0.9987351894378662 } }, { "TokenId": 5, "Text": "day", "BeginOffset": 18, "EndOffset": 21, "PartOfSpeech": { "Tag": "NOUN", "Score": 0.9999796748161316 } }, { "TokenId": 6, "Text": "in", "BeginOffset": 22, "EndOffset": 24, "PartOfSpeech": { "Tag": "ADP", "Score": 0.9998047947883606 } }, { "TokenId": 7, "Text": "Seattle", "BeginOffset": 25, "EndOffset": 32, "PartOfSpeech": { "Tag": "PROPN", "Score": 0.9940530061721802 } } ] }
如需詳細資訊,請參閱 Amazon Comprehend 開發人員指南中的語法分析。
-
如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 DetectSyntax
。
-
- Java
-
- Java 2.x 的 SDK
-
注意
還有更多 on 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.DetectSyntaxRequest; import software.amazon.awssdk.services.comprehend.model.DetectSyntaxResponse; import software.amazon.awssdk.services.comprehend.model.SyntaxToken; import java.util.List; /** * 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 DetectSyntax { 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 DetectSyntax"); detectAllSyntax(comClient, text); comClient.close(); } public static void detectAllSyntax(ComprehendClient comClient, String text) { try { DetectSyntaxRequest detectSyntaxRequest = DetectSyntaxRequest.builder() .text(text) .languageCode("en") .build(); DetectSyntaxResponse detectSyntaxResult = comClient.detectSyntax(detectSyntaxRequest); List<SyntaxToken> syntaxTokens = detectSyntaxResult.syntaxTokens(); for (SyntaxToken token : syntaxTokens) { System.out.println("Language is " + token.text()); System.out.println("Part of speech is " + token.partOfSpeech().tagAsString()); } } catch (ComprehendException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
-
如需 API 詳細資訊,請參閱 DetectSyntax AWS SDK for Java 2.x 參考中的 API。
-
- Python
-
- SDK for Python (Boto3)
-
注意
還有更多 on 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_syntax(self, text, language_code): """ Detects syntactical elements of a document. Syntax tokens are portions of text along with their use as parts of speech, such as nouns, verbs, and interjections. :param text: The document to inspect. :param language_code: The language of the document. :return: The list of syntax tokens along with their confidence scores. """ try: response = self.comprehend_client.detect_syntax( Text=text, LanguageCode=language_code ) tokens = response["SyntaxTokens"] logger.info("Detected %s syntax tokens.", len(tokens)) except ClientError: logger.exception("Couldn't detect syntax.") raise else: return tokens
-
如需 API 詳細資訊,請參閱 DetectSyntax AWS SDK for Python (Boto3) Word 參考中的 API。
-
DetectSentiment
ListDocumentClassificationJobs