Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Use this article to get started with Text Summarization using the client library and REST API. Follow these steps to try out examples code for mining text:
Reference documentation | Library source code | Package (NuGet) | Additional samples
- Azure subscription - Create one for trial
- The Visual Studio IDE
- Once you have your Azure subscription, create a Language resource in the Azure portal to get your key and endpoint. After it deploys, click Go to resource.
- You will need the key and endpoint from the resource you create to connect your application to the API. You'll paste your key and endpoint into the code below later in the quickstart.
- You can use the free pricing tier (
F0
) to try the service, and upgrade later to a paid tier for production.
- To use the Analyze feature, you will need a Language resource with the standard (S) pricing tier.
Using the Visual Studio IDE, create a new .NET Core console app. This will create a "Hello World" project with a single C# source file: program.cs.
Install the client library by right-clicking on the solution in the Solution Explorer and selecting Manage NuGet Packages. In the package manager that opens select Browse and search for Azure.AI.TextAnalytics
. Make sure Include prerelease is checked. Select version 5.2.0-beta.1
, and then Install. You can also use the Package Manager Console.
Copy the following code into your program.cs file. Remember to replace the key
variable with the key for your resource, and replace the endpoint
variable with the endpoint for your resource.
Important
Go to the Azure portal. If the Language resource you created in the Prerequisites section deployed successfully, click the Go to Resource button under Next Steps. You can find your key and endpoint by navigating to your resource's Keys and Endpoint page, under Resource Management.
Important
Remember to remove the key from your code when you're done, and never post it publicly. For production, use a secure way of storing and accessing your credentials like Azure Key Vault. See the Azure AI services security article for more information.
using Azure;
using System;
using Azure.AI.TextAnalytics;
using System.Threading.Tasks;
using System.Collections.Generic;
namespace LanguageDetectionExample
{
class Program
{
private static readonly AzureKeyCredential credentials = new AzureKeyCredential("replace-with-your-key-here");
private static readonly Uri endpoint = new Uri("replace-with-your-endpoint-here");
// Example method for summarizing text
static async Task TextSummarizationExample(TextAnalyticsClient client)
{
string document = @"The extractive summarization feature uses natural language processing techniques to locate key sentences in an unstructured text document.
These sentences collectively convey the main idea of the document. This feature is provided as an API for developers.
They can use it to build intelligent solutions based on the relevant information extracted to support various use cases.
In the public preview, extractive summarization supports several languages. It is based on pretrained multilingual transformer models, part of our quest for holistic representations.
It draws its strength from transfer learning across monolingual and harness the shared nature of languages to produce models of improved quality and efficiency." ;
// Prepare analyze operation input. You can add multiple documents to this list and perform the same
// operation to all of them.
var batchInput = new List<string>
{
document
};
TextAnalyticsActions actions = new TextAnalyticsActions()
{
ExtractSummaryActions = new List<ExtractSummaryAction>() { new ExtractSummaryAction() }
};
// Start analysis process.
AnalyzeActionsOperation operation = await client.StartAnalyzeActionsAsync(batchInput, actions);
await operation.WaitForCompletionAsync();
// View operation status.
Console.WriteLine($"AnalyzeActions operation has completed");
Console.WriteLine();
Console.WriteLine($"Created On : {operation.CreatedOn}");
Console.WriteLine($"Expires On : {operation.ExpiresOn}");
Console.WriteLine($"Id : {operation.Id}");
Console.WriteLine($"Status : {operation.Status}");
Console.WriteLine();
// View operation results.
await foreach (AnalyzeActionsResult documentsInPage in operation.Value)
{
IReadOnlyCollection<ExtractSummaryActionResult> summaryResults = documentsInPage.ExtractSummaryResults;
foreach (ExtractSummaryActionResult summaryActionResults in summaryResults)
{
if (summaryActionResults.HasError)
{
Console.WriteLine($" Error!");
Console.WriteLine($" Action error code: {summaryActionResults.Error.ErrorCode}.");
Console.WriteLine($" Message: {summaryActionResults.Error.Message}");
continue;
}
foreach (ExtractSummaryResult documentResults in summaryActionResults.DocumentsResults)
{
if (documentResults.HasError)
{
Console.WriteLine($" Error!");
Console.WriteLine($" Document error code: {documentResults.Error.ErrorCode}.");
Console.WriteLine($" Message: {documentResults.Error.Message}");
continue;
}
Console.WriteLine($" Extracted the following {documentResults.Sentences.Count} sentence(s):");
Console.WriteLine();
foreach (SummarySentence sentence in documentResults.Sentences)
{
Console.WriteLine($" Sentence: {sentence.Text}");
Console.WriteLine();
}
}
}
}
}
static async Task Main(string[] args)
{
var client = new TextAnalyticsClient(endpoint, credentials);
await TextSummarizationExample(client);
}
}
}
AnalyzeActions operation has completed
Created On : 9/16/2021 8:04:27 PM +00:00
Expires On : 9/17/2021 8:04:27 PM +00:00
Id : 2e63fa58-fbaa-4be9-a700-080cff098f91
Status : succeeded
Extracted the following 3 sentence(s):
Sentence: The extractive summarization feature in uses natural language processing techniques to locate key sentences in an unstructured text document.
Sentence: This feature is provided as an API for developers.
Sentence: They can use it to build intelligent solutions based on the relevant information extracted to support various use cases.
Reference documentation | Library source code | Package | Samples
- Azure subscription - Create one for trial
- Java Development Kit (JDK) with version 8 or above
- Once you have your Azure subscription, create a Language resource in the Azure portal to get your key and endpoint. After it deploys, click Go to resource.
- You will need the key and endpoint from the resource you create to connect your application to the API. You'll paste your key and endpoint into the code below later in the quickstart.
- You can use the free pricing tier (
F0
) to try the service, and upgrade later to a paid tier for production.
- To use the Analyze feature, you will need a Language resource with the standard (S) pricing tier.
Create a Maven project in your preferred IDE or development environment. Then add the following dependency to your project's pom.xml file. You can find the implementation syntax for other build tools online.
<dependencies>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-ai-textanalytics</artifactId>
<version>5.2.0-beta.1</version>
</dependency>
</dependencies>
Create a Java file named Example.java
. Open the file and copy the below code. Remember to replace the key
variable with the key for your resource, and replace the endpoint
variable with the endpoint for your resource.
Important
Go to the Azure portal. If the Language resource you created in the Prerequisites section deployed successfully, click the Go to Resource button under Next Steps. You can find your key and endpoint by navigating to your resource's Keys and Endpoint page, under Resource Management.
Important
Remember to remove the key from your code when you're done, and never post it publicly. For production, use a secure way of storing and accessing your credentials like Azure Key Vault. See the Azure AI services security article for more information.
import com.azure.core.credential.AzureKeyCredential;
import com.azure.ai.textanalytics.models.*;
import com.azure.ai.textanalytics.TextAnalyticsClientBuilder;
import com.azure.ai.textanalytics.TextAnalyticsClient;
import java.util.ArrayList;
import java.util.List;
import com.azure.core.util.polling.SyncPoller;
import com.azure.ai.textanalytics.util.*;
public class Example {
private static String KEY = "replace-with-your-key-here";
private static String ENDPOINT = "replace-with-your-endpoint-here";
public static void main(String[] args) {
TextAnalyticsClient client = authenticateClient(KEY, ENDPOINT);
summarizationExample(client);
}
// Method to authenticate the client object with your key and endpoint
static TextAnalyticsClient authenticateClient(String key, String endpoint) {
return new TextAnalyticsClientBuilder()
.credential(new AzureKeyCredential(key))
.endpoint(endpoint)
.buildClient();
}
// Example method for summarizing text
static void summarizationExample(TextAnalyticsClient client) {
List<String> documents = new ArrayList<>();
documents.add(
"The extractive summarization feature uses natural language processing techniques "
+ "to locate key sentences in an unstructured text document. "
+ "These sentences collectively convey the main idea of the document. This feature is provided as an API for developers. "
+ "They can use it to build intelligent solutions based on the relevant information extracted to support various use cases. "
+ "In the public preview, extractive summarization supports several languages. "
+ "It is based on pretrained multilingual transformer models, part of our quest for holistic representations. "
+ "It draws its strength from transfer learning across monolingual and harness the shared nature of languages "
+ "to produce models of improved quality and efficiency.");
SyncPoller<AnalyzeActionsOperationDetail, AnalyzeActionsResultPagedIterable> syncPoller =
client.beginAnalyzeActions(documents,
new TextAnalyticsActions().setDisplayName("{tasks_display_name}")
.setExtractSummaryActions(
new ExtractSummaryAction()),
"en",
new AnalyzeActionsOptions());
syncPoller.waitForCompletion();
syncPoller.getFinalResult().forEach(actionsResult -> {
System.out.println("Extractive Summarization action results:");
for (ExtractSummaryActionResult actionResult : actionsResult.getExtractSummaryResults()) {
if (!actionResult.isError()) {
for (ExtractSummaryResult documentResult : actionResult.getDocumentsResults()) {
if (!documentResult.isError()) {
System.out.println("\tExtracted summary sentences:");
for (SummarySentence summarySentence : documentResult.getSentences()) {
System.out.printf(
"\t\t Sentence text: %s, length: %d, offset: %d, rank score: %f.%n",
summarySentence.getText(), summarySentence.getLength(),
summarySentence.getOffset(), summarySentence.getRankScore());
}
} else {
System.out.printf("\tCannot extract summary sentences. Error: %s%n",
documentResult.getError().getMessage());
}
}
} else {
System.out.printf("\tCannot execute Extractive Summarization action. Error: %s%n",
actionResult.getError().getMessage());
}
}
});
}
}
Extractive Summarization action results:
Extracted summary sentences:
Sentence text: The extractive summarization feature uses natural language processing techniques to locate key sentences in an unstructured text document., length: 156, offset: 0, rank score: 0.980000.
Sentence text: This feature is provided as an API for developers., length: 50, offset: 224, rank score: 0.990000.
Sentence text: They can use it to build intelligent solutions based on the relevant information extracted to support various use cases., length: 120, offset: 275, rank score: 1.000000.
Reference documentation | Library source code | Package (NPM) | Samples
- Azure subscription - Create one for trial
- The current version of Node.js.
- Once you have your Azure subscription, create a Language resource in the Azure portal to get your key and endpoint. After it deploys, click Go to resource.
- You will need the key and endpoint from the resource you create to connect your application to the API. You'll paste your key and endpoint into the code below later in the quickstart.
- You can use the free pricing tier (
F0
) to try the service, and upgrade later to a paid tier for production.
- To use the Analyze feature, you will need a Language resource with the standard (S) pricing tier.
In a console window (such as cmd, PowerShell, or Bash), create a new directory for your app, and navigate to it.
mkdir myapp
cd myapp
Run the npm init
command to create a node application with a package.json
file.
npm init
Install the NPM packages:
npm install --save @azure/ai-text-analytics@5.2.0-beta.1
Open the file and copy the below code. Remember to replace the key
variable with the key for your resource, and replace the endpoint
variable with the endpoint for your resource.
Important
Go to the Azure portal. If the Language resource you created in the Prerequisites section deployed successfully, click the Go to Resource button under Next Steps. You can find your key and endpoint by navigating to your resource's Keys and Endpoint page, under Resource Management.
Important
Remember to remove the key from your code when you're done, and never post it publicly. For production, use a secure way of storing and accessing your credentials like Azure Key Vault. See the Azure AI services security article for more information.
"use strict";
const { TextAnalyticsClient, AzureKeyCredential } = require("@azure/ai-text-analytics");
const key = '<paste-your-key-here>';
const endpoint = '<paste-your-endpoint-here>';
// Authenticate the client with your key and endpoint
const textAnalyticsClient = new TextAnalyticsClient(endpoint, new AzureKeyCredential(key));
// Example method for summarizing text
async function summarization_example(client) {
const documents = [`The extractive summarization feature uses natural language processing techniques to locate key sentences in an unstructured text document.
These sentences collectively convey the main idea of the document. This feature is provided as an API for developers.
They can use it to build intelligent solutions based on the relevant information extracted to support various use cases.
In the public preview, extractive summarization supports several languages. It is based on pretrained multilingual transformer models, part of our quest for holistic representations.
It draws its strength from transfer learning across monolingual and harness the shared nature of languages to produce models of improved quality and efficiency.`];
console.log("== Analyze Sample For Extract Summary ==");
const actions = {
extractSummaryActions: [{ modelVersion: "latest", orderBy: "Rank", maxSentenceCount: 5 }],
};
const poller = await client.beginAnalyzeActions(documents, actions, "en");
poller.onProgress(() => {
console.log(
`Number of actions still in progress: ${poller.getOperationState().actionsInProgressCount}`
);
});
console.log(`The analyze actions operation created on ${poller.getOperationState().createdOn}`);
console.log(
`The analyze actions operation results will expire on ${poller.getOperationState().expiresOn}`
);
const resultPages = await poller.pollUntilDone();
for await (const page of resultPages) {
const extractSummaryAction = page.extractSummaryResults[0];
if (!extractSummaryAction.error) {
for (const doc of extractSummaryAction.results) {
console.log(`- Document ${doc.id}`);
if (!doc.error) {
console.log("\tSummary:");
for (const sentence of doc.sentences) {
console.log(`\t- ${sentence.text}`);
}
} else {
console.error("\tError:", doc.error);
}
}
}
}
}
summarization_example(textAnalyticsClient).catch((err) => {
console.error("The sample encountered an error:", err);
});
== Analyze Sample For Extract Summary ==
The analyze actions operation created on Thu Sep 16 2021 13:12:31 GMT-0700 (Pacific Daylight Time)
The analyze actions operation results will expire on Fri Sep 17 2021 13:12:31 GMT-0700 (Pacific Daylight Time)
- Document 0
Summary:
- They can use it to build intelligent solutions based on the relevant information extracted to support various use cases.
- This feature is provided as an API for developers.
- The extractive summarization feature uses natural language processing techniques to locate key sentences in an unstructured
text document.
- These sentences collectively convey the main idea of the document.
- In the public preview, extractive summarization supports several languages.
Reference documentation | Library source code | Package (PiPy) | Samples
- Azure subscription - Create one for trial
- Python 3.x
- Once you have your Azure subscription, create a Language resource in the Azure portal to get your key and endpoint. After it deploys, click Go to resource.
- You will need the key and endpoint from the resource you create to connect your application to the API. You'll paste your key and endpoint into the code below later in the quickstart.
- You can use the free pricing tier (
F0
) to try the service, and upgrade later to a paid tier for production.
- To use the Analyze feature, you will need a Language resource with the standard (S) pricing tier.
After installing Python, you can install the client library with:
pip install azure-ai-textanalytics==5.2.0b1
Create a new Python file and copy the below code. Remember to replace the key
variable with the key for your resource, and replace the endpoint
variable with the endpoint for your resource.
Important
Go to the Azure portal. If the Language resource you created in the Prerequisites section deployed successfully, click the Go to Resource button under Next Steps. You can find your key and endpoint by navigating to your resource's Keys and Endpoint page, under Resource Management.
Important
Remember to remove the key from your code when you're done, and never post it publicly. For production, use a secure way of storing and accessing your credentials like Azure Key Vault. See the Azure AI services security article for more information.
key = "paste-your-key-here"
endpoint = "paste-your-endpoint-here"
from azure.ai.textanalytics import TextAnalyticsClient
from azure.core.credentials import AzureKeyCredential
# Authenticate the client using your key and endpoint
def authenticate_client():
ta_credential = AzureKeyCredential(key)
text_analytics_client = TextAnalyticsClient(
endpoint=endpoint,
credential=ta_credential)
return text_analytics_client
client = authenticate_client()
# Example method for summarizing text
def sample_extractive_summarization(client):
from azure.core.credentials import AzureKeyCredential
from azure.ai.textanalytics import (
TextAnalyticsClient,
ExtractSummaryAction
)
document = [
"The extractive summarization feature uses natural language processing techniques to locate key sentences in an unstructured text document. "
"These sentences collectively convey the main idea of the document. This feature is provided as an API for developers. "
"They can use it to build intelligent solutions based on the relevant information extracted to support various use cases. "
"In the public preview, extractive summarization supports several languages. It is based on pretrained multilingual transformer models, part of our quest for holistic representations. "
"It draws its strength from transfer learning across monolingual and harness the shared nature of languages to produce models of improved quality and efficiency. "
]
poller = client.begin_analyze_actions(
document,
actions=[
ExtractSummaryAction(max_sentence_count=4)
],
)
document_results = poller.result()
for result in document_results:
extract_summary_result = result[0] # first document, first result
if extract_summary_result.is_error:
print("...Is an error with code '{}' and message '{}'".format(
extract_summary_result.code, extract_summary_result.message
))
else:
print("Summary extracted: \n{}".format(
" ".join([sentence.text for sentence in extract_summary_result.sentences]))
)
sample_extractive_summarization(client)
Summary extracted:
The extractive summarization feature uses natural language processing techniques to locate key sentences in an unstructured text document. This feature is provided as an API for developers. They can use it to build intelligent solutions based on the relevant information extracted to support various use cases.
- The current version of cURL.
- Once you have your Azure subscription, create a Language resource in the Azure portal to get your key and endpoint. After it deploys, click Go to resource.
- You will need the key and endpoint from the resource you create to connect your application to the API. You'll paste your key and endpoint into the code below later in the quickstart.
- You can use the free pricing tier (
F0
) to try the service, and upgrade later to a paid tier for production.
Note
- The following BASH examples use the
\
line continuation character. If your console or terminal uses a different line continuation character, use that character. - You can find language specific samples on GitHub.
- Go to the Azure portal and find the key and endpoint for the Language resource you created in the prerequisites. They will be located on the resource's key and endpoint page, under resource management. Then replace the strings in the code below with your key and endpoint. To call the API, you need the following information:
parameter | Description |
---|---|
-X POST <endpoint> |
Specifies your endpoint for accessing the API. |
-H Content-Type: application/json |
The content type for sending JSON data. |
-H "Ocp-Apim-Subscription-Key:<key> |
Specifies the key for accessing the API. |
-d <documents> |
The JSON containing the documents you want to send. |
The following cURL commands are executed from a BASH shell. Edit these commands with your own resource name, resource key, and JSON values.
- Copy the command into a text editor.
- Make the following changes in the command where needed:
- Replace the value
<your-language-resource-key>
with your key. - Replace the first part of the request URL
<your-language-resource-endpoint>
with your endpoint URL.
- Replace the value
- Open a command prompt window.
- Paste the command from the text editor into the command prompt window, and then run the command.
curl -i -X POST https://your-text-analytics-endpoint-here/text/analytics/v3.2-preview.1/analyze \
-H "Content-Type: application/json" \
-H "Ocp-Apim-Subscription-Key: your-key-here" \
-d \
'
{
"analysisInput": {
"documents": [
{
"language": "en",
"id": "1",
"text": "At Microsoft, we have been on a quest to advance AI beyond existing techniques, by taking a more holistic, human-centric approach to learning and understanding. As Chief Technology Officer of Azure AI Cognitive Services, I have been working with a team of amazing scientists and engineers to turn this quest into a reality. In my role, I enjoy a unique perspective in viewing the relationship among three attributes of human cognition: monolingual text (X), audio or visual sensory signals, (Y) and multilingual (Z). At the intersection of all three, there is magic-what we call XYZ-code as illustrated in Figure 1-a joint representation to create more powerful AI that can speak, hear, see, and understand humans better. We believe XYZ-code will enable us to fulfill our long-term vision: cross-domain transfer learning, spanning modalities and languages. The goal is to have pretrained models that can jointly learn representations to support a broad range of downstream AI tasks, much in the way humans do today. Over the past five years, we have achieved human performance on benchmarks in conversational speech recognition, machine translation, conversational question answering, machine reading comprehension, and image captioning. These five breakthroughs provided us with strong signals toward our more ambitious aspiration to produce a leap in AI capabilities, achieving multisensory and multilingual learning that is closer in line with how humans learn and understand. I believe the joint XYZ-code is a foundational component of this aspiration, if grounded with external knowledge sources in the downstream AI tasks."
}
]
},
"tasks": {
"extractiveSummarizationTasks": [
{
"parameters": {
"model-version": "latest",
"sentenceCount": 3,
"sortBy": "Offset"
}
}
]
}
}
'
Get the operation-location
from the response header. The value will look similar to the following URL:
https://your-resource.cognitiveservices.azure.cn/text/analytics/v3.2-preview.1/analyze/jobs/12345678-1234-1234-1234-12345678
To get the results of the request, use the following cURL command. Be sure to replace my-job-id
with the numerical ID value you received from the previous operation-location
response header:
curl -X GET https://your-text-analytics-endpoint-here/text/analytics/v3.2-preview.1/analyze/jobs/my-job-id \
-H "Content-Type: application/json" \
-H "Ocp-Apim-Subscription-Key: your-key-here"
{
"jobId":"da3a2f68-eb90-4410-b28b-76960d010ec6",
"lastUpdateDateTime":"2021-08-24T19:15:47Z",
"createdDateTime":"2021-08-24T19:15:28Z",
"expirationDateTime":"2021-08-25T19:15:28Z",
"status":"succeeded",
"errors":[
],
"displayName":"NA",
"tasks":{
"completed":1,
"failed":0,
"inProgress":0,
"total":1,
"extractiveSummarizationTasks":[
{
"lastUpdateDateTime":"2021-08-24T19:15:48.0011189Z",
"taskName":"ExtractiveSummarization_latest",
"state":"succeeded",
"results":{
"documents":[
{
"id":"1",
"sentences":[
{
"text":"At Microsoft, we have been on a quest to advance AI beyond existing techniques, by taking a more holistic, human-centric approach to learning and understanding.",
"rankScore":1.0,
"offset":0,
"length":160
},
{
"text":"In my role, I enjoy a unique perspective in viewing the relationship among three attributes of human cognition: monolingual text (X), audio or visual sensory signals, (Y) and multilingual (Z).",
"rankScore":0.9582327572675664,
"offset":324,
"length":192
},
{
"text":"At the intersection of all three, there’s magic—what we call XYZ-code as illustrated in Figure 1—a joint representation to create more powerful AI that can speak, hear, see, and understand humans better.",
"rankScore":0.9294747193132348,
"offset":517,
"length":203
}
],
"warnings":[
]
}
],
"errors":[
],
"modelVersion":"2021-08-01"
}
}
]
}
}
If you want to clean up and remove a Cognitive Services subscription, you can delete the resource or resource group. Deleting the resource group also deletes any other resources associated with it.