Skip to content
Permalink
Fetching contributors…
Cannot retrieve contributors at this time
135 lines (109 sloc) 5.43 KB
using System;
using Microsoft.Azure.CognitiveServices.Language.TextAnalytics;
using Microsoft.Azure.CognitiveServices.Language.TextAnalytics.Models;
using System.Collections.Generic;
using Microsoft.Rest;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
namespace CognitiveServicesTextAnalytics
{
class Program
{
/// <summary>
/// This sample is the code presented in this tutorial by CodeStories.gr
/// For more tutorials and news find me:
/// Blog: http://www.codestories.gr
/// Facebook: https://www.facebook.com/codestoriesgr/
/// Twitter: https://twitter.com/GeorgiaKalyva
/// LinkedIn: https://www.linkedin.com/in/georgiakalyva
/// </summary>
private const string SubscriptionKey = ""; //Insert your Text Anaytics subscription key
class ApiKeyServiceClientCredentials : ServiceClientCredentials
{
public override Task ProcessHttpRequestAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
request.Headers.Add("Ocp-Apim-Subscription-Key", SubscriptionKey);
return base.ProcessHttpRequestAsync(request, cancellationToken);
}
}
static void Main(string[] args)
{
// Create a client.
ITextAnalyticsClient client = new TextAnalyticsClient(new ApiKeyServiceClientCredentials())
{
Endpoint = "https://westeurope.api.cognitive.microsoft.com/"
}; //Replace endpoint with the correct region for your Text Analytics subscription
Console.OutputEncoding = System.Text.Encoding.UTF8;
// Extracting language
Console.WriteLine("===== Get Language ======");
var result = client.DetectLanguageAsync(new BatchInput(
new List<Input>()
{
new Input("1", "This is a document written in English."),
new Input("2", "Este es un document escrito en Español."),
new Input("3", "这是一个用中文写的文件")
})).Result;
// Printing language results.
foreach (var document in result.Documents)
{
Console.WriteLine($"Document ID: {document.Id} , Language: {document.DetectedLanguages[0].Name}");
}
// Getting key-phrases
Console.WriteLine("\n\n===== Extract Key Phrases======");
KeyPhraseBatchResult result2 = client.KeyPhrasesAsync(new MultiLanguageBatchInput(
new List<MultiLanguageInput>()
{
new MultiLanguageInput("ja", "1", "猫は幸せ"),
new MultiLanguageInput("de", "2", "Fahrt nach Stuttgart und dann zum Hotel zu Fu."),
new MultiLanguageInput("en", "3", "My cat is stiff as a rock."),
new MultiLanguageInput("es", "4", "A mi me encanta el fútbol!")
})).Result;
// Printing keyphrases
foreach (var document in result2.Documents)
{
Console.WriteLine($"Document ID: {document.Id} ");
Console.WriteLine("\t Key phrases:");
foreach (string keyphrase in document.KeyPhrases)
{
Console.WriteLine($"\t\t{keyphrase}");
}
}
// Extracting sentiment
Console.WriteLine("\n\n===== Sentiment ======");
SentimentBatchResult result3 = client.SentimentAsync(
new MultiLanguageBatchInput(
new List<MultiLanguageInput>()
{
new MultiLanguageInput("en", "0", "I had the best day of my life."),
new MultiLanguageInput("en", "1", "This was a waste of my time. The speaker put me to sleep."),
new MultiLanguageInput("es", "2", "No tengo dinero ni nada que dar..."),
new MultiLanguageInput("it", "3", "L'hotel veneziano era meraviglioso. È un bellissimo pezzo di architettura."),
})).Result;
// Printing sentiment results
foreach (var document in result3.Documents)
{
Console.WriteLine($"Document ID: {document.Id} , Sentiment Score: {document.Score:0.00}");
}
// Identify entities
Console.WriteLine("\n\n===== ENTITIES ======");
EntitiesBatchResultV2dot1 result4 = client.EntitiesAsync(
new MultiLanguageBatchInput(
new List<MultiLanguageInput>()
{
new MultiLanguageInput("en", "0", "The Great Depression began in 1929. By 1933, the GDP in America fell by 25%.")
})).Result;
// Printing entities results
foreach (var document in result4.Documents)
{
Console.WriteLine($"Document ID: {document.Id} ");
Console.WriteLine("\t Entities:");
foreach (EntityRecordV2dot1 entity in document.Entities)
{
Console.WriteLine($"\t\t{entity.Name}\t\t{entity.WikipediaUrl}\t\t{entity.Type}\t\t{entity.SubType}");
}
}
Console.ReadLine();
}
}
}
You can’t perform that action at this time.