public bool GetLuisAnswer(string pText, ref LuisOutputResponse pResponse) { log.Debug("-->GetLuisAnswer()"); pResponse = new LuisOutputResponse(); bool Status = false; string Json = string.Empty; try { log.Debug(string.Format("************************")); log.Debug(string.Format(" > pText[{0}]", pText)); var url = "https://westus.api.cognitive.microsoft.com/luis/v2.0/apps/bb0c5997-06bf-49d6-8499-f2782d4c2ec6?subscription-key=2797d44c476a440fb259a71880564f4f&staging=true&verbose=true&timezoneOffset=1.0&q="; log.Debug(string.Format(" > url[{0}]", url)); url = string.Format("{0}{1}", url, pText); log.Debug(string.Format(" > Req[{0}]", url)); log.Debug(string.Format("************************")); log.Debug(string.Format(" --> System.Net.WebRequest.Create({0})", url)); var webrequest = (HttpWebRequest)System.Net.WebRequest.Create(url); log.Debug(string.Format(" <-- System.Net.WebRequest.Create()")); log.Debug(string.Format(" --> webrequest.GetResponse()")); using (var response = webrequest.GetResponse()) using (var reader = new StreamReader(response.GetResponseStream())) { log.Debug(string.Format(" --> reader.ReadToEnd()")); var result = reader.ReadToEnd(); log.Debug(string.Format(" <-- reader.ReadToEnd()")); log.Debug(string.Format(" --> Convert.ToString from Json()")); Json = Convert.ToString(result); log.Debug(string.Format(" <-- Convert.ToString from Json()")); log.Debug(Json); log.Debug(string.Format(" --> ParseFromLuis.FromJson(Json)")); ParseFromLuis LuisResponse = new ParseFromLuis(); LuisResponse = ParseFromLuis.FromJson(Json); log.Debug(string.Format(" <-- ParseFromLuis.FromJson(Json)")); log.Debug(string.Format(" <-- LuisResponse.Query({0})", LuisResponse.Query)); pResponse.query = LuisResponse.Query; log.Debug(string.Format(" --> pResponse.topScoringIntent()")); pResponse.topScoringIntent = LuisResponse.TopScoringIntent.Intent; log.Debug(string.Format(" <-- pResponse.topScoringIntent({0})", pResponse.topScoringIntent.ToString())); log.Debug(string.Format(" --> pResponse.topScoringScore()")); pResponse.topScoringScore = LuisResponse.TopScoringIntent.Score; log.Debug(string.Format(" <-- pResponse.topScoringScore()", pResponse.topScoringScore)); foreach (Entity MyEntity in LuisResponse.Entities) { if (MyEntity.Type == "Localizacion::Origen") { pResponse.EntityFrom = MyEntity.PurpleEntity; pResponse.EntityFromScore = MyEntity.Score; pResponse.EntityFromType = MyEntity.Type; } if (MyEntity.Type == "Localizacion::Destino") { pResponse.EntityTo = MyEntity.PurpleEntity; pResponse.EntityToScore = MyEntity.Score; pResponse.EntityType = MyEntity.Type; } } Status = true; } log.Debug(string.Format(" <-- webrequest.GetResponse()")); } catch (Exception ex) { log.Error(ex); } finally { log.Debug(string.Format("<--GetLuisAnswer({0})", Status.ToString())); } return(Status); }
public static string ToJson(this ParseFromLuis self) { return(JsonConvert.SerializeObject(self, Converter.Settings)); }