public void AnalyzeTone_Success()
        {
            ToneAnalyzerService service = new ToneAnalyzerService();

            service.Endpoint = "https://watson-api-explorer.mybluemix.net/tone-analyzer/api";
            var results = service.AnalyzeTone("A word is dead when it is said, some say. Emily Dickinson");

            Assert.IsNotNull(results);
            Assert.IsTrue(results.DocumentTone.ToneCategories.Count >= 1);
        }
        public void AnalyzeTone_Success_With_Text()
        {
            IClient  client  = this.CreateClient();
            IRequest request = Substitute.For <IRequest>();

            #region response
            ToneAnalysis response = new ToneAnalysis()
            {
                SentencesTone = new List <SentenceAnalysis>()
                {
                    new SentenceAnalysis()
                    {
                        SentenceId     = 0,
                        InputFrom      = 0,
                        InputTo        = 0,
                        Text           = "string",
                        ToneCategories = new List <ToneCategory>()
                        {
                            new ToneCategory()
                            {
                                CategoryName = "string",
                                CategoryId   = "string",
                                Tones        = new List <ToneScore>()
                                {
                                    new ToneScore()
                                    {
                                        ToneName = "string",
                                        ToneId   = "string",
                                        Score    = 0
                                    }
                                }
                            }
                        }
                    }
                },
                DocumentTone = new DocumentTone()
                {
                    ToneCategories = new List <ToneCategory>()
                    {
                        new ToneCategory()
                        {
                            CategoryName = "string",
                            CategoryId   = "string",
                            Tones        = new List <ToneScore>()
                            {
                                new ToneScore()
                                {
                                    ToneName = "string",
                                    ToneId   = "string",
                                    Score    = 0
                                }
                            }
                        }
                    }
                }
            };

            #endregion

            client.PostAsync(Arg.Any <string>())
            .Returns(request);

            request.WithArgument(Arg.Any <string>(), Arg.Any <object>())
            .Returns(request);

            request.WithBody <JObject>(Arg.Any <JObject>(), Arg.Any <MediaTypeHeaderValue>())
            .Returns(request);

            request.As <ToneAnalysis>()
            .Returns(Task.FromResult(response));

            ToneAnalyzerService service = new ToneAnalyzerService(client);

            var analyzeTone = service.AnalyzeTone("A word is dead when it is said, some say. Emily Dickinson");

            Assert.IsNotNull(analyzeTone);
            client.Received().PostAsync(Arg.Any <string>());
            Assert.IsTrue(analyzeTone.DocumentTone.ToneCategories.Count >= 1);
        }
        public void AnalyzeTone_Cath_Exception()
        {
            IClient  client  = this.CreateClient();
            IRequest request = Substitute.For <IRequest>();

            #region response
            ToneAnalysis response = new ToneAnalysis()
            {
                SentencesTone = new List <SentenceAnalysis>()
                {
                    new SentenceAnalysis()
                    {
                        SentenceId     = 0,
                        InputFrom      = 0,
                        InputTo        = 0,
                        Text           = "string",
                        ToneCategories = new List <ToneCategory>()
                        {
                            new ToneCategory()
                            {
                                CategoryName = "string",
                                CategoryId   = "string",
                                Tones        = new List <ToneScore>()
                                {
                                    new ToneScore()
                                    {
                                        ToneName = "string",
                                        ToneId   = "string",
                                        Score    = 0
                                    }
                                }
                            }
                        }
                    }
                },
                DocumentTone = new DocumentTone()
                {
                    ToneCategories = new List <ToneCategory>()
                    {
                        new ToneCategory()
                        {
                            CategoryName = "string",
                            CategoryId   = "string",
                            Tones        = new List <ToneScore>()
                            {
                                new ToneScore()
                                {
                                    ToneName = "string",
                                    ToneId   = "string",
                                    Score    = 0
                                }
                            }
                        }
                    }
                }
            };

            #endregion

            client.PostAsync(Arg.Any <string>())
            .Returns(request);

            request.WithArgument(Arg.Any <string>(), Arg.Any <object>())
            .Returns(request);

            request.WithBody <JObject>(Arg.Any <JObject>(), Arg.Any <MediaTypeHeaderValue>())
            .Returns(x => { throw new AggregateException(new Exception()); });

            request.As <ToneAnalysis>()
            .Returns(Task.FromResult(response));

            ToneAnalyzerService service = new ToneAnalyzerService(client);

            service.AnalyzeTone("A word is dead when it is said, some say. Emily Dickinson", new List <Tone>()
            {
                Tone.EMOTION, Tone.LANGUAGE, Tone.SOCIAL
            }, false);
        }
Exemple #4
0
        private void AnalyzeTone()
        {
            Console.WriteLine("Calling GetTone()...");

            var result = _toneAnalyzer.AnalyzeTone(_text);

            if (result != null)
            {
                Console.WriteLine("Document tone");
                if (result.DocumentTone != null)
                {
                    if (result.DocumentTone.ToneCategories != null && result.DocumentTone.ToneCategories.Count > 0)
                    {
                        foreach (ToneCategory toneCategory in result.DocumentTone.ToneCategories)
                        {
                            Console.WriteLine(string.Format("Category name: {0} | Category ID: {1}", toneCategory.CategoryName, toneCategory.CategoryId));
                            if (toneCategory.Tones != null && toneCategory.Tones.Count > 0)
                            {
                                foreach (ToneScore toneScore in toneCategory.Tones)
                                {
                                    Console.WriteLine(string.Format("Tone name: {0} | Tone ID: {1} | Tone Score: {2}", toneScore.ToneName, toneScore.ToneId, toneScore.Score));
                                }
                            }
                        }
                    }
                }


                Console.WriteLine("Sentence tone");
                if (result.SentencesTone != null && result.SentencesTone.Count > 0)
                {
                    foreach (SentenceAnalysis sentenceTone in result.SentencesTone)
                    {
                        Console.WriteLine(string.Format("SentenceID: {0} | Text: {1} | InputFrom: {2} | InputTo: {3}",
                                                        sentenceTone.SentenceId,
                                                        sentenceTone.Text,
                                                        sentenceTone.InputFrom,
                                                        sentenceTone.InputTo));

                        if (sentenceTone.ToneCategories != null && sentenceTone.ToneCategories.Count > 0)
                        {
                            foreach (ToneCategory toneCategory in sentenceTone.ToneCategories)
                            {
                                Console.WriteLine(string.Format("Category name: {0} | Category ID: {1}", toneCategory.CategoryName, toneCategory.CategoryId));
                                if (toneCategory.Tones != null && toneCategory.Tones.Count > 0)
                                {
                                    foreach (ToneScore toneScore in toneCategory.Tones)
                                    {
                                        Console.WriteLine(string.Format("Tone name: {0} | Tone ID: {1} | Tone Score: {2}", toneScore.ToneName, toneScore.ToneId, toneScore.Score));
                                    }
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                Console.WriteLine("Result is null.");
            }
        }