diff --git a/Bot.WebStarter/settings.bot.json b/Bot.WebStarter/settings.bot.json new file mode 100644 index 00000000..96cf8614 --- /dev/null +++ b/Bot.WebStarter/settings.bot.json @@ -0,0 +1,5 @@ +{ + "Rasa": { + "Host": "http://rasa.local:5000" + } +} diff --git a/Bot.WebStarter/settings.db.json b/Bot.WebStarter/settings.db.json index 23309cfc..ebd2d785 100644 --- a/Bot.WebStarter/settings.db.json +++ b/Bot.WebStarter/settings.db.json @@ -3,8 +3,8 @@ "Default": "SqlServer", "ConnectionStrings": { "InMemory": "DataSource=:memory:", - "Sqlite": "Data Source=|DataDirectory|RasaBot.db;", - "SqlServer": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=RasaBot;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False" + "Sqlite": "Data Source=|DataDirectory|BotSharp.db;", + "SqlServer": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=BotSharp;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False" } } } \ No newline at end of file diff --git a/Bot.WebStarter/settings.rasa.json b/Bot.WebStarter/settings.rasa.json deleted file mode 100644 index 05127584..00000000 --- a/Bot.WebStarter/settings.rasa.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "Rasa": { - "Host": "http://81a1f75c.ngrok.io" - } -} diff --git a/BotSharp.Core/Adapters/Dialogflow/DialogflowEntityEntry.cs b/BotSharp.Core/Adapters/Dialogflow/DialogflowEntityEntry.cs index 8a601906..31b81e23 100644 --- a/BotSharp.Core/Adapters/Dialogflow/DialogflowEntityEntry.cs +++ b/BotSharp.Core/Adapters/Dialogflow/DialogflowEntityEntry.cs @@ -1,4 +1,5 @@ -using Newtonsoft.Json; +using BotSharp.Core.Entities; +using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Text; @@ -11,8 +12,9 @@ namespace BotSharp.Core.Adapters.Dialogflow [JsonProperty("value")] public string Value { get; set; } - [JsonProperty("synonyms")] - public List Synonyms { get; set; } + public List RawSynonyms { get; set; } + + public List Synonyms { get; set; } public DialogflowEntityEntry() { @@ -21,7 +23,7 @@ namespace BotSharp.Core.Adapters.Dialogflow public DialogflowEntityEntry(string value, List synonyms) { this.Value = value; - this.Synonyms = synonyms; + this.RawSynonyms = synonyms; } public DialogflowEntityEntry(string value, string[] synonyms) : this(value, new List(synonyms)) diff --git a/BotSharp.Core/Adapters/Rasa/RasaTraningEntity.cs b/BotSharp.Core/Adapters/Rasa/RasaTraningEntity.cs new file mode 100644 index 00000000..81fcf50a --- /dev/null +++ b/BotSharp.Core/Adapters/Rasa/RasaTraningEntity.cs @@ -0,0 +1,17 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Text; + +namespace BotSharp.Core.Adapters.Rasa +{ + public class RasaTraningEntity + { + public String EntityType { get; set; } + + [JsonProperty("value")] + public String EntityValue { get; set; } + + public List Synonyms { get; set; } + } +} diff --git a/BotSharp.Core/Agents/AgentExtension.cs b/BotSharp.Core/Agents/AgentExtension.cs index 1fa7ad59..42c44602 100644 --- a/BotSharp.Core/Agents/AgentExtension.cs +++ b/BotSharp.Core/Agents/AgentExtension.cs @@ -1,4 +1,6 @@ -using BotSharp.Core.Entities; +using BotSharp.Core.Adapters.Rasa; +using BotSharp.Core.Entities; +using BotSharp.Core.Expressions; using BotSharp.Core.Intents; using BotSharp.Core.Models; using EntityFrameworkCore.BootKit; @@ -32,9 +34,12 @@ namespace BotSharp.Core.Agents { var trainingData = new RasaTrainingData { + Entities = new List(), UserSays = new List() }; + var expressParts = new List(); + var intents = dc.Table() .Include(x => x.Contexts) .Include(x => x.UserSays).ThenInclude(say => say.Data) @@ -81,13 +86,35 @@ namespace BotSharp.Core.Agents End = start + x.Text.Length }; + if (say.Entities == null) say.Entities = new List(); say.Entities.Add(part); + + // assemble entity synonmus + if (!trainingData.Entities.Any(y => y.EntityType == x.Alias && y.EntityValue == x.Text)) + { + var allSynonyms = (from e in dc.Table() + join ee in dc.Table() on e.Id equals ee.EntityId + join ees in dc.Table() on ee.Id equals ees.EntityEntryId + where e.Name == x.Alias && ee.Value == x.Text & ees.Synonym != x.Text + select ees.Synonym ).ToList(); + + var te = new RasaTraningEntity + { + EntityType = x.Alias, + EntityValue = x.Text, + Synonyms = allSynonyms + }; + + trainingData.Entities.Add(te); + } }); trainingData.UserSays.Add(say); }); }); + + return trainingData; } } diff --git a/BotSharp.Core/BotSharp.Core.csproj b/BotSharp.Core/BotSharp.Core.csproj index 9ef96022..0b4df65f 100644 --- a/BotSharp.Core/BotSharp.Core.csproj +++ b/BotSharp.Core/BotSharp.Core.csproj @@ -1,5 +1,12 @@ + + SAK + SAK + SAK + SAK + + netcoreapp2.0 diff --git a/BotSharp.Core/Engines/AgentImporterInDialogflow.cs b/BotSharp.Core/Engines/AgentImporterInDialogflow.cs index 0cb352c9..dd0afa52 100644 --- a/BotSharp.Core/Engines/AgentImporterInDialogflow.cs +++ b/BotSharp.Core/Engines/AgentImporterInDialogflow.cs @@ -46,7 +46,12 @@ namespace BotSharp.Core.Engines if (File.Exists(entriesFileName)) { string entriesJson = File.ReadAllText($"{entriesFileName}"); + entriesJson = entriesJson.Replace("\"synonyms\":", "\"rawSynonyms\":"); entity.Entries = JsonConvert.DeserializeObject>(entriesJson); + entity.Entries.ForEach(x => x.Synonyms = x.RawSynonyms.Select(s => new EntityEntrySynonym + { + Synonym = s + }).ToList()); } agent.Entities.Add(entity.ToObject()); diff --git a/BotSharp.Core/Engines/RequestExtension.cs b/BotSharp.Core/Engines/RequestExtension.cs index 2e58105b..a6deb864 100644 --- a/BotSharp.Core/Engines/RequestExtension.cs +++ b/BotSharp.Core/Engines/RequestExtension.cs @@ -258,7 +258,8 @@ namespace BotSharp.Core.Engines string json = JsonConvert.SerializeObject(new { rasa_nlu_data = corpus }, new JsonSerializerSettings { - ContractResolver = new CamelCasePropertyNamesContractResolver() + ContractResolver = new CamelCasePropertyNamesContractResolver(), + NullValueHandling = NullValueHandling.Ignore }); var client = new RestClient($"{Database.Configuration.GetSection("Rasa:Host").Value}"); @@ -267,10 +268,11 @@ namespace BotSharp.Core.Engines rest.AddParameter("application/json", json, ParameterType.RequestBody); var response = client.Execute(rest); - var result = JObject.Parse(response.Content); if (response.IsSuccessful) { + var result = JObject.Parse(response.Content); + string modelName = result["info"].Value().Split(": ")[1]; dc.Table().Add(new ContextModelMapping @@ -284,6 +286,8 @@ namespace BotSharp.Core.Engines } else { + var result = JObject.Parse(response.Content); + Console.WriteLine(result["error"]); return String.Empty; diff --git a/BotSharp.Core/Entities/Entity.cs b/BotSharp.Core/Entities/Entity.cs index 80ecb7e2..7791bbe4 100644 --- a/BotSharp.Core/Entities/Entity.cs +++ b/BotSharp.Core/Entities/Entity.cs @@ -18,10 +18,11 @@ namespace BotSharp.Core.Entities [MaxLength(64)] public String Name { get; set; } - [NotMapped] - public List Values { get; set; } - [ForeignKey("EntityId")] public List Entries { get; set; } + + public bool IsOverridable { get; set; } + + public bool IsEnum { get; set; } } } diff --git a/BotSharp.Core/Entities/EntityEntry.cs b/BotSharp.Core/Entities/EntityEntry.cs index 69d392f6..95abbaf4 100644 --- a/BotSharp.Core/Entities/EntityEntry.cs +++ b/BotSharp.Core/Entities/EntityEntry.cs @@ -14,10 +14,10 @@ namespace BotSharp.Core.Entities [StringLength(36)] public String EntityId { get; set; } - [MaxLength(128)] + [MaxLength(64)] public String Value { get; set; } - [NotMapped] - public List Synonyms { get; set; } + [ForeignKey("EntityEntryId")] + public List Synonyms { get; set; } } } diff --git a/BotSharp.Core/Entities/EntityEntrySynonym.cs b/BotSharp.Core/Entities/EntityEntrySynonym.cs new file mode 100644 index 00000000..e9a5cbe5 --- /dev/null +++ b/BotSharp.Core/Entities/EntityEntrySynonym.cs @@ -0,0 +1,20 @@ +using EntityFrameworkCore.BootKit; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Text; + +namespace BotSharp.Core.Entities +{ + [Table("Bot_EntityEntrySynonym")] + public class EntityEntrySynonym : DbRecord, IDbRecord + { + [Required] + [StringLength(36)] + public String EntityEntryId { get; set; } + + [MaxLength(64)] + public String Synonym { get; set; } + } +} diff --git a/BotSharp.Core/Models/RasaIntentExpression.cs b/BotSharp.Core/Models/RasaIntentExpression.cs index 621a962b..480e711c 100644 --- a/BotSharp.Core/Models/RasaIntentExpression.cs +++ b/BotSharp.Core/Models/RasaIntentExpression.cs @@ -9,7 +9,6 @@ namespace BotSharp.Core.Models { public RasaIntentExpression() { - Entities = new List(); } public String Text { get; set; } diff --git a/BotSharp.Core/Models/RasaTrainingData.cs b/BotSharp.Core/Models/RasaTrainingData.cs index 02a1ed55..2b86ad03 100644 --- a/BotSharp.Core/Models/RasaTrainingData.cs +++ b/BotSharp.Core/Models/RasaTrainingData.cs @@ -1,4 +1,5 @@ -using Newtonsoft.Json; +using BotSharp.Core.Adapters.Rasa; +using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Text; @@ -9,5 +10,8 @@ namespace BotSharp.Core.Models { [JsonProperty("common_examples")] public List UserSays { get; set; } + + [JsonProperty("entity_synonyms")] + public List Entities { get; set; } } } diff --git a/BotSharp.UnitTest/EntityEntryTest.cs b/BotSharp.UnitTest/EntityEntryTest.cs new file mode 100644 index 00000000..085fb599 --- /dev/null +++ b/BotSharp.UnitTest/EntityEntryTest.cs @@ -0,0 +1,29 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; +using System.Text; + +namespace BotSharp.UnitTest +{ + [TestClass] + public class EntityEntryTest : TestEssential + { + [TestMethod] + public void CreateEntityEntryTest() + { + + } + + [TestMethod] + public void UpdateEntityEntryeTest() + { + + } + + [TestMethod] + public void DeleteEntityEntryTest() + { + + } + } +} diff --git a/BotSharp.UnitTest/EntityTest.cs b/BotSharp.UnitTest/EntityTest.cs deleted file mode 100644 index 132f47fb..00000000 --- a/BotSharp.UnitTest/EntityTest.cs +++ /dev/null @@ -1,12 +0,0 @@ -using Microsoft.VisualStudio.TestTools.UnitTesting; -using System; -using System.Collections.Generic; -using System.Text; - -namespace BotSharp.UnitTest -{ - [TestClass] - public class EntityTest : TestEssential - { - } -} diff --git a/BotSharp.UnitTest/EntityTypeTest.cs b/BotSharp.UnitTest/EntityTypeTest.cs new file mode 100644 index 00000000..88b90f0b --- /dev/null +++ b/BotSharp.UnitTest/EntityTypeTest.cs @@ -0,0 +1,29 @@ +using Microsoft.VisualStudio.TestTools.UnitTesting; +using System; +using System.Collections.Generic; +using System.Text; + +namespace BotSharp.UnitTest +{ + [TestClass] + public class EntityTypeTest : TestEssential + { + [TestMethod] + public void CreateEntityTypeTest() + { + + } + + [TestMethod] + public void UpdateEntityTypeTest() + { + + } + + [TestMethod] + public void DeleteEntityTypeTest() + { + + } + } +}