Skip to content

Commit

Permalink
Merge pull request #3 from swagfin/bot-playground
Browse files Browse the repository at this point in the history
Bot playground
  • Loading branch information
swagfin authored Sep 9, 2023
2 parents eedbd33 + e148bdc commit 0d341d8
Show file tree
Hide file tree
Showing 10 changed files with 263 additions and 113 deletions.
2 changes: 1 addition & 1 deletion Aiml.NET.Tests/defaults/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
"SetsDirectory": "sets",
"MapsDirectory": "maps",
//"Locale": "en-AU",
"LearnfFile": "aiml/learnf.aiml",
"LearnfFile": "learnf.aiml",
"UnbindPredicatesWithDefaultValue": false
}
8 changes: 8 additions & 0 deletions Aiml.NET.XChatBot/Aiml.NET.XChatBot.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
</PropertyGroup>

<ItemGroup>
<None Remove="aiml\badanswer.aiml" />
<None Remove="aiml\default.aiml" />
<None Remove="aiml\helloworld.aiml" />
<None Remove="botpredicates.json" />
<None Remove="config.json" />
Expand All @@ -20,6 +22,12 @@
</ItemGroup>

<ItemGroup>
<Content Include="aiml\badanswer.aiml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="aiml\default.aiml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="aiml\helloworld.aiml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
Expand Down
15 changes: 13 additions & 2 deletions Aiml.NET.XChatBot/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ namespace Aiml.NET.XChatBot
{
internal class Program
{
private static string _currentTopic = string.Empty;

/*
* THIS IS A SIMPLE CHAT BOT TO PLAY AROUND WITH IT
*/
* THIS IS A SIMPLE CHAT BOT TO PLAY AROUND WITH IT
*/
static void Main(string[] args)
{
try
Expand All @@ -35,6 +37,15 @@ static void Main(string[] args)
message = message.Substring(7);
}
Response botResponse = bot.Chat(new Request(message, user, bot), trace);

//Log Topic Changed
if (!_currentTopic.Equals(user.Topic))
{
_currentTopic = user.Topic;
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($":: topic changed to: [{_currentTopic}] ::");
Console.ResetColor();
}
//replace line breaks with Console Line Breaks
string[] lines = botResponse.ToString().Split(new[] { "\\r\\n" }, StringSplitOptions.None);
string responseString = string.Join(Environment.NewLine, lines);
Expand Down
77 changes: 77 additions & 0 deletions Aiml.NET.XChatBot/aiml/badanswer.aiml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?xml version='1.0' encoding='UTF-8'?>
<aiml version='2.0'>

<category>
<pattern>BAD ANSWER</pattern>
<template>
<think>
<set name="ThatQuestion">
<input index="2"/>
</set>
<set name="ThatQuestionTopic">
<get name="topic"/>
</set>
</think>
<!--check the topic-->
<condition name="ThatQuestionTopic">
<li value="unknown">
Hmmm, i can't learn without topic context
<think>
<set name="topic">*</set>
</think>
</li>
<li>
OK. You said "<get name="ThatQuestion"/>" and I replied "<response/>".
<think>
<set name="topic">BotTrainingProgramThatQuestionAnswer</set>
</think>
<srai>SAY</srai>
</li>
</condition>

</template>
</category>

<topic name="BotTrainingProgramThatQuestionAnswer">
<category>
<pattern>SAY ^</pattern>
<template>
<think>
<set name="ThatQuestionAnswer">
<star/>
</set>
</think>
<condition name="ThatQuestionAnswer">
<li value="nil">\r\nWhat should I say instead? Reply starting with "Say "</li>
<li>
<!--Set the Topic to what User was In Before Learn-->
<think>
<set name="topic">
<get name="ThatQuestionTopic" />
</set>
</think>
<!--Tell Bot to Learn-->
<learnf>
<topic>
<category>
<pattern>
<eval>
<get name="ThatQuestion" />
</eval>
</pattern>
<template>
<eval>
<get name="ThatQuestionAnswer" />
</eval>
</template>
</category>
</topic>
</learnf>
<!--Finnaly show success learning message-->
Got it, when am asked &#8220;<get name="ThatQuestion" />&#8221;, i will now respond &#8220;<get name="ThatQuestionAnswer" />&#8221; under topic: <get name="ThatQuestionTopic" />
</li>
</condition>
</template>
</category>
</topic>
</aiml>
7 changes: 7 additions & 0 deletions Aiml.NET.XChatBot/aiml/default.aiml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?xml version='1.0' encoding='UTF-8'?>
<aiml version='2.0'>
<category>
<pattern>*</pattern>
<template>Hmmm i have no answer for that</template>
</category>
</aiml>
7 changes: 6 additions & 1 deletion Aiml.NET.XChatBot/aiml/helloworld.aiml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@
<aiml version='2.0'>
<category>
<pattern>HELLO ^</pattern>
<template>Hello world!</template>
<template>
<think>
<set name="topic">greetings</set>
</think>
Hello world!
</template>
</category>
<category>
<pattern>HI ^</pattern>
Expand Down
2 changes: 1 addition & 1 deletion Aiml.NET.XChatBot/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@
"SetsDirectory": "sets",
"MapsDirectory": "maps",
//"Locale": "en-AU",
"LearnfFile": "aiml/learnf.aiml",
"LearnfFile": "learnf.aiml",
"UnbindPredicatesWithDefaultValue": false
}
2 changes: 1 addition & 1 deletion Aiml.NET/Aiml.NET.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>preview</LangVersion>
<NullableContextOptions>enable</NullableContextOptions>
<Version>2.0.1</Version>
<Version>2.0.2</Version>
<Company />
<Authors>Wainaina George</Authors>
<Copyright>© 2023 Crudsoft Technologies</Copyright>
Expand Down
105 changes: 62 additions & 43 deletions Aiml.NET/Tags/Learn.cs
Original file line number Diff line number Diff line change
@@ -1,53 +1,72 @@
using System;
using System.Xml;

namespace Aiml.NET {
public partial class TemplateNode {
/// <summary>
/// Processes the content as AIML and adds it to the bot's brain, temporarily and for the current user only, and thus temporary.
/// </summary>
/// <remarks>
/// <para>Unlike other elements with content, the content of this element is not normally evaluated.
/// However, the special child element <c>eval</c> is replaced with the result of evaluating its own content.</para>
/// <para>This element is defined by the AIML 2.0 specification.</para>
/// </remarks>
/// <seealso cref="AddTriple"/><seealso cref="LearnF"/><seealso cref="Set"/>
public sealed class Learn : TemplateNode {
public XmlNode Node { get; }
namespace Aiml.NET
{
public partial class TemplateNode
{
/// <summary>
/// Processes the content as AIML and adds it to the bot's brain, temporarily and for the current user only, and thus temporary.
/// </summary>
/// <remarks>
/// <para>Unlike other elements with content, the content of this element is not normally evaluated.
/// However, the special child element <c>eval</c> is replaced with the result of evaluating its own content.</para>
/// <para>This element is defined by the AIML 2.0 specification.</para>
/// </remarks>
/// <seealso cref="AddTriple"/><seealso cref="LearnF"/><seealso cref="Set"/>
public sealed class Learn : TemplateNode
{
public XmlNode Node { get; }

public Learn(XmlNode node) {
this.Node = node;
}
public Learn(XmlNode node)
{
this.Node = node;
}

public override string Evaluate(RequestProcess process) {
// Evaluate <eval> tags.
XmlNode node = this.Node.Clone();
this.ProcessXml(node, process);
public override string Evaluate(RequestProcess process)
{
// Evaluate <eval> tags.
XmlNode node = this.Node.Clone();
this.ProcessXml(node, process);

// Learn the result.
process.Log(LogLevel.Diagnostic, $"In element <learn>: learning new category for {process.User.ID}: {node.OuterXml}");
AimlLoader loader = new AimlLoader(process.Bot);
loader.ProcessCategory(process.User.Graphmaster, node, null);
// Check if topic exist and check it's Name attribute, Assign if Not specified
XmlNode topicNode = node.SelectSingleNode("/topic");
if (topicNode != null && topicNode.Attributes["name"] == null)
{
XmlAttribute nameAttribute = topicNode.OwnerDocument.CreateAttribute("name");
nameAttribute.Value = process?.User?.Topic ?? "*";
topicNode.Attributes.Append(nameAttribute);
}
// Learn
process.Log(LogLevel.Diagnostic, $"In element <learn>: learning new category for {process.User.ID}: {node.OuterXml}");
AimlLoader loader = new AimlLoader(process.Bot);
loader.LoadAIML(process.User.Graphmaster, node, null);

return string.Empty;
}
return string.Empty;
}

public static Learn FromXml(XmlNode node, AimlLoader loader) {
return new Learn(node);
}
public static Learn FromXml(XmlNode node, AimlLoader loader)
{
return new Learn(node);
}

private void ProcessXml(XmlNode node, RequestProcess process) {
for (int i = 0; i < node.ChildNodes.Count; ++i) {
XmlNode node2 = node.ChildNodes[i];
if (node2.NodeType == XmlNodeType.Element) {
if (node2.Name.Equals("eval", StringComparison.InvariantCultureIgnoreCase)) {
TemplateElementCollection tags = TemplateElementCollection.FromXml(node2, process.Bot.AimlLoader);
node2.ParentNode.ReplaceChild(node.OwnerDocument.CreateTextNode(tags.Evaluate(process)), node2);
} else
this.ProcessXml(node2, process);
}
}
}
}
}
private void ProcessXml(XmlNode node, RequestProcess process)
{
for (int i = 0; i < node.ChildNodes.Count; ++i)
{
XmlNode node2 = node.ChildNodes[i];
if (node2.NodeType == XmlNodeType.Element)
{
if (node2.Name.Equals("eval", StringComparison.InvariantCultureIgnoreCase))
{
TemplateElementCollection tags = TemplateElementCollection.FromXml(node2, process.Bot.AimlLoader);
node2.ParentNode.ReplaceChild(node.OwnerDocument.CreateTextNode(tags.Evaluate(process)), node2);
}
else
this.ProcessXml(node2, process);
}
}
}
}
}
}
Loading

0 comments on commit 0d341d8

Please sign in to comment.