예제 #1
0
        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();
            }
        }
예제 #2
0
        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);
        }
        public async Task KeyPhraseAnalysis(string input)
        {
            var document = new KeyPhraseDocument()
            {
                Id       = "1",
                Text     = input,
                Language = "en"
            };

            var request = new KeyPhraseRequest();

            request.Documents.Add(document);

            var client = new KeyPhraseClient(System.Configuration.ConfigurationManager.AppSettings["apikey"]);

            var response = await client.GetKeyPhrasesAsync(request);

            foreach (var doc in response.Documents)
            {
                foreach (var keyPhrase in doc.KeyPhrases)
                {
                    WriteNewLine("{0}", keyPhrase);
                }
            }
        }
예제 #4
0
        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 async Task AnalyzeKeyPhraseAsync(string id, string text, string language)
        {
            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 = await client.GetKeyPhrasesAsync(request);

                foreach (var doc in response.Documents)
                {
                    foreach (var keyPhrase in doc.KeyPhrases)
                    {
                        Console.WriteLine("Key Phrase: {0}", keyPhrase);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
예제 #6
0
        //public IConfiguration configuration;

        //public KeyExtraction(IConfiguration _configuration)
        //{
        //    configuration = _configuration;
        //}

        public static async Task <List <string> > CallWebAPIAsync(string utterance)
        {
            //var client = new HttpClient();
            //var queryString = HttpUtility.ParseQueryString(String.Empty);



            // Request headers
            //client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", configuration["TextAnalylisisAPIKey"]);
            //client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", "3ff10cb8e78442bab6ac035ea5c1630a");
            //client.DefaultRequestHeaders.Add("Accept", "application/json");
            //client.DefaultRequestHeaders.Add("Content-Type", "application/json");

            //var uri = configuration["TextAnalylisisAPIHostName"] + queryString;
            //var uri = "https://westus.api.cognitive.microsoft.com/text/analytics/v2.0/keyPhrases" ;

            //HttpResponseMessage response;
            //string body = "'documents': [{'language':'en','id':'1','text':'We love this trail and make the trip every year. The views are breathtaking and well worth the hike!'}]";
            //string body = "documents: [{language:en,id:1,text:We love this trail and make the trip every year. The views are breathtaking and well worth the hike!}]";
            //string body = @"{
            //    'documents': [
            //        {
            //         'language': 'en',
            //            'id': '1',
            //            'text': 'We love this trail and make the trip every year. The views are breathtaking and well worth the hike!'
            //        }
            //      ]
            //    }";

            //var data = JsonConvert.SerializeObject(body);

            var document = new KeyPhraseDocument()
            {
                Language = "en",
                Id       = "1",
                Text     = utterance
            };

            var keyclient  = new KeyPhraseClient("3ff10cb8e78442bab6ac035ea5c1630a");
            var keyrequest = new KeyPhraseRequest();

            keyrequest.Documents.Add(document);

            var keyresponse = await keyclient.GetKeyPhrasesAsync(keyrequest);

            return(keyresponse.Documents[0].KeyPhrases);

            // Request body
            //byte[] byteData = Encoding.UTF8.GetBytes(body);

            //using (var content = new ByteArrayContent(byteData))
            //{
            //    content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
            //    response = await client.PostAsync(uri, content);
            //}


            //return null;
        }
        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);
        }
예제 #8
0
        public void Validate_InvalidLanguage()
        {
            var text = "I had a wonderful experience! The rooms were wonderful and the staff were helpful.";
            var doc  = new KeyPhraseDocument()
            {
                Id = "TEST001", Text = text, Language = "it"
            };
            var request = new KeyPhraseRequest();

            request.Documents.Add(doc);
            request.Validate();
        }
예제 #9
0
        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);
            }
        }
예제 #10
0
        private static async Task RunKeyPhraseDetection()
        {
            Console.ForegroundColor = ConsoleColor.Magenta;
            Console.WriteLine("Begining key phrase detection ({0})", DateTime.Now.ToString());

            var client = new KeyPhraseClient(textAnalyticsApiKey);

            foreach (var conversation in conversations)
            {
                foreach (var message in conversation.Messages)
                {
                    try
                    {
                        var document = new KeyPhraseDocument()
                        {
                            Id       = message.Id.ToString(),
                            Text     = message.Text,
                            Language = message.Metadata.LanguageName
                        };

                        var request = new KeyPhraseRequest();
                        request.Documents.Add(document);

                        var response = await client.GetKeyPhrasesAsync(request);

                        foreach (var keyPhrase in response.Documents[0].KeyPhrases)
                        {
                            message.Metadata.KeyPhrases.Add(keyPhrase);
                        }

                        Console.Write("Conversation {0} | Message {1} | Key Phrase Count {2}", conversation.Id, message.Id, message.Metadata.KeyPhrases.Count);
                    }
                    catch (Exception ex)
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine();
                        Console.WriteLine("Exception encountered ({0})", DateTime.Now.ToString());
                        Console.WriteLine("Conversation {0} | Message {1}", conversation.Id, message.Id);
                        Console.WriteLine(ex.Message);
                    }
                }
            }

            Console.WriteLine("Key phrase detection complete ({0})", DateTime.Now.ToString());
            Console.WriteLine();
        }
예제 #11
0
        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}");
            }
        }
예제 #12
0
        private async void Detect_Key_Phrases(object sender, RoutedEventArgs e)
        {
            this.KeyPhrases = "";

            try
            {
                var language = await GetLanguage();

                var document = new KeyPhraseDocument()
                {
                    Id = Guid.NewGuid().ToString(), Text = this.InputText, Language = language
                };

                var request = new KeyPhraseRequest();
                request.Documents.Add(document);

                MainWindow mainWindow = Window.GetWindow(this) as MainWindow;
                var        client     = new KeyPhraseClient(mainWindow._scenariosControl.SubscriptionKey);

                MainWindow.Log("Request: Identifying key phrases..");
                var response = await client.GetKeyPhrasesAsync(request);

                MainWindow.Log("Response: Success. Key phrases identified.");

                var keyPhrases = new StringBuilder();

                foreach (var keyPhrase in response.Documents[0].KeyPhrases)
                {
                    keyPhrases.Append(string.Format("{0} ", keyPhrase));
                }

                this.KeyPhrases = keyPhrases.ToString();
            }
            catch (Exception ex)
            {
                MainWindow.Log(ex.Message);
            }
        }
        public async void DetectKeyPhrases(CNMAudioItem cnmAudioItem)
        {
            if (string.IsNullOrWhiteSpace(cnmAudioItem.RecognizedText))
            {
                return;
            }

            var keyPhraseClient  = _keyPhraseClient ?? new KeyPhraseClient("4f2c3df7714f4bbfbc742547624a6f2b");
            var keyPhraseRequest = new KeyPhraseRequest();

            keyPhraseRequest.Documents.Add(new KeyPhraseDocument {
                Id = Guid.NewGuid().ToString(), Text = cnmAudioItem.RecognizedText, Language = "es"
            });
            var keyPhraseResult = await keyPhraseClient.GetKeyPhrasesAsync(keyPhraseRequest);

            if (keyPhraseResult.Errors.Count == 0)
            {
                var doc = keyPhraseResult.Documents[0];
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    cnmAudioItem.KeyPhrases = doc.KeyPhrases;
                }), null).Wait();
            }
        }
예제 #14
0
        /// <summary>
        /// Get KeyPhrase Analysis from Expert AI API
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public async Task <KeyPhraseReponse> GetKeyPhrasesAsync(
            string text, AuthenticationHeaderValue authHeader
            )
        {
            var request = new KeyPhraseRequest
            {
                Document = new Document
                {
                    Text = text
                }
            };

            _httpClient.DefaultRequestHeaders.Authorization = authHeader;
            var content = new StringContent(
                JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json");

            var response = await _httpClient
                           .PostAsync("v2/analyze/standard/en/relevants", content);

            response.EnsureSuccessStatusCode();

            return(JsonConvert.DeserializeObject <KeyPhraseReponse>(
                       await response.Content.ReadAsStringAsync()));
        }
        private async void Detect_Key_Phrases(string text)
        {
            try
            {
                var document = new KeyPhraseDocument()
                {
                    Id = Guid.NewGuid().ToString(), Text = text, Language = "en"
                };

                var request = new KeyPhraseRequest();
                request.Documents.Add(document);

                MainWindow mainWindow = Window.GetWindow(this) as MainWindow;
                var        client     = new KeyPhraseClient(mainWindow._scenariosControl.SubscriptionKey);

                var response = await client.GetKeyPhrasesAsync(request);

                Log("Response: Success. Key phrases identified.");

                Dictionary <string, int> deltaPhrasesDict = new Dictionary <string, int>();

                var keyPhrases = new StringBuilder();

                //add only new phrases
                keyPhrasesList.AddRange(response.Documents[0].KeyPhrases.Where(x => !keyPhrasesList.Any(y => y == x)));

                //((MainWindow)Application.Current.MainWindow)._scenariosControl.HighlightWords(keyPhrasesList);
                ((MainWindow)Application.Current.MainWindow)._scenariosControl.HighlightWords(response.Documents[0].KeyPhrases);


                foreach (var keyPhrase in response.Documents[0].KeyPhrases.ConvertAll(d => ps.Singularize(d.ToLower())))
                {
                    if (keyPhrase.Trim() != "")
                    {
                        keyPhrases.Append(string.Format("{0}; ", keyPhrase));
                        if (keyPhrasesDict.ContainsKey(keyPhrase))
                        {
                            keyPhrasesDict[keyPhrase] = keyPhrasesDict[keyPhrase] + 1;
                            deltaPhrasesDict.Add(keyPhrase, keyPhrasesDict[keyPhrase]);
                        }
                        else
                        {
                            keyPhrasesDict.Add(keyPhrase, 1);
                            deltaPhrasesDict.Add(keyPhrase, 1);
                        }
                    }
                }
                keyPhrasesString = string.Join("; ", keyPhrasesDict.Keys);

                ((MainWindow)Application.Current.MainWindow)._scenariosControl.LogKeywords(keyPhrasesString);


                if (sendToPBICheckbox.IsChecked == true)
                {
                    string json = JsonConvert.SerializeObject(deltaPhrasesDict.Select(e => new { keyword = e.Key, frequency = e.Value, dt = (DateTime.UtcNow.ToString("s") + "Z") }));
                    //post(keywordUrl, json);
                    Thread myNewThread = new Thread(() => post(keywordUrl, json));
                    myNewThread.Start();
                }
            }
            catch (Exception ex)
            {
                Log(ex.Message);
            }
        }