From cd72868f38b551839d6f462b319ef2cba4cb7737 Mon Sep 17 00:00:00 2001 From: Oceania2018 Date: Wed, 15 Aug 2018 06:34:27 -0500 Subject: [PATCH] Support multiple models for one agent. --- BotSharp.Core/BotSharp.Core.csproj | 2 +- BotSharp.Core/Engines/BotEngineBase.cs | 5 +---- .../Engines/Dialogflow/AgentImporterInDialogflow.cs | 8 +++++++- .../Dialogflow/DialogflowIntentExpressionPart.cs | 1 + BotSharp.Core/Engines/Rasa/AgentImporterInRasa.cs | 12 ++++++++---- BotSharp.Core/Engines/Sebis/AgentImporterInSebis.cs | 9 ++++++--- .../Engines/TrainingIntentExpressionPart.cs | 2 +- BotSharp.Core/Intents/IntentExpressionPart.cs | 2 ++ BotSharp.RestApi/AgentController.cs | 2 +- 9 files changed, 28 insertions(+), 15 deletions(-) diff --git a/BotSharp.Core/BotSharp.Core.csproj b/BotSharp.Core/BotSharp.Core.csproj index 2405775d..e0fa9531 100644 --- a/BotSharp.Core/BotSharp.Core.csproj +++ b/BotSharp.Core/BotSharp.Core.csproj @@ -35,7 +35,7 @@ - + diff --git a/BotSharp.Core/Engines/BotEngineBase.cs b/BotSharp.Core/Engines/BotEngineBase.cs index 5696c699..7975ac41 100644 --- a/BotSharp.Core/Engines/BotEngineBase.cs +++ b/BotSharp.Core/Engines/BotEngineBase.cs @@ -173,14 +173,11 @@ namespace BotSharp.Core.Engines .ToList() .ForEach(x => { - int start = say.Text.IndexOf(x.Text); - var part = new TrainingIntentExpressionPart { Value = x.Text, Entity = $"{x.Meta}:{x.Alias}", - Start = start, - End = start + x.Text.Length + Start = x.Start }; if (say.Entities == null) say.Entities = new List(); diff --git a/BotSharp.Core/Engines/Dialogflow/AgentImporterInDialogflow.cs b/BotSharp.Core/Engines/Dialogflow/AgentImporterInDialogflow.cs index 21bf4eea..9d687523 100644 --- a/BotSharp.Core/Engines/Dialogflow/AgentImporterInDialogflow.cs +++ b/BotSharp.Core/Engines/Dialogflow/AgentImporterInDialogflow.cs @@ -30,7 +30,7 @@ namespace BotSharp.Core.Engines public Agent LoadAgent(AgentImportHeader agentHeader) { // load agent profile - string data = File.ReadAllText(Path.Join(AgentDir, "Dialogflow", $"agentHeader.Name{Path.DirectorySeparatorChar}agent.json")); + string data = File.ReadAllText(Path.Join(AgentDir, "Dialogflow", $"{agentHeader.Name}{Path.DirectorySeparatorChar}agent.json")); var agent = JsonConvert.DeserializeObject(data); agent.Name = agentHeader.Name; agent.Id = agentHeader.Id; @@ -155,6 +155,12 @@ namespace BotSharp.Core.Engines // remove @ say.Data.Where(x => x.Meta != null && x.Meta.StartsWith("@")).ToList().ForEach(x => x.Meta = x.Meta.Substring(1)); + + // calculate offset + for(int i = 1; i < say.Data.Count; i++) + { + say.Data[i].Start = String.Join("", say.Data.Select(x => x.Text).Take(i)).Length; + } }); } } diff --git a/BotSharp.Core/Engines/Dialogflow/DialogflowIntentExpressionPart.cs b/BotSharp.Core/Engines/Dialogflow/DialogflowIntentExpressionPart.cs index 5b1968b9..b558c5e3 100644 --- a/BotSharp.Core/Engines/Dialogflow/DialogflowIntentExpressionPart.cs +++ b/BotSharp.Core/Engines/Dialogflow/DialogflowIntentExpressionPart.cs @@ -9,6 +9,7 @@ namespace BotSharp.Core.Adapters.Dialogflow public String Text { get; set; } public String Alias { get; set; } public String Meta { get; set; } + public int Start { get; set; } public Boolean UserDefined { get; set; } } } diff --git a/BotSharp.Core/Engines/Rasa/AgentImporterInRasa.cs b/BotSharp.Core/Engines/Rasa/AgentImporterInRasa.cs index 1d5b1fd5..c64dd703 100644 --- a/BotSharp.Core/Engines/Rasa/AgentImporterInRasa.cs +++ b/BotSharp.Core/Engines/Rasa/AgentImporterInRasa.cs @@ -122,7 +122,8 @@ namespace BotSharp.Core.Engines.Rasa { expression.Data.Add(new IntentExpressionPart { - Text = say.Text.Substring(pos, entity.Start - pos) + Text = say.Text.Substring(pos, entity.Start - pos), + Start = pos }); } @@ -131,7 +132,8 @@ namespace BotSharp.Core.Engines.Rasa { Alias = entity.Entity, Meta = entity.Entity, - Text = say.Text.Substring(entity.Start, entity.Value.Length) + Text = say.Text.Substring(entity.Start, entity.Value.Length), + Start = entity.Start }); pos = entity.End + 1; @@ -141,7 +143,8 @@ namespace BotSharp.Core.Engines.Rasa // end expression.Data.Add(new IntentExpressionPart { - Text = say.Text.Substring(pos) + Text = say.Text.Substring(pos), + Start = pos }); } } @@ -181,7 +184,8 @@ namespace BotSharp.Core.Engines.Rasa .Select(x => new TrainingIntentExpressionPart { Value = x.Text, - Entity = x.Meta + Entity = x.Meta, + Start = x.Start }) .ToList() }); diff --git a/BotSharp.Core/Engines/Sebis/AgentImporterInSebis.cs b/BotSharp.Core/Engines/Sebis/AgentImporterInSebis.cs index 2144c515..3b6a9fac 100644 --- a/BotSharp.Core/Engines/Sebis/AgentImporterInSebis.cs +++ b/BotSharp.Core/Engines/Sebis/AgentImporterInSebis.cs @@ -95,7 +95,8 @@ namespace BotSharp.Core.Engines { expression.Data.Add(new IntentExpressionPart { - Text = say.Text.Substring(pos, entity.Start - pos) + Text = say.Text.Substring(pos, entity.Start - pos), + Start = pos }); } @@ -104,7 +105,8 @@ namespace BotSharp.Core.Engines { Alias = entity.Entity, Meta = entity.Entity, - Text = say.Text.Substring(entity.Start, entity.Value.Length) + Text = say.Text.Substring(entity.Start, entity.Value.Length), + Start = entity.Start }); pos = entity.End + 1; @@ -114,7 +116,8 @@ namespace BotSharp.Core.Engines // end expression.Data.Add(new IntentExpressionPart { - Text = say.Text.Substring(pos) + Text = say.Text.Substring(pos), + Start = pos }); } } diff --git a/BotSharp.Core/Engines/TrainingIntentExpressionPart.cs b/BotSharp.Core/Engines/TrainingIntentExpressionPart.cs index d5cf5d2a..070f960f 100644 --- a/BotSharp.Core/Engines/TrainingIntentExpressionPart.cs +++ b/BotSharp.Core/Engines/TrainingIntentExpressionPart.cs @@ -7,7 +7,7 @@ namespace BotSharp.Core.Engines public class TrainingIntentExpressionPart { public int Start { get; set; } - public int End { get; set; } + public int End { get { return Start + Value.Length; } } public String Value { get; set; } public String Entity { get; set; } } diff --git a/BotSharp.Core/Intents/IntentExpressionPart.cs b/BotSharp.Core/Intents/IntentExpressionPart.cs index e6e23668..e751f743 100644 --- a/BotSharp.Core/Intents/IntentExpressionPart.cs +++ b/BotSharp.Core/Intents/IntentExpressionPart.cs @@ -24,6 +24,8 @@ namespace BotSharp.Core.Intents [MaxLength(64)] public String Meta { get; set; } + public int Start { get; set; } + public Boolean UserDefined { get; set; } } } diff --git a/BotSharp.RestApi/AgentController.cs b/BotSharp.RestApi/AgentController.cs index d9a45a36..fdf0537d 100644 --- a/BotSharp.RestApi/AgentController.cs +++ b/BotSharp.RestApi/AgentController.cs @@ -53,7 +53,7 @@ namespace BotSharp.RestApi var rasa = new BotSharpAi(); var agentHeader = agents.First(x => x.Id == agentId); - rasa.RestoreAgent(agentHeader); + rasa.RestoreAgent(agentHeader); return Ok(); }