From da71e8d4df8ed61023fd002368bf68e85b0d4149 Mon Sep 17 00:00:00 2001 From: jackfiled Date: Mon, 20 Nov 2023 13:32:44 +0800 Subject: [PATCH] =?UTF-8?q?add:=20Grammar2=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Katheryne/Grammar2/grammar.yaml | 36 +++++++++++++ Katheryne.Tests/Katheryne/Grammar2/in.txt | 4 ++ Katheryne.Tests/Katheryne/Grammar2/out.txt | 9 ++++ .../Katheryne/KatheryneRobotTests.cs | 53 ++++++++++++++++++- 4 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 Katheryne.Tests/Katheryne/Grammar2/grammar.yaml create mode 100644 Katheryne.Tests/Katheryne/Grammar2/in.txt create mode 100644 Katheryne.Tests/Katheryne/Grammar2/out.txt diff --git a/Katheryne.Tests/Katheryne/Grammar2/grammar.yaml b/Katheryne.Tests/Katheryne/Grammar2/grammar.yaml new file mode 100644 index 0000000..464eab7 --- /dev/null +++ b/Katheryne.Tests/Katheryne/Grammar2/grammar.yaml @@ -0,0 +1,36 @@ +robotName: 凯瑟琳 +stages: + - name: running + answer: 向着星辰和深渊!欢迎来到冒险家协会。 + transformers: + - pattern: 领取每日委托奖励 + nextStageName: daily-task-question + - pattern: 你好|您好|[Hh]ello + nextStageName: hello + - pattern: .*? + nextStageName: unknown + + - name: daily-task-question + answer: 冒险家今日完成的任务是? + transformers: + - pattern: (.*) + nextStageName: daily-task + + - name: daily-task + answer: 感谢冒险家完成了“$1”, 这是你的奖励。 + transformers: + - pattern: + nextStageName: running + + - name: hello + answer: 你好,我是冒险家协会的接待员凯瑟琳。 + transformers: + - pattern: + nextStageName: running + + - name: unknown + answer: 对不起,做不到。 + transformers: + - pattern: + nextStageName: running +beginStageName: running \ No newline at end of file diff --git a/Katheryne.Tests/Katheryne/Grammar2/in.txt b/Katheryne.Tests/Katheryne/Grammar2/in.txt new file mode 100644 index 0000000..baa09fe --- /dev/null +++ b/Katheryne.Tests/Katheryne/Grammar2/in.txt @@ -0,0 +1,4 @@ +你好 +I asdasdasdfsavnakjnhvas;dvf +领取每日委托奖励 +sCSJDASKJDNVF \ No newline at end of file diff --git a/Katheryne.Tests/Katheryne/Grammar2/out.txt b/Katheryne.Tests/Katheryne/Grammar2/out.txt new file mode 100644 index 0000000..10dd24a --- /dev/null +++ b/Katheryne.Tests/Katheryne/Grammar2/out.txt @@ -0,0 +1,9 @@ +向着星辰和深渊!欢迎来到冒险家协会。 +你好,我是冒险家协会的接待员凯瑟琳。 +向着星辰和深渊!欢迎来到冒险家协会。 +对不起,做不到。 +向着星辰和深渊!欢迎来到冒险家协会。 +冒险家今日完成的任务是? +感谢冒险家完成了“sCSJDASKJDNVF”, 这是你的奖励。 +向着星辰和深渊!欢迎来到冒险家协会。 +再见。 diff --git a/Katheryne.Tests/Katheryne/KatheryneRobotTests.cs b/Katheryne.Tests/Katheryne/KatheryneRobotTests.cs index ad2ee0a..1512e61 100644 --- a/Katheryne.Tests/Katheryne/KatheryneRobotTests.cs +++ b/Katheryne.Tests/Katheryne/KatheryneRobotTests.cs @@ -1,4 +1,5 @@ using Katheryne.Abstractions; +using Katheryne.Modules; using Katheryne.Services; using Microsoft.Extensions.Logging; using Moq; @@ -46,7 +47,57 @@ public class KatheryneRobotTests ValidateOutput(_katheryneChatRobotFactory.GetRobot(), file); } - private void ValidateOutput(IChatRobot robot, InputOutputFile file) + [Fact] + public void KatheryneRobotTest2() + { + InputOutputFile file = new("Grammar2"); + StreamReader reader = new(Path.Combine(file.PrefixPath, "grammar.yaml")); + _katheryneChatRobotFactory.SetGrammar(reader.ReadToEnd()); + + ValidateOutput(_katheryneChatRobotFactory.GetRobot(), file); + } + + [Fact] + public void WeatherModuleTest() + { + const string grammar = + """ + robotName: 凯瑟琳 + stages: + - name: running + answer: 向着星辰和深渊!欢迎来到冒险家协会。 + transformers: + - pattern: .*?天气|气温.*? + nextStageName: weather + - pattern: .*? + nextStageName: running + + - name: weather + answer: 今天璃月港的天气是@weather/text,气温是@weather/temp。 + transformers: + - pattern: + nextStageName: running + beginStageName: running + """; + + ModuleBase weatherModule = new WeatherModule(); + + _katheryneChatRobotFactory.Modules.Clear(); + _katheryneChatRobotFactory.Modules.Add(weatherModule.ModuleName, weatherModule); + _katheryneChatRobotFactory.SetGrammar(grammar); + + IChatRobot robot = _katheryneChatRobotFactory.GetRobot(); + IEnumerable answers = robot.ChatNext("今天天气怎么样?"); + + Assert.Contains(answers, answer => + answer == $"今天璃月港的天气是{weatherModule["text"]},气温是{weatherModule["temp"]}。"); + + answers = robot.ChatNext("今天气温是多少度?"); + Assert.Contains(answers, answer => + answer == $"今天璃月港的天气是{weatherModule["text"]},气温是{weatherModule["temp"]}。"); + } + + private static void ValidateOutput(IChatRobot robot, InputOutputFile file) { foreach (string output in robot.OnChatStart()) {