ContentSafetyClient.AnalyzeText Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Overloads
AnalyzeText(RequestContent, RequestContext) |
[Protocol Method] Analyze Text
|
AnalyzeText(String, CancellationToken) |
Analyze Text. |
AnalyzeText(AnalyzeTextOptions, CancellationToken) |
Analyze Text. |
AnalyzeText(RequestContent, RequestContext)
- Source:
- ContentSafetyClient.cs
- Source:
- ContentSafetyClient.cs
[Protocol Method] Analyze Text
- This protocol method allows explicit creation of the request and processing of the response for advanced scenarios.
- Please try the simpler AnalyzeText(AnalyzeTextOptions, CancellationToken) convenience overload with strongly typed models first.
public virtual Azure.Response AnalyzeText (Azure.Core.RequestContent content, Azure.RequestContext context = default);
abstract member AnalyzeText : Azure.Core.RequestContent * Azure.RequestContext -> Azure.Response
override this.AnalyzeText : Azure.Core.RequestContent * Azure.RequestContext -> Azure.Response
Public Overridable Function AnalyzeText (content As RequestContent, Optional context As RequestContext = Nothing) As Response
Parameters
- content
- RequestContent
The content to send as the body of the request.
- context
- RequestContext
The request context, which can override default behaviors of the client pipeline on a per-call basis.
Returns
The response returned from the service.
Exceptions
content
is null.
Service returned a non-success status code.
Examples
This sample shows how to call AnalyzeText and parse the result.
Uri endpoint = new Uri("<https://my-service.azure.com>");
AzureKeyCredential credential = new AzureKeyCredential("<key>");
ContentSafetyClient client = new ContentSafetyClient(endpoint, credential);
using RequestContent content = RequestContent.Create(new
{
text = "<text>",
});
Response response = client.AnalyzeText(content);
JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement;
Console.WriteLine(result.GetProperty("categoriesAnalysis")[0].GetProperty("category").ToString());
This sample shows how to call AnalyzeText with all request content and parse the result.
Uri endpoint = new Uri("<https://my-service.azure.com>");
AzureKeyCredential credential = new AzureKeyCredential("<key>");
ContentSafetyClient client = new ContentSafetyClient(endpoint, credential);
using RequestContent content = RequestContent.Create(new
{
text = "<text>",
categories = new object[]
{
"Hate"
},
blocklistNames = new object[]
{
"<blocklistNames>"
},
haltOnBlocklistHit = true,
outputType = "FourSeverityLevels",
});
Response response = client.AnalyzeText(content);
JsonElement result = JsonDocument.Parse(response.ContentStream).RootElement;
Console.WriteLine(result.GetProperty("blocklistsMatch")[0].GetProperty("blocklistName").ToString());
Console.WriteLine(result.GetProperty("blocklistsMatch")[0].GetProperty("blocklistItemId").ToString());
Console.WriteLine(result.GetProperty("blocklistsMatch")[0].GetProperty("blocklistItemText").ToString());
Console.WriteLine(result.GetProperty("categoriesAnalysis")[0].GetProperty("category").ToString());
Console.WriteLine(result.GetProperty("categoriesAnalysis")[0].GetProperty("severity").ToString());
Applies to
AnalyzeText(String, CancellationToken)
- Source:
- ContentSafetyClient.cs
Analyze Text.
public virtual Azure.Response<Azure.AI.ContentSafety.AnalyzeTextResult> AnalyzeText (string text, System.Threading.CancellationToken cancellationToken = default);
abstract member AnalyzeText : string * System.Threading.CancellationToken -> Azure.Response<Azure.AI.ContentSafety.AnalyzeTextResult>
override this.AnalyzeText : string * System.Threading.CancellationToken -> Azure.Response<Azure.AI.ContentSafety.AnalyzeTextResult>
Public Overridable Function AnalyzeText (text As String, Optional cancellationToken As CancellationToken = Nothing) As Response(Of AnalyzeTextResult)
Parameters
- text
- String
- cancellationToken
- CancellationToken
Returns
Applies to
AnalyzeText(AnalyzeTextOptions, CancellationToken)
- Source:
- ContentSafetyClient.cs
- Source:
- ContentSafetyClient.cs
Analyze Text.
public virtual Azure.Response<Azure.AI.ContentSafety.AnalyzeTextResult> AnalyzeText (Azure.AI.ContentSafety.AnalyzeTextOptions options, System.Threading.CancellationToken cancellationToken = default);
public virtual Azure.Response<Azure.AI.ContentSafety.AnalyzeTextResult> AnalyzeText (Azure.AI.ContentSafety.AnalyzeTextOptions body, System.Threading.CancellationToken cancellationToken = default);
abstract member AnalyzeText : Azure.AI.ContentSafety.AnalyzeTextOptions * System.Threading.CancellationToken -> Azure.Response<Azure.AI.ContentSafety.AnalyzeTextResult>
override this.AnalyzeText : Azure.AI.ContentSafety.AnalyzeTextOptions * System.Threading.CancellationToken -> Azure.Response<Azure.AI.ContentSafety.AnalyzeTextResult>
abstract member AnalyzeText : Azure.AI.ContentSafety.AnalyzeTextOptions * System.Threading.CancellationToken -> Azure.Response<Azure.AI.ContentSafety.AnalyzeTextResult>
override this.AnalyzeText : Azure.AI.ContentSafety.AnalyzeTextOptions * System.Threading.CancellationToken -> Azure.Response<Azure.AI.ContentSafety.AnalyzeTextResult>
Public Overridable Function AnalyzeText (options As AnalyzeTextOptions, Optional cancellationToken As CancellationToken = Nothing) As Response(Of AnalyzeTextResult)
Public Overridable Function AnalyzeText (body As AnalyzeTextOptions, Optional cancellationToken As CancellationToken = Nothing) As Response(Of AnalyzeTextResult)
Parameters
- optionsbody
- AnalyzeTextOptions
The text analysis request.
- cancellationToken
- CancellationToken
The cancellation token to use.
Returns
Exceptions
options
is null.
Examples
This sample shows how to call AnalyzeText.
Uri endpoint = new Uri("<https://my-service.azure.com>");
AzureKeyCredential credential = new AzureKeyCredential("<key>");
ContentSafetyClient client = new ContentSafetyClient(endpoint, credential);
AnalyzeTextOptions options = new AnalyzeTextOptions("<text>");
Response<AnalyzeTextResult> response = client.AnalyzeText(options);
This sample shows how to call AnalyzeText with all parameters.
Uri endpoint = new Uri("<https://my-service.azure.com>");
AzureKeyCredential credential = new AzureKeyCredential("<key>");
ContentSafetyClient client = new ContentSafetyClient(endpoint, credential);
AnalyzeTextOptions options = new AnalyzeTextOptions("<text>")
{
Categories = { TextCategory.Hate },
BlocklistNames = { "<blocklistNames>" },
HaltOnBlocklistHit = true,
OutputType = AnalyzeTextOutputType.FourSeverityLevels,
};
Response<AnalyzeTextResult> response = client.AnalyzeText(options);
Remarks
A synchronous API for the analysis of potentially harmful text content. Currently, it supports four categories: Hate, SelfHarm, Sexual, and Violence.
Applies to
Azure SDK for .NET