0306 csharp bot tut 1 (#96)
* cscharp bot fix for empty entity object * removing unreferenced intent
This commit is contained in:
Родитель
cfbf2675aa
Коммит
133e677f8e
|
@ -0,0 +1,116 @@
|
|||
using System;
|
||||
using System.Configuration;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using Microsoft.Bot.Builder.Dialogs;
|
||||
using Microsoft.Bot.Builder.Luis;
|
||||
using Microsoft.Bot.Builder.Luis.Models;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
|
||||
using Microsoft.ApplicationInsights;
|
||||
|
||||
namespace Microsoft.Bot.Sample.LuisBot
|
||||
{
|
||||
// For more information about this template visit http://aka.ms/azurebots-csharp-luis
|
||||
[Serializable]
|
||||
public class BasicLuisDialog : LuisDialog<object>
|
||||
{
|
||||
// CONSTANTS
|
||||
// Entity
|
||||
public const string Entity_Device = "HomeAutomation.Device";
|
||||
public const string Entity_Room = "HomeAutomation.Room";
|
||||
public const string Entity_Operation = "HomeAutomation.Operation";
|
||||
|
||||
// Intents
|
||||
public const string Intent_TurnOn = "HomeAutomation.TurnOn";
|
||||
public const string Intent_TurnOff = "HomeAutomation.TurnOff";
|
||||
public const string Intent_None = "None";
|
||||
|
||||
public BasicLuisDialog() : base(new LuisService(new LuisModelAttribute(
|
||||
ConfigurationManager.AppSettings["LuisAppId"],
|
||||
ConfigurationManager.AppSettings["LuisAPIKey"],
|
||||
domain: ConfigurationManager.AppSettings["LuisAPIHostName"])))
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
// Entities found in result
|
||||
public string BotEntityRecognition(LuisResult result)
|
||||
{
|
||||
StringBuilder entityResults = new StringBuilder();
|
||||
|
||||
if(result.Entities.Count>0)
|
||||
{
|
||||
foreach (EntityRecommendation item in result.Entities)
|
||||
{
|
||||
// Query: Turn on the [light]
|
||||
// item.Type = "HomeAutomation.Device"
|
||||
// item.Entity = "light"
|
||||
entityResults.Append(item.Type + "=" + item.Entity + ",");
|
||||
}
|
||||
// remove last comma
|
||||
entityResults.Remove(entityResults.Length - 1, 1);
|
||||
}
|
||||
|
||||
return entityResults.ToString();
|
||||
}
|
||||
|
||||
public void LogToApplicationInsights(LuisResult result)
|
||||
{
|
||||
TelemetryClient telemetry = new TelemetryClient();
|
||||
telemetry.Context.InstrumentationKey = ConfigurationManager.AppSettings["BotDevAppInsightsKey"];
|
||||
|
||||
Dictionary<string, string> logProperties = new Dictionary<string, string>();
|
||||
logProperties.Add("query", result.Query);
|
||||
logProperties.Add("topScoringIntent", result.TopScoringIntent.Intent);
|
||||
logProperties.Add("topScoringIntentScore", result.TopScoringIntent.Score.ToString());
|
||||
|
||||
int i=1;
|
||||
if(result.Entities.Count>0)
|
||||
{
|
||||
foreach (EntityRecommendation item in result.Entities)
|
||||
{
|
||||
// Query: Turn on the [light]
|
||||
// item.Type = "HomeAutomation.Device"
|
||||
// item.Entity = "light"
|
||||
logProperties.Add("entity_" + i + "_" + item.Type, item.Entity);
|
||||
}
|
||||
}
|
||||
|
||||
telemetry.TrackTrace("LUIS", ApplicationInsights.DataContracts.SeverityLevel.Information, logProperties);
|
||||
}
|
||||
|
||||
[LuisIntent(Intent_None)]
|
||||
public async Task NoneIntent(IDialogContext context, LuisResult result)
|
||||
{
|
||||
await this.ShowLuisResult(context, result);
|
||||
}
|
||||
|
||||
[LuisIntent(Intent_TurnOn)]
|
||||
public async Task OnIntent(IDialogContext context, LuisResult result)
|
||||
{
|
||||
await this.ShowLuisResult(context, result);
|
||||
}
|
||||
|
||||
[LuisIntent(Intent_TurnOff)]
|
||||
public async Task OffIntent(IDialogContext context, LuisResult result)
|
||||
{
|
||||
await this.ShowLuisResult(context, result);
|
||||
}
|
||||
|
||||
private async Task ShowLuisResult(IDialogContext context, LuisResult result)
|
||||
{
|
||||
LogToApplicationInsights(result);
|
||||
|
||||
// get recognized entities
|
||||
string entities = this.BotEntityRecognition(result);
|
||||
|
||||
// round number
|
||||
string roundedScore = result.Intents[0].Score != null ? (Math.Round(result.Intents[0].Score.Value, 2).ToString()) : "0";
|
||||
|
||||
await context.PostAsync($"**Query**: {result.Query}, **Intent**: {result.Intents[0].Intent}, **Score**: {roundedScore}. **Entities**: {entities}");
|
||||
context.Wait(MessageReceived);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -32,21 +32,23 @@ namespace Microsoft.Bot.Sample.LuisBot
|
|||
public const string Intent_None = "None";
|
||||
|
||||
// Entities found in result
|
||||
public string BotEntityRecognition(string intentName, LuisResult result)
|
||||
public string BotEntityRecognition(LuisResult result)
|
||||
{
|
||||
IList<EntityRecommendation> listOfEntitiesFound = result.Entities;
|
||||
StringBuilder entityResults = new StringBuilder();
|
||||
|
||||
foreach (EntityRecommendation item in listOfEntitiesFound)
|
||||
|
||||
if(result.Entities.Count>0)
|
||||
{
|
||||
// Query: Turn on the [light]
|
||||
// item.Type = "HomeAutomation.Device"
|
||||
// item.Entity = "light"
|
||||
entityResults.Append(item.Type.Replace("HomeAutomation.","") + "=" + item.Entity + ",");
|
||||
foreach (EntityRecommendation item in result.Entities)
|
||||
{
|
||||
// Query: Turn on the [light]
|
||||
// item.Type = "HomeAutomation.Device"
|
||||
// item.Entity = "light"
|
||||
entityResults.Append(item.Type + "=" + item.Entity + ",");
|
||||
}
|
||||
// remove last comma
|
||||
entityResults.Remove(entityResults.Length - 1, 1);
|
||||
}
|
||||
// remove last comma
|
||||
entityResults.Remove(entityResults.Length - 1, 1);
|
||||
|
||||
|
||||
return entityResults.ToString();
|
||||
}
|
||||
|
||||
|
@ -71,7 +73,7 @@ namespace Microsoft.Bot.Sample.LuisBot
|
|||
private async Task ShowLuisResult(IDialogContext context, LuisResult result)
|
||||
{
|
||||
// get recognized entities
|
||||
string entities = this.BotEntityRecognition(Intent_TurnOff, result);
|
||||
string entities = this.BotEntityRecognition(result);
|
||||
|
||||
// round number
|
||||
string roundedScore = result.Intents[0].Score != null ? (Math.Round(result.Intents[0].Score.Value, 2).ToString()) : "0";
|
||||
|
|
Загрузка…
Ссылка в новой задаче