internal static KeyPhraseExtractionTasksItem DeserializeKeyPhraseExtractionTasksItem(JsonElement element)
        {
            KeyPhraseResult              results            = default;
            DateTimeOffset               lastUpdateDateTime = default;
            Optional <string>            name   = default;
            TextAnalyticsOperationStatus status = default;

            foreach (var property in element.EnumerateObject())
            {
                if (property.NameEquals("results"))
                {
                    results = KeyPhraseResult.DeserializeKeyPhraseResult(property.Value);
                    continue;
                }
                if (property.NameEquals("lastUpdateDateTime"))
                {
                    lastUpdateDateTime = property.Value.GetDateTimeOffset("O");
                    continue;
                }
                if (property.NameEquals("name"))
                {
                    name = property.Value.GetString();
                    continue;
                }
                if (property.NameEquals("status"))
                {
                    status = new TextAnalyticsOperationStatus(property.Value.GetString());
                    continue;
                }
            }
            return(new KeyPhraseExtractionTasksItem(lastUpdateDateTime, name.Value, status, results));
        }
Example #2
0
        public async Task <IDictionary <string, KeyPhraseResult> > GetBatchKeyPhrasesAsync(Dictionary <string, string> textBatch)
        {
            this.ValidateBatchRequest(textBatch);

            if (!textBatch.Any())
            {
                return(new Dictionary <string, KeyPhraseResult>());
            }

            string content;

            using (var response = await this._requestor.PostAsync(Constants.KeyPhraseBatchRequest, BuildInputString(textBatch)))
            {
                content = await response.Content.ReadAsStringAsync();

                if (!response.IsSuccessStatusCode)
                {
                    return(textBatch.ToDictionary(r => r.Key, r => KeyPhraseResult.Build(this._errorMessageGenerator.GenerateError(response.StatusCode, content))));
                }
            }

            var result = JsonConvert.DeserializeObject <AzureKeyPhrasesBatchResult>(content);

            var parsedResults = result.KeyPhrasesBatch.ToDictionary(sr => sr.Id, sr => KeyPhraseResult.Build(sr.KeyPhrases));

            foreach (var error in result.Errors)
            {
                parsedResults.Add(error.Id, KeyPhraseResult.Build(error.Message));
            }

            return(parsedResults);
        }
Example #3
0
        public void AnalyseText(string text)
        {
            if (string.IsNullOrWhiteSpace(text))
            {
                return;
            }

            SentimentResult sentiment = textAnalyticsClient.Sentiment(text);
            KeyPhraseResult keywords  = textAnalyticsClient.KeyPhrases(text);

            foreach (string keyword in keywords.KeyPhrases.Select(x => x.ToLower().Trim()))
            {
                if (!keywordCounts.ContainsKey(keyword))
                {
                    keywordCounts.Add(keyword, 1);
                }
                else
                {
                    keywordCounts[keyword]++;
                }
            }

            string sentimentWord  = sentiment.ToDescription();
            string commonKeywords = string.Join(", ",
                                                keywordCounts.OrderByDescending(x => x.Value)
                                                .Take(4)
                                                .Select(x => $"{x.Key} [{x.Value}]"));

            Console.WriteLine($"Sentiment is {sentimentWord} [{sentiment.Score}]");
            Console.WriteLine($" Common keywords are: {commonKeywords}");
            Console.WriteLine();
        }
Example #4
0
        public async Task <KeyPhraseResult> GetKeyPhrasesAsync(string text)
        {
            if (string.IsNullOrWhiteSpace(text))
            {
                return(KeyPhraseResult.Build(Constants.KeyPhraseNullInputErrorText));
            }

            var request = $"{Constants.KeyPhraseRequest}{HttpUtility.UrlEncode(text)}";

            string content;

            using (var response = await this._requestor.GetAsync(request))
            {
                content = await response.Content.ReadAsStringAsync();

                if (!response.IsSuccessStatusCode)
                {
                    return(KeyPhraseResult.Build(this._errorMessageGenerator.GenerateError(response.StatusCode, content)));
                }
            }

            var result = JsonConvert.DeserializeObject <AzureKeyPhraseResult>(content);

            return(KeyPhraseResult.Build(result.KeyPhrases));
        }
        public KeyPhraseResult ProcessKeyPhrases(string documentid, string text)
        {
            TextAnalyticsClient client = AuthenticateTextAnalytics(_azureRegion, _textAnalyticsKey);

            KeyPhraseResult result = client.KeyPhrases(text);

            return(result);
        }
Example #6
0
        public async Task DecodeResponse()
        {
            var expected = KeyPhraseResult.Build(new[] { "wonderful hotel", "unique decor", "friendly staff" });

            var sut    = TextAnalyticsTestHelper.BuildSut(GetMessage());
            var result = await sut.GetKeyPhrasesAsync(Input);

            Assert.AreEqual(expected, result);
        }
Example #7
0
        public async Task ReturnBadRequestIfEmptyInput()
        {
            var expected = KeyPhraseResult.Build(Constants.KeyPhraseNullInputErrorText);
            var sut      = TextAnalyticsTestHelper.BuildSut(TextAnalyticsTestHelper.GetErrorMessage(Error));

            var result = await sut.GetKeyPhrasesAsync(null);

            Assert.AreEqual(expected, result);
        }
Example #8
0
        internal KeyPhraseExtractionTasksItem(DateTimeOffset lastUpdateDateTime, TextAnalyticsOperationStatus status, KeyPhraseResult resultsInternal) : base(lastUpdateDateTime, status)
        {
            if (resultsInternal == null)
            {
                throw new ArgumentNullException(nameof(resultsInternal));
            }

            ResultsInternal = resultsInternal;
        }
Example #9
0
        internal KeyPhraseExtractionTasksItem(DateTimeOffset lastUpdateDateTime, string name, JobStatus status, KeyPhraseResult results) : base(lastUpdateDateTime, name, status)
        {
            if (name == null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            Results = results;
        }
Example #10
0
        public async Task GetResultFromAzure()
        {
            var          expected = KeyPhraseResult.Build(new[] { "bunch of phrases", "wonderful hotel", "great service", "text" });
            const string Input    = "I need some text that can extract a bunch of phrases from. This was a wonderful hotel with great service but really overpriced.";

            var sut    = ServiceFactory.Build();
            var result = await sut.GetKeyPhrasesAsync(Input);

            Assert.AreEqual(expected, result, string.Join(",", result.Phrases));
        }
Example #11
0
        public async Task ReturnFailureOnBadResult()
        {
            var error    = new ErrorMessageGenerator().GenerateError(HttpStatusCode.BadRequest, Error);
            var expected = KeyPhraseResult.Build(error);

            var sut    = TextAnalyticsTestHelper.BuildSut(TextAnalyticsTestHelper.GetErrorMessage(Error));
            var result = await sut.GetKeyPhrasesAsync(Input);

            Assert.AreEqual(expected, result);
        }
Example #12
0
        public void KeyPhrases()
        {
            using (MockContext context = MockContext.Start(this.GetType().FullName))
            {
                HttpMockServer.Initialize(this.GetType().FullName, "KeyPhrases");
                ITextAnalyticsClient client = GetClient(HttpMockServer.CreateInstance());
                KeyPhraseResult      result = client.KeyPhrases("I love my team mates");

                Assert.Equal("team mates", result.KeyPhrases[0]);
            }
        }
Example #13
0
        public async Task KeyPhrasesAsync()
        {
            using (MockContext context = MockContext.Start(this.GetType()))
            {
                HttpMockServer.Initialize(this.GetType(), "KeyPhrasesAsync");
                ITextAnalyticsClient client = GetClient(HttpMockServer.CreateInstance());
                KeyPhraseResult      result = await client.KeyPhrasesAsync("I love my team mates");

                Assert.Equal("team mates", result.KeyPhrases[0]);
            }
        }
Example #14
0
        public async Task DecodeResponse()
        {
            var expected = new Dictionary <string, KeyPhraseResult>
            {
                { "1", KeyPhraseResult.Build(new[] { "unique decor", "friendly staff", "wonderful hotel" }) },
                { "2", KeyPhraseResult.Build(new[] { "amazing build conference", "interesting talks" }) },
                { "3", KeyPhraseResult.Build(new[] { "hours", "traffic", "airport" }) },
                { "4", KeyPhraseResult.Build("Record cannot be null/empty") }
            };
            var sut = TextAnalyticsTestHelper.BuildSut(GetMessage());

            var result = await sut.GetBatchKeyPhrasesAsync(this._input);

            CollectionAssert.AreEquivalent(expected, result.ToList());
        }
Example #15
0
        private static void ProcessKeyPhrases(string documentid, string text)
        {
            TextAnalyticsClient client = AuthenticateTextAnalytics(_azureRegion, _textAnalyticsKey);

            KeyPhraseResult result = client.KeyPhrases(text);

            Console.WriteLine("Document ID: {0} ", documentid);

            Console.WriteLine("\t Key phrases:");

            foreach (string keyphrase in result.KeyPhrases)
            {
                Console.WriteLine("\t\t" + keyphrase);
            }
        }
Example #16
0
        public async Task ReturnAllFailureOnBadResult()
        {
            var err      = new ErrorMessageGenerator().GenerateError(HttpStatusCode.BadRequest, Error);
            var expected = new Dictionary <string, KeyPhraseResult>
            {
                { "1", KeyPhraseResult.Build(err) },
                { "2", KeyPhraseResult.Build(err) },
                { "3", KeyPhraseResult.Build(err) },
                { "4", KeyPhraseResult.Build(err) }
            };

            var sut    = TextAnalyticsTestHelper.BuildSut(TextAnalyticsTestHelper.GetErrorMessage(Error));
            var result = await sut.GetBatchKeyPhrasesAsync(this._input);

            CollectionAssert.AreEqual(expected, result.ToList());
        }
        public static async Task Main()
        {
            string endpoint = "<endpointGoesHere>";
            string key      = "<apiKeyGoesHere>";

            ApiKeyServiceClientCredentials credentials = new ApiKeyServiceClientCredentials(key);

            using (TextAnalyticsClient client = new TextAnalyticsClient(credentials)
            {
                Endpoint = endpoint
            })
            {
                while (true)
                {
                    // Read the users input
                    Console.Write("\n--------------------\nEnter some text: ");

                    string input = Console.ReadLine();

                    // Detect the language
                    LanguageResult languageResult = await client.DetectLanguageAsync(input);

                    Console.WriteLine("\n> Language: " + languageResult.ToDescription());

                    string languageCode = languageResult.HighestLanguageCode();

                    // Detect the sentiment
                    SentimentResult sentimentResult = await client.SentimentAsync(input, languageCode);

                    Console.WriteLine($"> Sentiment: {sentimentResult.Score} - {sentimentResult.ToDescription()}");

                    // Detect the key phrases
                    KeyPhraseResult keyPhraseResult = await client.KeyPhrasesAsync(input, languageCode);

                    Console.WriteLine("> Key Phrases: " + string.Join(", ", keyPhraseResult.KeyPhrases));
                }
            }
        }
        public async Task GetResultFromAzure()
        {
            var input = new Dictionary <string, string>
            {
                { "1", "This is very positive text because I love this service" },
                { "2", "Test is very bad because I hate this service" },
                { "3", "The service was OK, nothing special, I've had better" },
                { "4", "" }
            };

            var expected = new Dictionary <string, KeyPhraseResult>
            {
                { "1", KeyPhraseResult.Build(new[] { "service", "positive text" }) },
                { "2", KeyPhraseResult.Build(new[] { "Test", "service" }) },
                { "3", KeyPhraseResult.Build(new[] { "service" }) },
                { "4", KeyPhraseResult.Build("Record cannot be null/empty") }
            };

            var sut    = ServiceFactory.Build();
            var result = await sut.GetBatchKeyPhrasesAsync(input);

            CollectionAssert.AreEquivalent(expected, result.ToList());
        }
        internal static TasksStateTasksKeyPhraseExtractionTasksItem DeserializeTasksStateTasksKeyPhraseExtractionTasksItem(JsonElement element)
        {
            Optional <KeyPhraseResult> results            = default;
            DateTimeOffset             lastUpdateDateTime = default;
            Optional <string>          taskName           = default;
            State status = default;

            foreach (var property in element.EnumerateObject())
            {
                if (property.NameEquals("results"))
                {
                    if (property.Value.ValueKind == JsonValueKind.Null)
                    {
                        property.ThrowNonNullablePropertyIsNull();
                        continue;
                    }
                    results = KeyPhraseResult.DeserializeKeyPhraseResult(property.Value);
                    continue;
                }
                if (property.NameEquals("lastUpdateDateTime"))
                {
                    lastUpdateDateTime = property.Value.GetDateTimeOffset("O");
                    continue;
                }
                if (property.NameEquals("taskName"))
                {
                    taskName = property.Value.GetString();
                    continue;
                }
                if (property.NameEquals("status"))
                {
                    status = property.Value.GetString().ToState();
                    continue;
                }
            }
            return(new TasksStateTasksKeyPhraseExtractionTasksItem(lastUpdateDateTime, taskName.Value, status, results.Value));
        }
        public TextAnalyticsInsight GetInsights(string documentid, string text)
        {
            // create the TextAnalyticsInsight object
            if (!string.IsNullOrEmpty(text))
            {
                TextAnalyticsInsight textAnalyticsInsight = new TextAnalyticsInsight();
                EntitiesResult       entitiesResult       = this.ProcessEntities(documentid, text);
                KeyPhraseResult      keyPhraseResult      = this.ProcessKeyPhrases(documentid, text);
                SentimentResult      sentimentResult      = this.ProcessSentiment(documentid, text);

                foreach (EntityRecord record in entitiesResult.Entities)
                {
                    textAnalyticsInsight.EntityRecords.Add(new Entity.CognitiveServices.EntityRecord
                    {
                        Name              = record.Name,
                        SubType           = record.SubType,
                        Type              = record.Type,
                        WikipediaId       = record.WikipediaId,
                        WikipediaLanguage = record.WikipediaLanguage,
                        WikipediaUrl      = record.WikipediaUrl
                    });
                }

                foreach (string keyPhrase in keyPhraseResult.KeyPhrases)
                {
                    textAnalyticsInsight.KeyPhrases.Add(keyPhrase);
                }

                textAnalyticsInsight.SentimentScore = sentimentResult.Score.Value;

                // map the CogServices models to our custom model TextAnalyticsInsight which
                // contains all TextAnalytics insights for the paramter Text
                return(textAnalyticsInsight);
            }

            return(new TextAnalyticsInsight());
        }
        internal static ExtractKeyPhrasesResultCollection ConvertToExtractKeyPhrasesResultCollection(KeyPhraseResult results, IDictionary <string, int> idToIndexMap)
        {
            var keyPhrases = new List <ExtractKeyPhrasesResult>();

            //Read errors
            foreach (DocumentError error in results.Errors)
            {
                keyPhrases.Add(new ExtractKeyPhrasesResult(error.Id, ConvertToError(error.Error)));
            }

            //Read Key phrases
            foreach (DocumentKeyPhrases docKeyPhrases in results.Documents)
            {
                keyPhrases.Add(new ExtractKeyPhrasesResult(docKeyPhrases.Id, docKeyPhrases.Statistics ?? default, ConvertToKeyPhraseCollection(docKeyPhrases)));
            }

            keyPhrases = SortHeterogeneousCollection(keyPhrases, idToIndexMap);

            return(new ExtractKeyPhrasesResultCollection(keyPhrases, results.Statistics, results.ModelVersion));
        }
 internal KeyPhraseExtractionTasksItemProperties(KeyPhraseResult results)
 {
     Results = results;
 }
Example #23
0
 internal Components1D9IzucSchemasTasksstatePropertiesTasksPropertiesKeyphraseextractiontasksItemsAllof1(KeyPhraseResult results)
 {
     Results = results;
 }
Example #24
0
 internal TasksStateTasksKeyPhraseExtractionTasksItem(DateTimeOffset lastUpdateDateTime, string taskName, State status, KeyPhraseResult results) : base(lastUpdateDateTime, taskName, status)
 {
     Results = results;
 }
Example #25
0
 internal KeyPhraseExtractionTasksItem(DateTimeOffset lastUpdateDateTime, string name, TextAnalyticsOperationStatus status, KeyPhraseResult resultsInternal) : base(lastUpdateDateTime, name, status)
 {
     ResultsInternal = resultsInternal;
 }
 internal KeyPhraseTaskResult(KeyPhraseResult results)
 {
     Results = results;
 }