public JsonResult ProcesarWatsonNLU(String texto) { object result = null; try{ NaturalLanguageUnderstandingService natural_language_und = new NaturalLanguageUnderstandingService(Auth.Watson_NLU.Username, Auth.Watson_NLU.Password, Auth.Watson_NLU.VersionDate); Parameters parameters = new Parameters() { Text = texto, Features = new Features() { Keywords = new KeywordsOptions() { Limit = 8, Sentiment = true, Emotion = true }, Entities = new EntitiesOptions() { Limit = 8, Sentiment = true, Emotion = true }, SemanticRoles = new SemanticRolesOptions() { } } }; var resultado = natural_language_und.Analyze(parameters); var rolesSemanticos = resultado.SemanticRoles; bool accionAbrir = false; bool word = false; foreach (var item in rolesSemanticos) { if (item.Action != null) { if (item.Action.Text != null) { string accion = item.Action.Text.ToLower(); if (accion.Contains("abre") || accion.Contains("abrir") || accion.Contains("ejecutar") || accion.Contains("lanzar")) { accionAbrir = true; } } } if (item.Subject != null) { if (item.Subject.Text != null) { string sujeto = item.Subject.Text.ToLower(); if (sujeto.Contains("word")) { word = true; } } } if (item._Object != null) { if (item._Object.Text != null) { string objeto = item._Object.Text.ToLower(); if (objeto.Contains("word")) { word = true; } } } if (word && accionAbrir) { break; } } if (word && accionAbrir) { Process.Start(ExcecutablePaths.MicrosoftWord); } result = new { data = resultado, status = "Ok" }; }catch (Exception ex) { result = new { data = ex.Message, status = "Error" }; } return(Json(result)); }
public void Analyze_Success() { IClient client = CreateClient(); IRequest request = Substitute.For <IRequest>(); client.PostAsync(Arg.Any <string>()) .Returns(request); #region Response AnalysisResults response = new AnalysisResults() { Language = "en", AnalyzedText = "testText", RetrievedUrl = "retrivedUrl", Usage = new Usage() { Features = 1 }, Concepts = new List <ConceptsResult>() { new ConceptsResult() { Text = "text", Relevance = 1.0f, DbpediaResource = "dbpediaResouce" } }, Entities = new List <EntitiesResult>() { new EntitiesResult() { Type = "type", Relevance = 1.0f, Count = 1, Text = "text", Emotion = new EmotionScores() { Anger = 1.0f, Disgust = 1.0f, Fear = 1.0f, Joy = 1.0f, Sadness = 1.0f }, Sentiment = new FeatureSentimentResults() { Score = 1.0f }, Disambiguation = new DisambiguationResult() { Name = "name", DbpediaResource = "dbpediaResource", Subtype = new List <string>() { "subtype" } } } }, Keywords = new List <KeywordsResult>() { new KeywordsResult() { Relevance = 1.0f, Text = "text", Emotion = new EmotionScores() { Anger = 1.0f, Disgust = 1.0f, Fear = 1.0f, Joy = 1.0f, Sadness = 1.0f }, Sentiment = new FeatureSentimentResults() { Score = 1.0f } } }, Categories = new List <CategoriesResult>() { new CategoriesResult() { Label = "label", Score = 1.0f } }, Emotion = new EmotionResult() { Document = new DocumentEmotionResults() { Emotion = new EmotionScores() { Anger = 1.0f, Disgust = 1.0f, Fear = 1.0f, Joy = 1.0f, Sadness = 1.0f }, }, Targets = new List <TargetedEmotionResults>() { new TargetedEmotionResults() { Emotion = new EmotionScores() { Anger = 1.0f, Disgust = 1.0f, Fear = 1.0f, Joy = 1.0f, Sadness = 1.0f }, Text = "text" } } }, Metadata = new MetadataResult() { Authors = new List <Author>() { new Author() { Name = "name" } }, PublicationDate = "publicationDate", Title = "title" }, Relations = new List <RelationsResult>() { new RelationsResult() { Score = 1.0f, Sentence = "sentence", Type = "type", Arguments = new List <RelationArgument>() { new RelationArgument() { Entities = new List <RelationEntity>() { new RelationEntity() { Text = "text", Type = "type" } }, Text = "text" } } } }, SemanticRoles = new List <SemanticRolesResult>() { new SemanticRolesResult() { Sentence = "sentence", Subject = new SemanticRolesSubject() { Text = "text", Entities = new List <SemanticRolesEntity>() { new SemanticRolesEntity() { Text = "text", Type = "type" } }, Keywords = new List <SemanticRolesKeyword>() { new SemanticRolesKeyword() { Text = "text" } } }, Action = new SemanticRolesAction() { Normalized = "normalized", Text = "text", Verb = new SemanticRolesVerb() { Text = "text", Tense = "tense" } }, _Object = new SemanticRolesObject() { Text = "text", Keywords = new List <SemanticRolesKeyword>() { new SemanticRolesKeyword() { Text = "text" } } } } }, Sentiment = new SentimentResult() { Document = new DocumentSentimentResults() { Score = 1.0f }, Targets = new List <TargetedSentimentResults>() { new TargetedSentimentResults() { Score = 1.0f, Text = "text" } } } }; #endregion #region parameters Parameters parameters = new Parameters() { Text = "text", Html = "html", Url = "url", Features = new Features() { Concepts = new ConceptsOptions() { Limit = 1 }, Emotion = new EmotionOptions() { Document = true, Targets = new List <string>() { "target" } }, Entities = new EntitiesOptions() { Model = "model", Sentiment = true, Emotion = true, Limit = 1 }, Keywords = new KeywordsOptions() { Limit = 1, Sentiment = true, Emotion = true }, Metadata = new MetadataOptions() { }, Relations = new RelationsOptions() { Model = "model" }, SemanticRoles = new SemanticRolesOptions() { Limit = 1, Keywords = true, Entities = true }, Sentiment = new SentimentOptions() { Document = true, Targets = new List <string>() { "target" } }, Categories = new CategoriesOptions() { } }, Clean = true, Xpath = "xpath", FallbackToRaw = true, ReturnAnalyzedText = true, Language = "en" }; #endregion request.WithArgument(Arg.Any <string>(), Arg.Any <string>()) .Returns(request); request.WithBody <Parameters>(parameters) .Returns(request); request.As <AnalysisResults>() .Returns(Task.FromResult(response)); NaturalLanguageUnderstandingService service = new NaturalLanguageUnderstandingService(client); service.VersionDate = "versionDate"; var result = service.Analyze(parameters); Assert.IsNotNull(result); client.Received().PostAsync(Arg.Any <string>()); Assert.IsTrue(result.Language == "en"); Assert.IsTrue(result.AnalyzedText == "testText"); }
public void Analyze_Success() { IClient client = CreateClient(); IRequest request = Substitute.For <IRequest>(); client.PostAsync(Arg.Any <string>()) .Returns(request); #region Response var response = new DetailedResponse <AnalysisResults>() { Result = new AnalysisResults() { Language = "en", AnalyzedText = "testText", RetrievedUrl = "retrivedUrl", Usage = new AnalysisResultsUsage() { Features = 1 }, Concepts = new List <ConceptsResult>() { new ConceptsResult() { Text = "text", Relevance = 1.0f, DbpediaResource = "dbpediaResouce" } }, Entities = new List <EntitiesResult>() { new EntitiesResult() { Type = "type", Relevance = 1.0f, Count = 1, Text = "text", Emotion = new EmotionScores() { Anger = 1.0f, Disgust = 1.0f, Fear = 1.0f, Joy = 1.0f, Sadness = 1.0f }, Sentiment = new FeatureSentimentResults() { Score = 1.0f }, Disambiguation = new DisambiguationResult() { Name = "name", DbpediaResource = "dbpediaResource", Subtype = new List <string>() { "subtype" } } } }, Keywords = new List <KeywordsResult>() { new KeywordsResult() { Relevance = 1.0f, Text = "text", Emotion = new EmotionScores() { Anger = 1.0f, Disgust = 1.0f, Fear = 1.0f, Joy = 1.0f, Sadness = 1.0f }, Sentiment = new FeatureSentimentResults() { Score = 1.0f } } }, Categories = new List <CategoriesResult>() { new CategoriesResult() { Label = "label", Score = 1.0f } }, Emotion = new EmotionResult() { Document = new DocumentEmotionResults() { Emotion = new EmotionScores() { Anger = 1.0f, Disgust = 1.0f, Fear = 1.0f, Joy = 1.0f, Sadness = 1.0f }, }, Targets = new List <TargetedEmotionResults>() { new TargetedEmotionResults() { Emotion = new EmotionScores() { Anger = 1.0f, Disgust = 1.0f, Fear = 1.0f, Joy = 1.0f, Sadness = 1.0f }, Text = "text" } } }, Metadata = new AnalysisResultsMetadata() { Authors = new List <Author>() { new Author() { Name = "name" } }, PublicationDate = "publicationDate", Title = "title" }, Relations = new List <RelationsResult>() { new RelationsResult() { Score = 1.0f, Sentence = "sentence", Type = "type", Arguments = new List <RelationArgument>() { new RelationArgument() { Entities = new List <RelationEntity>() { new RelationEntity() { Text = "text", Type = "type" } }, Text = "text" } } } }, SemanticRoles = new List <SemanticRolesResult>() { new SemanticRolesResult() { Sentence = "sentence", Subject = new SemanticRolesResultSubject() { Text = "text", Entities = new List <SemanticRolesEntity>() { new SemanticRolesEntity() { Text = "text", Type = "type" } }, Keywords = new List <SemanticRolesKeyword>() { new SemanticRolesKeyword() { Text = "text" } } }, Action = new SemanticRolesResultAction() { Normalized = "normalized", Text = "text", Verb = new SemanticRolesVerb() { Text = "text", Tense = "tense" } }, _Object = new SemanticRolesResultObject() { Text = "text", Keywords = new List <SemanticRolesKeyword>() { new SemanticRolesKeyword() { Text = "text" } } } } }, Sentiment = new SentimentResult() { Document = new DocumentSentimentResults() { Score = 1.0f }, Targets = new List <TargetedSentimentResults>() { new TargetedSentimentResults() { Score = 1.0f, Text = "text" } } } } }; #endregion request.WithArgument(Arg.Any <string>(), Arg.Any <string>()) .Returns(request); request.WithBodyContent(new StringContent("features")) .Returns(request); request.As <AnalysisResults>() .Returns(Task.FromResult(response)); NaturalLanguageUnderstandingService service = new NaturalLanguageUnderstandingService(client); service.VersionDate = "versionDate"; var result = service.Analyze(new Features(), "testText", language: "en"); Assert.IsNotNull(result); client.Received().PostAsync(Arg.Any <string>()); Assert.IsTrue(result.Result.Language == "en"); Assert.IsTrue(result.Result.AnalyzedText == "testText"); }
public void Analyze_Success() { var text = nluText; var features = new Features() { Keywords = new KeywordsOptions() { Limit = 8, Sentiment = true, Emotion = true }, Categories = new CategoriesOptions() { Limit = 10 } }; service.WithHeader("X-Watson-Test", "1"); var result = service.Analyze( features: features, text: text, clean: true, fallbackToRaw: true, returnAnalyzedText: true, language: "en" ); Assert.IsNotNull(result); Assert.IsTrue(result.Result.Categories.Count > 0); Assert.IsTrue(result.Result.Keywords.Count > 0); }
public void Analyze_Success() { IClient client = Substitute.For <IClient>(); IRequest request = Substitute.For <IRequest>(); client.PostAsync(Arg.Any <string>()) .Returns(request); NaturalLanguageUnderstandingService service = new NaturalLanguageUnderstandingService(client); var versionDate = "versionDate"; service.VersionDate = versionDate; var features = new Features(); var text = "text"; var html = "html"; var url = "url"; var clean = false; var xpath = "xpath"; var fallbackToRaw = false; var returnAnalyzedText = false; var language = "language"; var limitTextCharacters = 1; var result = service.Analyze(features: features, text: text, html: html, url: url, clean: clean, xpath: xpath, fallbackToRaw: fallbackToRaw, returnAnalyzedText: returnAnalyzedText, language: language, limitTextCharacters: limitTextCharacters); JObject bodyObject = new JObject(); if (features != null) { bodyObject["features"] = JToken.FromObject(features); } if (!string.IsNullOrEmpty(text)) { bodyObject["text"] = JToken.FromObject(text); } if (!string.IsNullOrEmpty(html)) { bodyObject["html"] = JToken.FromObject(html); } if (!string.IsNullOrEmpty(url)) { bodyObject["url"] = JToken.FromObject(url); } bodyObject["clean"] = JToken.FromObject(clean); if (!string.IsNullOrEmpty(xpath)) { bodyObject["xpath"] = JToken.FromObject(xpath); } bodyObject["fallback_to_raw"] = JToken.FromObject(fallbackToRaw); bodyObject["return_analyzed_text"] = JToken.FromObject(returnAnalyzedText); if (!string.IsNullOrEmpty(language)) { bodyObject["language"] = JToken.FromObject(language); } if (limitTextCharacters != null) { bodyObject["limit_text_characters"] = JToken.FromObject(limitTextCharacters); } var json = JsonConvert.SerializeObject(bodyObject); request.Received().WithArgument("version", versionDate); request.Received().WithBodyContent(Arg.Is <StringContent>(x => x.ReadAsStringAsync().Result.Equals(json))); }