From 84e107c19b07664ed1c262cf54e23d07df14f231 Mon Sep 17 00:00:00 2001 From: "haiping008@gmail.com" Date: Wed, 9 May 2018 09:50:16 -0500 Subject: [PATCH] fill parameter value for reply message --- .vscode/launch.json | 28 +++++++++++++++++++ .vscode/tasks.json | 15 ++++++++++ BotSharp.Core/BotSharp.Core.csproj | 2 +- BotSharp.Core/Engines/RequestExtension.cs | 34 +++++++++++++++++------ 4 files changed, 69 insertions(+), 10 deletions(-) create mode 100644 .vscode/launch.json create mode 100644 .vscode/tasks.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 00000000..93d1e0b8 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,28 @@ +{ + // Use IntelliSense to find out which attributes exist for C# debugging + // Use hover for the description of the existing attributes + // For further information visit https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md + "version": "0.2.0", + "configurations": [ + { + "name": ".NET Core Launch (console)", + "type": "coreclr", + "request": "launch", + "preLaunchTask": "build", + // If you have changed target frameworks, make sure to update the program path. + "program": "${workspaceFolder}/BotSharp.UnitTest/bin/Debug/netcoreapp2.0/BotSharp.UnitTest.dll", + "args": [], + "cwd": "${workspaceFolder}/BotSharp.UnitTest", + // For more information about the 'console' field, see https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window + "console": "internalConsole", + "stopAtEntry": false, + "internalConsoleOptions": "openOnSessionStart" + }, + { + "name": ".NET Core Attach", + "type": "coreclr", + "request": "attach", + "processId": "${command:pickProcess}" + } + ,] +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 00000000..257ba444 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,15 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "build", + "command": "dotnet", + "type": "process", + "args": [ + "build", + "${workspaceFolder}/BotSharp.UnitTest/BotSharp.UnitTest.csproj" + ], + "problemMatcher": "$msCompile" + } + ] +} \ No newline at end of file diff --git a/BotSharp.Core/BotSharp.Core.csproj b/BotSharp.Core/BotSharp.Core.csproj index 41d0b6dd..ce66c15c 100644 --- a/BotSharp.Core/BotSharp.Core.csproj +++ b/BotSharp.Core/BotSharp.Core.csproj @@ -29,7 +29,7 @@ - + diff --git a/BotSharp.Core/Engines/RequestExtension.cs b/BotSharp.Core/Engines/RequestExtension.cs index 6dfc11fc..d1fe56ba 100644 --- a/BotSharp.Core/Engines/RequestExtension.cs +++ b/BotSharp.Core/Engines/RequestExtension.cs @@ -14,6 +14,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; +using System.Text.RegularExpressions; namespace BotSharp.Core.Engines { @@ -73,6 +74,8 @@ namespace BotSharp.Core.Engines HandleContext(dc, rasa, intentResponse, aiResponse); + Console.WriteLine(JsonConvert.SerializeObject(aiResponse.Result)); + return aiResponse; } @@ -135,6 +138,15 @@ namespace BotSharp.Core.Engines p.Value = query.Substring(entity.Start, entity.End - entity.Start); } + // convert to Standard entity value + if (!String.IsNullOrEmpty(p.Value) && !p.DataType.StartsWith("@sys.")) + { + p.Value = agent.Entities.FirstOrDefault(x => x.Name == p.Name).Entries.FirstOrDefault((entry) => { + return entry.Value.ToLower() == p.Value.ToLower() || + entry.Synonyms.Select(synonym => synonym.Synonym.ToLower()).Contains(p.Value.ToLower()); + })?.Value; + } + // fixed entity per request if (request.Entities != null) { @@ -147,15 +159,6 @@ namespace BotSharp.Core.Engines } } } - - // convert to Standard entity value - if (!String.IsNullOrEmpty(p.Value) && !p.DataType.StartsWith("@sys.")) - { - p.Value = agent.Entities.FirstOrDefault(x => x.Name == p.Name).Entries.FirstOrDefault((entry) => { - return entry.Value.ToLower() == p.Value.ToLower() || - entry.Synonyms.Select(synonym => synonym.Synonym.ToLower()).Contains(p.Value.ToLower()); - })?.Value; - } }); } @@ -174,10 +177,23 @@ namespace BotSharp.Core.Engines msg.Speech = msg.Speech.StartsWith("[") ? ArrayHelper.GetRandom(msg.Speech.Substring(2, msg.Speech.Length - 4).Split("\",\"").ToList()) : msg.Speech; + + msg.Speech = ReplaceParameters4Response(intentResponse.Parameters, msg.Speech); } }); } + private static string ReplaceParameters4Response(List parameters, string text) + { + var reg = new Regex(@"\$\w+"); + + reg.Matches(text).ToList().ForEach(token => { + text = text.Replace(token.Value, parameters.FirstOrDefault(x => x.Name == token.Value.Substring(1))?.Value.ToString()); + }); + + return text; + } + private static void HandleContext(Database dc, RasaAi rasa, IntentResponse intentResponse, AIResponse aiResponse) { // Merge context lifespan