public void ToneChat_Success() { IClient client = Substitute.For <IClient>(); IRequest request = Substitute.For <IRequest>(); client.PostAsync(Arg.Any <string>()) .Returns(request); ToneAnalyzerService service = new ToneAnalyzerService(client); var versionDate = "versionDate"; service.VersionDate = versionDate; var utterances = new List <Utterance>(); var contentLanguage = "contentLanguage"; var acceptLanguage = "acceptLanguage"; var result = service.ToneChat(utterances: utterances, contentLanguage: contentLanguage, acceptLanguage: acceptLanguage); JObject bodyObject = new JObject(); if (utterances != null && utterances.Count > 0) { bodyObject["utterances"] = JToken.FromObject(utterances); } var json = JsonConvert.SerializeObject(bodyObject); request.Received().WithArgument("version", versionDate); request.Received().WithBodyContent(Arg.Is <StringContent>(x => x.ReadAsStringAsync().Result.Equals(json))); }
public IEnumerator TestToneChat() { Log.Debug("ToneAnalyzerServiceV3IntegrationTests", "Attempting to ToneChat..."); UtteranceAnalyses toneChatResponse = null; List <Utterance> utterances = new List <Utterance>() { new Utterance() { Text = inputText, User = chatUser } }; service.ToneChat( callback: (DetailedResponse <UtteranceAnalyses> response, IBMError error) => { Log.Debug("ToneAnalyzerServiceV3IntegrationTests", "ToneChat result: {0}", response.Response); toneChatResponse = response.Result; Assert.IsNotNull(toneChatResponse); Assert.IsNotNull(toneChatResponse.UtterancesTone); Assert.IsNull(error); }, utterances: utterances, contentLanguage: "en", acceptLanguage: "en" ); while (toneChatResponse == null) { yield return(null); } }
public void ToneChat() { IamConfig config = new IamConfig( apikey: apikey ); ToneAnalyzerService service = new ToneAnalyzerService(versionDate, config); service.SetEndpoint(url); var utterances = new List <Utterance>() { new Utterance() { Text = "Hello! Welcome to IBM Watson! How can I help you?", User = "******" } }; var result = service.ToneChat( utterances: utterances, contentLanguage: "en-US", acceptLanguage: "en-US" ); Console.WriteLine(result.Response); }
public void ToneChat_Catch_Exception() { #region Mock IClient IClient client = Substitute.For <IClient>(); client.WithAuthentication(Arg.Any <string>(), Arg.Any <string>()) .Returns(client); IRequest request = Substitute.For <IRequest>(); client.PostAsync(Arg.Any <string>()) .Returns(x => { throw new AggregateException(new ServiceResponseException(Substitute.For <IResponse>(), Substitute.For <HttpResponseMessage>(HttpStatusCode.BadRequest), string.Empty)); }); #endregion ToneAnalyzerService service = new ToneAnalyzerService(client); service.VersionDate = versionDate; var utterances = new List <Utterance>() { new Utterance() { Text = "utteranceText", User = "******" } }; service.ToneChat(utterances); }
public void ToneChat() { TokenOptions tokenOptions = new TokenOptions() { IamApiKey = apikey, ServiceUrl = url }; ToneAnalyzerService service = new ToneAnalyzerService(tokenOptions, versionDate); var utterances = new List<Utterance>() { new Utterance() { Text = "Hello! Welcome to IBM Watson! How can I help you?", User = "******" } }; var result = service.ToneChat( utterances: utterances, contentLanguage: "en-US", acceptLanguage: "en-US" ); Console.WriteLine(result.Response); }
public static void Main(string[] args) { string credentials = string.Empty; try { credentials = Utility.SimpleGet( Environment.GetEnvironmentVariable("VCAP_URL"), Environment.GetEnvironmentVariable("VCAP_USERNAME"), Environment.GetEnvironmentVariable("VCAP_PASSWORD")).Result; } catch (Exception e) { Console.WriteLine(string.Format("Failed to get credentials: {0}", e.Message)); } Task.WaitAll(); var vcapServices = JObject.Parse(credentials); var _url = vcapServices["tone_analyzer"]["url"].Value <string>(); var _username = vcapServices["tone_analyzer"]["username"].Value <string>(); var _password = vcapServices["tone_analyzer"]["password"].Value <string>(); var versionDate = "2016-05-19"; ToneAnalyzerService _toneAnalyzer = new ToneAnalyzerService(_username, _password, versionDate); _toneAnalyzer.Endpoint = _url; // Test PostTone ToneInput toneInput = new ToneInput() { Text = "How are you doing? My name is Taj!" }; var postToneResult = _toneAnalyzer.Tone(toneInput, "application/json", null); Console.WriteLine(string.Format("post tone result: {0}", JsonConvert.SerializeObject(postToneResult, Formatting.Indented))); // Test ToneChat ToneChatInput toneChatInput = new ToneChatInput() { Utterances = new List <Utterance>() { new Utterance() { Text = "Hello how are you?" } } }; var toneChatResult = _toneAnalyzer.ToneChat(toneChatInput); Console.WriteLine(string.Format("tone chat result: {0}", JsonConvert.SerializeObject(toneChatResult, Formatting.Indented))); Console.ReadKey(); }
public void ToneChat_Success() { ToneChatInput toneChatInput = new ToneChatInput() { Utterances = new List <Utterance>() { new Utterance() { Text = inputText, User = chatUser } } }; var result = service.ToneChat(toneChatInput); Assert.IsNotNull(result); Assert.IsTrue(result.UtterancesTone.Count > 0); }
private UtteranceAnalyses ToneChat(ToneChatInput utterances, string contentLanguage = null, string acceptLanguage = null) { Console.WriteLine("\nAttempting to ToneChat()"); var result = _service.ToneChat(utterances: utterances, contentLanguage: contentLanguage, acceptLanguage: acceptLanguage); if (result != null) { Console.WriteLine("ToneChat() succeeded:\n{0}", JsonConvert.SerializeObject(result, Formatting.Indented)); } else { Console.WriteLine("Failed to ToneChat()"); } return(result); }
public void ToneChat_Empty_Version() { ToneAnalyzerService service = new ToneAnalyzerService(versionDate, new NoAuthAuthenticator()); service.VersionDate = null; var utterances = new List <Utterance>() { new Utterance() { Text = "utteranceText", User = "******" } }; service.ToneChat(utterances); }
public void ToneChat_Empty_Version() { ToneAnalyzerService service = new ToneAnalyzerService("username", "password", versionDate); service.VersionDate = null; var utterances = new List <Utterance>() { new Utterance() { Text = "utteranceText", User = "******" } }; service.ToneChat(utterances); }
public void ToneChat_Success() { _service = new ToneAnalyzerService(_username, _password, versionDate); ToneChatInput toneChatInput = new ToneChatInput() { Utterances = new List <Utterance>() { new Utterance() { Text = inputText, User = chatUser } } }; var result = _service.ToneChat(toneChatInput); Assert.IsNotNull(result); Assert.IsTrue(result.UtterancesTone.Count > 0); }
public void ToneChat_Success() { var utterances = new List <Utterance>() { new Utterance() { Text = inputText, User = chatUser } }; service.WithHeader("X-Watson-Test", "1"); var result = service.ToneChat( utterances: utterances, contentLanguage: "en-US", acceptLanguage: "en-US" ); Assert.IsNotNull(result.Result); Assert.IsTrue(result.Result.UtterancesTone.Count > 0); }
public static void Main(string[] args) { var environmentVariable = Environment.GetEnvironmentVariable("VCAP_SERVICES"); var fileContent = File.ReadAllText(environmentVariable); var vcapServices = JObject.Parse(fileContent); var _username = vcapServices["tone_analyzer"][0]["credentials"]["username"]; var _password = vcapServices["tone_analyzer"][0]["credentials"]["password"]; string versionDate = "2016-05-19"; ToneAnalyzerService _toneAnalyzer = new ToneAnalyzerService(_username.ToString(), _password.ToString(), versionDate); // Test PostTone ToneInput toneInput = new ToneInput() { Text = "How are you doing? My name is Taj!" }; var postToneResult = _toneAnalyzer.Tone(toneInput, null, null); Console.WriteLine(string.Format("postToneResult: {0}", postToneResult.SentencesTone)); // Test ToneChat ToneChatInput toneChatInput = new ToneChatInput() { Utterances = new List <Utterance>() { new Utterance() { Text = "Hello how are you?" } } }; var toneChatResult = _toneAnalyzer.ToneChat(toneChatInput); Console.WriteLine(string.Format("toneChatResult: {0}", toneChatResult)); Console.ReadKey(); }
private IEnumerator Examples() { ToneInput toneInput = new ToneInput() { Text = stringToTestTone }; List <string> tones = new List <string>() { "emotion", "language", "social" }; service.Tone(callback: OnTone, toneInput: toneInput, sentences: true, tones: tones, contentLanguage: "en", acceptLanguage: "en", contentType: "application/json"); while (!toneTested) { yield return(null); } List <Utterance> utterances = new List <Utterance>() { new Utterance() { Text = stringToTestTone, User = "******" } }; service.ToneChat(callback: OnToneChat, utterances: utterances, contentLanguage: "en", acceptLanguage: "en"); while (!toneChatTested) { yield return(null); } Log.Debug("ExampleToneAnalyzerV3.Examples()", "Examples complete!"); }
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); }
public void ToneChat_ToneChatInputEmpty() { ToneAnalyzerService service = new ToneAnalyzerService(versionDate, new NoAuthAuthenticator()); var result = service.ToneChat(null); }
public void ToneChat_ToneChatInputEmpty() { ToneAnalyzerService service = new ToneAnalyzerService("username", "password", versionDate); var result = service.ToneChat(null); }
public void ToneChat_Success_With_ToneChatInput() { IClient client = CreateClient(); #region response var response = new DetailedResponse <UtteranceAnalyses>() { Result = new UtteranceAnalyses() { UtterancesTone = new List <UtteranceAnalysis>() { new UtteranceAnalysis() { UtteranceId = 100, UtteranceText = "utteranceText", Tones = new List <ToneChatScore>() { new ToneChatScore() { ToneName = "string", ToneId = ToneChatScore.ToneIdEnumValue.SAD, Score = 0 } } } } } }; #endregion var utterances = new List <Utterance>() { new Utterance() { Text = "utteranceText", User = "******" } }; IRequest request = Substitute.For <IRequest>(); client.PostAsync(Arg.Any <string>()) .Returns(request); request.WithArgument(Arg.Any <string>(), Arg.Any <string>()) .Returns(request); request.WithHeader(Arg.Any <string>(), Arg.Any <string>()) .Returns(request); request.WithBodyContent(Arg.Any <StringContent>()) .Returns(request); request.As <UtteranceAnalyses>() .Returns(Task.FromResult(response)); ToneAnalyzerService service = new ToneAnalyzerService(client); service.VersionDate = versionDate; var result = service.ToneChat(utterances); Assert.IsNotNull(result); client.Received().PostAsync(Arg.Any <string>()); Assert.IsTrue(result.Result.UtterancesTone.Count >= 1); Assert.IsTrue(result.Result.UtterancesTone[0].Tones.Count >= 1); Assert.IsTrue(result.Result.UtterancesTone[0].Tones[0].ToneName == "string"); }
public static void Main(string[] args) { string credentials = string.Empty; #region Get Credentials string _endpoint = string.Empty; string _username = string.Empty; string _password = string.Empty; if (string.IsNullOrEmpty(credentials)) { var parentDirectory = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.Parent.Parent.Parent.FullName; string credentialsFilepath = parentDirectory + Path.DirectorySeparatorChar + "sdk-credentials" + Path.DirectorySeparatorChar + "credentials.json"; if (File.Exists(credentialsFilepath)) { try { credentials = File.ReadAllText(credentialsFilepath); credentials = Utility.AddTopLevelObjectToJson(credentials, "VCAP_SERVICES"); } catch (Exception e) { throw new Exception(string.Format("Failed to load credentials: {0}", e.Message)); } VcapCredentials vcapCredentials = JsonConvert.DeserializeObject <VcapCredentials>(credentials); var vcapServices = JObject.Parse(credentials); Credential credential = vcapCredentials.GetCredentialByname("tone-analyzer-sdk")[0].Credentials; _endpoint = credential.Url; _username = credential.Username; _password = credential.Password; } else { Console.WriteLine("Credentials file does not exist. Please define credentials."); _username = ""; _password = ""; _endpoint = ""; } } #endregion var versionDate = "2016-05-19"; ToneAnalyzerService _toneAnalyzer = new ToneAnalyzerService(_username, _password, versionDate); _toneAnalyzer.SetEndpoint(_endpoint); // Test PostTone ToneInput toneInput = new ToneInput() { Text = "How are you doing? My name is Taj!" }; var postToneResult = _toneAnalyzer.Tone(toneInput, "application/json", null); Console.WriteLine(string.Format("post tone result: {0}", JsonConvert.SerializeObject(postToneResult, Formatting.Indented))); // Test ToneChat ToneChatInput toneChatInput = new ToneChatInput() { Utterances = new List <Utterance>() { new Utterance() { Text = "Hello how are you?" } } }; var toneChatResult = _toneAnalyzer.ToneChat(toneChatInput); Console.WriteLine(string.Format("tone chat result: {0}", JsonConvert.SerializeObject(toneChatResult, Formatting.Indented))); Console.ReadKey(); }