public void DeserializeTextAnalyticsError()
        {
            var errors = @"
                {
                ""errors"": [
                    {
                      ""id"": ""2"",
                      ""error"": {
                        ""code"": ""InvalidArgument"",
                        ""innererror"": {
                            ""code"": ""InvalidDocument"",
                            ""message"": ""Document text is empty.""
                            },
                        ""message"": ""Invalid document in request.""
                        }
                    }
                ]
                }
                ";

            using JsonDocument json = JsonDocument.Parse(errors);
            TextAnalyticsResult result = TextAnalyticsServiceSerializer.ReadDocumentErrors(json.RootElement).FirstOrDefault();

            Assert.IsNotNull(result);
            Assert.IsNotNull(result.Error);
            Assert.IsTrue(result.Error.ErrorCode.ToString().Equals(TextAnalyticsErrorCode.InvalidDocument, StringComparison.OrdinalIgnoreCase));
            Assert.AreEqual(result.Error.Message, "Document text is empty.");
        }
Exemplo n.º 2
0
        public async Task <TextAnalyticsResult> AnalyzeTextAsync(string text)
        {
            try
            {
                // COGNITIVE SERVICE : client API REST
                ITextAnalyticsAPI client = new TextAnalyticsAPI(new ApiKeyServiceClientCredentials())
                {
                    AzureRegion = AzureRegions.Australiaeast
                };

                var result = new TextAnalyticsResult();

                // COGNITIVE SERVICE : Détection de la langue
                var resultLanguage = await client.DetectLanguageAsync(new BatchInput(
                                                                          new List <Input>()
                {
                    new Input("0", text)
                }));

                var l = resultLanguage.Documents[0].DetectedLanguages[0];
                result.Language    = l.Name;
                result.LanguageIso = l.Iso6391Name;

                // COGNITIVE SERVICE : Détection des phrases clés
                var resultKeyPhrases = client.KeyPhrasesAsync(new MultiLanguageBatchInput(
                                                                  new List <MultiLanguageInput>()
                {
                    new MultiLanguageInput(result.LanguageIso, "0", text)
                }));

                result.KeyPhrases = resultKeyPhrases.Result.Documents[0].KeyPhrases.ToArray();

                // COGNITIVE SERVICE : Détection du score de sentiment
                var resultSentiment = client.SentimentAsync(new MultiLanguageBatchInput(
                                                                new List <MultiLanguageInput>()
                {
                    new MultiLanguageInput(result.LanguageIso, "0", text)
                }));

                result.ScoreSentiment = resultSentiment.Result.Documents[0].Score;

                return(result);
            }
            catch (Exception ex)
            {
                _logger.LogError($"Text analyze : {ex.Message}");
                return(null);
            }
        }
Exemplo n.º 3
0
        private async Task <DialogAnalysisResult> InterpretResultsAsync(HandwritingRecognitionResult hwrResult, ComputerVisionImageAnalysisResult imageResult)
        {
            DialogAnalysisResult retResult = new DialogAnalysisResult();

            if (hwrResult.Lines?.Count() > 0)
            {
                // Get labels from handwriting recognition
                IEnumerable <ImageTextRegion> labels = hwrResult.Lines.Where(line => string.IsNullOrEmpty(line.Text) == false).Select(line => line.TextRegion);

                // Interpret dialog data
                DialogDataInterpreter dialogDataInterpreter = new DialogDataInterpreter(imageResult.Metadata.Height, imageResult.Metadata.Width, labels);
                retResult = dialogDataInterpreter.Result;

                // Extract text from dialog data interpreter's title and content result
                List <string> results = new List <string>();
                results.Add(retResult.TitleLabel?.TextLabel?.Text);
                results.AddRange(retResult.ContentLabels?.Select(label => label?.TextLabel?.Text));

                // Analyze key phrases from result text
                string text = StringUtility.GetTextOrDefault(results, string.Empty, " ");
                if (string.IsNullOrEmpty(text) == false)
                {
                    TextAnalyticsResult <TextAnalyticsKeyPhrasesResult> keyPhrasesResult = await this.TextAnalyticsService.AnalyzeKeyPhrasesAsync(text);

                    retResult.KeyPhrases = keyPhrasesResult.Results?.Select(kp => kp.KeyPhrases).FirstOrDefault();
                }
            }

            // Add image description tags
            if (imageResult.Description?.Tags?.Count() > 0)
            {
                retResult.Tags = imageResult.Description.Tags;
            }

            // Add image description captions
            if (imageResult.Description?.Captions?.Count() > 0)
            {
                retResult.Captions =
                    imageResult.Description.Captions.Where(cap => string.IsNullOrEmpty(cap.Text) == false)
                    .OrderByDescending(cap => cap.Confidence).Select(cap => cap.Text).ToArray();
            }

            return(retResult);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Get Ask Question Results from Bing Web Search Result
        /// </summary>
        public static async Task <string[]> GetTopics(string question, string[] currentTopics)
        {
            // Run keyphrases extraction
            TextAnalyticsResult <TextAnalyticsKeyPhrasesResult> textAnalyticsResult =
                await TextAnalytics.AnalyzeKeyPhrasesAsync(question);

            string[] topics = currentTopics;

            if (textAnalyticsResult.Results?.Count() > 0 == true)
            {
                topics = textAnalyticsResult.Results.Select(r => r.KeyPhrases).FirstOrDefault();
            }

            if (topics?.Count() > 0)
            {
                topics = topics.Select(t => t.ToLower()).ToArray();
            }

            return(topics);
        }