Intent, Intent Expression

This commit is contained in:
haiping008 2017-12-18 07:31:15 -06:00
parent 09c6ca862d
commit 7b05297c30
11 changed files with 171 additions and 47 deletions

View file

@ -1,8 +1,10 @@
using CustomEntityFoundation.Entities;
using Bot.Rasa.Intents;
using CustomEntityFoundation.Entities;
using EntityFrameworkCore.BootKit;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text;
namespace Bot.Rasa.Agents
@ -11,5 +13,8 @@ namespace Bot.Rasa.Agents
{
[MaxLength(64)]
public String Name { get; set; }
[ForeignKey("AgentId")]
public List<RasaIntent> Intents { get; set; }
}
}

View file

@ -0,0 +1,36 @@
using Bot.Rasa.Models;
using CustomEntityFoundation;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Bot.Rasa.Agents
{
public static class AgentExtension
{
public static RasaTrainingData GrabCorpus(this RasaAgent agent, EntityDbContext dc)
{
var trainingData = new RasaTrainingData
{
UserSays = new List<UserSay>()
};
var intents = dc.Intent().Include(x => x.Expressions).ToList();
intents.ForEach(intent => {
trainingData.UserSays.AddRange(intent.Expressions
.Select(exp => new UserSay
{
Intent = intent.Name,
Text = exp.Text
}));
});
return trainingData;
}
}
}

View file

@ -1,4 +1,5 @@
using Bot.Rasa.Agents;
using Bot.Rasa.Models;
using CustomEntityFoundation;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
@ -25,54 +26,25 @@ namespace Bot.Rasa.Console
return response.Data;
}
public static bool Train(this RasaConsole console, String agentId)
public static bool Train(this RasaConsole console, EntityDbContext dc, String agentId)
{
var client = new RestClient($"{console.options.HostUrl}");
var agent = dc.Agent().Find(agentId);
var corpus = agent.GrabCorpus(dc);
var request = new RestRequest("train", Method.POST);
request.AddQueryParameter("project", agentId);
string json = JsonConvert.SerializeObject(new
{
rasa_nlu_data = new RasaTrainingData
string json = JsonConvert.SerializeObject(new { rasa_nlu_data = corpus },
new JsonSerializerSettings
{
UserSays = new List<UserSay>
{
new UserSay{ Text = "What's the weather like today?", Intent = "Weather" },
new UserSay{ Text = "Is gonna rain tomorrow?", Intent = "Weather"},
new UserSay{ Text = "Sunny", Intent = "Weather"},
new UserSay{ Text = "Is it raining outside?", Intent = "Weather"},
new UserSay{ Text = "It is raining", Intent = "Weather"},
new UserSay{ Text = "How old are you?", Intent = "Age"},
new UserSay{ Text = "When were you born?", Intent = "Age"},
new UserSay{ Text = "Where do you come from", Intent = "Country"},
new UserSay{ Text = "Where are you from?", Intent = "Country"},
new UserSay{ Text = "are you from US?", Intent = "Country"},
new UserSay{ Text = "What do you like for lunch?", Intent = "Lunch"},
new UserSay{ Text = "Would you like some cookie?", Intent = "Lunch"}
}
}
}, new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() });
ContractResolver = new CamelCasePropertyNamesContractResolver()
});
var client = new RestClient($"{console.options.HostUrl}");
var request = new RestRequest("train", Method.POST);
request.AddQueryParameter("project", agentId);
request.AddParameter("application/json", json, ParameterType.RequestBody);
var response = client.Execute(request);
return true;
}
}
public class RasaTrainingData
{
[JsonProperty("common_examples")]
public List<UserSay> UserSays { get; set; }
}
public class UserSay
{
public String Text { get; set; }
public String Intent { get; set; }
}
}

View file

@ -1,4 +1,5 @@
using Bot.Rasa.Agents;
using Bot.Rasa.Intents;
using CustomEntityFoundation;
using EntityFrameworkCore.BootKit;
using Microsoft.EntityFrameworkCore;
@ -15,5 +16,15 @@ namespace Bot.Rasa
{
return dc.Table<RasaAgent>();
}
public static DbSet<RasaIntent> Intent(this EntityDbContext dc)
{
return dc.Table<RasaIntent>();
}
public static DbSet<RasaIntentExpression> IntentExpression(this EntityDbContext dc)
{
return dc.Table<RasaIntentExpression>();
}
}
}

View file

@ -2,6 +2,7 @@
using EntityFrameworkCore.BootKit;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text;
@ -9,6 +10,17 @@ namespace Bot.Rasa.Intents
{
public class RasaIntent : Entity, IDbRecord
{
[Required]
[StringLength(36)]
public String AgentId { get; set; }
[MaxLength(32)]
public String Name { get; set; }
[MaxLength(256)]
public String Description { get; set; }
[ForeignKey("IntentId")]
public List<RasaIntentExpression> Expressions { get; set; }
}
}

View file

@ -0,0 +1,29 @@
using CustomEntityFoundation;
using CustomEntityFoundation.Entities;
using EntityFrameworkCore.BootKit;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
namespace Bot.Rasa.Intents
{
public class RasaIntentExpression : Entity, IDbRecord
{
[Required]
[StringLength(36)]
public String IntentId { get; set; }
[Required]
[MaxLength(128)]
public String Text { get; set; }
public override bool IsExist<T>(EntityDbContext dc)
{
return dc.Table<RasaIntentExpression>().Any(x => x.IntentId == IntentId && x.Text == Text);
}
}
}

View file

@ -0,0 +1,13 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
namespace Bot.Rasa.Models
{
public class RasaTrainingData
{
[JsonProperty("common_examples")]
public List<UserSay> UserSays { get; set; }
}
}

View file

@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Bot.Rasa.Models
{
public class UserSay
{
public String Text { get; set; }
public String Intent { get; set; }
}
}

View file

@ -27,7 +27,12 @@ namespace Bot.UnitTest
Name = "Pizza Bot"
};
dc.DbTran(() => rasa.CreateAgent(agent));
int row = dc.DbTran(() => rasa.CreateAgent(agent));
if(row > 0)
{
var generator = new GenerateTestData();
dc.DbTran(() => generator.LoadData(dc, agent));
}
}
[TestMethod]
@ -43,7 +48,7 @@ namespace Bot.UnitTest
public void Train()
{
var rasa = new RasaConsole(dc, Options);
rasa.Train(PIZZA_BOT_ID);
rasa.Train(dc, PIZZA_BOT_ID);
}
}
}

View file

@ -1,11 +1,6 @@
using CustomEntityFoundation;
using CustomEntityFoundation.Entities;
using EntityFrameworkCore.BootKit;
using Microsoft.Data.Sqlite;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace Bot.UnitTest
{

View file

@ -0,0 +1,34 @@
using Bot.Rasa;
using Bot.Rasa.Agents;
using Bot.Rasa.Intents;
using CustomEntityFoundation;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Bot.UnitTest
{
public class GenerateTestData
{
public void LoadData(EntityDbContext dc, RasaAgent agent)
{
var intent = new RasaIntent
{
AgentId = agent.Id,
Name = "Weather",
Expressions = new List<RasaIntentExpression>
{
new RasaIntentExpression { Text ="What is the weather like today in Chicago?" },
new RasaIntentExpression { Text ="Is it will be rain?" },
new RasaIntentExpression { Text ="It's windy outside?" },
new RasaIntentExpression { Text ="It's very code there?" }
}
};
if (dc.Intent().Any(x => x.Name == intent.Name)) return;
dc.Intent().Add(intent);
}
}
}