DetectPiiEntities 搭配 a AWS SDK 或 CLI 使用 - AWS SDK 程式碼範例

文件 AWS SDK AWS 範例 SDK 儲存庫中有更多可用的 GitHub 範例。

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

DetectPiiEntities 搭配 a AWS SDK 或 CLI 使用

下列程式碼範例示範如何使用 DetectPiiEntities

動作範例是大型程式的程式碼摘錄,必須在內容中執行。您可以在下列程式碼範例的內容中看到此動作:

.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 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)

  • 如需 API 詳細資訊,請參閱 AWS CLI 命令參考中的 DetectPiiEntities

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_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 for Python (Boto3) Word 參考中的 API