public WatsonToneAnalyzer(string apiKey, string serviceUrl, SaraDbContext db)
        {
            IamAuthenticator authenticator = new IamAuthenticator(apikey: apiKey);

            ToneAnalyzer = new ToneAnalyzerService("2017-09-21", authenticator);
            ToneAnalyzer.SetServiceUrl(serviceUrl);
            SaraDbContext = db;
        }
Example #2
0
        public ToneService(IConfiguration configuration)
        {
            _authenticator = new IamAuthenticator(
                apikey: configuration["Watson:ApiKey"]
                );
            _toneAnalyzer = new ToneAnalyzerService("2017-09-21", _authenticator);

            _toneAnalyzer.SetServiceUrl(configuration["Watson:ApiUrl"]);
        }
Example #3
0
        public void Setup()
        {
            //Todo: Refactor this out into a service contract
            //Todo: This key needs to be injected by the build or inject by Ioc
            _authenticator = new IamAuthenticator(
                apikey: "key"
                );
            _toneAnalyzer = new ToneAnalyzerService("2017-09-21", _authenticator);

            //Todo: This key needs to be injected by the build or inject by Ioc
            _toneAnalyzer.SetServiceUrl("https://watson-api-explorer.mybluemix.net/apis/tone-analyzer-v3#!/tone/GetTone");
        }
Example #4
0
        public static IServiceCollection AddToneAnalyzerServices(this IServiceCollection services,
                                                                 IConfiguration configuration)
        {
            services.AddScoped <IToneAnalyzerService>(sp =>
            {
                services.Configure <ToneAnalyzerOptions>(configuration.GetSection(ToneAnalyzerOptions.ToneAnalyzer));
                var toneAnalyzerOptions = configuration.GetSection(ToneAnalyzerOptions.ToneAnalyzer).Get <ToneAnalyzerOptions>();
                var authenticator       = new IamAuthenticator(apikey: toneAnalyzerOptions.ApiKey);
                var toneAnalyzerService = new ToneAnalyzerService(toneAnalyzerOptions.Version, authenticator);
                toneAnalyzerService.SetServiceUrl(toneAnalyzerOptions.Endpoint);

                return(toneAnalyzerService);
            });

            return(services);
        }
Example #5
0
        public ToneAnalysisResultBLDto AnalyzeTextTone(int userId, string textForToneAnalysis)
        {
            IamAuthenticator authenticator = new IamAuthenticator(
                apikey: "");
            var service = new ToneAnalyzerService("", authenticator);

            service.SetServiceUrl("");
            ToneInput toneInput = new ToneInput()
            {
                Text = textForToneAnalysis
            };
            var result = service.Tone(
                toneInput: toneInput
                );
            WatsonResponseBLDto     watsonResponse = JsonConvert.DeserializeObject <WatsonResponseBLDto>(result.Response);
            ToneAnalysisResultBLDto analysisResult = new ToneAnalysisResultBLDto {
                UserID           = userId,
                Date             = DateTime.Now,
                Text             = textForToneAnalysis,
                DetectedEmotions = watsonResponse.document_tone.tones
            };
            double emotionScore      = 0;
            string prevailingEmotion = "";

            foreach (var emotion in analysisResult.DetectedEmotions)
            {
                if (emotion.score > emotionScore)
                {
                    emotionScore      = emotion.score;
                    prevailingEmotion = emotion.tone_name;
                }
            }
            foreach (KeyValuePair <string, List <string> > emotionType in GetListOfEmotions())
            {
                if (emotionType.Value.Contains(prevailingEmotion))
                {
                    analysisResult.PrevailingEmotion = emotionType.Key;
                }
            }
            if (analysisResult.PrevailingEmotion == null)
            {
                analysisResult.PrevailingEmotion = "OtherEmotion";
            }
            return(analysisResult);
        }
        public string AnalyseText(string text)
        {
            IamAuthenticator authenticator = new IamAuthenticator(apikey: ApiKey);

            ToneAnalyzerService toneAnalyzer = new ToneAnalyzerService("2017-09-21", authenticator);

            toneAnalyzer.SetServiceUrl(ApiEndpoint);

            ToneInput toneInput = new ToneInput()
            {
                Text = text
            };

            var result = toneAnalyzer.Tone(toneInput: toneInput);

            string deserializedResult = DeserializeResult(result.Response);

            return(deserializedResult);
        }
Example #7
0
        public void Tone()
        {
            IamAuthenticator authenticator = new IamAuthenticator(
                apikey: "{apikey}");

            ToneAnalyzerService service = new ToneAnalyzerService("2017-09-21", authenticator);

            service.SetServiceUrl("{serviceUrl}");

            ToneInput toneInput = new ToneInput()
            {
                Text = "Team, I know that times are tough! Product sales have been disappointing for the past three quarters. We have a competitive product, but we need to do a better job of selling it!"
            };

            var result = service.Tone(
                toneInput: toneInput
                );

            Console.WriteLine(result.Response);
        }
Example #8
0
        public void Tone()
        {
            IamAuthenticator authenticator = new IamAuthenticator(
                apikey: "{apikey}");

            ToneAnalyzerService service = new ToneAnalyzerService("2017-09-21", authenticator);

            service.SetServiceUrl("{serviceUrl}");

            byte[] bytes = Encoding.ASCII.GetBytes("Team, I know that times are tough! " +
                                                   "Product sales have been disappointing for the past three quarters. " +
                                                   "We have a competitive product, but we need to do a better job of selling it!");
            MemoryStream toneInput = new MemoryStream(bytes);


            var result = service.Tone(
                toneInput: toneInput
                );

            Console.WriteLine(result.Response);
        }
        private IEnumerator CreateService()
        {
            if (string.IsNullOrEmpty(iamApikey))
            {
                throw new IBMException("Plesae provide IAM ApiKey for the service.");
            }

            //  Create credential and instantiate service
            IamAuthenticator authenticator = new IamAuthenticator(apikey: iamApikey);

            //  Wait for tokendata
            while (!authenticator.CanAuthenticate())
            {
                yield return(null);
            }

            service = new ToneAnalyzerService(versionDate, authenticator);
            service.SetServiceUrl(serviceUrl);

            Runnable.Run(Examples());
        }
Example #10
0
        public void ToneChat()
        {
            IamAuthenticator authenticator = new IamAuthenticator(
                apikey: "{apikey}");

            ToneAnalyzerService service = new ToneAnalyzerService("2017-09-21", authenticator);

            service.SetServiceUrl("{serviceUrl}");

            var utterances = new List <Utterance>()
            {
                new Utterance()
                {
                    Text = "Hello, I'm having a problem with your product.",
                    User = "******"
                },
                new Utterance()
                {
                    Text = "OK, let me know what's going on, please.",
                    User = "******"
                },
                new Utterance()
                {
                    Text = "Well, nothing is working :(",
                    User = "******"
                },
                new Utterance()
                {
                    Text = "Sorry to hear that.",
                    User = "******"
                }
            };

            var result = service.ToneChat(
                utterances: utterances
                );

            Console.WriteLine(result.Response);
        }