Guest User

Untitled

a guest
May 20th, 2018
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. [Serializable]
  2. public class BasicLuisDialog : LuisDialog<object>
  3. {
  4.  
  5. public BasicLuisDialog() : base(new LuisService(new LuisModelAttribute(
  6. ConfigurationManager.AppSettings["LuisAppId"],
  7. ConfigurationManager.AppSettings["LuisAPIKey"],
  8. domain: ConfigurationManager.AppSettings["LuisAPIHostName"])))
  9. {
  10. }
  11. //Constants
  12.  
  13. // Intents
  14. public const string IntentContractualPower = "SearchContractualPower";
  15. public const string IntentNone = "None";
  16.  
  17. // Entities
  18. public const string EntityEAN = "EAN";
  19. public const string EntityBeginDate = "BeginDate";
  20.  
  21. // Entities found in result
  22. public string BotEntityRecognition(LuisResult result)
  23. {
  24. StringBuilder entityResults = new StringBuilder();
  25.  
  26. if (result.Entities.Count > 0)
  27. {
  28. foreach (EntityRecommendation item in result.Entities)
  29. {
  30. entityResults.Append(item.Type + "=" + item.Entity + ",");
  31. }
  32. // remove last comma
  33. entityResults.Remove(entityResults.Length - 1, 1);
  34. }
  35.  
  36. return entityResults.ToString();
  37. }
  38.  
  39. [LuisIntent("SearchContractualPower")]
  40. public async Task ContractualPowerIntent(IDialogContext context, LuisResult result)
  41. {
  42. await ContractualPower(context, result);
  43. }
  44.  
  45. private Task ContractualPower(IDialogContext context, LuisResult result)
  46. {
  47. throw new NotImplementedException();
  48. }
  49.  
  50. private void ContractualPower(object sender, EventArgs e)
  51. {
  52. try
  53. {
  54. SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
  55. builder.DataSource = "X";
  56. builder.UserID = "X";
  57. builder.Password = "X";
  58. builder.InitialCatalog = "ChatBot_EDWErrors";
  59.  
  60. using (SqlConnection connection = new SqlConnection(builder.ConnectionString))
  61. {
  62.  
  63. connection.Open();
  64. StringBuilder sb = new StringBuilder();
  65. sb.Append("SELECT ErrorDescription FROM [err].[ErrorFactContractualPower]");
  66. sb.Append(" WHERE EAN = ");
  67. sb.Append(EntityEAN);
  68. sb.Append(" AND BeginDate = ");
  69. sb.Append(EntityEAN);
  70.  
  71. String sql = sb.ToString();
  72.  
  73. using (SqlCommand command = new SqlCommand(sql, connection))
  74. {
  75. using (SqlDataReader reader = command.ExecuteReader())
  76. {
  77. while (reader.Read())
  78. {
  79. Console.WriteLine("{0}", reader.GetString(0));
  80. }
  81. }
  82. }
  83. }
  84. }
  85. catch (SqlException exception)
  86. {
  87. Console.WriteLine(exception.ToString());
  88. }
  89. Console.ReadLine();
  90.  
  91. }
  92.  
  93. [LuisIntent("None")]
  94. public async Task NoneIntent(IDialogContext context, LuisResult result)
  95. {
  96. await this.ShowLuisResult(context, result);
  97. }
  98.  
  99. private async Task ShowLuisResult(IDialogContext context, LuisResult result)
  100. {
  101. // get recognized entities
  102. string entities = this.BotEntityRecognition(result);
  103.  
  104. // round number
  105. string roundedScore = result.Intents[0].Score != null ? (Math.Round(result.Intents[0].Score.Value, 2).ToString()) : "0";
  106.  
  107. await context.PostAsync($"**Query**: {result.Query}, **Intent**: {result.Intents[0].Intent}, **Score**: {roundedScore}. **Entities**: {entities}");
  108. context.Wait(MessageReceived);
  109. }
  110. }
Add Comment
Please, Sign In to add comment