[AI] Azure PII進行資料遮罩(舊寫法,仍可用)

  • 13
  • 0
  • AI
  • 2025-05-02

參考文件: 快速入門:偵測文字中的個人識別資訊 (PII) - Azure AI services | Microsoft Learn

要先建好Azure Language Services或 Azure AI Service (包含很多azure服務)

以下只是.net寫法, 此服務能直接打REST API

string file1 = @"D:\Downloads\1.txt";
string endpoint = "https://xxxxxx.cognitiveservices.azure.com/";
string apiKey = "xxxxxxxxxxxxxxxxxxxxxxxx";
string responseBody = File.ReadAllText(file1);
using (HttpClient client = new HttpClient())
{                    
    client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", apiKey);
    var documents = new
    {
        documents = new[] { new { id = "1", text = responseBody } }
    };

    string json = JsonConvert.SerializeObject(documents);
    using (StringContent content = new StringContent(json, Encoding.UTF8, "application/json"))
    {
        HttpResponseMessage response = await client.PostAsync(endpoint + "/text/analytics/v3.1/entities/recognition/pii", content);
        string result = await response.Content.ReadAsStringAsync();
        Console.WriteLine(result);
    }
}

Taiwan is a country. 臺灣是我的國家