Skip to content

Instantly share code, notes, and snippets.

@hatsunea
Last active June 4, 2022 15:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hatsunea/19936369e5de9eae0dc7e075af57f66c to your computer and use it in GitHub Desktop.
Save hatsunea/19936369e5de9eae0dc7e075af57f66c to your computer and use it in GitHub Desktop.
TextSummarization SDK Exsample
using Azure;
using Azure.AI.TextAnalytics;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace TextSummarizationSDKSample.Models
{
internal class TextSummarizationModel
{
private static readonly AzureKeyCredential credentials = new AzureKeyCredential("<<your keys>>");
private static readonly string Resouce = "<<your resourcename>>";
// Example method for summarizing text
internal async Task<List<string>> TextSummarization(string document)
{
var result = new List<String>();
var documents = new List<string> { document };
var actions = new TextAnalyticsActions()
{
ExtractSummaryActions = new List<ExtractSummaryAction>()
{
new ExtractSummaryAction()
{
MaxSentenceCount=3,
OrderBy=SummarySentencesOrder.Rank
}
}
};
var client = new TextAnalyticsClient(new Uri($"https://{Resouce}.cognitiveservices.azure.com/"), credentials);
var operation = await client.StartAnalyzeActionsAsync(documents, actions);
await operation.WaitForCompletionAsync();
#if DEBUG
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}");
#endif
await foreach (var documentsInPage in operation.Value)
{
var summaryResults = documentsInPage.ExtractSummaryResults;
foreach (var summaryResult in summaryResults)
{
if (summaryResult.HasError)
{
throw new InvalidOperationException(summaryResult.Error.Message);
}
foreach (var documentsResult in summaryResult.DocumentsResults)
{
if (documentsResult.HasError)
{
throw new Exception(documentsResult.Error.Message);
}
foreach (var sentence in documentsResult.Sentences)
{
result.Add(sentence.Text);
}
}
}
}
return result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment