DetectDominantLanguage搭配使用 AWS SDK或 CLI - Amazon Comprehend

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

DetectDominantLanguage搭配使用 AWS SDK或 CLI

以下代码示例演示如何使用 DetectDominantLanguage

操作示例是大型程序的代码摘录,必须在上下文中运行。在以下代码示例中,您可以查看此操作的上下文:

.NET
AWS SDK for .NET
注意

还有更多相关信息 GitHub。查找完整的示例,学习如何设置和运行 AWS 代码示例存储库

using System; using System.Threading.Tasks; using Amazon.Comprehend; using Amazon.Comprehend.Model; /// <summary> /// This example calls the Amazon Comprehend service to determine the /// dominant language. /// </summary> public static class DetectDominantLanguage { /// <summary> /// Calls Amazon Comprehend to determine the dominant language used in /// the sample text. /// </summary> public static async Task Main() { string text = "It is raining today in Seattle."; var comprehendClient = new AmazonComprehendClient(Amazon.RegionEndpoint.USWest2); Console.WriteLine("Calling DetectDominantLanguage\n"); var detectDominantLanguageRequest = new DetectDominantLanguageRequest() { Text = text, }; var detectDominantLanguageResponse = await comprehendClient.DetectDominantLanguageAsync(detectDominantLanguageRequest); foreach (var dl in detectDominantLanguageResponse.Languages) { Console.WriteLine($"Language Code: {dl.LanguageCode}, Score: {dl.Score}"); } Console.WriteLine("Done"); } }
CLI
AWS CLI

检测输入文本的主要语言

以下 detect-dominant-language 分析输入文本并识别主要语言。预训练模型的置信度分数也是输出。

aws comprehend detect-dominant-language \ --text "It is a beautiful day in Seattle."

输出:

{ "Languages": [ { "LanguageCode": "en", "Score": 0.9877256155014038 } ] }

有关更多信息,请参阅《Amazon Comprehend 开发人员指南》中的主要语言

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.DetectDominantLanguageRequest; import software.amazon.awssdk.services.comprehend.model.DetectDominantLanguageResponse; import software.amazon.awssdk.services.comprehend.model.DominantLanguage; 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 DetectLanguage { public static void main(String[] args) { // Specify French text - "It is raining today in Seattle". String text = "Il pleut aujourd'hui à Seattle"; Region region = Region.US_EAST_1; ComprehendClient comClient = ComprehendClient.builder() .region(region) .build(); System.out.println("Calling DetectDominantLanguage"); detectTheDominantLanguage(comClient, text); comClient.close(); } public static void detectTheDominantLanguage(ComprehendClient comClient, String text) { try { DetectDominantLanguageRequest request = DetectDominantLanguageRequest.builder() .text(text) .build(); DetectDominantLanguageResponse resp = comClient.detectDominantLanguage(request); List<DominantLanguage> allLanList = resp.languages(); for (DominantLanguage lang : allLanList) { System.out.println("Language is " + lang.languageCode()); } } catch (ComprehendException e) { System.err.println(e.awsErrorDetails().errorMessage()); System.exit(1); } } }
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_languages(self, text): """ Detects languages used in a document. :param text: The document to inspect. :return: The list of languages along with their confidence scores. """ try: response = self.comprehend_client.detect_dominant_language(Text=text) languages = response["Languages"] logger.info("Detected %s languages.", len(languages)) except ClientError: logger.exception("Couldn't detect languages.") raise else: return languages

有关完整列表 AWS SDK开发者指南和代码示例,请参阅将 Amazon Comprehend 与 SDK 配合 AWS 使用。本主题还包括有关入门的信息以及有关先前SDK版本的详细信息。