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

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

DetectPiiEntities搭配使用 AWS SDK或 CLI

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

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

.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 use the Amazon Comprehend service to find /// personally identifiable information (PII) within text submitted to the /// DetectPiiEntitiesAsync method. /// </summary> public class DetectingPII { /// <summary> /// This method calls the DetectPiiEntitiesAsync method to locate any /// personally dientifiable information within the supplied text. /// </summary> public static async Task Main() { var comprehendClient = new AmazonComprehendClient(); var text = @"Hello Paul Santos. The latest statement for your credit card account 1111-0000-1111-0000 was mailed to 123 Any Street, Seattle, WA 98109."; var request = new DetectPiiEntitiesRequest { Text = text, LanguageCode = "EN", }; var response = await comprehendClient.DetectPiiEntitiesAsync(request); if (response.Entities.Count > 0) { foreach (var entity in response.Entities) { var entityValue = text.Substring(entity.BeginOffset, entity.EndOffset - entity.BeginOffset); Console.WriteLine($"{entity.Type}: {entityValue}"); } } } }
  • 有关API详细信息,请参阅DetectPiiEntities中的 AWS SDK for .NET API参考

CLI
AWS CLI

检测输入文本中的 PII 实体

以下detect-pii-entities示例分析输入文本并识别包含个人身份信息的实体 (PII)。预训练模型的置信度分数也是每个预测的输出。

aws comprehend detect-pii-entities \ --language-code en \ --text "Hello Zhang Wei, I am John. Your AnyCompany Financial Services, LLC credit card \ account 1111-XXXX-1111-XXXX has a minimum payment of $24.53 that is due by July 31st. Based on your autopay settings, \ we will withdraw your payment on the due date from your bank account number XXXXXX1111 with the routing number XXXXX0000. \ Customer feedback for Sunshine Spa, 123 Main St, Anywhere. Send comments to Alice at AnySpa@example.com."

输出:

{ "Entities": [ { "Score": 0.9998322129249573, "Type": "NAME", "BeginOffset": 6, "EndOffset": 15 }, { "Score": 0.9998878240585327, "Type": "NAME", "BeginOffset": 22, "EndOffset": 26 }, { "Score": 0.9994089603424072, "Type": "CREDIT_DEBIT_NUMBER", "BeginOffset": 88, "EndOffset": 107 }, { "Score": 0.9999760985374451, "Type": "DATE_TIME", "BeginOffset": 152, "EndOffset": 161 }, { "Score": 0.9999449253082275, "Type": "BANK_ACCOUNT_NUMBER", "BeginOffset": 271, "EndOffset": 281 }, { "Score": 0.9999847412109375, "Type": "BANK_ROUTING", "BeginOffset": 306, "EndOffset": 315 }, { "Score": 0.999925434589386, "Type": "ADDRESS", "BeginOffset": 354, "EndOffset": 365 }, { "Score": 0.9989161491394043, "Type": "NAME", "BeginOffset": 394, "EndOffset": 399 }, { "Score": 0.9994171857833862, "Type": "EMAIL", "BeginOffset": 403, "EndOffset": 418 } ] }

有关更多信息,请参阅 Amazon Comprehend 开发者指南中的个人身份信息 (PII)

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_pii(self, text, language_code): """ Detects personally identifiable information (PII) in a document. PII can be things like names, account numbers, or addresses. :param text: The document to inspect. :param language_code: The language of the document. :return: The list of PII entities along with their confidence scores. """ try: response = self.comprehend_client.detect_pii_entities( Text=text, LanguageCode=language_code ) entities = response["Entities"] logger.info("Detected %s PII entities.", len(entities)) except ClientError: logger.exception("Couldn't detect PII entities.") raise else: return entities
  • 有关API详细信息,请参阅DetectPiiEntities中的 AWS SDK供参考 Python (Boto3) API。

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