public void GetKeyPhrases() { var text = "how is the weather? how is the food? how are the people?"; var doc = new KeyPhraseDocument() { Id = "TEST001", Text = text, Language = "en" }; var request = new KeyPhraseRequest(); request.Documents.Add(doc); var client = new KeyPhraseClient(this.apiKey); var response = client.GetKeyPhrases(request); string expected = ""; string actual = ""; expected = "weather"; actual = response.Documents[0].KeyPhrases[0]; Assert.AreEqual(expected, actual); expected = "food"; actual = response.Documents[0].KeyPhrases[1]; Assert.AreEqual(expected, actual); expected = "people"; actual = response.Documents[0].KeyPhrases[2]; Assert.AreEqual(expected, actual); }
private static void GetKeyPhrases() { var client = new KeyPhraseClient(apiKey); var request = new KeyPhraseRequest(); var document = new KeyPhraseDocument() { Id = "YOUR-UNIQUE-ID", Text = "YOUR-TEXT", Language = "en" }; request.Documents.Add(document); var response = client.GetKeyPhrases(request); foreach (var doc in response.Documents) { Console.WriteLine("Document Id: {0}", doc.Id); foreach (var keyPhrase in doc.KeyPhrases) { Console.WriteLine(" Key Phrase: {0}", keyPhrase); } Console.WriteLine(); } }
static void Main(string[] args) { var apiKey = "YOUR-TEXT-ANALYTICS-API-SUBSCRIPTION-KEY"; var document = new KeyPhraseDocument() { Id = "YOUR-UNIQUE-ID", Text = "YOUR-TEXT", Language = "en" }; var request = new KeyPhraseRequest(); request.Documents.Add(document); var client = new KeyPhraseClient(apiKey); var response = client.GetKeyPhrases(request); foreach (var doc in response.Documents) { Console.WriteLine("Document Id: {0}", doc.Id); foreach (var keyPhrase in doc.KeyPhrases) { Console.WriteLine(" Key Phrase: {0}", keyPhrase); } Console.WriteLine(); } Console.WriteLine("Press any key to exit..."); Console.ReadLine(); }
public static IList <string> AnalyzeKeyPhrases(string id, string text, string language) { IList <string> keyPhrases = new List <string>(); var document = new KeyPhraseDocument() { Id = id, Text = text, Language = language }; var client = new KeyPhraseClient(Constants.ApiKey) { Url = "https://westeurope.api.cognitive.microsoft.com/text/analytics/v2.0/keyPhrases" }; var request = new KeyPhraseRequest(); request.Documents.Add(document); try { var response = client.GetKeyPhrases(request); var doc = response.Documents.First(); foreach (var keyPhrase in doc.KeyPhrases) { keyPhrases.Add(keyPhrase); } } catch (Exception ex) { var message = ""; var innerMessage = ""; if (!String.IsNullOrEmpty(ex.Message)) { message = ex.Message; } try { if ((ex.InnerException != null) && (!String.IsNullOrEmpty(ex.InnerException.Message))) { innerMessage = ex.InnerException.Message; } } catch (Exception innerEx) { if ((innerEx.InnerException != null) && (!String.IsNullOrEmpty(innerEx.InnerException.Message))) { innerMessage = innerEx.InnerException.Message; } } Console.WriteLine(String.Format("Error in AnalyzeSentiment: {0}:{1}", message, innerMessage)); } return(keyPhrases); }
static void Main(string[] args) { // MS Cognitive Services API key var apiKeyTextAnalytics = "REPLACE_WITH_API_KEY"; var text = @"A thing of beauty is a joy for ever: Its loveliness increases; it will never Pass into nothingness; but still will keep A bower quiet for us, and a sleep Full of sweet dreams, and health, and quiet breathing."; // GET text sentiment var sentimentDocument = new SentimentDocument() { Id = "Sentiment", Text = text, Language = "en" }; Console.WriteLine("Analyzing {0}", sentimentDocument.Id + ".."); var sentimentRequest = new SentimentRequest(); sentimentRequest.Documents.Add(sentimentDocument); var sentimentClient = new SentimentClient(apiKeyTextAnalytics); var sentimentResponse = sentimentClient.GetSentiment(sentimentRequest); Console.WriteLine(" Score: {0}%", (sentimentResponse.Documents[0].Score * 100) + " [0% - negative, 100% - positive]"); // GET text keyphrases var keyPhraseDocument = new KeyPhraseDocument() { Id = "KeyPhrases", Text = text, Language = "en" }; Console.WriteLine("Recognizing {0}", keyPhraseDocument.Id + ".."); var keyPhraseRequest = new KeyPhraseRequest(); keyPhraseRequest.Documents.Add(keyPhraseDocument); var keyPhraseClient = new KeyPhraseClient(apiKeyTextAnalytics); var keyPhraseResponse = keyPhraseClient.GetKeyPhrases(keyPhraseRequest); foreach (var keyPhrase in keyPhraseResponse.Documents[0].KeyPhrases) { Console.WriteLine(" KeyPhrase: {0}", keyPhrase); } }
public void Analyse(string gitPath) { var repo = OpenRepository(gitPath); if (repo == null) { return; } System.Console.WriteLine("OK"); //Must have min of 100 docs for Topic API var commits = repo.Commits.Where(c => !c.Message.StartsWith("Merge")).Take(100); if (commits.Count() == 0) { System.Console.WriteLine("-- No data --"); } var request = new KeyPhraseRequest(); foreach (var commit in commits) { var document = new KeyPhraseDocument() { Id = commit.Id.Sha, Text = commit.Message, Language = "en" }; request.Documents.Add(document); } var apiKey = System.Configuration.ConfigurationManager.AppSettings["ApiKey"]; var client = new KeyPhraseClient(apiKey); var response = client.GetKeyPhrases(request); var phrases = response.Documents.SelectMany(d => d.KeyPhrases).ToList(); var phraseStats = phrases.Distinct().Select(p => new { phrase = p, count = phrases.Count(x => x.Equals(p)) }); foreach (var result in phraseStats.OrderBy(ps => ps.count).Take(10)) { System.Console.WriteLine($"Key Phrase: {result.phrase} | Occurrences: {result.count}"); } }