Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hatsunea/1ed3aadea0a55f302bb31c5e438bf0fe to your computer and use it in GitHub Desktop.
Save hatsunea/1ed3aadea0a55f302bb31c5e438bf0fe to your computer and use it in GitHub Desktop.
private static readonly string AccessKey = "<<your Key>>";
private static readonly string Resouce = "<<your resourcename>>";
private HttpClient SendClient = null;
// Example method for summarizing text
internal async Task<List<string>> TextSummarization(string document)
{
var result = new List<String>();
var body = new TRequest.TRootobject()
{
analysisInput = new TRequest.TAnalysisinput()
{
documents = new TRequest.TDocument[]
{
new TRequest.TDocument()
{
id = "1",
text = document,
}
}
},
tasks = new TRequest.TTasks()
{
extractiveSummarizationTasks = new TRequest.TExtractivesummarizationtask[]
{
new TRequest.TExtractivesummarizationtask()
{
parameters = new TRequest.TParameters
{
sentenceCount = 3,
sortBy = "rank",
}
}
}
}
};
try
{
if (this.SendClient == null)
{
this.SendClient = new HttpClient();
}
if (this.SendClient != null)
{
var requestBody = "";
var jobLocation = "";
using (var ms = new MemoryStream())
{
var serializer = new DataContractJsonSerializer(typeof(TRequest.TRootobject));
using (var sr = new StreamReader(ms))
{
serializer.WriteObject(ms, body);
ms.Position = 0;
requestBody = sr.ReadToEnd();
}
}
using (var request = new HttpRequestMessage())
{
request.Method = HttpMethod.Post;
request.RequestUri = new Uri($"https://{Resouce}.cognitiveservices.azure.com/text/analytics/v3.2-preview.2/analyze");
request.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
request.Headers.Add("Ocp-Apim-Subscription-Key", AccessKey);
using (var response = await this.SendClient.SendAsync(request))
{
response.EnsureSuccessStatusCode();
// 返却用ロケーションを取得する
jobLocation = response.Headers.GetValues("operation-location").ToList()[0];
}
}
if (!string.IsNullOrEmpty(jobLocation))
{
while (true)
{
using (var request = new HttpRequestMessage())
{
request.Method = HttpMethod.Get;
request.RequestUri = new Uri(jobLocation);
request.Headers.Add("Ocp-Apim-Subscription-Key", AccessKey);
using (var response = await this.SendClient.SendAsync(request))
{
response.EnsureSuccessStatusCode();
var jsonString = await response.Content.ReadAsStringAsync();
using (var json = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(jsonString)))
{
var ser = new System.Runtime.Serialization.Json.DataContractJsonSerializer(typeof(TResult.TRootobject));
{
var summaryResults = ser.ReadObject(json) as TResult.TRootobject;
if (summaryResults != null && summaryResults.tasks.completed == 1)
{
foreach (var item in summaryResults.tasks.extractiveSummarizationTasks[0].results.documents[0].sentences)
{
result.Add(item.text);
}
json.Close();
break;
}
}
}
}
await Task.Delay(1000);
}
}
}
}
}
catch (Exception ex)
{
this.SendClient = null;
throw;
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment